@open-norantec/utilities 2.7.0 → 2.9.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.
@@ -24,30 +24,41 @@ 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>>> = BaseContext<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 OnAfterHandlingContext<Bindings extends BindingsType, SS extends z.ZodObject, A extends Array<keyof AuthenticationMethodMap<Bindings, SS>>, HWR, G> extends BaseContext<Bindings, SS>, HookContext {
37
+ authentication: A extends [] ? null : A[number];
38
+ identifier: string;
39
+ globals: G;
40
+ wrapped: HWR;
41
+ }
42
+ interface OnBeforeHandlingContext<G> extends HookContext {
43
+ globals: G;
44
+ }
35
45
  export declare class HonoUtil {
36
- static createHelper<Bindings extends BindingsType>(): <SS extends z.ZodObject, AMM extends AuthenticationMethodMap<Bindings, SS>>({ storageSchema, openAPIObject, openAPIPath, openAPIPrefix, authenticationMethods, onAfterSetRoutes, onBeforeSetRoutes, }: {
46
+ static createHelper<Bindings extends BindingsType>(): <SS extends z.ZodObject, AMM extends AuthenticationMethodMap<Bindings, SS>, HWR, G>({ storageSchema, openAPIObject, openAPIPath, openAPIPrefix, authenticationMethods, onAfterHandling, onAppInitialized, onBeforeHandling, }: {
37
47
  storageSchema: SS;
38
48
  authenticationMethods?: AMM;
39
49
  openAPIObject?: PartialDeep<Omit<OpenAPIObject, "paths">>;
40
50
  openAPIPath?: string;
41
51
  openAPIPrefix?: string;
42
- onAfterSetRoutes?: (context: HookContext) => void;
43
- onBeforeSetRoutes?: (context: HookContext) => void;
52
+ onAfterHandling?: (context: OnAfterHandlingContext<Bindings, SS, Array<keyof AuthenticationMethodMap<Bindings, SS>>, HWR, G>, error: Error | null) => void | Promise<void>;
53
+ onAppInitialized?: (context: HookContext) => G;
54
+ onBeforeHandling?: (context: OnBeforeHandlingContext<G>) => HWR | Promise<HWR>;
44
55
  }) => {
45
56
  register: <IS extends HTTPBodyType, OS extends HTTPBodyType, const A extends Array<AuthKey<AMM>>>(pathname: string, options: {
46
57
  inputSchema: IS;
47
58
  outputSchema: OS;
48
59
  authenticationTypes?: A;
49
- operatorId?: string;
50
- }, handler: (context: HandlerContext<IS, Bindings, SS, A>) => Promise<z.infer<OS>>) => void;
60
+ operationId?: string;
61
+ }, handler: (context: HandlerContext<IS, Bindings, SS, A, HWR>) => Promise<z.infer<OS>>) => void;
51
62
  getApp: () => Hono<{
52
63
  Bindings: Bindings;
53
64
  }, import("hono/types").BlankSchema, "/">;
@@ -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, onAfterSetRoutes, onBeforeSetRoutes, }) {
60
+ return function ({ storageSchema, openAPIObject = {}, openAPIPath = '/openapi', openAPIPrefix = '', authenticationMethods, onAfterHandling, onAppInitialized, onBeforeHandling, }) {
61
61
  const config = {};
62
62
  return {
63
63
  register: function (pathname, options, handler) {
@@ -74,10 +74,11 @@ class HonoUtil {
74
74
  },
75
75
  paths: {},
76
76
  }, _.omit(openAPIObject, ['paths']));
77
- onBeforeSetRoutes === null || onBeforeSetRoutes === void 0 ? void 0 : onBeforeSetRoutes(hookContext);
78
- Object.entries(config).forEach(([pathname, { inputSchema, outputSchema, authenticationTypes, handler }]) => {
77
+ const globals = onAppInitialized === null || onAppInitialized === void 0 ? void 0 : onAppInitialized(hookContext);
78
+ Object.entries(config).forEach(([pathname, { inputSchema, outputSchema, operationId, authenticationTypes, handler }]) => {
79
79
  openAPIDocument.paths[`${openAPIPrefix}${pathname}`.replace(/^\/+/g, '/')] = {
80
80
  post: {
81
+ operationId,
81
82
  requestBody: {
82
83
  required: true,
83
84
  content: typeof inputSchema === 'string'
@@ -108,6 +109,7 @@ class HonoUtil {
108
109
  },
109
110
  };
110
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(Object.assign(Object.assign({}, hookContext), { globals: globals }))));
111
113
  const input = typeof inputSchema === 'string' ? c.req.raw.body.getReader() : inputSchema.parse(yield c.req.json());
112
114
  const headers = c.req.header();
113
115
  const baseContext = {
@@ -150,26 +152,17 @@ class HonoUtil {
150
152
  return c.json({ error: authenticateResult.message }, authenticateResult instanceof StatusError ? authenticateResult.status : 400);
151
153
  }
152
154
  const { authentication, authenticationValue } = authenticateResult;
153
- const rawOutput = yield attempt_util_class_1.AttemptUtil.execPromise(handler(Object.assign({ input, authentication: authentication, identifier: authenticationValue }, baseContext)));
154
- if (rawOutput instanceof Error) {
155
- console.log('Handler error', rawOutput, rawOutput === null || rawOutput === void 0 ? void 0 : rawOutput.message, rawOutput === null || rawOutput === void 0 ? void 0 : rawOutput.stack);
156
- return c.json({ error: rawOutput.message }, rawOutput instanceof StatusError ? rawOutput.status : 400);
157
- }
158
- if (typeof outputSchema === 'string') {
159
- return c.body(rawOutput, 200, { 'Content-Type': outputSchema });
160
- }
161
- else {
162
- const output = attempt_util_class_1.AttemptUtil.exec(() => outputSchema.parse(rawOutput));
163
- if (output instanceof Error) {
164
- console.log('Return error', output, output === null || output === void 0 ? void 0 : output.message, output === null || output === void 0 ? void 0 : output.stack);
165
- return c.json({ error: output.message }, 400);
166
- }
167
- 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));
168
158
  }
159
+ onAfterHandling === null || onAfterHandling === void 0 ? void 0 : onAfterHandling(Object.assign(Object.assign(Object.assign({}, hookContext), baseContext), { wrapped: wrapped, authentication: authentication, identifier: authenticationValue, globals: globals }), 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 } : { data: output }, output instanceof Error ? (output instanceof StatusError ? output.status : 500) : 200);
169
163
  }));
170
164
  });
171
165
  app.get(openAPIPath, (c) => __awaiter(this, void 0, void 0, function* () { return c.json(openAPIDocument); }));
172
- onAfterSetRoutes === null || onAfterSetRoutes === void 0 ? void 0 : onAfterSetRoutes(hookContext);
173
166
  return app;
174
167
  },
175
168
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-norantec/utilities",
3
- "version": "2.7.0",
3
+ "version": "2.9.0",
4
4
  "description": "NoranTec Utilities",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {