@open-norantec/utilities 2.8.0 → 2.9.1

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.
@@ -33,21 +33,25 @@ type HandlerContext<IS extends HTTPBodyType, Bindings extends BindingsType, SS e
33
33
  interface HookContext {
34
34
  use: typeof Hono.prototype.use;
35
35
  }
36
- interface OnHandlingContext<Bindings extends BindingsType, SS extends z.ZodObject, A extends Array<keyof AuthenticationMethodMap<Bindings, SS>>, HWR> extends BaseContext<Bindings, SS>, HookContext {
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
37
  authentication: A extends [] ? null : A[number];
38
38
  identifier: string;
39
+ globals: G;
39
40
  wrapped: HWR;
40
41
  }
42
+ interface OnBeforeHandlingContext<G> extends HookContext {
43
+ globals: G;
44
+ }
41
45
  export declare class HonoUtil {
42
- static createHelper<Bindings extends BindingsType>(): <SS extends z.ZodObject, AMM extends AuthenticationMethodMap<Bindings, SS>, HWR>({ storageSchema, openAPIObject, openAPIPath, openAPIPrefix, authenticationMethods, onAfterHandling, onAppInitialized, onBeforeHandling, }: {
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, }: {
43
47
  storageSchema: SS;
44
48
  authenticationMethods?: AMM;
45
49
  openAPIObject?: PartialDeep<Omit<OpenAPIObject, "paths">>;
46
50
  openAPIPath?: string;
47
51
  openAPIPrefix?: string;
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>;
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>;
51
55
  }) => {
52
56
  register: <IS extends HTTPBodyType, OS extends HTTPBodyType, const A extends Array<AuthKey<AMM>>>(pathname: string, options: {
53
57
  inputSchema: IS;
@@ -74,7 +74,7 @@ class HonoUtil {
74
74
  },
75
75
  paths: {},
76
76
  }, _.omit(openAPIObject, ['paths']));
77
- onAppInitialized === null || onAppInitialized === void 0 ? void 0 : onAppInitialized(hookContext);
77
+ const globals = onAppInitialized === null || onAppInitialized === void 0 ? void 0 : onAppInitialized(hookContext);
78
78
  Object.entries(config).forEach(([pathname, { inputSchema, outputSchema, operationId, authenticationTypes, handler }]) => {
79
79
  openAPIDocument.paths[`${openAPIPrefix}${pathname}`.replace(/^\/+/g, '/')] = {
80
80
  post: {
@@ -109,7 +109,7 @@ class HonoUtil {
109
109
  },
110
110
  };
111
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)));
112
+ const wrapped = (yield Promise.resolve(onBeforeHandling === null || onBeforeHandling === void 0 ? void 0 : onBeforeHandling(Object.assign(Object.assign({}, hookContext), { globals: Object.assign({}, globals) }))));
113
113
  const input = typeof inputSchema === 'string' ? c.req.raw.body.getReader() : inputSchema.parse(yield c.req.json());
114
114
  const headers = c.req.header();
115
115
  const baseContext = {
@@ -152,14 +152,14 @@ class HonoUtil {
152
152
  return c.json({ error: authenticateResult.message }, authenticateResult instanceof StatusError ? authenticateResult.status : 400);
153
153
  }
154
154
  const { authentication, authenticationValue } = authenticateResult;
155
- let output = yield attempt_util_class_1.AttemptUtil.execPromise(handler(Object.assign({ input, authentication: authentication, identifier: authenticationValue, wrapped: wrapped }, baseContext)));
155
+ let output = yield attempt_util_class_1.AttemptUtil.execPromise(handler(Object.assign({ input, authentication: authentication, identifier: authenticationValue, wrapped: Object.assign({}, wrapped) }, baseContext)));
156
156
  if (!(output instanceof Error) && typeof outputSchema !== 'string') {
157
157
  output = attempt_util_class_1.AttemptUtil.exec(() => outputSchema.parse(output));
158
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);
159
+ onAfterHandling === null || onAfterHandling === void 0 ? void 0 : onAfterHandling(Object.assign(Object.assign(Object.assign({}, hookContext), baseContext), { wrapped: Object.assign({}, wrapped), authentication: authentication, identifier: authenticationValue, globals: Object.assign({}, globals) }), output instanceof Error ? output : null);
160
160
  return typeof outputSchema === 'string'
161
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);
162
+ : c.json(output instanceof Error ? { error: output.message } : { data: output }, output instanceof Error ? (output instanceof StatusError ? output.status : 500) : 200);
163
163
  }));
164
164
  });
165
165
  app.get(openAPIPath, (c) => __awaiter(this, void 0, void 0, function* () { return c.json(openAPIDocument); }));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-norantec/utilities",
3
- "version": "2.8.0",
3
+ "version": "2.9.1",
4
4
  "description": "NoranTec Utilities",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {