diff --git a/bun.lock b/bun.lock index c2579d1..90a4ddd 100644 --- a/bun.lock +++ b/bun.lock @@ -4,6 +4,9 @@ "workspaces": { "": { "name": "test-api", + "dependencies": { + "hono": "^4.13.8", + }, "devDependencies": { "@types/bun": "latest", }, @@ -59,6 +62,8 @@ "bun-types": ["bun-types@1.4.2", "", { "dependencies": { "@types/node": "*" } }, "sha512-bxV1FgK7yBIzjRe5zBozIM4Bem11ZJcCXSrjWRG3YWLt8yFDePu4cLjpebO8OvPeIE9trbyPF4fuj3Cia4Fj3w=="], + "hono": ["hono@4.13.8", "", {}, "sha512-/Gng7NfoykZl2pjukW5Z6+8Yxm3BPRf86GTbQnt0SbySkvax4fyL4H3HhY1cCpBGmiW9XDRFzRV+CXK2W8QudQ=="], + "typescript": ["typescript@7.0.2", "", { "optionalDependencies": { "@typescript/typescript-aix-ppc64": "7.0.2", "@typescript/typescript-darwin-arm64": "7.0.2", "@typescript/typescript-darwin-x64": "7.0.2", "@typescript/typescript-freebsd-arm64": "7.0.2", "@typescript/typescript-freebsd-x64": "7.0.2", "@typescript/typescript-linux-arm": "7.0.2", "@typescript/typescript-linux-arm64": "7.0.2", "@typescript/typescript-linux-loong64": "7.0.2", "@typescript/typescript-linux-mips64el": "7.0.2", "@typescript/typescript-linux-ppc64": "7.0.2", "@typescript/typescript-linux-riscv64": "7.0.2", "@typescript/typescript-linux-s390x": "7.0.2", "@typescript/typescript-linux-x64": "7.0.2", "@typescript/typescript-netbsd-arm64": "7.0.2", "@typescript/typescript-netbsd-x64": "7.0.2", "@typescript/typescript-openbsd-arm64": "7.0.2", "@typescript/typescript-openbsd-x64": "7.0.2", "@typescript/typescript-sunos-x64": "7.0.2", "@typescript/typescript-win32-arm64": "7.0.2", "@typescript/typescript-win32-x64": "7.0.2" }, "bin": { "tsc": "bin/tsc" } }, "sha512-8FYau96o3NKOhbjKi/qNvG/W5jhzxkbdm5sj9AbZ/5T5sWqn3hJgLfGx27sRKZWTvyzCP8dLRBTf5tBTSRVUNA=="], "undici-types": ["undici-types@8.9.0", "", {}, "sha512-KTDyRTYX8sWmKXAikPHHSyc63CRPETMctyjKFupcC6OBLXT3xsN0e9aF7m+mIXutFWpUXuedtowG7iLOzp0kQg=="], diff --git a/package.json b/package.json index 28e980e..d32f36d 100644 --- a/package.json +++ b/package.json @@ -4,12 +4,15 @@ "type": "module", "private": true, "scripts": { - "dev": "bun run src/index.ts" + "dev": "bun run --hot src/index.ts" }, "devDependencies": { "@types/bun": "latest" }, "peerDependencies": { "typescript": "^7" + }, + "dependencies": { + "hono": "^4.13.8" } } diff --git a/src/index.ts b/src/index.ts index f67b2c6..7f18384 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1 +1,82 @@ -console.log("Hello via Bun!"); \ No newline at end of file +import { Hono } from "hono"; +import { ApiResponse } from "./modules/api_response"; +import { validator } from "hono/validator"; +import { postJson } from "./service/postJson"; +import { isEmpty } from "./utils/is_empty"; +import { get } from "./service/get"; +import { cors } from "hono/cors"; + +const tmpApp: Hono = new Hono() +export const app = tmpApp.basePath("/test/") +app.use("/test/*", cors()) + +// routers +app.get("/user", (c) => { + const re = get() + return c.json(ApiResponse.success(200, "Get user successfully", re)) +}) + +app.post( + "/user", + validator("json", (val, c) => { + if (isEmpty(val.name, val.passwd)) { + c.status(400) + return c.json(ApiResponse.err(400, "Param is invalid")) + } + return val + }), + c => { + const re = postJson() + return c.json(ApiResponse.success(200, "Add successfully", re)) + } +) + +app.patch("/user", + validator("json", (val, c) => { + if (isEmpty(val.id, val.changeFields)) { + c.status(400) + return c.json(ApiResponse.err(400, "Param is invalid")) + } + return val + }), + c => { + return c.json(ApiResponse.success(200, "Change user information successfully")) + } +) + +app.put("/user", + validator("json", (val, c) => { + if (isEmpty(val.id, val.changeFields)) { + c.status(400) + return c.json(ApiResponse.err(400, "Param is invalid")) + } + return val + }), + c => { + return c.json(ApiResponse.success(200, "Change user information successfully")) + } +) + +app.delete("/user", + validator("json", (val, c) => { + if (isEmpty(val.id)) { + c.status(400) + return c.json(ApiResponse.err(400, "Param is invalid")) + } + return val + }), + async (c) => { + const id = await c.req.json() + return c.json(ApiResponse.success(200, `Delete user successfully by id: ${id.id}`)) + } +) + +app.notFound((c) => { + console.log(`Not found [${c.req.method}]: ${c.req.path}`); + return c.json(ApiResponse.err(404, "Not Found")) +}) + +export default { + port: "8080", + fetch: app.fetch +} \ No newline at end of file diff --git a/src/modules/api_response.ts b/src/modules/api_response.ts new file mode 100644 index 0000000..b0f8318 --- /dev/null +++ b/src/modules/api_response.ts @@ -0,0 +1,27 @@ +export class ApiResponse { + code: number + msg: string + data?: T + + constructor(code: number, msg: string, data?: T) { + this.code = code + this.msg = msg + this.data = data + } + + static success( + code?: number | undefined, + msg?: string | undefined, + data?: T + ): ApiResponse { + return new ApiResponse(code ?? 200, msg ?? "ok", data) + } + + static err( + code?: number | undefined, + msg?: string | undefined, + data?: T + ): ApiResponse { + return new ApiResponse(code ?? 500, msg ?? "err", data) + } +} \ No newline at end of file diff --git a/src/service/get.ts b/src/service/get.ts new file mode 100644 index 0000000..e1fc2e1 --- /dev/null +++ b/src/service/get.ts @@ -0,0 +1,19 @@ +export function get() { + return [ + { + "id": "001", + "name": "XiaoAi", + "passwd": "000000" + }, + { + "id": "002", + "name": "XiaoMing", + "passwd": "000000" + }, + { + "id": "003", + "name": "XiaoHong", + "passwd": "000000" + } + ] +} \ No newline at end of file diff --git a/src/service/postJson.ts b/src/service/postJson.ts new file mode 100644 index 0000000..a7abc9f --- /dev/null +++ b/src/service/postJson.ts @@ -0,0 +1,5 @@ +export function postJson() { + return { + "id": "004" + } +} \ No newline at end of file diff --git a/src/utils/getMD5.ts b/src/utils/getMD5.ts new file mode 100644 index 0000000..4aeab8e --- /dev/null +++ b/src/utils/getMD5.ts @@ -0,0 +1,3 @@ +export function getMD5(str: string): string { + return Bun.CryptoHasher.hash("md5", str, "hex") +} \ No newline at end of file diff --git a/src/utils/is_empty.ts b/src/utils/is_empty.ts new file mode 100644 index 0000000..f9d34fb --- /dev/null +++ b/src/utils/is_empty.ts @@ -0,0 +1,10 @@ +export function isEmpty(...data: any[]): boolean { + var empty = false + data.forEach(e => { + if (e == null || e == undefined) { + empty = true + return + } + }) + return empty +} \ No newline at end of file