vovk 3.0.0-draft.291 → 3.0.0-draft.293
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/cjs/openapi/openAPIToVovkSchema.js +17 -80
- package/cjs/types.d.ts +6 -1
- package/mjs/openapi/openAPIToVovkSchema.js +17 -80
- package/mjs/types.d.ts +6 -1
- package/package.json +1 -1
|
@@ -4,6 +4,7 @@ exports.openAPIToVovkSchema = openAPIToVovkSchema;
|
|
|
4
4
|
const types_1 = require("../types");
|
|
5
5
|
const generateFnName_1 = require("./generateFnName");
|
|
6
6
|
const camelCase_1 = require("../utils/camelCase");
|
|
7
|
+
const lodash_1 = require("lodash");
|
|
7
8
|
// fast clone JSON object while ignoring Date, RegExp, and Function types
|
|
8
9
|
function cloneJSON(obj) {
|
|
9
10
|
if (obj === null || typeof obj !== 'object')
|
|
@@ -18,69 +19,7 @@ function cloneJSON(obj) {
|
|
|
18
19
|
}
|
|
19
20
|
return result;
|
|
20
21
|
}
|
|
21
|
-
|
|
22
|
-
/* in: {
|
|
23
|
-
name: 'petId',
|
|
24
|
-
in: 'path',
|
|
25
|
-
description: 'ID of pet to update',
|
|
26
|
-
required: true,
|
|
27
|
-
type: 'integer',
|
|
28
|
-
format: 'int64'
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
out: {
|
|
32
|
-
"name": "petId",
|
|
33
|
-
"in": "path",
|
|
34
|
-
"description": 'ID of pet to update',
|
|
35
|
-
"required": true,
|
|
36
|
-
"schema": {
|
|
37
|
-
"type": "integer",
|
|
38
|
-
"format": "int64"
|
|
39
|
-
} */
|
|
40
|
-
// If the parameter already has a schema, return it as is
|
|
41
|
-
if (param.schema) {
|
|
42
|
-
return param;
|
|
43
|
-
}
|
|
44
|
-
// Create a new object for the result
|
|
45
|
-
const result = { ...param };
|
|
46
|
-
// Schema-related properties to move from parameter object to schema object
|
|
47
|
-
const schemaProps = [
|
|
48
|
-
'type',
|
|
49
|
-
'format',
|
|
50
|
-
'items',
|
|
51
|
-
'collectionFormat',
|
|
52
|
-
'default',
|
|
53
|
-
'maximum',
|
|
54
|
-
'exclusiveMaximum',
|
|
55
|
-
'minimum',
|
|
56
|
-
'exclusiveMinimum',
|
|
57
|
-
'maxLength',
|
|
58
|
-
'minLength',
|
|
59
|
-
'pattern',
|
|
60
|
-
'maxItems',
|
|
61
|
-
'minItems',
|
|
62
|
-
'uniqueItems',
|
|
63
|
-
'enum',
|
|
64
|
-
'multipleOf',
|
|
65
|
-
];
|
|
66
|
-
// Create schema object
|
|
67
|
-
const schema = {};
|
|
68
|
-
// Move properties from parameter to schema
|
|
69
|
-
for (const prop of schemaProps) {
|
|
70
|
-
if (prop in param) {
|
|
71
|
-
// @ts-expect-error - Dynamic property access
|
|
72
|
-
schema[prop] = param[prop];
|
|
73
|
-
// @ts-expect-error - Dynamic property access
|
|
74
|
-
delete result[prop];
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
// Only add schema if we have properties to include
|
|
78
|
-
if (Object.keys(schema).length > 0) {
|
|
79
|
-
result.schema = schema;
|
|
80
|
-
}
|
|
81
|
-
return result;
|
|
82
|
-
};
|
|
83
|
-
function applyComponents(schema, key, components) {
|
|
22
|
+
function applyComponents(schema, key, components, mixinName) {
|
|
84
23
|
if (!components || !Object.keys(components).length)
|
|
85
24
|
return schema;
|
|
86
25
|
// Create a deep copy of the schema
|
|
@@ -95,10 +34,12 @@ function applyComponents(schema, key, components) {
|
|
|
95
34
|
return obj;
|
|
96
35
|
// Create a new object/array to avoid modifying the input
|
|
97
36
|
const newObj = Array.isArray(obj) ? [...obj] : { ...obj };
|
|
98
|
-
|
|
99
|
-
|
|
37
|
+
const $ref = newObj.$ref;
|
|
38
|
+
if ($ref && typeof $ref === 'string' && $ref.startsWith(`#/${key}/`)) {
|
|
39
|
+
const componentName = $ref.replace(`#/${key}/`, '');
|
|
100
40
|
if (components[componentName]) {
|
|
101
41
|
newObj.$ref = `#/$defs/${componentName}`;
|
|
42
|
+
newObj['tsType'] = `Mixins.${(0, lodash_1.capitalize)((0, camelCase_1.camelCase)(mixinName))}.${(0, lodash_1.capitalize)((0, camelCase_1.camelCase)(componentName))}`;
|
|
102
43
|
}
|
|
103
44
|
else {
|
|
104
45
|
delete newObj.$ref; // Remove $ref if component not found (Telegram API has Type $refs that is not defined in components)
|
|
@@ -197,15 +138,11 @@ function openAPIToVovkSchema({ apiRoot, source: { object: openAPIObject }, getMo
|
|
|
197
138
|
segmentName: mixinName,
|
|
198
139
|
segmentType: 'mixin',
|
|
199
140
|
controllers: {},
|
|
141
|
+
meta: {
|
|
142
|
+
components: openAPIObject.components,
|
|
143
|
+
},
|
|
200
144
|
},
|
|
201
145
|
},
|
|
202
|
-
meta: {
|
|
203
|
-
$schema: types_1.VovkSchemaIdEnum.META,
|
|
204
|
-
config: {
|
|
205
|
-
$schema: types_1.VovkSchemaIdEnum.CONFIG,
|
|
206
|
-
},
|
|
207
|
-
openapi: openAPIObject,
|
|
208
|
-
},
|
|
209
146
|
};
|
|
210
147
|
const segment = schema.segments[mixinName];
|
|
211
148
|
getModuleName = normalizeGetModuleName(getModuleName);
|
|
@@ -235,14 +172,14 @@ function openAPIToVovkSchema({ apiRoot, source: { object: openAPIObject }, getMo
|
|
|
235
172
|
const query = queryProperties?.length
|
|
236
173
|
? {
|
|
237
174
|
type: 'object',
|
|
238
|
-
properties: Object.fromEntries(queryProperties.map((p) => [p.name,
|
|
175
|
+
properties: Object.fromEntries(queryProperties.map((p) => [p.name, p.schema])),
|
|
239
176
|
required: queryProperties.filter((p) => p.required).map((p) => p.name),
|
|
240
177
|
}
|
|
241
178
|
: null;
|
|
242
179
|
const params = pathProperties?.length
|
|
243
180
|
? {
|
|
244
181
|
type: 'object',
|
|
245
|
-
properties: Object.fromEntries(pathProperties.map((p) => [p.name,
|
|
182
|
+
properties: Object.fromEntries(pathProperties.map((p) => [p.name, p.schema])),
|
|
246
183
|
required: pathProperties.filter((p) => p.required).map((p) => p.name),
|
|
247
184
|
}
|
|
248
185
|
: null;
|
|
@@ -276,7 +213,7 @@ function openAPIToVovkSchema({ apiRoot, source: { object: openAPIObject }, getMo
|
|
|
276
213
|
if (errorMessageKey) {
|
|
277
214
|
operation['x-errorMessageKey'] = errorMessageKey;
|
|
278
215
|
}
|
|
279
|
-
const componentsKey =
|
|
216
|
+
const componentsKey = 'components/schemas';
|
|
280
217
|
const components = openAPIObject.components?.schemas ??
|
|
281
218
|
('definitions' in openAPIObject ? openAPIObject.definitions : {});
|
|
282
219
|
segment.controllers[rpcModuleName].handlers[handlerName] = {
|
|
@@ -285,19 +222,19 @@ function openAPIToVovkSchema({ apiRoot, source: { object: openAPIObject }, getMo
|
|
|
285
222
|
openapi: operation,
|
|
286
223
|
validation: {
|
|
287
224
|
...(query && {
|
|
288
|
-
query: applyComponents(query, componentsKey, components),
|
|
225
|
+
query: applyComponents(query, componentsKey, components, mixinName),
|
|
289
226
|
}),
|
|
290
227
|
...(params && {
|
|
291
|
-
params: applyComponents(params, componentsKey, components),
|
|
228
|
+
params: applyComponents(params, componentsKey, components, mixinName),
|
|
292
229
|
}),
|
|
293
230
|
...(body && {
|
|
294
|
-
body: applyComponents(body, componentsKey, components),
|
|
231
|
+
body: applyComponents(body, componentsKey, components, mixinName),
|
|
295
232
|
}),
|
|
296
233
|
...(output && {
|
|
297
|
-
output: applyComponents(output, componentsKey, components),
|
|
234
|
+
output: applyComponents(output, componentsKey, components, mixinName),
|
|
298
235
|
}),
|
|
299
236
|
...(iteration && {
|
|
300
|
-
iteration: applyComponents(iteration, componentsKey, components),
|
|
237
|
+
iteration: applyComponents(iteration, componentsKey, components, mixinName),
|
|
301
238
|
}),
|
|
302
239
|
},
|
|
303
240
|
};
|
package/cjs/types.d.ts
CHANGED
|
@@ -32,6 +32,10 @@ export type VovkSegmentSchema<T = KnownAny> = {
|
|
|
32
32
|
segmentType: 'segment' | 'mixin' | (string & {});
|
|
33
33
|
forceApiRoot?: string;
|
|
34
34
|
controllers: Record<string, VovkControllerSchema<T>>;
|
|
35
|
+
meta?: {
|
|
36
|
+
components?: OpenAPIObject['components'];
|
|
37
|
+
[key: string]: KnownAny;
|
|
38
|
+
};
|
|
35
39
|
};
|
|
36
40
|
export type VovkMetaSchema = {
|
|
37
41
|
$schema: typeof VovkSchemaIdEnum.META | (string & {});
|
|
@@ -44,7 +48,7 @@ export type VovkMetaSchema = {
|
|
|
44
48
|
export type VovkSchema<T = KnownAny> = {
|
|
45
49
|
$schema: typeof VovkSchemaIdEnum.SCHEMA | (string & {});
|
|
46
50
|
segments: Record<string, VovkSegmentSchema<T>>;
|
|
47
|
-
meta
|
|
51
|
+
meta?: VovkMetaSchema;
|
|
48
52
|
};
|
|
49
53
|
export type VovkErrorResponse = {
|
|
50
54
|
cause?: unknown;
|
|
@@ -293,6 +297,7 @@ export type ClientTemplateDef = {
|
|
|
293
297
|
segmentedClient?: Omit<ClientConfigSegmented, 'fromTemplates' | 'enabled'>;
|
|
294
298
|
segmentConfig?: false | SegmentConfig;
|
|
295
299
|
requires?: Record<string, string>;
|
|
300
|
+
/** @deprecated */
|
|
296
301
|
isTsClient?: boolean;
|
|
297
302
|
};
|
|
298
303
|
export type GetOpenAPINameFn = (config: {
|
|
@@ -4,6 +4,7 @@ exports.openAPIToVovkSchema = openAPIToVovkSchema;
|
|
|
4
4
|
const types_1 = require("../types");
|
|
5
5
|
const generateFnName_1 = require("./generateFnName");
|
|
6
6
|
const camelCase_1 = require("../utils/camelCase");
|
|
7
|
+
const lodash_1 = require("lodash");
|
|
7
8
|
// fast clone JSON object while ignoring Date, RegExp, and Function types
|
|
8
9
|
function cloneJSON(obj) {
|
|
9
10
|
if (obj === null || typeof obj !== 'object')
|
|
@@ -18,69 +19,7 @@ function cloneJSON(obj) {
|
|
|
18
19
|
}
|
|
19
20
|
return result;
|
|
20
21
|
}
|
|
21
|
-
|
|
22
|
-
/* in: {
|
|
23
|
-
name: 'petId',
|
|
24
|
-
in: 'path',
|
|
25
|
-
description: 'ID of pet to update',
|
|
26
|
-
required: true,
|
|
27
|
-
type: 'integer',
|
|
28
|
-
format: 'int64'
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
out: {
|
|
32
|
-
"name": "petId",
|
|
33
|
-
"in": "path",
|
|
34
|
-
"description": 'ID of pet to update',
|
|
35
|
-
"required": true,
|
|
36
|
-
"schema": {
|
|
37
|
-
"type": "integer",
|
|
38
|
-
"format": "int64"
|
|
39
|
-
} */
|
|
40
|
-
// If the parameter already has a schema, return it as is
|
|
41
|
-
if (param.schema) {
|
|
42
|
-
return param;
|
|
43
|
-
}
|
|
44
|
-
// Create a new object for the result
|
|
45
|
-
const result = { ...param };
|
|
46
|
-
// Schema-related properties to move from parameter object to schema object
|
|
47
|
-
const schemaProps = [
|
|
48
|
-
'type',
|
|
49
|
-
'format',
|
|
50
|
-
'items',
|
|
51
|
-
'collectionFormat',
|
|
52
|
-
'default',
|
|
53
|
-
'maximum',
|
|
54
|
-
'exclusiveMaximum',
|
|
55
|
-
'minimum',
|
|
56
|
-
'exclusiveMinimum',
|
|
57
|
-
'maxLength',
|
|
58
|
-
'minLength',
|
|
59
|
-
'pattern',
|
|
60
|
-
'maxItems',
|
|
61
|
-
'minItems',
|
|
62
|
-
'uniqueItems',
|
|
63
|
-
'enum',
|
|
64
|
-
'multipleOf',
|
|
65
|
-
];
|
|
66
|
-
// Create schema object
|
|
67
|
-
const schema = {};
|
|
68
|
-
// Move properties from parameter to schema
|
|
69
|
-
for (const prop of schemaProps) {
|
|
70
|
-
if (prop in param) {
|
|
71
|
-
// @ts-expect-error - Dynamic property access
|
|
72
|
-
schema[prop] = param[prop];
|
|
73
|
-
// @ts-expect-error - Dynamic property access
|
|
74
|
-
delete result[prop];
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
// Only add schema if we have properties to include
|
|
78
|
-
if (Object.keys(schema).length > 0) {
|
|
79
|
-
result.schema = schema;
|
|
80
|
-
}
|
|
81
|
-
return result;
|
|
82
|
-
};
|
|
83
|
-
function applyComponents(schema, key, components) {
|
|
22
|
+
function applyComponents(schema, key, components, mixinName) {
|
|
84
23
|
if (!components || !Object.keys(components).length)
|
|
85
24
|
return schema;
|
|
86
25
|
// Create a deep copy of the schema
|
|
@@ -95,10 +34,12 @@ function applyComponents(schema, key, components) {
|
|
|
95
34
|
return obj;
|
|
96
35
|
// Create a new object/array to avoid modifying the input
|
|
97
36
|
const newObj = Array.isArray(obj) ? [...obj] : { ...obj };
|
|
98
|
-
|
|
99
|
-
|
|
37
|
+
const $ref = newObj.$ref;
|
|
38
|
+
if ($ref && typeof $ref === 'string' && $ref.startsWith(`#/${key}/`)) {
|
|
39
|
+
const componentName = $ref.replace(`#/${key}/`, '');
|
|
100
40
|
if (components[componentName]) {
|
|
101
41
|
newObj.$ref = `#/$defs/${componentName}`;
|
|
42
|
+
newObj['tsType'] = `Mixins.${(0, lodash_1.capitalize)((0, camelCase_1.camelCase)(mixinName))}.${(0, lodash_1.capitalize)((0, camelCase_1.camelCase)(componentName))}`;
|
|
102
43
|
}
|
|
103
44
|
else {
|
|
104
45
|
delete newObj.$ref; // Remove $ref if component not found (Telegram API has Type $refs that is not defined in components)
|
|
@@ -197,15 +138,11 @@ function openAPIToVovkSchema({ apiRoot, source: { object: openAPIObject }, getMo
|
|
|
197
138
|
segmentName: mixinName,
|
|
198
139
|
segmentType: 'mixin',
|
|
199
140
|
controllers: {},
|
|
141
|
+
meta: {
|
|
142
|
+
components: openAPIObject.components,
|
|
143
|
+
},
|
|
200
144
|
},
|
|
201
145
|
},
|
|
202
|
-
meta: {
|
|
203
|
-
$schema: types_1.VovkSchemaIdEnum.META,
|
|
204
|
-
config: {
|
|
205
|
-
$schema: types_1.VovkSchemaIdEnum.CONFIG,
|
|
206
|
-
},
|
|
207
|
-
openapi: openAPIObject,
|
|
208
|
-
},
|
|
209
146
|
};
|
|
210
147
|
const segment = schema.segments[mixinName];
|
|
211
148
|
getModuleName = normalizeGetModuleName(getModuleName);
|
|
@@ -235,14 +172,14 @@ function openAPIToVovkSchema({ apiRoot, source: { object: openAPIObject }, getMo
|
|
|
235
172
|
const query = queryProperties?.length
|
|
236
173
|
? {
|
|
237
174
|
type: 'object',
|
|
238
|
-
properties: Object.fromEntries(queryProperties.map((p) => [p.name,
|
|
175
|
+
properties: Object.fromEntries(queryProperties.map((p) => [p.name, p.schema])),
|
|
239
176
|
required: queryProperties.filter((p) => p.required).map((p) => p.name),
|
|
240
177
|
}
|
|
241
178
|
: null;
|
|
242
179
|
const params = pathProperties?.length
|
|
243
180
|
? {
|
|
244
181
|
type: 'object',
|
|
245
|
-
properties: Object.fromEntries(pathProperties.map((p) => [p.name,
|
|
182
|
+
properties: Object.fromEntries(pathProperties.map((p) => [p.name, p.schema])),
|
|
246
183
|
required: pathProperties.filter((p) => p.required).map((p) => p.name),
|
|
247
184
|
}
|
|
248
185
|
: null;
|
|
@@ -276,7 +213,7 @@ function openAPIToVovkSchema({ apiRoot, source: { object: openAPIObject }, getMo
|
|
|
276
213
|
if (errorMessageKey) {
|
|
277
214
|
operation['x-errorMessageKey'] = errorMessageKey;
|
|
278
215
|
}
|
|
279
|
-
const componentsKey =
|
|
216
|
+
const componentsKey = 'components/schemas';
|
|
280
217
|
const components = openAPIObject.components?.schemas ??
|
|
281
218
|
('definitions' in openAPIObject ? openAPIObject.definitions : {});
|
|
282
219
|
segment.controllers[rpcModuleName].handlers[handlerName] = {
|
|
@@ -285,19 +222,19 @@ function openAPIToVovkSchema({ apiRoot, source: { object: openAPIObject }, getMo
|
|
|
285
222
|
openapi: operation,
|
|
286
223
|
validation: {
|
|
287
224
|
...(query && {
|
|
288
|
-
query: applyComponents(query, componentsKey, components),
|
|
225
|
+
query: applyComponents(query, componentsKey, components, mixinName),
|
|
289
226
|
}),
|
|
290
227
|
...(params && {
|
|
291
|
-
params: applyComponents(params, componentsKey, components),
|
|
228
|
+
params: applyComponents(params, componentsKey, components, mixinName),
|
|
292
229
|
}),
|
|
293
230
|
...(body && {
|
|
294
|
-
body: applyComponents(body, componentsKey, components),
|
|
231
|
+
body: applyComponents(body, componentsKey, components, mixinName),
|
|
295
232
|
}),
|
|
296
233
|
...(output && {
|
|
297
|
-
output: applyComponents(output, componentsKey, components),
|
|
234
|
+
output: applyComponents(output, componentsKey, components, mixinName),
|
|
298
235
|
}),
|
|
299
236
|
...(iteration && {
|
|
300
|
-
iteration: applyComponents(iteration, componentsKey, components),
|
|
237
|
+
iteration: applyComponents(iteration, componentsKey, components, mixinName),
|
|
301
238
|
}),
|
|
302
239
|
},
|
|
303
240
|
};
|
package/mjs/types.d.ts
CHANGED
|
@@ -32,6 +32,10 @@ export type VovkSegmentSchema<T = KnownAny> = {
|
|
|
32
32
|
segmentType: 'segment' | 'mixin' | (string & {});
|
|
33
33
|
forceApiRoot?: string;
|
|
34
34
|
controllers: Record<string, VovkControllerSchema<T>>;
|
|
35
|
+
meta?: {
|
|
36
|
+
components?: OpenAPIObject['components'];
|
|
37
|
+
[key: string]: KnownAny;
|
|
38
|
+
};
|
|
35
39
|
};
|
|
36
40
|
export type VovkMetaSchema = {
|
|
37
41
|
$schema: typeof VovkSchemaIdEnum.META | (string & {});
|
|
@@ -44,7 +48,7 @@ export type VovkMetaSchema = {
|
|
|
44
48
|
export type VovkSchema<T = KnownAny> = {
|
|
45
49
|
$schema: typeof VovkSchemaIdEnum.SCHEMA | (string & {});
|
|
46
50
|
segments: Record<string, VovkSegmentSchema<T>>;
|
|
47
|
-
meta
|
|
51
|
+
meta?: VovkMetaSchema;
|
|
48
52
|
};
|
|
49
53
|
export type VovkErrorResponse = {
|
|
50
54
|
cause?: unknown;
|
|
@@ -293,6 +297,7 @@ export type ClientTemplateDef = {
|
|
|
293
297
|
segmentedClient?: Omit<ClientConfigSegmented, 'fromTemplates' | 'enabled'>;
|
|
294
298
|
segmentConfig?: false | SegmentConfig;
|
|
295
299
|
requires?: Record<string, string>;
|
|
300
|
+
/** @deprecated */
|
|
296
301
|
isTsClient?: boolean;
|
|
297
302
|
};
|
|
298
303
|
export type GetOpenAPINameFn = (config: {
|