@strapi/typescript-utils 0.0.0-next.c443fb2cf1a0b330fbf8f4bf6b967c3002ccbd92 → 0.0.0-next.c511e0f2d5c8549e150e6a69dda1a37589419a1b
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/LICENSE +18 -3
- package/lib/__tests__/generators/schemas/attributes.test.js +285 -140
- package/lib/__tests__/generators/schemas/imports.test.js +18 -16
- package/lib/__tests__/generators/schemas/utils.test.js +42 -88
- package/lib/compile.js +2 -6
- package/lib/compilers/basic.js +12 -4
- package/lib/compilers/index.js +0 -2
- package/lib/generators/common/imports.js +34 -0
- package/lib/generators/common/index.js +9 -0
- package/lib/generators/{schemas → common/models}/attributes.js +65 -41
- package/lib/generators/common/models/index.js +15 -0
- package/lib/generators/common/models/mappers.js +144 -0
- package/lib/generators/{schemas → common/models}/schema.js +15 -8
- package/lib/generators/{schemas → common/models}/utils.js +36 -12
- package/lib/generators/components/index.js +74 -0
- package/lib/generators/constants.js +6 -0
- package/lib/generators/content-types/index.js +74 -0
- package/lib/generators/index.js +118 -3
- package/lib/generators/utils.js +216 -0
- package/lib/index.js +0 -3
- package/lib/utils/index.js +2 -0
- package/lib/utils/resolve-outdir-sync.js +18 -0
- package/package.json +15 -8
- package/tsconfigs/admin.json +18 -19
- package/tsconfigs/server.json +18 -16
- package/lib/__tests__/generators/schemas/global.test.js +0 -108
- package/lib/admin/create-tsconfig-file.js +0 -37
- package/lib/admin/index.js +0 -5
- package/lib/compilers/watch.js +0 -37
- package/lib/generators/schemas/global.js +0 -67
- package/lib/generators/schemas/imports.js +0 -32
- package/lib/generators/schemas/index.js +0 -185
- package/lib/generators/schemas/mappers.js +0 -131
|
@@ -6,7 +6,7 @@ const {
|
|
|
6
6
|
addImport,
|
|
7
7
|
generateImportDefinition,
|
|
8
8
|
getImports,
|
|
9
|
-
} = require('../../../generators/
|
|
9
|
+
} = require('../../../generators/common/imports');
|
|
10
10
|
|
|
11
11
|
describe('Imports', () => {
|
|
12
12
|
test('When first loaded, the list of imports should be empty', () => {
|
|
@@ -27,28 +27,30 @@ describe('Imports', () => {
|
|
|
27
27
|
});
|
|
28
28
|
|
|
29
29
|
test('Generate an import type definition containing the registered import', () => {
|
|
30
|
-
const
|
|
30
|
+
const defs = generateImportDefinition();
|
|
31
31
|
|
|
32
|
-
|
|
32
|
+
defs.forEach((def) => {
|
|
33
|
+
expect(def.kind).toBe(ts.SyntaxKind.ImportDeclaration);
|
|
33
34
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
35
|
+
// Module specifier
|
|
36
|
+
expect(def.moduleSpecifier.kind).toBe(ts.SyntaxKind.StringLiteral);
|
|
37
|
+
expect(def.moduleSpecifier.text).toBe('@strapi/strapi');
|
|
37
38
|
|
|
38
|
-
|
|
39
|
-
|
|
39
|
+
// Import clause (should be named imports)
|
|
40
|
+
expect(def.importClause.kind).toBe(ts.SyntaxKind.ImportClause);
|
|
40
41
|
|
|
41
|
-
|
|
42
|
+
const { elements } = def.importClause.namedBindings;
|
|
42
43
|
|
|
43
|
-
|
|
44
|
+
expect(elements).toHaveLength(2);
|
|
44
45
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
46
|
+
// Import clauses
|
|
47
|
+
getImports().forEach((namedImport, index) => {
|
|
48
|
+
const element = elements[index];
|
|
48
49
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
50
|
+
expect(element.kind).toBe(ts.SyntaxKind.ImportSpecifier);
|
|
51
|
+
expect(element.name.kind).toBe(ts.SyntaxKind.Identifier);
|
|
52
|
+
expect(element.name.escapedText).toBe(namedImport);
|
|
53
|
+
});
|
|
52
54
|
});
|
|
53
55
|
});
|
|
54
56
|
});
|
|
@@ -4,67 +4,15 @@ const ts = require('typescript');
|
|
|
4
4
|
const { factory } = require('typescript');
|
|
5
5
|
|
|
6
6
|
const {
|
|
7
|
-
getAllStrapiSchemas,
|
|
8
7
|
getDefinitionAttributesCount,
|
|
9
8
|
getSchemaExtendsTypeName,
|
|
10
9
|
getSchemaInterfaceName,
|
|
11
10
|
getSchemaModelType,
|
|
12
11
|
getTypeNode,
|
|
13
12
|
toTypeLiteral,
|
|
14
|
-
} = require('../../../generators/
|
|
13
|
+
} = require('../../../generators/common/models/utils');
|
|
15
14
|
|
|
16
15
|
describe('Utils', () => {
|
|
17
|
-
describe('Get All Strapi Schemas', () => {
|
|
18
|
-
test('Get both components and content types', () => {
|
|
19
|
-
const strapi = {
|
|
20
|
-
contentTypes: {
|
|
21
|
-
ctA: {},
|
|
22
|
-
ctB: {},
|
|
23
|
-
},
|
|
24
|
-
components: {
|
|
25
|
-
comp1: {},
|
|
26
|
-
comp2: {},
|
|
27
|
-
comp3: {},
|
|
28
|
-
},
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
const schemas = getAllStrapiSchemas(strapi);
|
|
32
|
-
|
|
33
|
-
expect(schemas).toMatchObject({ ctA: {}, ctB: {}, comp1: {}, comp2: {}, comp3: {} });
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
test('Get only components if there is no content type', () => {
|
|
37
|
-
const strapi = {
|
|
38
|
-
contentTypes: {},
|
|
39
|
-
|
|
40
|
-
components: {
|
|
41
|
-
comp1: {},
|
|
42
|
-
comp2: {},
|
|
43
|
-
comp3: {},
|
|
44
|
-
},
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
const schemas = getAllStrapiSchemas(strapi);
|
|
48
|
-
|
|
49
|
-
expect(schemas).toMatchObject({ comp1: {}, comp2: {}, comp3: {} });
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
test('Get only content types if there is no component', () => {
|
|
53
|
-
const strapi = {
|
|
54
|
-
contentTypes: {
|
|
55
|
-
ctA: {},
|
|
56
|
-
ctB: {},
|
|
57
|
-
},
|
|
58
|
-
|
|
59
|
-
components: {},
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
const schemas = getAllStrapiSchemas(strapi);
|
|
63
|
-
|
|
64
|
-
expect(schemas).toMatchObject({ ctA: {}, ctB: {} });
|
|
65
|
-
});
|
|
66
|
-
});
|
|
67
|
-
|
|
68
16
|
describe('Get Definition Attributes Count', () => {
|
|
69
17
|
const createMainNode = (members = []) => {
|
|
70
18
|
return factory.createInterfaceDeclaration(
|
|
@@ -172,10 +120,10 @@ describe('Utils', () => {
|
|
|
172
120
|
|
|
173
121
|
describe('Get Schema Extends Type Name', () => {
|
|
174
122
|
test.each([
|
|
175
|
-
[{ modelType: 'component', kind: null }, 'ComponentSchema'],
|
|
176
|
-
[{ modelType: 'contentType', kind: 'singleType' }, 'SingleTypeSchema'],
|
|
177
|
-
[{ modelType: 'contentType', kind: 'collectionType' }, 'CollectionTypeSchema'],
|
|
178
|
-
[{ modelType: 'invalidType', kind: 'foo' },
|
|
123
|
+
[{ modelType: 'component', kind: null }, 'Struct.ComponentSchema'],
|
|
124
|
+
[{ modelType: 'contentType', kind: 'singleType' }, 'Struct.SingleTypeSchema'],
|
|
125
|
+
[{ modelType: 'contentType', kind: 'collectionType' }, 'Struct.CollectionTypeSchema'],
|
|
126
|
+
[{ modelType: 'invalidType', kind: 'foo' }, null],
|
|
179
127
|
])("Expect %p to generate %p as the base type for a schema's interface", (schema, expected) => {
|
|
180
128
|
expect(getSchemaExtendsTypeName(schema)).toBe(expected);
|
|
181
129
|
});
|
|
@@ -220,10 +168,16 @@ describe('Utils', () => {
|
|
|
220
168
|
});
|
|
221
169
|
|
|
222
170
|
test('Number', () => {
|
|
223
|
-
const
|
|
171
|
+
const nodePositive = toTypeLiteral(42);
|
|
172
|
+
const nodeNegative = toTypeLiteral(-42);
|
|
173
|
+
|
|
174
|
+
expect(nodePositive.kind).toBe(ts.SyntaxKind.FirstLiteralToken);
|
|
175
|
+
expect(nodePositive.text).toBe('42');
|
|
224
176
|
|
|
225
|
-
expect(
|
|
226
|
-
expect(
|
|
177
|
+
expect(nodeNegative.kind).toBe(ts.SyntaxKind.PrefixUnaryExpression);
|
|
178
|
+
expect(nodeNegative.operator).toBe(ts.SyntaxKind.MinusToken);
|
|
179
|
+
expect(nodeNegative.operand.kind).toBe(ts.SyntaxKind.FirstLiteralToken);
|
|
180
|
+
expect(nodeNegative.operand.text).toBe('42');
|
|
227
181
|
});
|
|
228
182
|
|
|
229
183
|
test('Boolean', () => {
|
|
@@ -299,13 +253,13 @@ describe('Utils', () => {
|
|
|
299
253
|
expect(objectNode.members).toHaveLength(2);
|
|
300
254
|
|
|
301
255
|
expect(objectNode.members[0].kind).toBe(ts.SyntaxKind.PropertyDeclaration);
|
|
302
|
-
expect(objectNode.members[0].name.escapedText).toBe('
|
|
303
|
-
expect(objectNode.members[0].type.kind).toBe(ts.SyntaxKind.
|
|
304
|
-
expect(objectNode.members[0].type.text).toBe('bar');
|
|
256
|
+
expect(objectNode.members[0].name.escapedText).toBe('bar');
|
|
257
|
+
expect(objectNode.members[0].type.kind).toBe(ts.SyntaxKind.TrueKeyword);
|
|
305
258
|
|
|
306
259
|
expect(objectNode.members[1].kind).toBe(ts.SyntaxKind.PropertyDeclaration);
|
|
307
|
-
expect(objectNode.members[1].name.escapedText).toBe('
|
|
308
|
-
expect(objectNode.members[1].type.kind).toBe(ts.SyntaxKind.
|
|
260
|
+
expect(objectNode.members[1].name.escapedText).toBe('foo');
|
|
261
|
+
expect(objectNode.members[1].type.kind).toBe(ts.SyntaxKind.StringLiteral);
|
|
262
|
+
expect(objectNode.members[1].type.text).toBe('bar');
|
|
309
263
|
});
|
|
310
264
|
|
|
311
265
|
test('Object', () => {
|
|
@@ -314,20 +268,20 @@ describe('Utils', () => {
|
|
|
314
268
|
expect(node.kind).toBe(ts.SyntaxKind.TypeLiteral);
|
|
315
269
|
expect(node.members).toHaveLength(2);
|
|
316
270
|
|
|
317
|
-
const [
|
|
271
|
+
const [barMember, fooMember] = node.members;
|
|
318
272
|
|
|
319
|
-
expect(
|
|
320
|
-
expect(
|
|
321
|
-
expect(
|
|
322
|
-
expect(
|
|
323
|
-
expect(firstMember.type.elements[0].kind).toBe(ts.SyntaxKind.StringLiteral);
|
|
324
|
-
expect(firstMember.type.elements[1].kind).toBe(ts.SyntaxKind.TrueKeyword);
|
|
325
|
-
expect(firstMember.type.elements[2].kind).toBe(ts.SyntaxKind.FirstLiteralToken);
|
|
273
|
+
expect(barMember.kind).toBe(ts.SyntaxKind.PropertyDeclaration);
|
|
274
|
+
expect(barMember.name.escapedText).toBe('bar');
|
|
275
|
+
expect(barMember.type.kind).toBe(ts.SyntaxKind.LiteralType);
|
|
276
|
+
expect(barMember.type.literal).toBe(ts.SyntaxKind.NullKeyword);
|
|
326
277
|
|
|
327
|
-
expect(
|
|
328
|
-
expect(
|
|
329
|
-
expect(
|
|
330
|
-
expect(
|
|
278
|
+
expect(fooMember.kind).toBe(ts.SyntaxKind.PropertyDeclaration);
|
|
279
|
+
expect(fooMember.name.escapedText).toBe('foo');
|
|
280
|
+
expect(fooMember.type.kind).toBe(ts.SyntaxKind.TupleType);
|
|
281
|
+
expect(fooMember.type.elements).toHaveLength(3);
|
|
282
|
+
expect(fooMember.type.elements[0].kind).toBe(ts.SyntaxKind.StringLiteral);
|
|
283
|
+
expect(fooMember.type.elements[1].kind).toBe(ts.SyntaxKind.TrueKeyword);
|
|
284
|
+
expect(fooMember.type.elements[2].kind).toBe(ts.SyntaxKind.FirstLiteralToken);
|
|
331
285
|
});
|
|
332
286
|
|
|
333
287
|
test('Object with complex keys', () => {
|
|
@@ -336,19 +290,19 @@ describe('Utils', () => {
|
|
|
336
290
|
expect(node.kind).toBe(ts.SyntaxKind.TypeLiteral);
|
|
337
291
|
expect(node.members).toHaveLength(2);
|
|
338
292
|
|
|
339
|
-
const [
|
|
293
|
+
const [fooBar, fooDashBar] = node.members;
|
|
340
294
|
|
|
341
|
-
expect(
|
|
342
|
-
expect(
|
|
343
|
-
expect(
|
|
344
|
-
expect(
|
|
345
|
-
expect(
|
|
295
|
+
expect(fooBar.kind).toBe(ts.SyntaxKind.PropertyDeclaration);
|
|
296
|
+
expect(fooBar.name.kind).toBe(ts.SyntaxKind.Identifier);
|
|
297
|
+
expect(fooBar.name.escapedText).toBe('foo');
|
|
298
|
+
expect(fooBar.type.kind).toBe(ts.SyntaxKind.StringLiteral);
|
|
299
|
+
expect(fooBar.type.text).toBe('bar');
|
|
346
300
|
|
|
347
|
-
expect(
|
|
348
|
-
expect(
|
|
349
|
-
expect(
|
|
350
|
-
expect(
|
|
351
|
-
expect(
|
|
301
|
+
expect(fooDashBar.kind).toBe(ts.SyntaxKind.PropertyDeclaration);
|
|
302
|
+
expect(fooDashBar.name.kind).toBe(ts.SyntaxKind.StringLiteral);
|
|
303
|
+
expect(fooDashBar.name.text).toBe('foo-bar');
|
|
304
|
+
expect(fooDashBar.type.kind).toBe(ts.SyntaxKind.StringLiteral);
|
|
305
|
+
expect(fooDashBar.type.text).toBe('foobar');
|
|
352
306
|
});
|
|
353
307
|
|
|
354
308
|
test('Invalid data type supplied (function)', () => {
|
package/lib/compile.js
CHANGED
|
@@ -3,12 +3,8 @@
|
|
|
3
3
|
const compilers = require('./compilers');
|
|
4
4
|
const getConfigPath = require('./utils/get-config-path');
|
|
5
5
|
|
|
6
|
-
module.exports = async (srcDir, {
|
|
7
|
-
// TODO: Use the Strapi debug logger instead or don't log at all
|
|
8
|
-
console.log(`Starting the compilation for TypeScript files in ${srcDir}`);
|
|
9
|
-
|
|
10
|
-
const compiler = watch ? compilers.watch : compilers.basic;
|
|
6
|
+
module.exports = async (srcDir, { configOptions = {} } = {}) => {
|
|
11
7
|
const configPath = getConfigPath(srcDir);
|
|
12
8
|
|
|
13
|
-
|
|
9
|
+
compilers.basic.run(configPath, configOptions);
|
|
14
10
|
};
|
package/lib/compilers/basic.js
CHANGED
|
@@ -13,15 +13,23 @@ module.exports = {
|
|
|
13
13
|
* @param {Object} configOptions
|
|
14
14
|
* @param {Array.<string>} configOptions.fileNames
|
|
15
15
|
* @param {Object} configOptions.options
|
|
16
|
+
* @param {boolean} configOptions.ignoreDiagnostics
|
|
16
17
|
*/
|
|
17
18
|
run(tsConfigPath, configOptions = {}) {
|
|
19
|
+
const { ignoreDiagnostics = false } = configOptions;
|
|
18
20
|
// Parse the tsconfig.json file & resolve the configuration options
|
|
19
21
|
const { fileNames, options, projectReferences } = resolveConfigOptions(tsConfigPath);
|
|
20
22
|
|
|
23
|
+
const compilerOptions = merge(options, configOptions.options);
|
|
24
|
+
|
|
25
|
+
if (ignoreDiagnostics) {
|
|
26
|
+
Object.assign(compilerOptions, { noEmit: false, noEmitOnError: false });
|
|
27
|
+
}
|
|
28
|
+
|
|
21
29
|
const program = ts.createProgram({
|
|
22
30
|
rootNames: configOptions.fileNames ? configOptions.fileNames : fileNames,
|
|
23
31
|
projectReferences,
|
|
24
|
-
options:
|
|
32
|
+
options: compilerOptions,
|
|
25
33
|
});
|
|
26
34
|
|
|
27
35
|
const emitResults = program.emit();
|
|
@@ -30,12 +38,12 @@ module.exports = {
|
|
|
30
38
|
ts.getPreEmitDiagnostics(program).concat(emitResults.diagnostics)
|
|
31
39
|
);
|
|
32
40
|
|
|
33
|
-
if (diagnostics.length > 0) {
|
|
41
|
+
if (!ignoreDiagnostics && diagnostics.length > 0) {
|
|
34
42
|
reportDiagnostics(diagnostics);
|
|
35
43
|
}
|
|
36
44
|
|
|
37
|
-
// If the compilation failed, exit early
|
|
38
|
-
if (emitResults.emitSkipped) {
|
|
45
|
+
// If the compilation failed and diagnostics are not ignored, exit early
|
|
46
|
+
if (!ignoreDiagnostics && emitResults.emitSkipped) {
|
|
39
47
|
process.exit(1);
|
|
40
48
|
}
|
|
41
49
|
},
|
package/lib/compilers/index.js
CHANGED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { factory } = require('typescript');
|
|
4
|
+
|
|
5
|
+
const imports = [];
|
|
6
|
+
|
|
7
|
+
module.exports = {
|
|
8
|
+
getImports() {
|
|
9
|
+
return imports;
|
|
10
|
+
},
|
|
11
|
+
|
|
12
|
+
addImport(type) {
|
|
13
|
+
const hasType = imports.includes(type);
|
|
14
|
+
|
|
15
|
+
if (!hasType) {
|
|
16
|
+
imports.push(type);
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
|
|
20
|
+
generateImportDefinition() {
|
|
21
|
+
const formattedImports = imports
|
|
22
|
+
.sort()
|
|
23
|
+
.map((key) => factory.createImportSpecifier(false, undefined, factory.createIdentifier(key)));
|
|
24
|
+
|
|
25
|
+
return [
|
|
26
|
+
factory.createImportDeclaration(
|
|
27
|
+
undefined,
|
|
28
|
+
factory.createImportClause(true, undefined, factory.createNamedImports(formattedImports)),
|
|
29
|
+
factory.createStringLiteral('@strapi/strapi'),
|
|
30
|
+
undefined
|
|
31
|
+
),
|
|
32
|
+
];
|
|
33
|
+
},
|
|
34
|
+
};
|
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const
|
|
3
|
+
const ts = require('typescript');
|
|
4
4
|
const _ = require('lodash/fp');
|
|
5
5
|
|
|
6
|
-
const { addImport } = require('
|
|
7
|
-
const { getTypeNode, toTypeLiteral } = require('./utils');
|
|
6
|
+
const { addImport } = require('../imports');
|
|
7
|
+
const { getTypeNode, toTypeLiteral, withAttributeNamespace, NAMESPACES } = require('./utils');
|
|
8
8
|
const mappers = require('./mappers');
|
|
9
9
|
|
|
10
|
+
const { factory } = ts;
|
|
11
|
+
|
|
10
12
|
/**
|
|
11
13
|
* Create the base type node for a given attribute
|
|
12
14
|
*
|
|
@@ -26,7 +28,8 @@ const getAttributeType = (attributeName, attribute, uid) => {
|
|
|
26
28
|
|
|
27
29
|
const [attributeType, typeParams] = mappers[attribute.type]({ uid, attribute, attributeName });
|
|
28
30
|
|
|
29
|
-
|
|
31
|
+
// Make sure the schema namespace is imported
|
|
32
|
+
addImport(NAMESPACES.Schema);
|
|
30
33
|
|
|
31
34
|
return getTypeNode(attributeType, typeParams);
|
|
32
35
|
};
|
|
@@ -42,38 +45,36 @@ const getAttributeModifiers = (attribute) => {
|
|
|
42
45
|
|
|
43
46
|
// Required
|
|
44
47
|
if (attribute.required) {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
+
modifiers.push(
|
|
49
|
+
factory.createTypeReferenceNode(factory.createIdentifier(withAttributeNamespace('Required')))
|
|
50
|
+
);
|
|
48
51
|
}
|
|
49
52
|
|
|
50
53
|
// Private
|
|
51
54
|
if (attribute.private) {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
+
modifiers.push(
|
|
56
|
+
factory.createTypeReferenceNode(factory.createIdentifier(withAttributeNamespace('Private')))
|
|
57
|
+
);
|
|
55
58
|
}
|
|
56
59
|
|
|
57
60
|
// Unique
|
|
58
61
|
if (attribute.unique) {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
+
modifiers.push(
|
|
63
|
+
factory.createTypeReferenceNode(factory.createIdentifier(withAttributeNamespace('Unique')))
|
|
64
|
+
);
|
|
62
65
|
}
|
|
63
66
|
|
|
64
67
|
// Configurable
|
|
65
68
|
if (attribute.configurable) {
|
|
66
|
-
addImport('ConfigurableAttribute');
|
|
67
|
-
|
|
68
69
|
modifiers.push(
|
|
69
|
-
factory.createTypeReferenceNode(
|
|
70
|
+
factory.createTypeReferenceNode(
|
|
71
|
+
factory.createIdentifier(withAttributeNamespace('Configurable'))
|
|
72
|
+
)
|
|
70
73
|
);
|
|
71
74
|
}
|
|
72
75
|
|
|
73
76
|
// Custom field
|
|
74
77
|
if (attribute.customField) {
|
|
75
|
-
addImport('CustomField');
|
|
76
|
-
|
|
77
78
|
const customFieldUid = factory.createStringLiteral(attribute.customField);
|
|
78
79
|
const typeArguments = [customFieldUid];
|
|
79
80
|
|
|
@@ -82,17 +83,18 @@ const getAttributeModifiers = (attribute) => {
|
|
|
82
83
|
}
|
|
83
84
|
|
|
84
85
|
modifiers.push(
|
|
85
|
-
factory.createTypeReferenceNode(
|
|
86
|
+
factory.createTypeReferenceNode(
|
|
87
|
+
factory.createIdentifier(withAttributeNamespace('CustomField')),
|
|
88
|
+
typeArguments
|
|
89
|
+
)
|
|
86
90
|
);
|
|
87
91
|
}
|
|
88
92
|
|
|
89
93
|
// Plugin Options
|
|
90
94
|
if (!_.isEmpty(attribute.pluginOptions)) {
|
|
91
|
-
addImport('SetPluginOptions');
|
|
92
|
-
|
|
93
95
|
modifiers.push(
|
|
94
96
|
factory.createTypeReferenceNode(
|
|
95
|
-
factory.createIdentifier('SetPluginOptions'),
|
|
97
|
+
factory.createIdentifier(withAttributeNamespace('SetPluginOptions')),
|
|
96
98
|
// Transform the pluginOptions object into an object literal expression
|
|
97
99
|
[toTypeLiteral(attribute.pluginOptions)]
|
|
98
100
|
)
|
|
@@ -100,40 +102,64 @@ const getAttributeModifiers = (attribute) => {
|
|
|
100
102
|
}
|
|
101
103
|
|
|
102
104
|
// Min / Max
|
|
103
|
-
// TODO: Always provide a second type argument for min/max (ie: resolve the attribute scalar type with a `GetAttributeType<${mappers[attribute][0]}>` (useful for biginter (string values)))
|
|
104
105
|
if (!_.isNil(attribute.min) || !_.isNil(attribute.max)) {
|
|
105
|
-
addImport('SetMinMax');
|
|
106
|
-
|
|
107
106
|
const minMaxProperties = _.pick(['min', 'max'], attribute);
|
|
107
|
+
const { min, max } = minMaxProperties;
|
|
108
|
+
|
|
109
|
+
const typeofMin = typeof min;
|
|
110
|
+
const typeofMax = typeof max;
|
|
111
|
+
|
|
112
|
+
// Throws error if min/max exist but have different types to prevent unexpected behavior
|
|
113
|
+
if (min !== undefined && max !== undefined && typeofMin !== typeofMax) {
|
|
114
|
+
throw new Error('typeof min/max values mismatch');
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
let typeKeyword;
|
|
118
|
+
|
|
119
|
+
// use 'string'
|
|
120
|
+
if (typeofMin === 'string' || typeofMax === 'string') {
|
|
121
|
+
typeKeyword = ts.SyntaxKind.StringKeyword;
|
|
122
|
+
}
|
|
123
|
+
// use 'number'
|
|
124
|
+
else if (typeofMin === 'number' || typeofMax === 'number') {
|
|
125
|
+
typeKeyword = ts.SyntaxKind.NumberKeyword;
|
|
126
|
+
}
|
|
127
|
+
// invalid type
|
|
128
|
+
else {
|
|
129
|
+
throw new Error(
|
|
130
|
+
`Invalid data type for min/max options. Must be string, number or undefined, but found { min: ${min} (${typeofMin}), max: ${max} (${typeofMax}) }`
|
|
131
|
+
);
|
|
132
|
+
}
|
|
108
133
|
|
|
109
134
|
modifiers.push(
|
|
110
|
-
factory.createTypeReferenceNode(
|
|
111
|
-
|
|
112
|
-
|
|
135
|
+
factory.createTypeReferenceNode(
|
|
136
|
+
factory.createIdentifier(withAttributeNamespace('SetMinMax')),
|
|
137
|
+
[toTypeLiteral(minMaxProperties), factory.createKeywordTypeNode(typeKeyword)]
|
|
138
|
+
)
|
|
113
139
|
);
|
|
114
140
|
}
|
|
115
141
|
|
|
116
142
|
// Min length / Max length
|
|
117
143
|
if (!_.isNil(attribute.minLength) || !_.isNil(attribute.maxLength)) {
|
|
118
|
-
addImport('SetMinMaxLength');
|
|
119
|
-
|
|
120
144
|
const minMaxProperties = _.pick(['minLength', 'maxLength'], attribute);
|
|
121
145
|
|
|
122
146
|
modifiers.push(
|
|
123
|
-
factory.createTypeReferenceNode(
|
|
124
|
-
|
|
125
|
-
|
|
147
|
+
factory.createTypeReferenceNode(
|
|
148
|
+
factory.createIdentifier(withAttributeNamespace('SetMinMaxLength')),
|
|
149
|
+
[toTypeLiteral(minMaxProperties)]
|
|
150
|
+
)
|
|
126
151
|
);
|
|
127
152
|
}
|
|
128
153
|
|
|
129
|
-
// Default
|
|
130
|
-
if (!_.isNil(attribute.default)) {
|
|
131
|
-
addImport('DefaultTo');
|
|
132
|
-
|
|
154
|
+
// Default (ignore if default is a function)
|
|
155
|
+
if (!_.isNil(attribute.default) && !_.isFunction(attribute.default)) {
|
|
133
156
|
const defaultLiteral = toTypeLiteral(attribute.default);
|
|
134
157
|
|
|
135
158
|
modifiers.push(
|
|
136
|
-
factory.createTypeReferenceNode(
|
|
159
|
+
factory.createTypeReferenceNode(
|
|
160
|
+
factory.createIdentifier(withAttributeNamespace('DefaultTo')),
|
|
161
|
+
[defaultLiteral]
|
|
162
|
+
)
|
|
137
163
|
);
|
|
138
164
|
}
|
|
139
165
|
|
|
@@ -169,6 +195,4 @@ const attributeToPropertySignature = (schema, attributeName, attribute) => {
|
|
|
169
195
|
|
|
170
196
|
module.exports = attributeToPropertySignature;
|
|
171
197
|
|
|
172
|
-
module.exports
|
|
173
|
-
module.exports.getAttributeType = getAttributeType;
|
|
174
|
-
module.exports.getAttributeModifiers = getAttributeModifiers;
|
|
198
|
+
Object.assign(module.exports, { mappers, getAttributeModifiers, getAttributeType });
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const schema = require('./schema');
|
|
4
|
+
const attributes = require('./attributes');
|
|
5
|
+
const mappers = require('./mappers');
|
|
6
|
+
const utils = require('./utils');
|
|
7
|
+
const imports = require('../imports');
|
|
8
|
+
|
|
9
|
+
module.exports = {
|
|
10
|
+
schema,
|
|
11
|
+
attributes,
|
|
12
|
+
mappers,
|
|
13
|
+
utils,
|
|
14
|
+
imports,
|
|
15
|
+
};
|