vovk 3.4.0 → 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/internal.d.ts +1 -0
- package/dist/internal.js +1 -0
- package/dist/openapi/openAPIToVovkSchema/applyComponentsSchemas.d.ts +21 -1
- package/dist/openapi/openAPIToVovkSchema/applyComponentsSchemas.js +37 -12
- package/dist/openapi/openAPIToVovkSchema/index.d.ts +1 -1
- package/dist/openapi/openAPIToVovkSchema/index.js +32 -6
- package/dist/openapi/openAPIToVovkSchema/pruneComponentsSchemas.d.ts +7 -0
- package/dist/openapi/openAPIToVovkSchema/pruneComponentsSchemas.js +51 -0
- package/dist/types/config.d.ts +11 -0
- package/package.json +1 -1
package/dist/internal.d.ts
CHANGED
|
@@ -5,6 +5,7 @@ export { withValidationLibrary } from './validation/withValidationLibrary.js';
|
|
|
5
5
|
export { validationSchemasObjectToSingleValidationSchema } from './validation/validationSchemasObjectToSingleValidationSchema.js';
|
|
6
6
|
export { operation } from './openapi/operation.js';
|
|
7
7
|
export { openAPIToVovkSchema } from './openapi/openAPIToVovkSchema/index.js';
|
|
8
|
+
export { applyComponentsSchemas, reattachMixinDefs, } from './openapi/openAPIToVovkSchema/applyComponentsSchemas.js';
|
|
8
9
|
export { vovkSchemaToOpenAPI } from './openapi/vovkSchemaToOpenAPI.js';
|
|
9
10
|
export { readableStreamToAsyncIterable } from './client/defaultStreamHandler.js';
|
|
10
11
|
export { VovkSchemaIdEnum } from './types/enums.js';
|
package/dist/internal.js
CHANGED
|
@@ -6,6 +6,7 @@ export { withValidationLibrary } from './validation/withValidationLibrary.js';
|
|
|
6
6
|
export { validationSchemasObjectToSingleValidationSchema } from './validation/validationSchemasObjectToSingleValidationSchema.js';
|
|
7
7
|
export { operation } from './openapi/operation.js';
|
|
8
8
|
export { openAPIToVovkSchema } from './openapi/openAPIToVovkSchema/index.js';
|
|
9
|
+
export { applyComponentsSchemas, reattachMixinDefs, } from './openapi/openAPIToVovkSchema/applyComponentsSchemas.js';
|
|
9
10
|
export { vovkSchemaToOpenAPI } from './openapi/vovkSchemaToOpenAPI.js';
|
|
10
11
|
export { readableStreamToAsyncIterable } from './client/defaultStreamHandler.js';
|
|
11
12
|
export { VovkSchemaIdEnum } from './types/enums.js';
|
|
@@ -1,3 +1,23 @@
|
|
|
1
1
|
import type { ComponentsObject } from 'openapi3-ts/oas31';
|
|
2
2
|
import type { VovkJSONSchemaBase } from '../../types/json-schema.js';
|
|
3
|
-
export declare function applyComponentsSchemas(schema: VovkJSONSchemaBase, components: ComponentsObject['schemas'], mixinName: string
|
|
3
|
+
export declare function applyComponentsSchemas(schema: VovkJSONSchemaBase, components: ComponentsObject['schemas'], mixinName: string,
|
|
4
|
+
/**
|
|
5
|
+
* true (default): embed the ref closure in `$defs` (self-contained — for AJV + Rust).
|
|
6
|
+
* false: keep `#/components/schemas/X`, emit no `$defs` (response slots, typed via
|
|
7
|
+
* `x-tsType`) — avoids the per-handler dup that overflows JSON.stringify on big specs.
|
|
8
|
+
*/
|
|
9
|
+
emitDefs?: boolean): VovkJSONSchemaBase | VovkJSONSchemaBase[];
|
|
10
|
+
/**
|
|
11
|
+
* Re-attach a response slot's `$defs` closure at render time, for generators that
|
|
12
|
+
* resolve `$ref` against a self-contained schema (Rust). Pulls components from the
|
|
13
|
+
* segment's shared meta → identical to the `emitDefs=true` slot. No-op for non-mixin.
|
|
14
|
+
*/
|
|
15
|
+
export declare function reattachMixinDefs(slot: VovkJSONSchemaBase | undefined, segment: {
|
|
16
|
+
segmentType?: string;
|
|
17
|
+
segmentName: string;
|
|
18
|
+
meta?: {
|
|
19
|
+
openAPIObject?: {
|
|
20
|
+
components?: ComponentsObject;
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
}): VovkJSONSchemaBase | VovkJSONSchemaBase[] | undefined;
|
|
@@ -14,14 +14,22 @@ function cloneJSON(obj) {
|
|
|
14
14
|
}
|
|
15
15
|
return result;
|
|
16
16
|
}
|
|
17
|
-
export function applyComponentsSchemas(schema, components, mixinName
|
|
17
|
+
export function applyComponentsSchemas(schema, components, mixinName,
|
|
18
|
+
/**
|
|
19
|
+
* true (default): embed the ref closure in `$defs` (self-contained — for AJV + Rust).
|
|
20
|
+
* false: keep `#/components/schemas/X`, emit no `$defs` (response slots, typed via
|
|
21
|
+
* `x-tsType`) — avoids the per-handler dup that overflows JSON.stringify on big specs.
|
|
22
|
+
*/
|
|
23
|
+
emitDefs = true) {
|
|
18
24
|
const key = 'components/schemas';
|
|
19
25
|
if (!components || !Object.keys(components).length)
|
|
20
26
|
return schema;
|
|
21
27
|
// Create a deep copy of the schema
|
|
22
28
|
const result = cloneJSON(schema);
|
|
23
|
-
// Initialize $defs
|
|
24
|
-
|
|
29
|
+
// Initialize $defs only when embedding (self-contained slots).
|
|
30
|
+
if (emitDefs) {
|
|
31
|
+
result.$defs = result.$defs || {};
|
|
32
|
+
}
|
|
25
33
|
// Set to track components we've added to $defs
|
|
26
34
|
const addedComponents = new Set();
|
|
27
35
|
// Process a schema object and replace $refs
|
|
@@ -38,18 +46,22 @@ export function applyComponentsSchemas(schema, components, mixinName) {
|
|
|
38
46
|
if ($ref && typeof $ref === 'string' && $ref.startsWith(`#/${key}/`)) {
|
|
39
47
|
const componentName = $ref.replace(`#/${key}/`, '');
|
|
40
48
|
if (components?.[componentName]) {
|
|
41
|
-
|
|
49
|
+
// Set `x-tsType` so TS resolves the ref without local `$defs`.
|
|
42
50
|
newObj['x-tsType'] ??= `Mixins.${upperFirst(camelCase(mixinName))}.${upperFirst(camelCase(componentName))}`;
|
|
51
|
+
if (emitDefs) {
|
|
52
|
+
// Self-contained slot: local $defs + embedded closure.
|
|
53
|
+
newObj.$ref = `#/$defs/${componentName}`;
|
|
54
|
+
if (!addedComponents.has(componentName)) {
|
|
55
|
+
addedComponents.add(componentName);
|
|
56
|
+
if (result.$defs) {
|
|
57
|
+
result.$defs[componentName] = processSchema(cloneJSON(components[componentName]));
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
// emitDefs === false: keep `#/components/schemas/X`, no `$defs` (lives once in meta).
|
|
43
62
|
}
|
|
44
63
|
else {
|
|
45
|
-
delete newObj.$ref; //
|
|
46
|
-
}
|
|
47
|
-
// Add the component to $defs if not already added
|
|
48
|
-
if (!addedComponents.has(componentName) && components?.[componentName]) {
|
|
49
|
-
addedComponents.add(componentName);
|
|
50
|
-
if (result.$defs) {
|
|
51
|
-
result.$defs[componentName] = processSchema(cloneJSON(components[componentName]));
|
|
52
|
-
}
|
|
64
|
+
delete newObj.$ref; // $ref to a component not in components (e.g. Telegram API)
|
|
53
65
|
}
|
|
54
66
|
}
|
|
55
67
|
// Process properties recursively
|
|
@@ -63,3 +75,16 @@ export function applyComponentsSchemas(schema, components, mixinName) {
|
|
|
63
75
|
// Process the main schema
|
|
64
76
|
return processSchema(result);
|
|
65
77
|
}
|
|
78
|
+
/**
|
|
79
|
+
* Re-attach a response slot's `$defs` closure at render time, for generators that
|
|
80
|
+
* resolve `$ref` against a self-contained schema (Rust). Pulls components from the
|
|
81
|
+
* segment's shared meta → identical to the `emitDefs=true` slot. No-op for non-mixin.
|
|
82
|
+
*/
|
|
83
|
+
export function reattachMixinDefs(slot, segment) {
|
|
84
|
+
if (!slot || segment?.segmentType !== 'mixin')
|
|
85
|
+
return slot;
|
|
86
|
+
const components = segment.meta?.openAPIObject?.components?.schemas;
|
|
87
|
+
if (!components)
|
|
88
|
+
return slot;
|
|
89
|
+
return applyComponentsSchemas(slot, components, segment.segmentName, true);
|
|
90
|
+
}
|
|
@@ -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,
|
|
@@ -140,14 +150,30 @@ export function openAPIToVovkSchema({ apiRoot, source: { object: openAPIObject }
|
|
|
140
150
|
body: applyComponentsSchemas(body, componentsSchemas, segmentName),
|
|
141
151
|
}),
|
|
142
152
|
...(output && {
|
|
143
|
-
|
|
153
|
+
// Response slot: not validated + typed via x-tsType → skip $defs (dedup).
|
|
154
|
+
output: applyComponentsSchemas(output, componentsSchemas, segmentName, false),
|
|
144
155
|
}),
|
|
145
156
|
...(iteration && {
|
|
146
|
-
iteration: applyComponentsSchemas(iteration, componentsSchemas, segmentName),
|
|
157
|
+
iteration: applyComponentsSchemas(iteration, componentsSchemas, segmentName, false),
|
|
147
158
|
}),
|
|
148
159
|
},
|
|
149
160
|
};
|
|
150
161
|
});
|
|
151
|
-
|
|
152
|
-
|
|
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;
|
|
153
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
|
+
}
|
package/dist/types/config.d.ts
CHANGED
|
@@ -88,6 +88,17 @@ export interface VovkOpenAPIMixin {
|
|
|
88
88
|
apiRoot?: string;
|
|
89
89
|
getModuleName?: 'nestjs-operation-id' | (string & {}) | 'api' | GetOpenAPINameFn;
|
|
90
90
|
getMethodName?: 'nestjs-operation-id' | 'camel-case-operation-id' | 'auto' | GetOpenAPINameFn;
|
|
91
|
+
/**
|
|
92
|
+
* Keep only operations the predicate returns `true` for; omitted = keep all. Runs before
|
|
93
|
+
* `getModuleName`/`getMethodName`, so a module whose operations are all filtered out is never created.
|
|
94
|
+
*/
|
|
95
|
+
filterOperations?: (config: Parameters<GetOpenAPINameFn>[0]) => boolean;
|
|
96
|
+
/**
|
|
97
|
+
* Prune `meta.openAPIObject.components.schemas` to the transitive `$ref` closure of the kept operations,
|
|
98
|
+
* shrinking the generated schema for large specs. Removes `Mixins.<Segment>.<Component>` types for
|
|
99
|
+
* components nothing kept references — keep `false` (default) if you import such types directly.
|
|
100
|
+
*/
|
|
101
|
+
pruneComponents?: boolean;
|
|
91
102
|
errorMessageKey?: string;
|
|
92
103
|
mixinName?: string;
|
|
93
104
|
}
|