jax-hono 1.0.22 → 1.0.23
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/core/api-controller.ts +1 -22
- package/core/controller.ts +20 -0
- package/middleware/api-response.ts +5 -5
- package/package.json +1 -1
package/core/api-controller.ts
CHANGED
|
@@ -9,8 +9,6 @@ import { escapeRegex } from "../utils/regexp";
|
|
|
9
9
|
import type { Context } from "hono";
|
|
10
10
|
import { omit, pick, queue } from "jax-hono/utils";
|
|
11
11
|
|
|
12
|
-
const bodyCache = new WeakMap<Context, unknown>();
|
|
13
|
-
|
|
14
12
|
export abstract class ApiController extends Controller {
|
|
15
13
|
declare protected omit?: string | string[];
|
|
16
14
|
declare protected pick?: string | string[];
|
|
@@ -62,23 +60,7 @@ export abstract class ApiController extends Controller {
|
|
|
62
60
|
return this.context.req.param();
|
|
63
61
|
}
|
|
64
62
|
|
|
65
|
-
protected async body<T = Record<string, any>>() {
|
|
66
|
-
if (bodyCache.has(this.context)) {
|
|
67
|
-
return bodyCache.get(this.context) as T;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
try {
|
|
71
|
-
const body = await this.context.req.json<T>();
|
|
72
|
-
bodyCache.set(this.context, body);
|
|
73
|
-
|
|
74
|
-
return body;
|
|
75
|
-
} catch {
|
|
76
|
-
const body = {} as T;
|
|
77
|
-
bodyCache.set(this.context, body);
|
|
78
63
|
|
|
79
|
-
return body;
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
64
|
|
|
83
65
|
async index(c: Context) {
|
|
84
66
|
const Model = this.requireModel();
|
|
@@ -198,10 +180,7 @@ export abstract class ApiController extends Controller {
|
|
|
198
180
|
(this as any).searchKey,
|
|
199
181
|
(this as any).searchKeys,
|
|
200
182
|
);
|
|
201
|
-
const likeKeys = collectKeys(
|
|
202
|
-
(this as any).likeKey,
|
|
203
|
-
(this as any).likeKeys,
|
|
204
|
-
);
|
|
183
|
+
const likeKeys = collectKeys((this as any).likeKey, (this as any).likeKeys);
|
|
205
184
|
const keywordKeys = collectKeys((this as any).keywordKeys);
|
|
206
185
|
|
|
207
186
|
if (this.enableAutoFilter) {
|
package/core/controller.ts
CHANGED
|
@@ -6,6 +6,8 @@ const controllerContextStorage = new AsyncLocalStorage<Context>();
|
|
|
6
6
|
type ControllerConstructor<T extends Controller> = new (...args: any[]) => T;
|
|
7
7
|
type ControllerExport<T extends Controller> = T | ControllerConstructor<T>;
|
|
8
8
|
|
|
9
|
+
const bodyCache = new WeakMap<Context, unknown>();
|
|
10
|
+
|
|
9
11
|
export function runWithJaxContext<T>(c: Context, callback: () => T) {
|
|
10
12
|
return controllerContextStorage.run(c, callback);
|
|
11
13
|
}
|
|
@@ -86,4 +88,22 @@ export abstract class Controller {
|
|
|
86
88
|
prototype = Object.getPrototypeOf(prototype);
|
|
87
89
|
}
|
|
88
90
|
}
|
|
91
|
+
|
|
92
|
+
protected async body<T = Record<string, any>>() {
|
|
93
|
+
if (bodyCache.has(this.context)) {
|
|
94
|
+
return bodyCache.get(this.context) as T;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
try {
|
|
98
|
+
const body = await this.context.req.json<T>();
|
|
99
|
+
bodyCache.set(this.context, body);
|
|
100
|
+
|
|
101
|
+
return body;
|
|
102
|
+
} catch {
|
|
103
|
+
const body = {} as T;
|
|
104
|
+
bodyCache.set(this.context, body);
|
|
105
|
+
|
|
106
|
+
return body;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
89
109
|
}
|
|
@@ -7,7 +7,7 @@ export type ApiSuccessResponse<T = unknown> = {
|
|
|
7
7
|
};
|
|
8
8
|
|
|
9
9
|
export type ApiFailResponse = {
|
|
10
|
-
code:
|
|
10
|
+
code: number;
|
|
11
11
|
msg: string;
|
|
12
12
|
};
|
|
13
13
|
|
|
@@ -21,9 +21,9 @@ export function ok<T>(c: Context, data?: T, msg = 'success'): Response {
|
|
|
21
21
|
});
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
export function fail(c: Context, msg: string): Response {
|
|
24
|
+
export function fail(c: Context, msg: string, code = 1): Response {
|
|
25
25
|
return c.json<ApiFailResponse>({
|
|
26
|
-
code
|
|
26
|
+
code,
|
|
27
27
|
msg
|
|
28
28
|
});
|
|
29
29
|
}
|
|
@@ -31,14 +31,14 @@ export function fail(c: Context, msg: string): Response {
|
|
|
31
31
|
declare module 'hono' {
|
|
32
32
|
interface Context {
|
|
33
33
|
ok: <T>(data?: T, msg?: string) => Response;
|
|
34
|
-
fail: (msg: string) => Response;
|
|
34
|
+
fail: (msg: string, code?: number) => Response;
|
|
35
35
|
}
|
|
36
36
|
}
|
|
37
37
|
|
|
38
38
|
export const apiResponse: MiddlewareHandler = async (c, next) => {
|
|
39
39
|
// 把统一响应方法挂到 Hono Context 上,业务接口直接使用 c.ok / c.fail。
|
|
40
40
|
c.ok = <T>(data?: T, msg = 'success') => ok(c, data, msg);
|
|
41
|
-
c.fail = (msg: string) => fail(c, msg);
|
|
41
|
+
c.fail = (msg: string, code?: number) => fail(c, msg, code);
|
|
42
42
|
|
|
43
43
|
await next();
|
|
44
44
|
};
|