done: 基础接口
This commit is contained in:
1 parent
cd99a2736b
commit
30d499f858
8 files changed
+155
-2
No files matched your search
+82
-1
@@ -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
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
export function postJson() {
|
||||
return {
|
||||
"id": "004"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
export function getMD5(str: string): string {
|
||||
return Bun.CryptoHasher.hash("md5", str, "hex")
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
Reference in new issue
Block a user