change: 规范响应描述
This commit is contained in:
1 parent
33172b0968
commit
3ad5f0915b
3 files changed
+40
-22
No files matched your search
@@ -0,0 +1,17 @@
|
||||
export class responseStr {
|
||||
static REQ_PARAM_ERR = "请求参数错误"
|
||||
static REQ_SYNTAX_ERR = "请求解析错误"
|
||||
|
||||
static USER_GET_SUCCESS = "用户获取成功"
|
||||
static USER_ADD_SUCCESS = "用户添加成功"
|
||||
static USER_DEL_SUCCESS = "用户删除成功"
|
||||
static USER_MOD_SUCCESS = "用户修改成功"
|
||||
|
||||
static FILE_SIZE_LIMIT_5M = "文件大小应不超过5MB"
|
||||
static FILE_TYPE_INVALID = "不是合法的文件"
|
||||
static FILE_UPLOAD_SUCCESS = "文件上传成功"
|
||||
|
||||
static UNEXPECTED_ERR = "未知错误"
|
||||
|
||||
static API_NOTFOUND = "找不到请求路径"
|
||||
}
|
||||
+15
-15
@@ -6,14 +6,14 @@ import { isEmpty } from "./utils/is_empty";
|
||||
import { getUser } from "./service/getUser";
|
||||
import { bodyLimit } from "hono/body-limit";
|
||||
import { getRandom } from "./service/getRandom";
|
||||
import { responseStr } from "./const/response_str";
|
||||
|
||||
const tmpApp: Hono = new Hono()
|
||||
export const app = tmpApp.basePath("/test/")
|
||||
|
||||
// routers
|
||||
app.get("/user", (c) => {
|
||||
const re = getUser()
|
||||
return c.json(ApiResponse.success(200, "Get user successfully", re))
|
||||
return c.json(ApiResponse.success(200, responseStr.USER_GET_SUCCESS, re))
|
||||
})
|
||||
|
||||
app.post(
|
||||
@@ -21,13 +21,13 @@ app.post(
|
||||
validator("json", (val, c) => {
|
||||
if (isEmpty(val.name, val.passwd)) {
|
||||
c.status(400)
|
||||
return c.json(ApiResponse.err(400, "Param is invalid"))
|
||||
return c.json(ApiResponse.err(400, responseStr.REQ_PARAM_ERR))
|
||||
}
|
||||
return val
|
||||
}),
|
||||
c => {
|
||||
const re = postUser()
|
||||
return c.json(ApiResponse.success(200, "Add successfully", re))
|
||||
return c.json(ApiResponse.success(200, responseStr.USER_ADD_SUCCESS, re))
|
||||
}
|
||||
)
|
||||
|
||||
@@ -35,12 +35,12 @@ 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 c.json(ApiResponse.err(400, responseStr.REQ_PARAM_ERR))
|
||||
}
|
||||
return val
|
||||
}),
|
||||
c => {
|
||||
return c.json(ApiResponse.success(200, "Change user information successfully"))
|
||||
return c.json(ApiResponse.success(200, responseStr.USER_MOD_SUCCESS))
|
||||
}
|
||||
)
|
||||
|
||||
@@ -48,12 +48,12 @@ 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 c.json(ApiResponse.err(400, responseStr.REQ_PARAM_ERR))
|
||||
}
|
||||
return val
|
||||
}),
|
||||
c => {
|
||||
return c.json(ApiResponse.success(200, "Change user information successfully"))
|
||||
return c.json(ApiResponse.success(200, responseStr.USER_MOD_SUCCESS))
|
||||
}
|
||||
)
|
||||
|
||||
@@ -61,13 +61,13 @@ app.delete("/user",
|
||||
validator("json", (val, c) => {
|
||||
if (isEmpty(val.id)) {
|
||||
c.status(400)
|
||||
return c.json(ApiResponse.err(400, "Param is invalid"))
|
||||
return c.json(ApiResponse.err(400, responseStr.REQ_PARAM_ERR))
|
||||
}
|
||||
return val
|
||||
}),
|
||||
async (c) => {
|
||||
const id = await c.req.json()
|
||||
return c.json(ApiResponse.success(200, `Delete user successfully by id: ${id.id}`))
|
||||
return c.json(ApiResponse.success(200, responseStr.USER_DEL_SUCCESS))
|
||||
}
|
||||
)
|
||||
|
||||
@@ -75,19 +75,19 @@ app.post("/file",
|
||||
bodyLimit({
|
||||
maxSize: 5 * 1024 * 1024,
|
||||
onError: (c) => {
|
||||
return c.json(ApiResponse.err(400, "File must less than 5MB"))
|
||||
return c.json(ApiResponse.err(400, responseStr.FILE_SIZE_LIMIT_5M))
|
||||
}
|
||||
}),
|
||||
async (c) => {
|
||||
const body = await c.req.parseBody()
|
||||
const file = body["file"]
|
||||
if (!(file instanceof File)) {
|
||||
return c.json(ApiResponse.err(400, "Not file"))
|
||||
return c.json(ApiResponse.err(400, responseStr.FILE_TYPE_INVALID))
|
||||
}
|
||||
|
||||
const fileName = file.name
|
||||
const fileSize = file.size
|
||||
return c.json(ApiResponse.success(200, "Upload file successfully", {
|
||||
return c.json(ApiResponse.success(200, responseStr.FILE_UPLOAD_SUCCESS, {
|
||||
fileName,
|
||||
fileSize
|
||||
}))
|
||||
@@ -98,7 +98,7 @@ 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 c.json(ApiResponse.err(400, responseStr.REQ_PARAM_ERR))
|
||||
}
|
||||
return val
|
||||
}),
|
||||
@@ -109,7 +109,7 @@ app.get("/random",
|
||||
|
||||
app.notFound((c) => {
|
||||
console.log(`Not found [${c.req.method}]: ${c.req.path}`);
|
||||
return c.json(ApiResponse.err(404, "Not Found"))
|
||||
return c.json(ApiResponse.err(404, responseStr.API_NOTFOUND))
|
||||
})
|
||||
|
||||
export default {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import type { Context } from "hono"
|
||||
import { ApiResponse } from "../modules/api_response"
|
||||
import { Random } from "../utils/random"
|
||||
import { responseStr } from "../const/response_str"
|
||||
|
||||
export function getRandom(c: Context) {
|
||||
const typeInReq = c.req.query("typeList")
|
||||
@@ -8,28 +9,28 @@ export function getRandom(c: Context) {
|
||||
let len: number
|
||||
if (typeInReq == "") {
|
||||
c.status(400)
|
||||
return c.json(ApiResponse.err(400, "Param is invalid"))
|
||||
return c.json(ApiResponse.err(400, responseStr.REQ_PARAM_ERR))
|
||||
}
|
||||
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]"))
|
||||
return c.json(ApiResponse.err(400, responseStr.REQ_PARAM_ERR))
|
||||
}
|
||||
if (typeList.length == 0) {
|
||||
c.status(400)
|
||||
return c.json(ApiResponse.err(400, "Array is empty in parameter[typeList]"))
|
||||
return c.json(ApiResponse.err(400, responseStr.REQ_PARAM_ERR))
|
||||
}
|
||||
|
||||
if (isNaN((Number(lenTmp)))) {
|
||||
c.status(400)
|
||||
return c.json(ApiResponse.err(400, "Parameter[length] must be a number"))
|
||||
return c.json(ApiResponse.err(400, responseStr.REQ_PARAM_ERR))
|
||||
} else {
|
||||
len = Number(lenTmp)
|
||||
if (len <= 0) {
|
||||
c.status(400)
|
||||
return c.json(ApiResponse.err(400, "Parameter[length] must > 0"))
|
||||
return c.json(ApiResponse.err(400, responseStr.REQ_PARAM_ERR))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,13 +42,13 @@ export function getRandom(c: Context) {
|
||||
c.status(400)
|
||||
return c.json(ApiResponse.err(400, e.message))
|
||||
} else {
|
||||
console.log("Unexcepted error");
|
||||
console.log(responseStr.UNEXPECTED_ERR);
|
||||
}
|
||||
}
|
||||
|
||||
if (result != undefined) {
|
||||
return c.json(ApiResponse.success(200, "ok", result))
|
||||
} else {
|
||||
return c.json(ApiResponse.err(500, "Unexcepted error"))
|
||||
return c.json(ApiResponse.err(500, responseStr.UNEXPECTED_ERR))
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user