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
|
||||
}
|
||||
Reference in new issue
Block a user