@open-norantec/utilities 2.4.3 → 2.5.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 +42 -0
- package/dist/hono-util.class.js +152 -0
- package/dist/octo-util.class.js +1 -1
- package/package.json +8 -2
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { ExecutionContext, Hono } from 'hono';
|
|
2
|
+
import { z } from './zod';
|
|
3
|
+
import { Bindings as BindingsType } from 'hono/types';
|
|
4
|
+
import { PartialDeep } from 'type-fest';
|
|
5
|
+
import { ContentfulStatusCode } from 'hono/utils/http-status';
|
|
6
|
+
import { OpenAPIObject } from 'openapi3-ts/oas31';
|
|
7
|
+
type HTTPBodyType = z.ZodType<any> | string;
|
|
8
|
+
export declare class StatusError extends Error {
|
|
9
|
+
readonly status: ContentfulStatusCode;
|
|
10
|
+
readonly statusText?: string | undefined;
|
|
11
|
+
constructor(status: ContentfulStatusCode, statusText?: string | undefined);
|
|
12
|
+
}
|
|
13
|
+
interface ConfigItem<IS extends HTTPBodyType, OS extends HTTPBodyType, Bindings extends BindingsType, SS extends z.ZodObject> {
|
|
14
|
+
inputSchema: IS;
|
|
15
|
+
outputSchema: OS;
|
|
16
|
+
handler: (context: {
|
|
17
|
+
env: Bindings;
|
|
18
|
+
executionCtx: ExecutionContext;
|
|
19
|
+
headers: Record<string, string>;
|
|
20
|
+
input: IS extends string ? ReadableStreamDefaultReader<Uint8Array> : z.infer<IS>;
|
|
21
|
+
storage: z.infer<SS>;
|
|
22
|
+
}) => Promise<z.infer<OS>>;
|
|
23
|
+
}
|
|
24
|
+
interface HookContext {
|
|
25
|
+
use: typeof Hono.prototype.use;
|
|
26
|
+
}
|
|
27
|
+
export declare class HonoUtil {
|
|
28
|
+
static createHelper<Bindings extends BindingsType>(): <SS extends z.ZodObject>({ storageSchema, openAPIObject, openAPIPath, openAPIPrefix, onAfterSetRoutes, onBeforeSetRoutes, }: {
|
|
29
|
+
storageSchema: SS;
|
|
30
|
+
openAPIObject?: PartialDeep<Omit<OpenAPIObject, "paths">>;
|
|
31
|
+
openAPIPath?: string;
|
|
32
|
+
openAPIPrefix?: string;
|
|
33
|
+
onAfterSetRoutes?: (context: HookContext) => void;
|
|
34
|
+
onBeforeSetRoutes?: (context: HookContext) => void;
|
|
35
|
+
}) => {
|
|
36
|
+
register: <IS extends HTTPBodyType, OS extends HTTPBodyType>(pathname: string, inputSchema: IS, outputSchema: OS, handler: ConfigItem<IS, OS, Bindings, SS>["handler"]) => void;
|
|
37
|
+
getApp: () => Hono<{
|
|
38
|
+
Bindings: Bindings;
|
|
39
|
+
}, import("hono/types").BlankSchema, "/">;
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
export {};
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
36
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
37
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
38
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
39
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
40
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
41
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
42
|
+
});
|
|
43
|
+
};
|
|
44
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
45
|
+
exports.HonoUtil = exports.StatusError = void 0;
|
|
46
|
+
const hono_1 = require("hono");
|
|
47
|
+
const zod_1 = require("./zod");
|
|
48
|
+
const attempt_util_class_1 = require("./attempt-util.class");
|
|
49
|
+
const _ = __importStar(require("lodash"));
|
|
50
|
+
class StatusError extends Error {
|
|
51
|
+
constructor(status, statusText) {
|
|
52
|
+
super();
|
|
53
|
+
this.status = status;
|
|
54
|
+
this.statusText = statusText;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
exports.StatusError = StatusError;
|
|
58
|
+
class HonoUtil {
|
|
59
|
+
static createHelper() {
|
|
60
|
+
return function ({ storageSchema, openAPIObject = {}, openAPIPath = '/openapi', openAPIPrefix = '', onAfterSetRoutes, onBeforeSetRoutes, }) {
|
|
61
|
+
const config = {};
|
|
62
|
+
return {
|
|
63
|
+
register: function (pathname, inputSchema, outputSchema, handler) {
|
|
64
|
+
config[pathname] = {
|
|
65
|
+
inputSchema,
|
|
66
|
+
outputSchema,
|
|
67
|
+
handler,
|
|
68
|
+
};
|
|
69
|
+
},
|
|
70
|
+
getApp: function () {
|
|
71
|
+
const app = new hono_1.Hono();
|
|
72
|
+
const hookContext = { use: app.use.bind(app) };
|
|
73
|
+
const openAPIDocument = _.merge({
|
|
74
|
+
openapi: '3.1.0',
|
|
75
|
+
info: {
|
|
76
|
+
title: 'API Documentation',
|
|
77
|
+
version: '1.0.0',
|
|
78
|
+
},
|
|
79
|
+
paths: {},
|
|
80
|
+
}, _.omit(openAPIObject, ['paths']));
|
|
81
|
+
onBeforeSetRoutes === null || onBeforeSetRoutes === void 0 ? void 0 : onBeforeSetRoutes(hookContext);
|
|
82
|
+
Object.entries(config).forEach(([pathname, { inputSchema, outputSchema, handler }]) => {
|
|
83
|
+
openAPIDocument.paths[`${openAPIPrefix}${pathname}`.replace(/^\/+/g, '/')] = {
|
|
84
|
+
post: {
|
|
85
|
+
requestBody: {
|
|
86
|
+
required: true,
|
|
87
|
+
content: typeof inputSchema === 'string'
|
|
88
|
+
? {
|
|
89
|
+
[inputSchema]: {},
|
|
90
|
+
}
|
|
91
|
+
: {
|
|
92
|
+
'application/json': { schema: inputSchema.toJSONSchema() },
|
|
93
|
+
},
|
|
94
|
+
},
|
|
95
|
+
responses: {
|
|
96
|
+
'200': {
|
|
97
|
+
content: typeof outputSchema === 'string'
|
|
98
|
+
? {
|
|
99
|
+
[outputSchema]: {},
|
|
100
|
+
}
|
|
101
|
+
: {
|
|
102
|
+
'application/json': {
|
|
103
|
+
schema: zod_1.z
|
|
104
|
+
.object({
|
|
105
|
+
data: outputSchema,
|
|
106
|
+
})
|
|
107
|
+
.toJSONSchema(),
|
|
108
|
+
},
|
|
109
|
+
},
|
|
110
|
+
},
|
|
111
|
+
},
|
|
112
|
+
},
|
|
113
|
+
};
|
|
114
|
+
app.post(pathname, (c) => __awaiter(this, void 0, void 0, function* () {
|
|
115
|
+
const input = typeof inputSchema === 'string' ? c.req.raw.body.getReader() : inputSchema.parse(yield c.req.json());
|
|
116
|
+
const headers = c.req.header();
|
|
117
|
+
const rawOutput = yield attempt_util_class_1.AttemptUtil.execPromise(handler({
|
|
118
|
+
input,
|
|
119
|
+
headers,
|
|
120
|
+
env: c.env,
|
|
121
|
+
executionCtx: c.executionCtx,
|
|
122
|
+
storage: storageSchema.parse(Object.keys(storageSchema.shape).reduce((result, key) => {
|
|
123
|
+
result[key] = c.get(key);
|
|
124
|
+
return result;
|
|
125
|
+
}, {})),
|
|
126
|
+
}));
|
|
127
|
+
if (rawOutput instanceof Error) {
|
|
128
|
+
console.log('Handler error', rawOutput, rawOutput === null || rawOutput === void 0 ? void 0 : rawOutput.message, rawOutput === null || rawOutput === void 0 ? void 0 : rawOutput.stack);
|
|
129
|
+
return c.json({ error: rawOutput.message }, rawOutput instanceof StatusError ? rawOutput.status : 400);
|
|
130
|
+
}
|
|
131
|
+
if (typeof outputSchema === 'string') {
|
|
132
|
+
return c.body(rawOutput, 200, { 'Content-Type': outputSchema });
|
|
133
|
+
}
|
|
134
|
+
else {
|
|
135
|
+
const output = attempt_util_class_1.AttemptUtil.exec(() => outputSchema.parse(rawOutput));
|
|
136
|
+
if (output instanceof Error) {
|
|
137
|
+
console.log('Return error', output, output === null || output === void 0 ? void 0 : output.message, output === null || output === void 0 ? void 0 : output.stack);
|
|
138
|
+
return c.json({ error: output.message }, 400);
|
|
139
|
+
}
|
|
140
|
+
return c.json({ data: output });
|
|
141
|
+
}
|
|
142
|
+
}));
|
|
143
|
+
});
|
|
144
|
+
app.get(openAPIPath, (c) => __awaiter(this, void 0, void 0, function* () { return c.json(openAPIDocument); }));
|
|
145
|
+
onAfterSetRoutes === null || onAfterSetRoutes === void 0 ? void 0 : onAfterSetRoutes(hookContext);
|
|
146
|
+
return app;
|
|
147
|
+
},
|
|
148
|
+
};
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
exports.HonoUtil = HonoUtil;
|
package/dist/octo-util.class.js
CHANGED
|
@@ -34,7 +34,7 @@ class OctoUtil {
|
|
|
34
34
|
get octokit() {
|
|
35
35
|
if (this.octokitInstance instanceof octokit_1.Octokit)
|
|
36
36
|
return this.octokitInstance;
|
|
37
|
-
this.octokitInstance = new octokit_1.Octokit({ auth:
|
|
37
|
+
this.octokitInstance = new octokit_1.Octokit({ auth: this.config.auth });
|
|
38
38
|
return this.octokitInstance;
|
|
39
39
|
}
|
|
40
40
|
ensureReleaseByTagName(_a) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@open-norantec/utilities",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.5.0",
|
|
4
4
|
"description": "NoranTec Utilities",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -36,6 +36,7 @@
|
|
|
36
36
|
"@types/node": "18.11.18",
|
|
37
37
|
"cross-env": "^7.0.3",
|
|
38
38
|
"eslint": "^10.3.0",
|
|
39
|
+
"hono": "^4.13.2",
|
|
39
40
|
"octokit": "^5.0.5",
|
|
40
41
|
"rimraf": "^6.0.1",
|
|
41
42
|
"ts-node": "^10.0.0",
|
|
@@ -50,16 +51,21 @@
|
|
|
50
51
|
"eslint-config-prettier": "^10.0.0",
|
|
51
52
|
"eslint-plugin-prettier": "^5.0.0",
|
|
52
53
|
"lodash": "^4.18.1",
|
|
54
|
+
"openapi3-ts": "^4.6.1",
|
|
53
55
|
"prettier": "^3.0.0",
|
|
54
56
|
"uuid": "^11.1.0",
|
|
55
57
|
"zod": "^4.4.3"
|
|
56
58
|
},
|
|
57
59
|
"peerDependencies": {
|
|
58
|
-
"octokit": ">=5.0.0"
|
|
60
|
+
"octokit": ">=5.0.0",
|
|
61
|
+
"hono": ">=4.0.0"
|
|
59
62
|
},
|
|
60
63
|
"peerDependenciesMeta": {
|
|
61
64
|
"octokit": {
|
|
62
65
|
"optional": true
|
|
66
|
+
},
|
|
67
|
+
"hono": {
|
|
68
|
+
"optional": true
|
|
63
69
|
}
|
|
64
70
|
}
|
|
65
71
|
}
|