diff --git a/src/index.ts b/src/index.ts index 3aea9ca..e31d500 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,11 +1,12 @@ import { Hono } from "hono"; import { ApiResponse } from "./modules/api_response"; import { validator } from "hono/validator"; -import { postJson } from "./service/postJson"; +import { postUser } from "./service/postUser"; import { isEmpty } from "./utils/is_empty"; -import { get } from "./service/get"; +import { getUser } from "./service/getUser"; import { cors } from "hono/cors"; import { bodyLimit } from "hono/body-limit"; +import { getRandom } from "./service/getRandom"; const tmpApp: Hono = new Hono() export const app = tmpApp.basePath("/test/") @@ -13,7 +14,7 @@ app.use("/test/*", cors()) // routers app.get("/user", (c) => { - const re = get() + const re = getUser() return c.json(ApiResponse.success(200, "Get user successfully", re)) }) @@ -27,7 +28,7 @@ app.post( return val }), c => { - const re = postJson() + const re = postUser() return c.json(ApiResponse.success(200, "Add successfully", re)) } ) @@ -95,6 +96,19 @@ app.post("/file", } ) +app.get("/random", + validator("query", (val, c) => { + if (isEmpty(val.typeList, val.length)) { + c.status(400) + return c.json(ApiResponse.err(400, "Param is invalid")) + } + return val + }), + c => { + return getRandom(c) + } +) + app.notFound((c) => { console.log(`Not found [${c.req.method}]: ${c.req.path}`); return c.json(ApiResponse.err(404, "Not Found")) diff --git a/src/service/getRandom.ts b/src/service/getRandom.ts new file mode 100644 index 0000000..2761ebc --- /dev/null +++ b/src/service/getRandom.ts @@ -0,0 +1,53 @@ +import type { Context } from "hono" +import { ApiResponse } from "../modules/api_response" +import { Random } from "../utils/random" + +export function getRandom(c: Context) { + const typeInReq = c.req.query("typeList") + const lenTmp = c.req.query("length") + let len: number + if (typeInReq == "") { + c.status(400) + return c.json(ApiResponse.err(400, "Param is invalid")) + } + let typeList: string[] + try { + typeList = JSON.parse(typeInReq!.toString()) + } catch (e) { + c.status(400) + return c.json(ApiResponse.err(400, "Syntax error in parameter[typeList]")) + } + if (typeList.length == 0) { + c.status(400) + return c.json(ApiResponse.err(400, "Array is empty in parameter[typeList]")) + } + + if (isNaN((Number(lenTmp)))) { + c.status(400) + return c.json(ApiResponse.err(400, "Parameter[length] must be a number")) + } else { + len = Number(lenTmp) + if (len <= 0) { + c.status(400) + return c.json(ApiResponse.err(400, "Parameter[length] must > 0")) + } + } + + let result + try { + result = new Random(typeList, len).get() + } catch (e) { + if (e instanceof Error) { + c.status(400) + return c.json(ApiResponse.err(400, e.message)) + } else { + console.log("Unexcepted error"); + } + } + + if (result != undefined) { + return c.json(ApiResponse.success(200, "ok", result)) + } else { + return c.json(ApiResponse.err(500, "Unexcepted error")) + } +} \ No newline at end of file diff --git a/src/utils/random.ts b/src/utils/random.ts new file mode 100644 index 0000000..c816dab --- /dev/null +++ b/src/utils/random.ts @@ -0,0 +1,77 @@ +import type { Context, ContextVariableMap } from "hono" +import { updateExpressionWithTypeArguments } from "typescript/unstable/ast/factory" +import { ApiResponse } from "../modules/api_response" + +enum Type { + lcLetter = "lcLetter", + ucLetter = "ucLetter", + number = "number", + specialChar = "specialChar" +} + +export class Random { + typeList: string[] + len: number + lowerCaseLetterList: string[] = ((): string[] => { + return [...Array(26)].map((_, i) => String.fromCharCode(i + 97)) + })() + upperCaseLetterList: string[] = ((): string[] => { + return [...Array(26)].map((_, i) => String.fromCharCode(i + 65)) + })() + numberList: string[] = ((): string[] => { + return [...Array(10)].map((_, i) => String.fromCharCode(i + 48)) + })() + specialCharList = Array.from(`!@#$%^&*()_+-=[]{}|;':",./<>?`) + all: string[] = [] + + constructor(typeList: string[], len: number) { + this.typeList = typeList + this.len = len + if ( + !(this.typeList.length == 1 && + this.typeList[0] === "all") + ) { + for (let i = 0; i < this.typeList.length; i++) { + switch (this.typeList[i]) { + case Type.lcLetter: + this.all = [...this.all, ...this.lowerCaseLetterList] + break + case Type.ucLetter: + this.all = [...this.all, ...this.upperCaseLetterList] + break + case Type.number: + this.all = [...this.all, ...this.numberList] + break + case Type.specialChar: + this.all = [...this.all, ...this.specialCharList] + break + default: + throw new Error( + `At least contains "all"|"${Type.lcLetter}"|"${Type.ucLetter}"|"${Type.number}"|"${Type.specialChar}" in parameter[typeList]` + ); + } + } + } else { + this.all = [ + ...this.lowerCaseLetterList, + ...this.upperCaseLetterList, + ...this.numberList, + ...this.specialCharList + ] + } + + } + + get(): string { + let resultArr: string[] = [] + for (let i = 0; i < this.len; i++) { + const randomNumber = Math.floor(Math.random() * this.all.length) + resultArr = [...resultArr, this.all[randomNumber]!] + } + let result = "" + resultArr.map((val, _) => { + result += val + }) + return result + } +} \ No newline at end of file