@tsofist/schema-forge 1.32.1 → 1.34.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/lib/fake-generator.d.ts +0 -1
- package/lib/fake-generator.js +6 -2
- package/lib/fake-generator.spec.d.ts +0 -1
- package/lib/fake-generator.spec.js +16 -18
- package/lib/generator/schema-generator.d.ts +0 -1
- package/lib/generator/schema-generator.js +61 -43
- package/lib/generator/types-generator.d.ts +0 -1
- package/lib/generator/types-generator.js +109 -98
- package/lib/generator/types.d.ts +0 -1
- package/lib/generator/types.js +0 -1
- package/lib/generator.d.ts +0 -1
- package/lib/generator.js +97 -101
- package/lib/generator.spec.d.ts +0 -1
- package/lib/generator.spec.js +94 -65
- package/lib/index.d.ts +0 -1
- package/lib/index.js +0 -1
- package/lib/types.d.ts +0 -1
- package/lib/types.js +0 -1
- package/lib/util/format.error.d.ts +0 -1
- package/lib/util/format.error.js +0 -1
- package/lib/util/patch.extended-annotations-reader.d.ts +0 -1
- package/lib/util/patch.extended-annotations-reader.js +1 -3
- package/lib/util/sort-properties.d.ts +0 -1
- package/lib/util/sort-properties.js +1 -3
- package/lib/util/tsc.d.ts +0 -1
- package/lib/util/tsc.js +4 -7
- package/lib/validator.d.ts +16 -2
- package/lib/validator.js +18 -10
- package/package.json +7 -7
- package/lib/fake-generator.d.ts.map +0 -1
- package/lib/fake-generator.js.map +0 -1
- package/lib/fake-generator.spec.d.ts.map +0 -1
- package/lib/fake-generator.spec.js.map +0 -1
- package/lib/generator/schema-generator.d.ts.map +0 -1
- package/lib/generator/schema-generator.js.map +0 -1
- package/lib/generator/types-generator.d.ts.map +0 -1
- package/lib/generator/types-generator.js.map +0 -1
- package/lib/generator/types.d.ts.map +0 -1
- package/lib/generator/types.js.map +0 -1
- package/lib/generator.d.ts.map +0 -1
- package/lib/generator.js.map +0 -1
- package/lib/generator.spec.d.ts.map +0 -1
- package/lib/generator.spec.js.map +0 -1
- package/lib/index.d.ts.map +0 -1
- package/lib/index.js.map +0 -1
- package/lib/tsconfig.build.tsbuildinfo +0 -1
- package/lib/types.d.ts.map +0 -1
- package/lib/types.js.map +0 -1
- package/lib/util/format.error.d.ts.map +0 -1
- package/lib/util/format.error.js.map +0 -1
- package/lib/util/patch.extended-annotations-reader.d.ts.map +0 -1
- package/lib/util/patch.extended-annotations-reader.js.map +0 -1
- package/lib/util/sort-properties.d.ts.map +0 -1
- package/lib/util/sort-properties.js.map +0 -1
- package/lib/util/tsc.d.ts.map +0 -1
- package/lib/util/tsc.js.map +0 -1
- package/lib/validator.d.ts.map +0 -1
- package/lib/validator.js.map +0 -1
package/lib/fake-generator.d.ts
CHANGED
|
@@ -19,4 +19,3 @@ export interface FakeGeneratorHost {
|
|
|
19
19
|
export declare function createFakeGeneratorHost(source: SchemaForgeValidator, options?: FakeGeneratorOptions): FakeGeneratorHost;
|
|
20
20
|
export declare function generateFakeData<T = unknown>(validator: SchemaForgeValidator, source: SchemaForgeDefinitionRef, options?: FakeGeneratorOptions): T;
|
|
21
21
|
export declare function generateFakeData<T = unknown>(host: FakeGeneratorHost, source: SchemaForgeDefinitionRef): T;
|
|
22
|
-
//# sourceMappingURL=fake-generator.d.ts.map
|
package/lib/fake-generator.js
CHANGED
|
@@ -27,7 +27,12 @@ function createFakeGeneratorHost(source, options = {}) {
|
|
|
27
27
|
}
|
|
28
28
|
return faker;
|
|
29
29
|
});
|
|
30
|
-
generator.option(
|
|
30
|
+
generator.option({
|
|
31
|
+
alwaysFakeOptionals: true,
|
|
32
|
+
refDepthMax: 1_000,
|
|
33
|
+
...options,
|
|
34
|
+
resolveJsonPath: false,
|
|
35
|
+
});
|
|
31
36
|
generator.format('iso-time', () => {
|
|
32
37
|
return (0, substr_1.substr)(faker.date.anytime(), 'T', '.');
|
|
33
38
|
});
|
|
@@ -125,4 +130,3 @@ function cleanJSFQuirksArtefacts(target) {
|
|
|
125
130
|
}
|
|
126
131
|
return target;
|
|
127
132
|
}
|
|
128
|
-
//# sourceMappingURL=fake-generator.js.map
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const tslib_1 = require("tslib");
|
|
4
3
|
const guards_1 = require("@tsofist/stem/lib/number/guards");
|
|
5
4
|
const fake_generator_1 = require("./fake-generator");
|
|
6
5
|
const validator_1 = require("./validator");
|
|
@@ -161,44 +160,44 @@ describe('generateFakeData', () => {
|
|
|
161
160
|
beforeEach(() => {
|
|
162
161
|
validator = (0, validator_1.createSchemaForgeValidator)({ schemas: [testSchema1, testSchema2, testSchema3] }, true);
|
|
163
162
|
});
|
|
164
|
-
it('default behavior', () =>
|
|
163
|
+
it('default behavior', async () => {
|
|
165
164
|
{
|
|
166
|
-
const data =
|
|
165
|
+
const data = await (0, fake_generator_1.generateFakeData)(validator, 'test-1#/definitions/Int');
|
|
167
166
|
expect(data).toBeDefined();
|
|
168
167
|
expect(data).toBeGreaterThanOrEqual(0);
|
|
169
168
|
expect((0, guards_1.isInt)(data)).toStrictEqual(true);
|
|
170
169
|
}
|
|
171
170
|
{
|
|
172
|
-
const data =
|
|
171
|
+
const data = await (0, fake_generator_1.generateFakeData)(validator, 'test-2#/definitions/Url');
|
|
173
172
|
expect(data).toBeDefined();
|
|
174
173
|
expect(data).toMatch(/^https?:\/\/(.*)/);
|
|
175
174
|
}
|
|
176
|
-
})
|
|
177
|
-
it('cross-schema references should be handled correctly', () =>
|
|
175
|
+
});
|
|
176
|
+
it('cross-schema references should be handled correctly', async () => {
|
|
178
177
|
const source = 'test-2#/definitions/ForeignInt';
|
|
179
|
-
const data =
|
|
178
|
+
const data = await (0, fake_generator_1.generateFakeData)(validator, source);
|
|
180
179
|
expect(data).toBeDefined();
|
|
181
180
|
expect(data).toBeGreaterThanOrEqual(0);
|
|
182
181
|
expect((0, guards_1.isInt)(data)).toStrictEqual(true);
|
|
183
182
|
expect(validator.validateBySchema(source, data).valid).toStrictEqual(true);
|
|
184
|
-
})
|
|
185
|
-
it('complex, nested data should be handled correctly', () =>
|
|
183
|
+
});
|
|
184
|
+
it('complex, nested data should be handled correctly', async () => {
|
|
186
185
|
{
|
|
187
186
|
const source = 'test-2#/definitions/JSFQuirks';
|
|
188
|
-
const data =
|
|
187
|
+
const data = await (0, fake_generator_1.generateFakeData)(validator, source, {});
|
|
189
188
|
expect(data).toBeDefined();
|
|
190
189
|
expect(validator.validateBySchema(source, data).valid).toStrictEqual(true);
|
|
191
190
|
}
|
|
192
191
|
{
|
|
193
192
|
const source = 'test-2#/definitions/List';
|
|
194
|
-
const data =
|
|
193
|
+
const data = await (0, fake_generator_1.generateFakeData)(validator, source);
|
|
195
194
|
expect(data).toBeDefined();
|
|
196
195
|
expect(validator.validateBySchema(source, data).valid).toStrictEqual(true);
|
|
197
196
|
}
|
|
198
197
|
{
|
|
199
198
|
const source = 'test-2#/definitions/ListOfNames';
|
|
200
199
|
const len = 15;
|
|
201
|
-
const data =
|
|
200
|
+
const data = await (0, fake_generator_1.generateFakeData)(validator, source, { minItems: len, locale: 'ru' });
|
|
202
201
|
expect(data).toBeDefined();
|
|
203
202
|
expect(validator.validateBySchema(source, data).valid).toStrictEqual(true);
|
|
204
203
|
expect(Array.isArray(data)).toStrictEqual(true);
|
|
@@ -207,7 +206,7 @@ describe('generateFakeData', () => {
|
|
|
207
206
|
}
|
|
208
207
|
{
|
|
209
208
|
const source = 'test-2#/definitions/Entity';
|
|
210
|
-
const data =
|
|
209
|
+
const data = await (0, fake_generator_1.generateFakeData)(validator, source, { locale: 'ru' });
|
|
211
210
|
expect(validator.validateBySchema(source, data).valid).toStrictEqual(true);
|
|
212
211
|
expect(typeof data.datetime).toStrictEqual('string');
|
|
213
212
|
expect(data.datetime).toMatch(/.*Z$/);
|
|
@@ -215,12 +214,12 @@ describe('generateFakeData', () => {
|
|
|
215
214
|
expect(typeof data.time).toStrictEqual('string');
|
|
216
215
|
expect(data.time).toMatch(/^[0-9]{2}:[0-9]{2}:[0-9]{2}$/);
|
|
217
216
|
}
|
|
218
|
-
})
|
|
219
|
-
it('should handle recursive schemas', () =>
|
|
217
|
+
});
|
|
218
|
+
it('should handle recursive schemas', async () => {
|
|
220
219
|
const source = 'test-3#/definitions/RecursiveObject';
|
|
221
220
|
const count = 10;
|
|
222
221
|
for (let i = 0; i < count; i++) {
|
|
223
|
-
const data =
|
|
222
|
+
const data = await (0, fake_generator_1.generateFakeData)(validator, source, {
|
|
224
223
|
reuseProperties: false,
|
|
225
224
|
failOnInvalidFormat: true,
|
|
226
225
|
failOnInvalidTypes: true,
|
|
@@ -234,6 +233,5 @@ describe('generateFakeData', () => {
|
|
|
234
233
|
expect(data).toBeDefined();
|
|
235
234
|
expect(validator.validateBySchema(source, data).valid).toStrictEqual(true);
|
|
236
235
|
}
|
|
237
|
-
})
|
|
236
|
+
});
|
|
238
237
|
});
|
|
239
|
-
//# sourceMappingURL=fake-generator.spec.js.map
|
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.TupleTypeParser = void 0;
|
|
4
4
|
exports.generateSchemaByDraftTypes = generateSchemaByDraftTypes;
|
|
5
|
-
const tslib_1 = require("tslib");
|
|
6
5
|
require("../util/patch.extended-annotations-reader");
|
|
7
6
|
const error_1 = require("@tsofist/stem/lib/error");
|
|
8
7
|
const ajv_1 = require("ajv");
|
|
@@ -11,51 +10,66 @@ const typescript_1 = require("typescript");
|
|
|
11
10
|
const sort_properties_1 = require("../util/sort-properties");
|
|
12
11
|
const tsc_1 = require("../util/tsc");
|
|
13
12
|
const types_1 = require("./types");
|
|
14
|
-
function generateSchemaByDraftTypes(options) {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
{
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
(0, error_1.raise)(`Definition ${name} is duplicated`);
|
|
22
|
-
seen.add(name);
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
const allowUseFallbackDescription = options.allowUseFallbackDescription;
|
|
26
|
-
const generatorConfig = Object.assign(Object.assign(Object.assign(Object.assign({}, ts_json_schema_generator_1.DEFAULT_CONFIG), types_1.SG_CONFIG_DEFAULTS), { expose: (_a = options.expose) !== null && _a !== void 0 ? _a : types_1.SG_CONFIG_DEFAULTS.expose, path: `${options.sourcesDirectoryPattern}/*${types_1.TMP_FILES_SUFFIX}.ts`, tsconfig: options.tsconfig, discriminatorType: options.openAPI ? 'open-api' : ts_json_schema_generator_1.DEFAULT_CONFIG.discriminatorType }), types_1.SG_CONFIG_MANDATORY);
|
|
27
|
-
const generatorProgram = (0, ts_json_schema_generator_1.createProgram)(generatorConfig);
|
|
28
|
-
const typeChecker = generatorProgram.getTypeChecker();
|
|
29
|
-
const parser = (0, ts_json_schema_generator_1.createParser)(generatorProgram, options.sourcesTypesGeneratorConfig, (parser) => {
|
|
30
|
-
parser.addNodeParser(new TupleTypeParser(parser, allowUseFallbackDescription));
|
|
31
|
-
parser.addNodeParser(new ArrayLiteralExpressionIdentifierParser(typeChecker));
|
|
32
|
-
});
|
|
33
|
-
const formatter = (0, ts_json_schema_generator_1.createFormatter)(options.sourcesTypesGeneratorConfig);
|
|
34
|
-
const generator = new ts_json_schema_generator_1.SchemaGenerator(generatorProgram, parser, formatter, generatorConfig);
|
|
35
|
-
const result = {
|
|
36
|
-
$schema: 'http://json-schema.org/draft-07/schema#',
|
|
37
|
-
$id: options.schemaId,
|
|
38
|
-
hash: '',
|
|
39
|
-
definitions: {},
|
|
40
|
-
};
|
|
41
|
-
for (const definitionName of options.definitions) {
|
|
42
|
-
const schema = generator.createSchema(definitionName);
|
|
43
|
-
Object.assign(result.definitions, schema.definitions);
|
|
13
|
+
async function generateSchemaByDraftTypes(options) {
|
|
14
|
+
{
|
|
15
|
+
const seen = new Set();
|
|
16
|
+
for (const name of options.definitions) {
|
|
17
|
+
if (seen.has(name))
|
|
18
|
+
(0, error_1.raise)(`Definition ${name} is duplicated`);
|
|
19
|
+
seen.add(name);
|
|
44
20
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
}
|
|
52
|
-
|
|
21
|
+
}
|
|
22
|
+
const allowUseFallbackDescription = options.allowUseFallbackDescription;
|
|
23
|
+
const generatorConfig = {
|
|
24
|
+
...ts_json_schema_generator_1.DEFAULT_CONFIG,
|
|
25
|
+
...types_1.SG_CONFIG_DEFAULTS,
|
|
26
|
+
expose: options.expose ?? types_1.SG_CONFIG_DEFAULTS.expose,
|
|
27
|
+
path: `${options.sourcesDirectoryPattern}/*${types_1.TMP_FILES_SUFFIX}.ts`,
|
|
28
|
+
tsconfig: options.tsconfig,
|
|
29
|
+
discriminatorType: options.openAPI ? 'open-api' : ts_json_schema_generator_1.DEFAULT_CONFIG.discriminatorType,
|
|
30
|
+
...types_1.SG_CONFIG_MANDATORY,
|
|
31
|
+
};
|
|
32
|
+
const generatorProgram = (0, ts_json_schema_generator_1.createProgram)(generatorConfig);
|
|
33
|
+
const typeChecker = generatorProgram.getTypeChecker();
|
|
34
|
+
const parser = (0, ts_json_schema_generator_1.createParser)(generatorProgram, options.sourcesTypesGeneratorConfig, (parser) => {
|
|
35
|
+
parser.addNodeParser(new TupleTypeParser(parser, allowUseFallbackDescription));
|
|
36
|
+
parser.addNodeParser(new ArrayLiteralExpressionIdentifierParser(typeChecker));
|
|
53
37
|
});
|
|
38
|
+
const formatter = (0, ts_json_schema_generator_1.createFormatter)(options.sourcesTypesGeneratorConfig);
|
|
39
|
+
const generator = new ts_json_schema_generator_1.SchemaGenerator(generatorProgram, parser, formatter, generatorConfig);
|
|
40
|
+
const result = {
|
|
41
|
+
$schema: 'http://json-schema.org/draft-07/schema#',
|
|
42
|
+
$id: options.schemaId,
|
|
43
|
+
hash: '',
|
|
44
|
+
definitions: {},
|
|
45
|
+
};
|
|
46
|
+
for (const definitionName of options.definitions) {
|
|
47
|
+
const schema = generator.createSchema(definitionName);
|
|
48
|
+
Object.assign(result.definitions, schema.definitions);
|
|
49
|
+
}
|
|
50
|
+
result.definitions = Object.fromEntries(Object.entries((result.definitions || {})).sort());
|
|
51
|
+
if (options.sortObjectProperties)
|
|
52
|
+
(0, sort_properties_1.sortProperties)(result.definitions);
|
|
53
|
+
await new ajv_1.default({
|
|
54
|
+
strict: true,
|
|
55
|
+
allErrors: true,
|
|
56
|
+
}).validateSchema(result, true);
|
|
57
|
+
return result;
|
|
54
58
|
}
|
|
55
59
|
class TupleTypeParser {
|
|
56
60
|
constructor(childNodeParser, allowUseFallbackDescription) {
|
|
57
|
-
this
|
|
58
|
-
|
|
61
|
+
Object.defineProperty(this, "childNodeParser", {
|
|
62
|
+
enumerable: true,
|
|
63
|
+
configurable: true,
|
|
64
|
+
writable: true,
|
|
65
|
+
value: childNodeParser
|
|
66
|
+
});
|
|
67
|
+
Object.defineProperty(this, "allowUseFallbackDescription", {
|
|
68
|
+
enumerable: true,
|
|
69
|
+
configurable: true,
|
|
70
|
+
writable: true,
|
|
71
|
+
value: allowUseFallbackDescription
|
|
72
|
+
});
|
|
59
73
|
}
|
|
60
74
|
supportsNode(node) {
|
|
61
75
|
return (0, typescript_1.isTupleTypeNode)(node);
|
|
@@ -78,7 +92,12 @@ class TupleTypeParser {
|
|
|
78
92
|
exports.TupleTypeParser = TupleTypeParser;
|
|
79
93
|
class ArrayLiteralExpressionIdentifierParser {
|
|
80
94
|
constructor(checker) {
|
|
81
|
-
this
|
|
95
|
+
Object.defineProperty(this, "checker", {
|
|
96
|
+
enumerable: true,
|
|
97
|
+
configurable: true,
|
|
98
|
+
writable: true,
|
|
99
|
+
value: checker
|
|
100
|
+
});
|
|
82
101
|
}
|
|
83
102
|
supportsNode(node) {
|
|
84
103
|
return (node.kind === typescript_1.SyntaxKind.Identifier &&
|
|
@@ -111,4 +130,3 @@ function getIdentifierLiteralValue(node, checker) {
|
|
|
111
130
|
}
|
|
112
131
|
return undefined;
|
|
113
132
|
}
|
|
114
|
-
//# sourceMappingURL=schema-generator.js.map
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.generateDraftTypeFiles = generateDraftTypeFiles;
|
|
4
|
-
const tslib_1 = require("tslib");
|
|
5
4
|
const promises_1 = require("fs/promises");
|
|
6
5
|
const path_1 = require("path");
|
|
7
6
|
const as_bool_1 = require("@tsofist/stem/lib/as-bool");
|
|
@@ -14,97 +13,102 @@ const index_1 = require("../index");
|
|
|
14
13
|
const types_1 = require("../types");
|
|
15
14
|
const tsc_1 = require("../util/tsc");
|
|
16
15
|
const types_2 = require("./types");
|
|
17
|
-
function generateDraftTypeFiles(options) {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
16
|
+
async function generateDraftTypeFiles(options) {
|
|
17
|
+
const explicitPublic = (0, as_bool_1.asBool)(options.explicitPublic, true);
|
|
18
|
+
const sourcesTypesGeneratorConfig = {
|
|
19
|
+
...Config_1.DEFAULT_CONFIG,
|
|
20
|
+
...types_2.SG_CONFIG_DEFAULTS,
|
|
21
|
+
expose: options.expose ?? types_2.SG_CONFIG_DEFAULTS.expose,
|
|
22
|
+
path: options.sourcesPattern.length > 1 ? undefined : options.sourcesPattern[0],
|
|
23
|
+
tsconfig: options.tsconfig,
|
|
24
|
+
discriminatorType: Config_1.DEFAULT_CONFIG.discriminatorType,
|
|
25
|
+
...types_2.SG_CONFIG_MANDATORY,
|
|
26
|
+
};
|
|
27
|
+
const files = [];
|
|
28
|
+
let definitions = [];
|
|
29
|
+
const program = (0, ts_json_schema_generator_1.createProgram)(sourcesTypesGeneratorConfig);
|
|
30
|
+
const checker = program.getTypeChecker();
|
|
31
|
+
const compilerOptions = program.getCompilerOptions();
|
|
32
|
+
const compilerHost = (0, typescript_1.createCompilerHost)(compilerOptions);
|
|
33
|
+
const fileNames = program.getRootFileNames();
|
|
34
|
+
const namesBySourceFile = new Map();
|
|
35
|
+
for (const sourceFileName of fileNames) {
|
|
36
|
+
const context = {
|
|
37
|
+
program,
|
|
38
|
+
compilerOptions,
|
|
39
|
+
compilerHost,
|
|
40
|
+
typeChecker: checker,
|
|
41
|
+
options,
|
|
42
|
+
fileContent: [],
|
|
43
|
+
registerDefinition(...names) {
|
|
44
|
+
let set = namesBySourceFile.get(sourceFileName);
|
|
45
|
+
if (!set)
|
|
46
|
+
namesBySourceFile.set(sourceFileName, (set = new Set()));
|
|
47
|
+
for (const name of names) {
|
|
48
|
+
definitions.push(name);
|
|
49
|
+
set.add(name);
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
const source = program.getSourceFile(sourceFileName);
|
|
54
|
+
if (!source)
|
|
55
|
+
continue;
|
|
56
|
+
const statements = [...source.statements];
|
|
57
|
+
while (statements.length) {
|
|
58
|
+
const statement = statements.pop();
|
|
59
|
+
if (!statement)
|
|
60
|
+
break;
|
|
61
|
+
if ((0, typescript_1.isExportDeclaration)(statement)) {
|
|
62
|
+
processExportDeclaration(statement, sourceFileName, statements, context);
|
|
50
63
|
continue;
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
const
|
|
54
|
-
if (!
|
|
55
|
-
break;
|
|
56
|
-
if ((0, typescript_1.isExportDeclaration)(statement)) {
|
|
57
|
-
processExportDeclaration(statement, sourceFileName, statements, context);
|
|
64
|
+
}
|
|
65
|
+
if (explicitPublic) {
|
|
66
|
+
const isPublic = (0, typescript_1.getJSDocPublicTag)(statement) != null;
|
|
67
|
+
if (!isPublic)
|
|
58
68
|
continue;
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
const isPrivate = (0, typescript_1.getJSDocPrivateTag)(statement) != null || (0, tsc_1.hasJSDocTag)(statement, 'internal');
|
|
72
|
+
if (isPrivate)
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
switch (statement.kind) {
|
|
76
|
+
case typescript_1.SyntaxKind.ImportDeclaration:
|
|
77
|
+
case typescript_1.SyntaxKind.VariableStatement:
|
|
78
|
+
continue;
|
|
79
|
+
case typescript_1.SyntaxKind.EnumDeclaration:
|
|
80
|
+
passDeclaration(statement, context);
|
|
81
|
+
break;
|
|
82
|
+
case typescript_1.SyntaxKind.TypeAliasDeclaration:
|
|
83
|
+
passDeclaration(statement, context);
|
|
84
|
+
break;
|
|
85
|
+
case typescript_1.SyntaxKind.InterfaceDeclaration:
|
|
86
|
+
if ((0, tsc_1.hasJSDocTag)(statement, 'api')) {
|
|
87
|
+
processAPIInterfaceDeclaration(statement, context);
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
78
90
|
passDeclaration(statement, context);
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
else {
|
|
85
|
-
passDeclaration(statement, context);
|
|
86
|
-
}
|
|
87
|
-
break;
|
|
88
|
-
default:
|
|
89
|
-
console.error(`[current statement]`, statement.getText(), '\n');
|
|
90
|
-
(0, error_1.raise)(`Unsupported statement kind: ${statement.kind} (${typescript_1.SyntaxKind[statement.kind]}) in ${sourceFileName}`);
|
|
91
|
-
}
|
|
91
|
+
}
|
|
92
|
+
break;
|
|
93
|
+
default:
|
|
94
|
+
console.error(`[current statement]`, statement.getText(), '\n');
|
|
95
|
+
(0, error_1.raise)(`Unsupported statement kind: ${statement.kind} (${typescript_1.SyntaxKind[statement.kind]}) in ${sourceFileName}`);
|
|
92
96
|
}
|
|
93
|
-
const outputFileName = `${(0, path_1.dirname)(sourceFileName)}/${(0, path_1.basename)(sourceFileName, (0, path_1.extname)(sourceFileName))}${types_2.TMP_FILES_SUFFIX}.ts`;
|
|
94
|
-
yield (0, promises_1.writeFile)(outputFileName, [source.getFullText(), context.fileContent.join('\n')].join('\n'), { encoding: 'utf8' });
|
|
95
|
-
files.push(outputFileName);
|
|
96
97
|
}
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
98
|
+
const outputFileName = `${(0, path_1.dirname)(sourceFileName)}/${(0, path_1.basename)(sourceFileName, (0, path_1.extname)(sourceFileName))}${types_2.TMP_FILES_SUFFIX}.ts`;
|
|
99
|
+
await (0, promises_1.writeFile)(outputFileName, [source.getFullText(), context.fileContent.join('\n')].join('\n'), { encoding: 'utf8' });
|
|
100
|
+
files.push(outputFileName);
|
|
101
|
+
}
|
|
102
|
+
if (options.definitionsFilter) {
|
|
103
|
+
definitions = definitions.filter(options.definitionsFilter);
|
|
104
|
+
}
|
|
105
|
+
definitions.sort();
|
|
106
|
+
return {
|
|
107
|
+
sourcesTypesGeneratorConfig,
|
|
108
|
+
files,
|
|
109
|
+
definitions,
|
|
110
|
+
namesBySourceFile,
|
|
111
|
+
};
|
|
108
112
|
}
|
|
109
113
|
const S = ' '.repeat(4);
|
|
110
114
|
function passDeclaration(statement, context) {
|
|
@@ -151,23 +155,21 @@ function processExportDeclaration(statement, sourceFileName, list, context) {
|
|
|
151
155
|
}
|
|
152
156
|
}
|
|
153
157
|
function processAPIInterfaceDeclaration(statement, context) {
|
|
154
|
-
var _a, _b, _c, _d;
|
|
155
158
|
const allowUseFallbackDescription = context.options.allowUseFallbackDescription;
|
|
156
159
|
const definitionsMetaList = [];
|
|
157
160
|
const interfaceName = (0, tsc_1.readNodeName)(statement);
|
|
158
161
|
const interfaceGenericText = (0, tsc_1.readInterfaceGenericText)(statement);
|
|
159
162
|
const interfaceDesc = (0, tsc_1.readJSDocDescription)(statement, allowUseFallbackDescription);
|
|
160
|
-
const interfaceDeprecated = (0, typescript_1.getTextOfJSDocComment)((
|
|
163
|
+
const interfaceDeprecated = (0, typescript_1.getTextOfJSDocComment)((0, typescript_1.getJSDocDeprecatedTag)(statement)?.comment);
|
|
161
164
|
function onSignature(method, memberName, memberDescription, deprecated) {
|
|
162
|
-
const minArgsNum = method.parameters
|
|
165
|
+
const minArgsNum = countRequiredParams(method.parameters);
|
|
163
166
|
const maxArgsNum = method.parameters.length;
|
|
164
167
|
const argsNames = [];
|
|
165
168
|
const argsTypesText = method.parameters
|
|
166
169
|
.map((parameter) => {
|
|
167
|
-
var _a;
|
|
168
170
|
const name = parameter.name.getText();
|
|
169
171
|
argsNames.push(name);
|
|
170
|
-
const text =
|
|
172
|
+
const text = parameter.type?.getText() || (0, error_1.raise)(`No type specified for ${name}`);
|
|
171
173
|
let desc = (0, tsc_1.readJSDocDescription)(parameter, allowUseFallbackDescription, false);
|
|
172
174
|
desc = desc ? `/** @description ${desc} */ ` : '';
|
|
173
175
|
return `${S}${desc}${name}: ${text}`;
|
|
@@ -197,7 +199,7 @@ function processAPIInterfaceDeclaration(statement, context) {
|
|
|
197
199
|
` * @description Arguments for ${comment} ${argsNames.length ? `(${argsNamesText})` : ''}`,
|
|
198
200
|
` * @comment ${comment}`,
|
|
199
201
|
` *`,
|
|
200
|
-
|
|
202
|
+
` * @minItems ${minArgsNum}`,
|
|
201
203
|
` * @maxItems ${maxArgsNum}`,
|
|
202
204
|
` */`,
|
|
203
205
|
`export type ${definitionNameArgs} = readonly [\n${argsTypesText}\n];`,
|
|
@@ -214,13 +216,12 @@ function processAPIInterfaceDeclaration(statement, context) {
|
|
|
214
216
|
]);
|
|
215
217
|
}
|
|
216
218
|
function onMember(member) {
|
|
217
|
-
var _a;
|
|
218
219
|
const isPrivate = (0, typescript_1.getJSDocPrivateTag)(member) != null || (0, tsc_1.hasJSDocTag)(member, 'internal');
|
|
219
220
|
if (isPrivate)
|
|
220
221
|
return;
|
|
221
222
|
const memberName = (0, tsc_1.readNodeName)(member);
|
|
222
223
|
const memberDescription = (0, tsc_1.readJSDocDescription)(member, allowUseFallbackDescription);
|
|
223
|
-
const deprecated = (0, typescript_1.getTextOfJSDocComment)((
|
|
224
|
+
const deprecated = (0, typescript_1.getTextOfJSDocComment)((0, typescript_1.getJSDocDeprecatedTag)(member)?.comment);
|
|
224
225
|
if ((0, typescript_1.isMethodSignature)(member)) {
|
|
225
226
|
onSignature(member, memberName, memberDescription, deprecated);
|
|
226
227
|
}
|
|
@@ -242,10 +243,10 @@ function processAPIInterfaceDeclaration(statement, context) {
|
|
|
242
243
|
if (statement.heritageClauses) {
|
|
243
244
|
for (const clause of statement.heritageClauses) {
|
|
244
245
|
for (const type of clause.types) {
|
|
245
|
-
const declaration =
|
|
246
|
+
const declaration = context.typeChecker.getTypeAtLocation(type).symbol?.declarations?.[0];
|
|
246
247
|
if (declaration &&
|
|
247
248
|
(0, typescript_1.isInterfaceDeclaration)(declaration) &&
|
|
248
|
-
|
|
249
|
+
declaration.members?.length) {
|
|
249
250
|
for (const member of declaration.members) {
|
|
250
251
|
onMember(member);
|
|
251
252
|
}
|
|
@@ -289,4 +290,14 @@ function processAPIInterfaceDeclaration(statement, context) {
|
|
|
289
290
|
context.fileContent.push(interfaceText.stringify('\n'));
|
|
290
291
|
}
|
|
291
292
|
}
|
|
292
|
-
|
|
293
|
+
function countRequiredParams(params) {
|
|
294
|
+
let result = 0;
|
|
295
|
+
for (const param of params) {
|
|
296
|
+
if (param.questionToken != null)
|
|
297
|
+
break;
|
|
298
|
+
if (param.dotDotDotToken != null)
|
|
299
|
+
(0, error_1.raise)('Rest arguments are not supported yet');
|
|
300
|
+
result++;
|
|
301
|
+
}
|
|
302
|
+
return result;
|
|
303
|
+
}
|
package/lib/generator/types.d.ts
CHANGED
package/lib/generator/types.js
CHANGED
package/lib/generator.d.ts
CHANGED
|
@@ -3,4 +3,3 @@ import { SchemaForgeOptions, SchemaForgeResult } from './types';
|
|
|
3
3
|
export declare function forgeSchema(options: SchemaForgeOptions): Promise<SchemaForgeResult>;
|
|
4
4
|
export declare function loadJSONSchema(files: string[]): Promise<SchemaObject[]>;
|
|
5
5
|
export declare function loadJSONSchemaSync(files: string[]): SchemaObject[];
|
|
6
|
-
//# sourceMappingURL=generator.d.ts.map
|