@strapi/typescript-utils 4.2.0-beta.3 → 4.3.0-beta.2

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.
@@ -0,0 +1,81 @@
1
+ 'use strict';
2
+
3
+ jest.mock('../../../generators/schemas/imports', () => ({ addImport: jest.fn() }));
4
+
5
+ const consoleWarnMock = jest.spyOn(console, 'warn').mockImplementation();
6
+
7
+ const ts = require('typescript');
8
+
9
+ const { getAttributeType } = require('../../../generators/schemas/attributes');
10
+ const { addImport } = require('../../../generators/schemas/imports');
11
+
12
+ describe('Attributes', () => {
13
+ afterEach(() => {
14
+ jest.resetAllMocks();
15
+ });
16
+
17
+ // TODO
18
+ // describe('Attribute to Property Signature', () => {});
19
+
20
+ // TODO
21
+ // describe('Mappers', () => {});
22
+
23
+ describe('Get Attribute Type', () => {
24
+ test('If the attribute type is not valid then log an error and exit early without importing the type', () => {
25
+ const typeNode = getAttributeType('foo', { type: 'invalid', uid: 'api::foo.foo' });
26
+
27
+ expect(typeNode).toBeNull();
28
+ expect(consoleWarnMock).toHaveBeenCalledWith(
29
+ '"foo" attribute from "undefined" has an invalid type: "invalid"'
30
+ );
31
+ expect(addImport).not.toHaveBeenCalled();
32
+ });
33
+
34
+ test('Return a basic type node without generic type parameter', () => {
35
+ const typeNode = getAttributeType('foo', { type: 'string' });
36
+
37
+ expect(ts.isTypeNode(typeNode)).toBeTruthy();
38
+
39
+ expect(typeNode.kind).toBe(ts.SyntaxKind.TypeReference);
40
+ expect(typeNode.typeName.escapedText).toBe('StringAttribute');
41
+ expect(typeNode.typeArguments).toBeUndefined();
42
+
43
+ expect(consoleWarnMock).not.toHaveBeenCalled();
44
+ expect(addImport).toHaveBeenCalledWith('StringAttribute');
45
+ });
46
+
47
+ describe('Complex types (with generic type parameters)', () => {
48
+ const defaultAssertions = (typeNode, typeName) => {
49
+ expect(ts.isTypeNode(typeNode)).toBeTruthy();
50
+
51
+ expect(typeNode.kind).toBe(ts.SyntaxKind.TypeReference);
52
+ expect(typeNode.typeName.escapedText).toBe(typeName);
53
+
54
+ expect(consoleWarnMock).not.toHaveBeenCalled();
55
+ expect(addImport).toHaveBeenCalledWith(typeName);
56
+ };
57
+
58
+ test('Enumeration', () => {
59
+ const attribute = { type: 'enumeration', enum: ['a', 'b', 'c'] };
60
+ const typeNode = getAttributeType('foo', attribute);
61
+
62
+ defaultAssertions(typeNode, 'EnumerationAttribute');
63
+
64
+ expect(typeNode.typeArguments).toHaveLength(1);
65
+ expect(typeNode.typeArguments[0].kind).toBe(ts.SyntaxKind.TupleType);
66
+
67
+ const tupleElements = typeNode.typeArguments[0].elements;
68
+
69
+ attribute.enum.forEach((value, index) => {
70
+ const element = tupleElements[index];
71
+
72
+ expect(element.kind).toBe(ts.SyntaxKind.StringLiteral);
73
+ expect(element.text).toBe(value);
74
+ });
75
+ });
76
+ });
77
+ });
78
+
79
+ // TODO
80
+ // describe('Get Attribute Modifiers', () => {});
81
+ });
@@ -0,0 +1,54 @@
1
+ 'use strict';
2
+
3
+ const ts = require('typescript');
4
+
5
+ const {
6
+ addImport,
7
+ generateImportDefinition,
8
+ getImports,
9
+ } = require('../../../generators/schemas/imports');
10
+
11
+ describe('Imports', () => {
12
+ test('When first loaded, the list of imports should be empty', () => {
13
+ expect(getImports()).toHaveLength(0);
14
+ });
15
+
16
+ test('Can add new imports to the list', () => {
17
+ addImport('foo');
18
+ addImport('bar');
19
+
20
+ expect(getImports()).toHaveLength(2);
21
+ });
22
+
23
+ test('When adding an already registered import, ignore it', () => {
24
+ addImport('foo');
25
+
26
+ expect(getImports()).toHaveLength(2);
27
+ });
28
+
29
+ test('Generate an import type definition containing the registered import', () => {
30
+ const def = generateImportDefinition();
31
+
32
+ expect(def.kind).toBe(ts.SyntaxKind.ImportDeclaration);
33
+
34
+ // Module specifier
35
+ expect(def.moduleSpecifier.kind).toBe(ts.SyntaxKind.StringLiteral);
36
+ expect(def.moduleSpecifier.text).toBe('@strapi/strapi');
37
+
38
+ // Import clause (should be named imports)
39
+ expect(def.importClause.kind).toBe(ts.SyntaxKind.ImportClause);
40
+
41
+ const { elements } = def.importClause.namedBindings;
42
+
43
+ expect(elements).toHaveLength(2);
44
+
45
+ // Import clauses
46
+ getImports().forEach((namedImport, index) => {
47
+ const element = elements[index];
48
+
49
+ expect(element.kind).toBe(ts.SyntaxKind.ImportSpecifier);
50
+ expect(element.name.kind).toBe(ts.SyntaxKind.Identifier);
51
+ expect(element.name.escapedText).toBe(namedImport);
52
+ });
53
+ });
54
+ });
@@ -0,0 +1,362 @@
1
+ 'use strict';
2
+
3
+ const ts = require('typescript');
4
+ const { factory } = require('typescript');
5
+
6
+ const {
7
+ getAllStrapiSchemas,
8
+ getDefinitionAttributesCount,
9
+ getSchemaExtendsTypeName,
10
+ getSchemaInterfaceName,
11
+ getSchemaModelType,
12
+ getTypeNode,
13
+ toTypeLiteral,
14
+ } = require('../../../generators/schemas/utils');
15
+
16
+ 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
+ describe('Get Definition Attributes Count', () => {
69
+ const createMainNode = (members = []) => {
70
+ return factory.createInterfaceDeclaration(
71
+ undefined,
72
+ undefined,
73
+ factory.createIdentifier('Foo'),
74
+ undefined,
75
+ undefined,
76
+ members
77
+ );
78
+ };
79
+
80
+ const createPropertyDeclaration = (name, type) => {
81
+ return factory.createPropertyDeclaration(
82
+ undefined,
83
+ undefined,
84
+ factory.createIdentifier(name),
85
+ undefined,
86
+ type
87
+ );
88
+ };
89
+
90
+ test('Returns null if there are no members in the parent node', () => {
91
+ const mainNode = createMainNode();
92
+
93
+ const count = getDefinitionAttributesCount(mainNode);
94
+
95
+ expect(count).toBeNull();
96
+ });
97
+
98
+ test('Returns null if there are members in the parent node, but none named "attributes"', () => {
99
+ const mainNode = createMainNode([
100
+ createPropertyDeclaration(
101
+ 'bar',
102
+ factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword)
103
+ ),
104
+ createPropertyDeclaration(
105
+ 'foobar',
106
+ factory.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword)
107
+ ),
108
+ ]);
109
+
110
+ const count = getDefinitionAttributesCount(mainNode);
111
+
112
+ expect(count).toBeNull();
113
+ });
114
+
115
+ test('Returns the number of attributes if the property is present', () => {
116
+ const mainNode = createMainNode([
117
+ createPropertyDeclaration(
118
+ 'bar',
119
+ factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword)
120
+ ),
121
+ createPropertyDeclaration(
122
+ 'attributes',
123
+ factory.createTypeLiteralNode([
124
+ createPropertyDeclaration(
125
+ 'a',
126
+ factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword)
127
+ ),
128
+ createPropertyDeclaration(
129
+ 'b',
130
+ factory.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword)
131
+ ),
132
+ createPropertyDeclaration(
133
+ 'c',
134
+ factory.createKeywordTypeNode(ts.SyntaxKind.BooleanKeyword)
135
+ ),
136
+ ])
137
+ ),
138
+ createPropertyDeclaration(
139
+ 'foobar',
140
+ factory.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword)
141
+ ),
142
+ ]);
143
+
144
+ const count = getDefinitionAttributesCount(mainNode);
145
+
146
+ expect(count).toBe(3);
147
+ });
148
+
149
+ test("Returns 0 if the attributes node is present but don't have any members", () => {
150
+ const mainNode = createMainNode([
151
+ createPropertyDeclaration('attributes', factory.createTypeLiteralNode()),
152
+ createPropertyDeclaration(
153
+ 'foobar',
154
+ factory.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword)
155
+ ),
156
+ ]);
157
+
158
+ const count = getDefinitionAttributesCount(mainNode);
159
+
160
+ expect(count).toBe(0);
161
+ });
162
+ });
163
+
164
+ describe('Get Schema Model Type', () => {
165
+ test.each([
166
+ [{ modelType: 'component', kind: null }, 'component'],
167
+ [{ modelType: 'contentType', kind: 'singleType' }, 'singleType'],
168
+ [{ modelType: 'contentType', kind: 'collectionType' }, 'collectionType'],
169
+ [{ modelType: 'invalidType', kind: 'foo' }, null],
170
+ ])('%p to be evaluated to %p', (schema, expected) => {
171
+ expect(getSchemaModelType(schema)).toBe(expected);
172
+ });
173
+ });
174
+
175
+ describe('Get Schema Extends Type Name', () => {
176
+ test.each([
177
+ [{ modelType: 'component', kind: null }, 'ComponentSchema'],
178
+ [{ modelType: 'contentType', kind: 'singleType' }, 'SingleTypeSchema'],
179
+ [{ modelType: 'contentType', kind: 'collectionType' }, 'CollectionTypeSchema'],
180
+ [{ modelType: 'invalidType', kind: 'foo' }, 'Schema'],
181
+ ])("Expect %p to generate %p as the base type for a schema's interface", (schema, expected) => {
182
+ expect(getSchemaExtendsTypeName(schema)).toBe(expected);
183
+ });
184
+ });
185
+
186
+ describe('Get Schema Interface Name', () => {
187
+ test.each([
188
+ ['api::foo.foo', 'ApiFooFoo'],
189
+ ['plugin::bar.foo', 'PluginBarFoo'],
190
+ ['default.dish', 'DefaultDish'],
191
+ ])('Should transform UID (%p) to interface name (%p)', (uid, interfaceName) => {
192
+ expect(getSchemaInterfaceName(uid)).toBe(interfaceName);
193
+ });
194
+ });
195
+
196
+ describe('Get Type Node', () => {
197
+ test('Create a valid type reference node based on the given generic parameters', () => {
198
+ const node = getTypeNode('FooBar', [
199
+ factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword),
200
+ factory.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword),
201
+ ]);
202
+
203
+ expect(node.typeArguments).toHaveLength(2);
204
+
205
+ expect(node.typeArguments[0].kind).toBe(ts.SyntaxKind.StringKeyword);
206
+ expect(node.typeArguments[1].kind).toBe(ts.SyntaxKind.NumberKeyword);
207
+ });
208
+
209
+ test('Create a valid empty type reference node', () => {
210
+ const node = getTypeNode('FooBar');
211
+
212
+ expect(node.typeArguments).toBeUndefined();
213
+ });
214
+ });
215
+
216
+ describe('To Type Literal', () => {
217
+ test('String', () => {
218
+ const node = toTypeLiteral('foo');
219
+
220
+ expect(node.kind).toBe(ts.SyntaxKind.StringLiteral);
221
+ expect(node.text).toBe('foo');
222
+ });
223
+
224
+ test('Number', () => {
225
+ const node = toTypeLiteral(42);
226
+
227
+ expect(node.kind).toBe(ts.SyntaxKind.FirstLiteralToken);
228
+ expect(node.text).toBe('42');
229
+ });
230
+
231
+ test('Boolean', () => {
232
+ const trueNode = toTypeLiteral(true);
233
+ const falseNode = toTypeLiteral(false);
234
+
235
+ expect(trueNode.kind).toBe(ts.SyntaxKind.TrueKeyword);
236
+ expect(falseNode.kind).toBe(ts.SyntaxKind.FalseKeyword);
237
+ });
238
+
239
+ test('undefined', () => {
240
+ const node = toTypeLiteral(undefined);
241
+
242
+ expect(node.kind).toBe(ts.SyntaxKind.LiteralType);
243
+ expect(node.literal).toBe(ts.SyntaxKind.UndefinedKeyword);
244
+ });
245
+
246
+ test('null', () => {
247
+ const node = toTypeLiteral(null);
248
+
249
+ expect(node.kind).toBe(ts.SyntaxKind.LiteralType);
250
+ expect(node.literal).toBe(ts.SyntaxKind.NullKeyword);
251
+ });
252
+
253
+ test('Array (empty)', () => {
254
+ const node = toTypeLiteral([]);
255
+
256
+ expect(node.kind).toBe(ts.SyntaxKind.TupleType);
257
+ expect(node.elements).toHaveLength(0);
258
+ });
259
+
260
+ test('Array (with elements)', () => {
261
+ const node = toTypeLiteral(['foo', 2]);
262
+
263
+ expect(node.kind).toBe(ts.SyntaxKind.TupleType);
264
+ expect(node.elements).toHaveLength(2);
265
+
266
+ expect(node.elements[0].kind).toBe(ts.SyntaxKind.StringLiteral);
267
+ expect(node.elements[0].text).toBe('foo');
268
+
269
+ expect(node.elements[1].kind).toBe(ts.SyntaxKind.FirstLiteralToken);
270
+ expect(node.elements[1].text).toBe('2');
271
+ });
272
+
273
+ test('Array (nested)', () => {
274
+ const node = toTypeLiteral(['foo', ['bar', 'foobar']]);
275
+
276
+ expect(node.kind).toBe(ts.SyntaxKind.TupleType);
277
+ expect(node.elements).toHaveLength(2);
278
+
279
+ expect(node.elements[0].kind).toBe(ts.SyntaxKind.StringLiteral);
280
+ expect(node.elements[0].text).toBe('foo');
281
+
282
+ expect(node.elements[1].kind).toBe(ts.SyntaxKind.TupleType);
283
+ expect(node.elements[1].elements).toHaveLength(2);
284
+
285
+ expect(node.elements[1].elements[0].kind).toBe(ts.SyntaxKind.StringLiteral);
286
+ expect(node.elements[1].elements[0].text).toBe('bar');
287
+
288
+ expect(node.elements[1].elements[1].kind).toBe(ts.SyntaxKind.StringLiteral);
289
+ expect(node.elements[1].elements[1].text).toBe('foobar');
290
+ });
291
+
292
+ test('Array (with object)', () => {
293
+ const node = toTypeLiteral([{ foo: 'bar', bar: true }]);
294
+
295
+ expect(node.kind).toBe(ts.SyntaxKind.TupleType);
296
+ expect(node.elements).toHaveLength(1);
297
+
298
+ const objectNode = node.elements[0];
299
+
300
+ expect(objectNode.kind).toBe(ts.SyntaxKind.TypeLiteral);
301
+ expect(objectNode.members).toHaveLength(2);
302
+
303
+ expect(objectNode.members[0].kind).toBe(ts.SyntaxKind.PropertyDeclaration);
304
+ expect(objectNode.members[0].name.escapedText).toBe('foo');
305
+ expect(objectNode.members[0].type.kind).toBe(ts.SyntaxKind.StringLiteral);
306
+ expect(objectNode.members[0].type.text).toBe('bar');
307
+
308
+ expect(objectNode.members[1].kind).toBe(ts.SyntaxKind.PropertyDeclaration);
309
+ expect(objectNode.members[1].name.escapedText).toBe('bar');
310
+ expect(objectNode.members[1].type.kind).toBe(ts.SyntaxKind.TrueKeyword);
311
+ });
312
+
313
+ test('Object', () => {
314
+ const node = toTypeLiteral({ foo: ['bar', true, 2], bar: null });
315
+
316
+ expect(node.kind).toBe(ts.SyntaxKind.TypeLiteral);
317
+ expect(node.members).toHaveLength(2);
318
+
319
+ const [firstMember, secondMember] = node.members;
320
+
321
+ expect(firstMember.kind).toBe(ts.SyntaxKind.PropertyDeclaration);
322
+ expect(firstMember.name.escapedText).toBe('foo');
323
+ expect(firstMember.type.kind).toBe(ts.SyntaxKind.TupleType);
324
+ expect(firstMember.type.elements).toHaveLength(3);
325
+ expect(firstMember.type.elements[0].kind).toBe(ts.SyntaxKind.StringLiteral);
326
+ expect(firstMember.type.elements[1].kind).toBe(ts.SyntaxKind.TrueKeyword);
327
+ expect(firstMember.type.elements[2].kind).toBe(ts.SyntaxKind.FirstLiteralToken);
328
+
329
+ expect(secondMember.kind).toBe(ts.SyntaxKind.PropertyDeclaration);
330
+ expect(secondMember.name.escapedText).toBe('bar');
331
+ expect(secondMember.type.kind).toBe(ts.SyntaxKind.LiteralType);
332
+ expect(secondMember.type.literal).toBe(ts.SyntaxKind.NullKeyword);
333
+ });
334
+
335
+ test('Object with complex keys', () => {
336
+ const node = toTypeLiteral({ 'foo-bar': 'foobar', foo: 'bar' });
337
+
338
+ expect(node.kind).toBe(ts.SyntaxKind.TypeLiteral);
339
+ expect(node.members).toHaveLength(2);
340
+
341
+ const [firstMember, secondMember] = node.members;
342
+
343
+ expect(firstMember.kind).toBe(ts.SyntaxKind.PropertyDeclaration);
344
+ expect(firstMember.name.kind).toBe(ts.SyntaxKind.StringLiteral);
345
+ expect(firstMember.name.text).toBe('foo-bar');
346
+ expect(firstMember.type.kind).toBe(ts.SyntaxKind.StringLiteral);
347
+ expect(firstMember.type.text).toBe('foobar');
348
+
349
+ expect(secondMember.kind).toBe(ts.SyntaxKind.PropertyDeclaration);
350
+ expect(secondMember.name.kind).toBe(ts.SyntaxKind.Identifier);
351
+ expect(secondMember.name.escapedText).toBe('foo');
352
+ expect(secondMember.type.kind).toBe(ts.SyntaxKind.StringLiteral);
353
+ expect(secondMember.type.text).toBe('bar');
354
+ });
355
+
356
+ test('Invalid data type supplied (function)', () => {
357
+ expect(() => toTypeLiteral(() => {})).toThrowError(
358
+ 'Cannot convert to object literal. Unknown type "function"'
359
+ );
360
+ });
361
+ });
362
+ });
@@ -0,0 +1,37 @@
1
+ 'use strict';
2
+
3
+ const path = require('path');
4
+ const fs = require('fs-extra');
5
+
6
+ module.exports = async dest => {
7
+ const tsConfig = {
8
+ compilerOptions: {
9
+ lib: ['es2019', 'es2020.promise', 'es2020.bigint', 'es2020.string', 'DOM'],
10
+ noImplicitAny: false,
11
+ module: 'es2020',
12
+ target: 'es5',
13
+ jsx: 'react',
14
+ allowJs: true,
15
+ strict: true,
16
+ moduleResolution: 'node',
17
+ skipLibCheck: true,
18
+ esModuleInterop: true,
19
+ allowSyntheticDefaultImports: true,
20
+ resolveJsonModule: true,
21
+ noEmit: false,
22
+ incremental: true,
23
+ },
24
+ include: ['../../../src/admin/*', '../../../src/**/**/admin/src/*'],
25
+ exclude: ['node_modules', '**/*.test.js', '*.js'],
26
+ };
27
+
28
+ const filePath = path.join(dest, 'admin', 'src', 'tsconfig.json');
29
+
30
+ try {
31
+ await fs.ensureFile(filePath);
32
+
33
+ await fs.writeJSON(filePath, tsConfig, { spaces: 2 });
34
+ } catch (err) {
35
+ console.log(err);
36
+ }
37
+ };
@@ -0,0 +1,5 @@
1
+ 'use strict';
2
+
3
+ const createTSConfigFile = require('./create-tsconfig-file');
4
+
5
+ module.exports = { createTSConfigFile };
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+
3
+ const generateSchemasDefinitions = require('./schemas');
4
+
5
+ module.exports = {
6
+ generateSchemasDefinitions,
7
+ };