vovk 3.4.1 → 3.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/HttpException.d.ts +7 -0
- package/dist/HttpException.js +15 -0
- package/dist/JSONLinesResponse.d.ts +14 -0
- package/dist/JSONLinesResponse.js +59 -0
- package/dist/VovkApp.d.ts +29 -0
- package/dist/VovkApp.js +189 -0
- package/dist/client/index.d.ts +3 -0
- package/dist/client/index.js +7 -0
- package/dist/client/types.d.ts +102 -0
- package/dist/client/types.js +2 -0
- package/dist/createDecorator.d.ts +6 -0
- package/dist/createDecorator.js +43 -0
- package/dist/createVovkApp.d.ts +62 -0
- package/dist/createVovkApp.js +129 -0
- package/dist/openapi/openAPIToVovkSchema/index.d.ts +1 -1
- package/dist/openapi/openAPIToVovkSchema/index.js +29 -4
- package/dist/openapi/openAPIToVovkSchema/pruneComponentsSchemas.d.ts +7 -0
- package/dist/openapi/openAPIToVovkSchema/pruneComponentsSchemas.js +51 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/types/config.d.ts +11 -0
- package/dist/types.d.ts +239 -0
- package/dist/types.js +66 -0
- package/dist/utils/generateStaticAPI.d.ts +4 -0
- package/dist/utils/generateStaticAPI.js +30 -0
- package/dist/utils/getSchema.d.ts +20 -0
- package/dist/utils/getSchema.js +33 -0
- package/dist/utils/parseQuery.d.ts +25 -0
- package/dist/utils/parseQuery.js +156 -0
- package/dist/utils/reqForm.d.ts +2 -0
- package/dist/utils/reqForm.js +32 -0
- package/dist/utils/reqMeta.d.ts +2 -0
- package/dist/utils/reqMeta.js +13 -0
- package/dist/utils/reqQuery.d.ts +2 -0
- package/dist/utils/reqQuery.js +10 -0
- package/dist/utils/serializeQuery.d.ts +13 -0
- package/dist/utils/serializeQuery.js +65 -0
- package/dist/utils/setHandlerSchema.d.ts +4 -0
- package/dist/utils/setHandlerSchema.js +15 -0
- package/dist/utils/withValidation.d.ts +21 -0
- package/dist/utils/withValidation.js +88 -0
- package/package.json +1 -1
- package/dist/core/compose.d.ts +0 -38
- package/dist/core/compose.js +0 -31
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.createVovkApp = createVovkApp;
|
|
7
|
+
const VovkApp_1 = require("./VovkApp");
|
|
8
|
+
const types_1 = require("./types");
|
|
9
|
+
const getSchema_1 = __importDefault(require("./utils/getSchema"));
|
|
10
|
+
const trimPath = (path) => path.trim().replace(/^\/|\/$/g, '');
|
|
11
|
+
const isClass = (func) => typeof func === 'function' && /class/.test(func.toString());
|
|
12
|
+
const toKebabCase = (str) => str
|
|
13
|
+
.replace(/([a-z0-9])([A-Z])/g, '$1-$2') // Add hyphen between lowercase/digit and uppercase
|
|
14
|
+
.replace(/([A-Z])([A-Z])(?=[a-z])/g, '$1-$2') // Add hyphen between uppercase letters if the second one is followed by a lowercase
|
|
15
|
+
.toLowerCase()
|
|
16
|
+
.replace(/^-/, ''); // Remove leading hyphen
|
|
17
|
+
const assignSchema = ({ controller, propertyKey, path, options, httpMethod, vovkApp, }) => {
|
|
18
|
+
if (typeof window !== 'undefined') {
|
|
19
|
+
throw new Error('Decorators are intended for server-side use only. You have probably imported a controller on the client-side.');
|
|
20
|
+
}
|
|
21
|
+
if (!isClass(controller)) {
|
|
22
|
+
let decoratorName = httpMethod.toLowerCase();
|
|
23
|
+
if (decoratorName === 'delete')
|
|
24
|
+
decoratorName = 'del';
|
|
25
|
+
throw new Error(`Decorator must be used on a static class method. Check the controller method named "${propertyKey}" used with @${decoratorName}().`);
|
|
26
|
+
}
|
|
27
|
+
const methods = vovkApp.routes[httpMethod].get(controller) ?? {};
|
|
28
|
+
vovkApp.routes[httpMethod].set(controller, methods);
|
|
29
|
+
const originalMethod = controller[propertyKey];
|
|
30
|
+
originalMethod._controller = controller;
|
|
31
|
+
originalMethod._sourceMethod = originalMethod._sourceMethod ?? originalMethod;
|
|
32
|
+
const schema = originalMethod._sourceMethod._getSchema?.(controller);
|
|
33
|
+
controller._handlers = {
|
|
34
|
+
...controller._handlers,
|
|
35
|
+
[propertyKey]: {
|
|
36
|
+
...schema,
|
|
37
|
+
...(controller._handlers ?? {})[propertyKey],
|
|
38
|
+
path,
|
|
39
|
+
httpMethod,
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
methods[path] = controller[propertyKey];
|
|
43
|
+
methods[path]._options = options;
|
|
44
|
+
controller._handlersMetadata = {
|
|
45
|
+
...controller._handlersMetadata,
|
|
46
|
+
[propertyKey]: {
|
|
47
|
+
...(controller._handlersMetadata ?? {})[propertyKey],
|
|
48
|
+
staticParams: options?.staticParams,
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
function createVovkApp() {
|
|
53
|
+
const vovkApp = new VovkApp_1.VovkApp();
|
|
54
|
+
const createHTTPDecorator = (httpMethod) => {
|
|
55
|
+
function decoratorCreator(givenPath = '', options) {
|
|
56
|
+
const path = trimPath(givenPath);
|
|
57
|
+
function decorator(givenTarget, propertyKey) {
|
|
58
|
+
const controller = givenTarget;
|
|
59
|
+
assignSchema({ controller, propertyKey, path, options, httpMethod, vovkApp });
|
|
60
|
+
}
|
|
61
|
+
return decorator;
|
|
62
|
+
}
|
|
63
|
+
const auto = (options) => {
|
|
64
|
+
function decorator(givenTarget, propertyKey) {
|
|
65
|
+
const controller = givenTarget;
|
|
66
|
+
const methods = vovkApp.routes[httpMethod].get(controller) ?? {};
|
|
67
|
+
vovkApp.routes[httpMethod].set(controller, methods);
|
|
68
|
+
controller._handlers = {
|
|
69
|
+
...controller._handlers,
|
|
70
|
+
[propertyKey]: {
|
|
71
|
+
...(controller._handlers ?? {})[propertyKey],
|
|
72
|
+
httpMethod,
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
const properties = Object.keys(controller._handlers[propertyKey]?.validation?.params?.properties ?? {});
|
|
76
|
+
const kebab = toKebabCase(propertyKey); // 🥙
|
|
77
|
+
const path = properties.length ? `${kebab}/${properties.map((prop) => `:${prop}`).join('/')}` : kebab;
|
|
78
|
+
assignSchema({ controller, propertyKey, path, options, httpMethod, vovkApp });
|
|
79
|
+
}
|
|
80
|
+
return decorator;
|
|
81
|
+
};
|
|
82
|
+
const enhancedDecoratorCreator = decoratorCreator;
|
|
83
|
+
enhancedDecoratorCreator.auto = auto;
|
|
84
|
+
return enhancedDecoratorCreator;
|
|
85
|
+
};
|
|
86
|
+
const prefix = (givenPath = '') => {
|
|
87
|
+
const path = trimPath(givenPath);
|
|
88
|
+
return (givenTarget) => {
|
|
89
|
+
const controller = givenTarget;
|
|
90
|
+
controller._prefix = path;
|
|
91
|
+
return givenTarget;
|
|
92
|
+
};
|
|
93
|
+
};
|
|
94
|
+
const initVovk = (options) => {
|
|
95
|
+
for (const [rpcModuleName, controller] of Object.entries(options.controllers)) {
|
|
96
|
+
controller._rpcModuleName = rpcModuleName;
|
|
97
|
+
controller._activated = true;
|
|
98
|
+
controller._onError = options?.onError;
|
|
99
|
+
}
|
|
100
|
+
async function GET_DEV(req, data) {
|
|
101
|
+
const params = await data.params;
|
|
102
|
+
if (params[Object.keys(params)[0]]?.[0] === '_schema_') {
|
|
103
|
+
const schema = (0, getSchema_1.default)(options);
|
|
104
|
+
return vovkApp.respond(200, { schema });
|
|
105
|
+
}
|
|
106
|
+
return vovkApp.GET(req, data);
|
|
107
|
+
}
|
|
108
|
+
return {
|
|
109
|
+
GET: process.env.NODE_ENV === 'development' ? GET_DEV : vovkApp.GET,
|
|
110
|
+
POST: vovkApp.POST,
|
|
111
|
+
PUT: vovkApp.PUT,
|
|
112
|
+
PATCH: vovkApp.PATCH,
|
|
113
|
+
DELETE: vovkApp.DELETE,
|
|
114
|
+
HEAD: vovkApp.HEAD,
|
|
115
|
+
OPTIONS: vovkApp.OPTIONS,
|
|
116
|
+
};
|
|
117
|
+
};
|
|
118
|
+
return {
|
|
119
|
+
get: createHTTPDecorator(types_1.HttpMethod.GET),
|
|
120
|
+
post: createHTTPDecorator(types_1.HttpMethod.POST),
|
|
121
|
+
put: createHTTPDecorator(types_1.HttpMethod.PUT),
|
|
122
|
+
patch: createHTTPDecorator(types_1.HttpMethod.PATCH),
|
|
123
|
+
del: createHTTPDecorator(types_1.HttpMethod.DELETE),
|
|
124
|
+
head: createHTTPDecorator(types_1.HttpMethod.HEAD),
|
|
125
|
+
options: createHTTPDecorator(types_1.HttpMethod.OPTIONS),
|
|
126
|
+
prefix,
|
|
127
|
+
initVovk,
|
|
128
|
+
};
|
|
129
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { VovkSchema } from '../../types/core.js';
|
|
2
2
|
import type { VovkOpenAPIMixinNormalized } from '../../types/config.js';
|
|
3
|
-
export declare function openAPIToVovkSchema({ apiRoot, source: { object: openAPIObject }, getModuleName, getMethodName, errorMessageKey, segmentName, }: VovkOpenAPIMixinNormalized & {
|
|
3
|
+
export declare function openAPIToVovkSchema({ apiRoot, source: { object: openAPIObject }, getModuleName, getMethodName, filterOperations, pruneComponents, errorMessageKey, segmentName, }: VovkOpenAPIMixinNormalized & {
|
|
4
4
|
segmentName?: string;
|
|
5
5
|
}): VovkSchema;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { applyComponentsSchemas } from './applyComponentsSchemas.js';
|
|
2
2
|
import { inlineRefs } from './inlineRefs.js';
|
|
3
|
+
import { pruneComponentsSchemas } from './pruneComponentsSchemas.js';
|
|
3
4
|
import { VovkSchemaIdEnum } from '../../types/enums.js';
|
|
4
5
|
import { schemaToTsType } from '../../samples/schemaToTsType.js';
|
|
5
6
|
function getTsTypeString(contentType, schema) {
|
|
@@ -19,7 +20,7 @@ function getTsTypeString(contentType, schema) {
|
|
|
19
20
|
}));
|
|
20
21
|
return [...tsTypes].join(' | ') || schemaToTsType(schema);
|
|
21
22
|
}
|
|
22
|
-
export function openAPIToVovkSchema({ apiRoot, source: { object: openAPIObject }, getModuleName, getMethodName, errorMessageKey, segmentName, }) {
|
|
23
|
+
export function openAPIToVovkSchema({ apiRoot, source: { object: openAPIObject }, getModuleName, getMethodName, filterOperations, pruneComponents, errorMessageKey, segmentName, }) {
|
|
23
24
|
segmentName = segmentName ?? '';
|
|
24
25
|
const forceApiRoot = apiRoot ||
|
|
25
26
|
(openAPIObject.servers?.[0]?.url ??
|
|
@@ -47,10 +48,19 @@ export function openAPIToVovkSchema({ apiRoot, source: { object: openAPIObject }
|
|
|
47
48
|
},
|
|
48
49
|
};
|
|
49
50
|
const segment = schema.segments[segmentName];
|
|
50
|
-
|
|
51
|
+
Object.entries(paths ?? {}).forEach(([path, operations]) => {
|
|
51
52
|
Object.entries(operations ?? {})
|
|
52
53
|
.filter(([, operation]) => operation && typeof operation === 'object')
|
|
53
54
|
.forEach(([method, operation]) => {
|
|
55
|
+
if (filterOperations &&
|
|
56
|
+
!filterOperations({
|
|
57
|
+
method: method.toUpperCase(),
|
|
58
|
+
path,
|
|
59
|
+
openAPIObject,
|
|
60
|
+
operationObject: operation,
|
|
61
|
+
})) {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
54
64
|
const rpcModuleName = getModuleName({
|
|
55
65
|
method: method.toUpperCase(),
|
|
56
66
|
path,
|
|
@@ -149,6 +159,21 @@ export function openAPIToVovkSchema({ apiRoot, source: { object: openAPIObject }
|
|
|
149
159
|
},
|
|
150
160
|
};
|
|
151
161
|
});
|
|
152
|
-
|
|
153
|
-
|
|
162
|
+
});
|
|
163
|
+
if (pruneComponents && noPathsOpenAPIObject.components?.schemas) {
|
|
164
|
+
// Reassign with fresh objects only — `noPathsOpenAPIObject` shares references with the
|
|
165
|
+
// caller's spec, so the original `components.schemas` must stay untouched. Walking the
|
|
166
|
+
// whole controllers tree (validation slots + raw operation objects) keeps every `$ref`
|
|
167
|
+
// a kept handler carries resolvable against the pruned meta.
|
|
168
|
+
segment.meta = {
|
|
169
|
+
openAPIObject: {
|
|
170
|
+
...noPathsOpenAPIObject,
|
|
171
|
+
components: {
|
|
172
|
+
...noPathsOpenAPIObject.components,
|
|
173
|
+
schemas: pruneComponentsSchemas(segment.controllers, noPathsOpenAPIObject.components.schemas),
|
|
174
|
+
},
|
|
175
|
+
},
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
return schema;
|
|
154
179
|
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { ComponentsObject } from 'openapi3-ts/oas31';
|
|
2
|
+
/**
|
|
3
|
+
* Shrinks a `components.schemas` dict to the transitive `$ref` closure of `roots`
|
|
4
|
+
* (BFS with a visited set — component graphs of large specs like Stripe are cyclic).
|
|
5
|
+
* Preserves the original key order for deterministic output.
|
|
6
|
+
*/
|
|
7
|
+
export declare function pruneComponentsSchemas(roots: unknown, componentsSchemas: NonNullable<ComponentsObject['schemas']>): NonNullable<ComponentsObject['schemas']>;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// Collect the trailing name of every `$ref` in the tree. Covers both pointer styles
|
|
2
|
+
// the transform emits: `#/components/schemas/X` (response slots, raw operation objects)
|
|
3
|
+
// and `#/$defs/X` (request slots embed components under their original names).
|
|
4
|
+
function collectRefNames(node, into) {
|
|
5
|
+
if (!node || typeof node !== 'object')
|
|
6
|
+
return;
|
|
7
|
+
if (Array.isArray(node)) {
|
|
8
|
+
for (const item of node) {
|
|
9
|
+
collectRefNames(item, into);
|
|
10
|
+
}
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
for (const [key, value] of Object.entries(node)) {
|
|
14
|
+
if (key === '$ref' && typeof value === 'string') {
|
|
15
|
+
const name = value.split('/').pop();
|
|
16
|
+
if (name)
|
|
17
|
+
into.add(name);
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
collectRefNames(value, into);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Shrinks a `components.schemas` dict to the transitive `$ref` closure of `roots`
|
|
26
|
+
* (BFS with a visited set — component graphs of large specs like Stripe are cyclic).
|
|
27
|
+
* Preserves the original key order for deterministic output.
|
|
28
|
+
*/
|
|
29
|
+
export function pruneComponentsSchemas(roots, componentsSchemas) {
|
|
30
|
+
const required = new Set();
|
|
31
|
+
collectRefNames(roots, required);
|
|
32
|
+
const queue = [...required];
|
|
33
|
+
const visited = new Set();
|
|
34
|
+
while (queue.length) {
|
|
35
|
+
const name = queue.pop();
|
|
36
|
+
if (!name || visited.has(name))
|
|
37
|
+
continue;
|
|
38
|
+
visited.add(name);
|
|
39
|
+
const component = componentsSchemas[name];
|
|
40
|
+
if (!component)
|
|
41
|
+
continue;
|
|
42
|
+
const refs = new Set();
|
|
43
|
+
collectRefNames(component, refs);
|
|
44
|
+
for (const ref of refs) {
|
|
45
|
+
required.add(ref);
|
|
46
|
+
if (!visited.has(ref))
|
|
47
|
+
queue.push(ref);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return Object.fromEntries(Object.entries(componentsSchemas).filter(([name]) => required.has(name)));
|
|
51
|
+
}
|