@open-norantec/utilities 2.9.1 → 2.10.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 +18 -11
- package/dist/hono-util.class.js +7 -5
- package/package.json +1 -1
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { Context, ExecutionContext, Hono } from 'hono';
|
|
2
2
|
import { z } from './zod';
|
|
3
3
|
import { Bindings as BindingsType } from 'hono/types';
|
|
4
|
-
import { PartialDeep } from 'type-fest';
|
|
4
|
+
import { Constructor, PartialDeep } from 'type-fest';
|
|
5
5
|
import { ContentfulStatusCode } from 'hono/utils/http-status';
|
|
6
6
|
import { OpenAPIObject } from 'openapi3-ts/oas31';
|
|
7
7
|
type HTTPBodyType = z.ZodType<any> | string;
|
|
8
|
+
type GlobalsClassType = Constructor<any> | null;
|
|
9
|
+
type InstanceType<T extends (abstract new (...args: any) => any) | null> = T extends null ? null : T extends abstract new (...args: any) => infer R ? R : any;
|
|
8
10
|
export declare class StatusError extends Error {
|
|
9
11
|
readonly status: ContentfulStatusCode;
|
|
10
12
|
readonly statusText?: string | undefined;
|
|
@@ -24,8 +26,9 @@ interface AuthenticationMethod<Bindings extends BindingsType, SS extends z.ZodOb
|
|
|
24
26
|
}
|
|
25
27
|
type AuthenticationMethodMap<Bindings extends BindingsType, SS extends z.ZodObject> = Record<string, AuthenticationMethod<Bindings, SS>>;
|
|
26
28
|
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>>, HWR> = BaseContext<Bindings, SS> & {
|
|
29
|
+
type HandlerContext<IS extends HTTPBodyType, Bindings extends BindingsType, SS extends z.ZodObject, A extends Array<keyof AuthenticationMethodMap<Bindings, SS>>, HWR, GC extends GlobalsClassType> = BaseContext<Bindings, SS> & {
|
|
28
30
|
authentication: A extends [] ? null : A[number];
|
|
31
|
+
globals: InstanceType<GC>;
|
|
29
32
|
identifier: string;
|
|
30
33
|
input: IS extends string ? ReadableStreamDefaultReader<Uint8Array> : z.infer<IS>;
|
|
31
34
|
wrapped: HWR;
|
|
@@ -33,32 +36,36 @@ type HandlerContext<IS extends HTTPBodyType, Bindings extends BindingsType, SS e
|
|
|
33
36
|
interface HookContext {
|
|
34
37
|
use: typeof Hono.prototype.use;
|
|
35
38
|
}
|
|
36
|
-
interface OnAfterHandlingContext<Bindings extends BindingsType, SS extends z.ZodObject, A extends Array<keyof AuthenticationMethodMap<Bindings, SS>>, HWR,
|
|
39
|
+
interface OnAfterHandlingContext<Bindings extends BindingsType, SS extends z.ZodObject, A extends Array<keyof AuthenticationMethodMap<Bindings, SS>>, HWR, GC extends GlobalsClassType> extends BaseContext<Bindings, SS>, HookContext {
|
|
37
40
|
authentication: A extends [] ? null : A[number];
|
|
38
41
|
identifier: string;
|
|
39
|
-
globals:
|
|
42
|
+
globals: InstanceType<GC>;
|
|
40
43
|
wrapped: HWR;
|
|
41
44
|
}
|
|
42
|
-
interface OnBeforeHandlingContext<
|
|
43
|
-
globals:
|
|
45
|
+
interface OnBeforeHandlingContext<GC extends GlobalsClassType> extends HookContext {
|
|
46
|
+
globals: InstanceType<GC>;
|
|
47
|
+
}
|
|
48
|
+
interface OnAppInitializedContext<GC extends GlobalsClassType> extends HookContext {
|
|
49
|
+
globals: InstanceType<GC>;
|
|
44
50
|
}
|
|
45
51
|
export declare class HonoUtil {
|
|
46
|
-
static createHelper<Bindings extends BindingsType>(): <SS extends z.ZodObject, AMM extends AuthenticationMethodMap<Bindings, SS>, HWR,
|
|
52
|
+
static createHelper<Bindings extends BindingsType>(): <SS extends z.ZodObject, AMM extends AuthenticationMethodMap<Bindings, SS>, HWR, GC extends GlobalsClassType>({ storageSchema, openAPIObject, openAPIPath, openAPIPrefix, authenticationMethods, GlobalsClass, onAfterHandling, onAppInitialized, onBeforeHandling, }: {
|
|
47
53
|
storageSchema: SS;
|
|
48
54
|
authenticationMethods?: AMM;
|
|
55
|
+
GlobalsClass: GC | null;
|
|
49
56
|
openAPIObject?: PartialDeep<Omit<OpenAPIObject, "paths">>;
|
|
50
57
|
openAPIPath?: string;
|
|
51
58
|
openAPIPrefix?: string;
|
|
52
|
-
onAfterHandling?: (context: OnAfterHandlingContext<Bindings, SS, Array<keyof AuthenticationMethodMap<Bindings, SS>>, HWR,
|
|
53
|
-
onAppInitialized?: (context:
|
|
54
|
-
onBeforeHandling?: (context: OnBeforeHandlingContext<
|
|
59
|
+
onAfterHandling?: (context: OnAfterHandlingContext<Bindings, SS, Array<keyof AuthenticationMethodMap<Bindings, SS>>, HWR, GC>, error: Error | null) => void | Promise<void>;
|
|
60
|
+
onAppInitialized?: (context: OnAppInitializedContext<GC>) => void | Promise<void>;
|
|
61
|
+
onBeforeHandling?: (context: OnBeforeHandlingContext<GC>) => HWR | Promise<HWR>;
|
|
55
62
|
}) => {
|
|
56
63
|
register: <IS extends HTTPBodyType, OS extends HTTPBodyType, const A extends Array<AuthKey<AMM>>>(pathname: string, options: {
|
|
57
64
|
inputSchema: IS;
|
|
58
65
|
outputSchema: OS;
|
|
59
66
|
authenticationTypes?: A;
|
|
60
67
|
operationId?: string;
|
|
61
|
-
}, handler: (context: HandlerContext<IS, Bindings, SS, A, HWR>) => Promise<z.infer<OS>>) => void;
|
|
68
|
+
}, handler: (context: HandlerContext<IS, Bindings, SS, A, HWR, GC>) => Promise<z.infer<OS>>) => void;
|
|
62
69
|
getApp: () => Hono<{
|
|
63
70
|
Bindings: Bindings;
|
|
64
71
|
}, import("hono/types").BlankSchema, "/">;
|
package/dist/hono-util.class.js
CHANGED
|
@@ -57,7 +57,7 @@ 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, onAfterHandling, onAppInitialized, onBeforeHandling, }) {
|
|
60
|
+
return function ({ storageSchema, openAPIObject = {}, openAPIPath = '/openapi', openAPIPrefix = '', authenticationMethods, GlobalsClass, onAfterHandling, onAppInitialized, onBeforeHandling, }) {
|
|
61
61
|
const config = {};
|
|
62
62
|
return {
|
|
63
63
|
register: function (pathname, options, handler) {
|
|
@@ -74,7 +74,8 @@ class HonoUtil {
|
|
|
74
74
|
},
|
|
75
75
|
paths: {},
|
|
76
76
|
}, _.omit(openAPIObject, ['paths']));
|
|
77
|
-
const globals =
|
|
77
|
+
const globals = GlobalsClass === null ? null : new GlobalsClass();
|
|
78
|
+
onAppInitialized === null || onAppInitialized === void 0 ? void 0 : onAppInitialized(Object.assign(Object.assign({}, hookContext), { globals }));
|
|
78
79
|
Object.entries(config).forEach(([pathname, { inputSchema, outputSchema, operationId, authenticationTypes, handler }]) => {
|
|
79
80
|
openAPIDocument.paths[`${openAPIPrefix}${pathname}`.replace(/^\/+/g, '/')] = {
|
|
80
81
|
post: {
|
|
@@ -109,7 +110,8 @@ class HonoUtil {
|
|
|
109
110
|
},
|
|
110
111
|
};
|
|
111
112
|
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(Object.assign(Object.assign({}, hookContext), { globals
|
|
113
|
+
const wrapped = (yield Promise.resolve(onBeforeHandling === null || onBeforeHandling === void 0 ? void 0 : onBeforeHandling(Object.assign(Object.assign({}, hookContext), { globals }))));
|
|
114
|
+
const getWrapped = () => (_.isObject(wrapped) ? Object.assign({}, wrapped) : wrapped);
|
|
113
115
|
const input = typeof inputSchema === 'string' ? c.req.raw.body.getReader() : inputSchema.parse(yield c.req.json());
|
|
114
116
|
const headers = c.req.header();
|
|
115
117
|
const baseContext = {
|
|
@@ -152,11 +154,11 @@ class HonoUtil {
|
|
|
152
154
|
return c.json({ error: authenticateResult.message }, authenticateResult instanceof StatusError ? authenticateResult.status : 400);
|
|
153
155
|
}
|
|
154
156
|
const { authentication, authenticationValue } = authenticateResult;
|
|
155
|
-
let output = yield attempt_util_class_1.AttemptUtil.execPromise(handler(Object.assign({ input, authentication: authentication, identifier: authenticationValue, wrapped:
|
|
157
|
+
let output = yield attempt_util_class_1.AttemptUtil.execPromise(handler(Object.assign({ input, authentication: authentication, identifier: authenticationValue, wrapped: getWrapped(), globals }, baseContext)));
|
|
156
158
|
if (!(output instanceof Error) && typeof outputSchema !== 'string') {
|
|
157
159
|
output = attempt_util_class_1.AttemptUtil.exec(() => outputSchema.parse(output));
|
|
158
160
|
}
|
|
159
|
-
onAfterHandling === null || onAfterHandling === void 0 ? void 0 : onAfterHandling(Object.assign(Object.assign(Object.assign({}, hookContext), baseContext), { wrapped:
|
|
161
|
+
onAfterHandling === null || onAfterHandling === void 0 ? void 0 : onAfterHandling(Object.assign(Object.assign(Object.assign({}, hookContext), baseContext), { wrapped: getWrapped(), authentication: authentication, identifier: authenticationValue, globals }), output instanceof Error ? output : null);
|
|
160
162
|
return typeof outputSchema === 'string'
|
|
161
163
|
? c.body(output, 200, { 'Content-Type': outputSchema })
|
|
162
164
|
: c.json(output instanceof Error ? { error: output.message } : { data: output }, output instanceof Error ? (output instanceof StatusError ? output.status : 500) : 200);
|