@open-norantec/utilities 2.9.2 → 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 -12
- package/dist/hono-util.class.js +6 -6
- 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,9 +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,
|
|
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];
|
|
29
|
-
globals:
|
|
31
|
+
globals: InstanceType<GC>;
|
|
30
32
|
identifier: string;
|
|
31
33
|
input: IS extends string ? ReadableStreamDefaultReader<Uint8Array> : z.infer<IS>;
|
|
32
34
|
wrapped: HWR;
|
|
@@ -34,32 +36,36 @@ type HandlerContext<IS extends HTTPBodyType, Bindings extends BindingsType, SS e
|
|
|
34
36
|
interface HookContext {
|
|
35
37
|
use: typeof Hono.prototype.use;
|
|
36
38
|
}
|
|
37
|
-
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 {
|
|
38
40
|
authentication: A extends [] ? null : A[number];
|
|
39
41
|
identifier: string;
|
|
40
|
-
globals:
|
|
42
|
+
globals: InstanceType<GC>;
|
|
41
43
|
wrapped: HWR;
|
|
42
44
|
}
|
|
43
|
-
interface OnBeforeHandlingContext<
|
|
44
|
-
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>;
|
|
45
50
|
}
|
|
46
51
|
export declare class HonoUtil {
|
|
47
|
-
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, }: {
|
|
48
53
|
storageSchema: SS;
|
|
49
54
|
authenticationMethods?: AMM;
|
|
55
|
+
GlobalsClass: GC | null;
|
|
50
56
|
openAPIObject?: PartialDeep<Omit<OpenAPIObject, "paths">>;
|
|
51
57
|
openAPIPath?: string;
|
|
52
58
|
openAPIPrefix?: string;
|
|
53
|
-
onAfterHandling?: (context: OnAfterHandlingContext<Bindings, SS, Array<keyof AuthenticationMethodMap<Bindings, SS>>, HWR,
|
|
54
|
-
onAppInitialized?: (context:
|
|
55
|
-
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>;
|
|
56
62
|
}) => {
|
|
57
63
|
register: <IS extends HTTPBodyType, OS extends HTTPBodyType, const A extends Array<AuthKey<AMM>>>(pathname: string, options: {
|
|
58
64
|
inputSchema: IS;
|
|
59
65
|
outputSchema: OS;
|
|
60
66
|
authenticationTypes?: A;
|
|
61
67
|
operationId?: string;
|
|
62
|
-
}, handler: (context: HandlerContext<IS, Bindings, SS, A, HWR,
|
|
68
|
+
}, handler: (context: HandlerContext<IS, Bindings, SS, A, HWR, GC>) => Promise<z.infer<OS>>) => void;
|
|
63
69
|
getApp: () => Hono<{
|
|
64
70
|
Bindings: Bindings;
|
|
65
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,8 +74,8 @@ class HonoUtil {
|
|
|
74
74
|
},
|
|
75
75
|
paths: {},
|
|
76
76
|
}, _.omit(openAPIObject, ['paths']));
|
|
77
|
-
const globals =
|
|
78
|
-
|
|
77
|
+
const globals = GlobalsClass === null ? null : new GlobalsClass();
|
|
78
|
+
onAppInitialized === null || onAppInitialized === void 0 ? void 0 : onAppInitialized(Object.assign(Object.assign({}, hookContext), { globals }));
|
|
79
79
|
Object.entries(config).forEach(([pathname, { inputSchema, outputSchema, operationId, authenticationTypes, handler }]) => {
|
|
80
80
|
openAPIDocument.paths[`${openAPIPrefix}${pathname}`.replace(/^\/+/g, '/')] = {
|
|
81
81
|
post: {
|
|
@@ -110,7 +110,7 @@ class HonoUtil {
|
|
|
110
110
|
},
|
|
111
111
|
};
|
|
112
112
|
app.post(pathname, (c) => __awaiter(this, void 0, void 0, function* () {
|
|
113
|
-
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
114
|
const getWrapped = () => (_.isObject(wrapped) ? Object.assign({}, wrapped) : wrapped);
|
|
115
115
|
const input = typeof inputSchema === 'string' ? c.req.raw.body.getReader() : inputSchema.parse(yield c.req.json());
|
|
116
116
|
const headers = c.req.header();
|
|
@@ -154,11 +154,11 @@ class HonoUtil {
|
|
|
154
154
|
return c.json({ error: authenticateResult.message }, authenticateResult instanceof StatusError ? authenticateResult.status : 400);
|
|
155
155
|
}
|
|
156
156
|
const { authentication, authenticationValue } = authenticateResult;
|
|
157
|
-
let output = yield attempt_util_class_1.AttemptUtil.execPromise(handler(Object.assign({ input, authentication: authentication, identifier: authenticationValue, wrapped: getWrapped(), globals
|
|
157
|
+
let output = yield attempt_util_class_1.AttemptUtil.execPromise(handler(Object.assign({ input, authentication: authentication, identifier: authenticationValue, wrapped: getWrapped(), globals }, baseContext)));
|
|
158
158
|
if (!(output instanceof Error) && typeof outputSchema !== 'string') {
|
|
159
159
|
output = attempt_util_class_1.AttemptUtil.exec(() => outputSchema.parse(output));
|
|
160
160
|
}
|
|
161
|
-
onAfterHandling === null || onAfterHandling === void 0 ? void 0 : onAfterHandling(Object.assign(Object.assign(Object.assign({}, hookContext), baseContext), { wrapped: getWrapped(), authentication: authentication, identifier: authenticationValue, globals
|
|
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);
|
|
162
162
|
return typeof outputSchema === 'string'
|
|
163
163
|
? c.body(output, 200, { 'Content-Type': outputSchema })
|
|
164
164
|
: c.json(output instanceof Error ? { error: output.message } : { data: output }, output instanceof Error ? (output instanceof StatusError ? output.status : 500) : 200);
|