@open-norantec/utilities 2.9.2 → 2.10.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.
@@ -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, G> = 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];
29
- globals: G;
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, G> extends BaseContext<Bindings, SS>, HookContext {
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: G;
42
+ globals: InstanceType<GC>;
41
43
  wrapped: HWR;
42
44
  }
43
- interface OnBeforeHandlingContext<G> extends HookContext {
44
- globals: G;
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, G>({ storageSchema, openAPIObject, openAPIPath, openAPIPrefix, authenticationMethods, onAfterHandling, onAppInitialized, onBeforeHandling, }: {
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, G>, error: Error | null) => void | Promise<void>;
54
- onAppInitialized?: (context: HookContext) => G;
55
- onBeforeHandling?: (context: OnBeforeHandlingContext<G>) => HWR | Promise<HWR>;
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, G>) => Promise<z.infer<OS>>) => void;
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, "/">;
@@ -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 = onAppInitialized === null || onAppInitialized === void 0 ? void 0 : onAppInitialized(hookContext);
78
- const getGlobals = () => (_.isObject(globals) ? Object.assign({}, globals) : globals);
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: getGlobals() }))));
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: getGlobals() }, baseContext)));
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: getGlobals() }), output instanceof Error ? output : null);
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);
@@ -23,10 +23,11 @@ export declare class OctoUtil {
23
23
  retryRequest: (error: import("octokit").RequestError, retries: number, retryAfter: number) => import("octokit").RequestError;
24
24
  };
25
25
  };
26
- ensureReleaseByTagName({ tagName, createReleaseRequestParameters, getReleaseByTagRequestParameters, }: {
26
+ ensureReleaseByTagName({ tagName, sha, createReleaseRequestParameters, getReleaseByTagRequestParameters, }: {
27
27
  tagName: string;
28
28
  createReleaseRequestParameters?: RequestParameters;
29
29
  getReleaseByTagRequestParameters?: RequestParameters;
30
+ sha?: string;
30
31
  }): Promise<{
31
32
  url: string;
32
33
  html_url: string;
@@ -38,14 +38,14 @@ class OctoUtil {
38
38
  return this.octokitInstance;
39
39
  }
40
40
  ensureReleaseByTagName(_a) {
41
- return __awaiter(this, arguments, void 0, function* ({ tagName, createReleaseRequestParameters, getReleaseByTagRequestParameters, }) {
41
+ return __awaiter(this, arguments, void 0, function* ({ tagName, sha, createReleaseRequestParameters, getReleaseByTagRequestParameters, }) {
42
42
  return yield this.octokit.rest.repos
43
43
  .getReleaseByTag(Object.assign(Object.assign({}, getReleaseByTagRequestParameters), { owner: this.config.owner, repo: this.config.repo, tag: tagName }))
44
44
  .then((response) => response.data)
45
45
  .catch((error) => {
46
46
  if ((error === null || error === void 0 ? void 0 : error.status) === 404) {
47
47
  return this.octokit.rest.repos
48
- .createRelease(Object.assign(Object.assign({}, createReleaseRequestParameters), { tag_name: tagName, target_commitish: 'main', owner: this.config.owner, repo: this.config.repo }))
48
+ .createRelease(Object.assign(Object.assign({}, createReleaseRequestParameters), { tag_name: tagName, target_commitish: sha !== null && sha !== void 0 ? sha : 'main', owner: this.config.owner, repo: this.config.repo }))
49
49
  .then((response) => response.data);
50
50
  }
51
51
  else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-norantec/utilities",
3
- "version": "2.9.2",
3
+ "version": "2.10.1",
4
4
  "description": "NoranTec Utilities",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {