done: 基础接口

This commit is contained in:
cancerai committed 2026-09-23 20:02:23 +08:00
1 parent cd99a2736b
commit 30d499f858
8 files changed
+155 -2

No files matched your search

+5
View File
@@ -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=="],
+4 -1
View File
@@ -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"
}
}
+82 -1
View File
@@ -1 +1,82 @@
console.log("Hello via Bun!");
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
}
+27
View File
@@ -0,0 +1,27 @@
export class ApiResponse<T> {
code: number
msg: string
data?: T
constructor(code: number, msg: string, data?: T) {
this.code = code
this.msg = msg
this.data = data
}
static success<T>(
code?: number | undefined,
msg?: string | undefined,
data?: T
): ApiResponse<T> {
return new ApiResponse(code ?? 200, msg ?? "ok", data)
}
static err<T>(
code?: number | undefined,
msg?: string | undefined,
data?: T
): ApiResponse<T> {
return new ApiResponse(code ?? 500, msg ?? "err", data)
}
}
+19
View File
@@ -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"
}
]
}
+5
View File
@@ -0,0 +1,5 @@
export function postJson() {
return {
"id": "004"
}
}
+3
View File
@@ -0,0 +1,3 @@
export function getMD5(str: string): string {
return Bun.CryptoHasher.hash("md5", str, "hex")
}
+10
View File
@@ -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
}