@open-norantec/utilities 2.6.2 → 2.8.0
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/dist/hono-util.class.d.ts +17 -5
- package/dist/hono-util.class.js +14 -26
- package/package.json +1 -1
|
@@ -24,25 +24,37 @@ interface AuthenticationMethod<Bindings extends BindingsType, SS extends z.ZodOb
|
|
|
24
24
|
}
|
|
25
25
|
type AuthenticationMethodMap<Bindings extends BindingsType, SS extends z.ZodObject> = Record<string, AuthenticationMethod<Bindings, SS>>;
|
|
26
26
|
type AuthKey<AMM extends AuthenticationMethodMap<{}, z.ZodObject>> = Extract<keyof AMM, string>;
|
|
27
|
-
type HandlerContext<IS extends HTTPBodyType, Bindings extends BindingsType, SS extends z.ZodObject, A extends Array<keyof AuthenticationMethodMap<Bindings, SS
|
|
27
|
+
type HandlerContext<IS extends HTTPBodyType, Bindings extends BindingsType, SS extends z.ZodObject, A extends Array<keyof AuthenticationMethodMap<Bindings, SS>>, HWR> = BaseContext<Bindings, SS> & {
|
|
28
28
|
authentication: A extends [] ? null : A[number];
|
|
29
29
|
identifier: string;
|
|
30
30
|
input: IS extends string ? ReadableStreamDefaultReader<Uint8Array> : z.infer<IS>;
|
|
31
|
+
wrapped: HWR;
|
|
31
32
|
};
|
|
32
33
|
interface HookContext {
|
|
33
34
|
use: typeof Hono.prototype.use;
|
|
34
35
|
}
|
|
36
|
+
interface OnHandlingContext<Bindings extends BindingsType, SS extends z.ZodObject, A extends Array<keyof AuthenticationMethodMap<Bindings, SS>>, HWR> extends BaseContext<Bindings, SS>, HookContext {
|
|
37
|
+
authentication: A extends [] ? null : A[number];
|
|
38
|
+
identifier: string;
|
|
39
|
+
wrapped: HWR;
|
|
40
|
+
}
|
|
35
41
|
export declare class HonoUtil {
|
|
36
|
-
static createHelper<Bindings extends BindingsType>(): <SS extends z.ZodObject, AMM extends AuthenticationMethodMap<Bindings, SS
|
|
42
|
+
static createHelper<Bindings extends BindingsType>(): <SS extends z.ZodObject, AMM extends AuthenticationMethodMap<Bindings, SS>, HWR>({ storageSchema, openAPIObject, openAPIPath, openAPIPrefix, authenticationMethods, onAfterHandling, onAppInitialized, onBeforeHandling, }: {
|
|
37
43
|
storageSchema: SS;
|
|
38
44
|
authenticationMethods?: AMM;
|
|
39
45
|
openAPIObject?: PartialDeep<Omit<OpenAPIObject, "paths">>;
|
|
40
46
|
openAPIPath?: string;
|
|
41
47
|
openAPIPrefix?: string;
|
|
42
|
-
|
|
43
|
-
|
|
48
|
+
onAfterHandling?: (context: OnHandlingContext<Bindings, SS, Array<keyof AuthenticationMethodMap<Bindings, SS>>, HWR>, error: Error | null) => void | Promise<void>;
|
|
49
|
+
onAppInitialized?: (context: HookContext) => void | Promise<void>;
|
|
50
|
+
onBeforeHandling?: (context: HookContext) => HWR | Promise<HWR>;
|
|
44
51
|
}) => {
|
|
45
|
-
register: <IS extends HTTPBodyType, OS extends HTTPBodyType, const A extends Array<AuthKey<AMM>>>(pathname: string,
|
|
52
|
+
register: <IS extends HTTPBodyType, OS extends HTTPBodyType, const A extends Array<AuthKey<AMM>>>(pathname: string, options: {
|
|
53
|
+
inputSchema: IS;
|
|
54
|
+
outputSchema: OS;
|
|
55
|
+
authenticationTypes?: A;
|
|
56
|
+
operationId?: string;
|
|
57
|
+
}, handler: (context: HandlerContext<IS, Bindings, SS, A, HWR>) => Promise<z.infer<OS>>) => void;
|
|
46
58
|
getApp: () => Hono<{
|
|
47
59
|
Bindings: Bindings;
|
|
48
60
|
}, import("hono/types").BlankSchema, "/">;
|
package/dist/hono-util.class.js
CHANGED
|
@@ -57,16 +57,11 @@ class StatusError extends Error {
|
|
|
57
57
|
exports.StatusError = StatusError;
|
|
58
58
|
class HonoUtil {
|
|
59
59
|
static createHelper() {
|
|
60
|
-
return function ({ storageSchema, openAPIObject = {}, openAPIPath = '/openapi', openAPIPrefix = '', authenticationMethods,
|
|
60
|
+
return function ({ storageSchema, openAPIObject = {}, openAPIPath = '/openapi', openAPIPrefix = '', authenticationMethods, onAfterHandling, onAppInitialized, onBeforeHandling, }) {
|
|
61
61
|
const config = {};
|
|
62
62
|
return {
|
|
63
|
-
register: function (pathname,
|
|
64
|
-
config[pathname] = {
|
|
65
|
-
inputSchema,
|
|
66
|
-
outputSchema,
|
|
67
|
-
authenticationTypes,
|
|
68
|
-
handler,
|
|
69
|
-
};
|
|
63
|
+
register: function (pathname, options, handler) {
|
|
64
|
+
config[pathname] = Object.assign(Object.assign({}, options), { handler, authenticationTypes: Array.isArray(options === null || options === void 0 ? void 0 : options.authenticationTypes) ? options.authenticationTypes : [] });
|
|
70
65
|
},
|
|
71
66
|
getApp: function () {
|
|
72
67
|
const app = new hono_1.Hono();
|
|
@@ -79,10 +74,11 @@ class HonoUtil {
|
|
|
79
74
|
},
|
|
80
75
|
paths: {},
|
|
81
76
|
}, _.omit(openAPIObject, ['paths']));
|
|
82
|
-
|
|
83
|
-
Object.entries(config).forEach(([pathname, { inputSchema, outputSchema, authenticationTypes, handler }]) => {
|
|
77
|
+
onAppInitialized === null || onAppInitialized === void 0 ? void 0 : onAppInitialized(hookContext);
|
|
78
|
+
Object.entries(config).forEach(([pathname, { inputSchema, outputSchema, operationId, authenticationTypes, handler }]) => {
|
|
84
79
|
openAPIDocument.paths[`${openAPIPrefix}${pathname}`.replace(/^\/+/g, '/')] = {
|
|
85
80
|
post: {
|
|
81
|
+
operationId,
|
|
86
82
|
requestBody: {
|
|
87
83
|
required: true,
|
|
88
84
|
content: typeof inputSchema === 'string'
|
|
@@ -113,6 +109,7 @@ class HonoUtil {
|
|
|
113
109
|
},
|
|
114
110
|
};
|
|
115
111
|
app.post(pathname, (c) => __awaiter(this, void 0, void 0, function* () {
|
|
112
|
+
const wrapped = (yield Promise.resolve(onBeforeHandling === null || onBeforeHandling === void 0 ? void 0 : onBeforeHandling(hookContext)));
|
|
116
113
|
const input = typeof inputSchema === 'string' ? c.req.raw.body.getReader() : inputSchema.parse(yield c.req.json());
|
|
117
114
|
const headers = c.req.header();
|
|
118
115
|
const baseContext = {
|
|
@@ -155,26 +152,17 @@ class HonoUtil {
|
|
|
155
152
|
return c.json({ error: authenticateResult.message }, authenticateResult instanceof StatusError ? authenticateResult.status : 400);
|
|
156
153
|
}
|
|
157
154
|
const { authentication, authenticationValue } = authenticateResult;
|
|
158
|
-
|
|
159
|
-
if (
|
|
160
|
-
|
|
161
|
-
return c.json({ error: rawOutput.message }, rawOutput instanceof StatusError ? rawOutput.status : 400);
|
|
162
|
-
}
|
|
163
|
-
if (typeof outputSchema === 'string') {
|
|
164
|
-
return c.body(rawOutput, 200, { 'Content-Type': outputSchema });
|
|
165
|
-
}
|
|
166
|
-
else {
|
|
167
|
-
const output = attempt_util_class_1.AttemptUtil.exec(() => outputSchema.parse(rawOutput));
|
|
168
|
-
if (output instanceof Error) {
|
|
169
|
-
console.log('Return error', output, output === null || output === void 0 ? void 0 : output.message, output === null || output === void 0 ? void 0 : output.stack);
|
|
170
|
-
return c.json({ error: output.message }, 400);
|
|
171
|
-
}
|
|
172
|
-
return c.json({ data: output });
|
|
155
|
+
let output = yield attempt_util_class_1.AttemptUtil.execPromise(handler(Object.assign({ input, authentication: authentication, identifier: authenticationValue, wrapped: wrapped }, baseContext)));
|
|
156
|
+
if (!(output instanceof Error) && typeof outputSchema !== 'string') {
|
|
157
|
+
output = attempt_util_class_1.AttemptUtil.exec(() => outputSchema.parse(output));
|
|
173
158
|
}
|
|
159
|
+
onAfterHandling === null || onAfterHandling === void 0 ? void 0 : onAfterHandling(Object.assign(Object.assign(Object.assign({}, hookContext), baseContext), { wrapped: wrapped, authentication: authentication, identifier: authenticationValue }), output instanceof Error ? output : null);
|
|
160
|
+
return typeof outputSchema === 'string'
|
|
161
|
+
? c.body(output, 200, { 'Content-Type': outputSchema })
|
|
162
|
+
: c.json(output instanceof Error ? { error: output.message } : output, output instanceof Error ? (output instanceof StatusError ? output.status : 500) : 200);
|
|
174
163
|
}));
|
|
175
164
|
});
|
|
176
165
|
app.get(openAPIPath, (c) => __awaiter(this, void 0, void 0, function* () { return c.json(openAPIDocument); }));
|
|
177
|
-
onAfterSetRoutes === null || onAfterSetRoutes === void 0 ? void 0 : onAfterSetRoutes(hookContext);
|
|
178
166
|
return app;
|
|
179
167
|
},
|
|
180
168
|
};
|