@strapi/typescript-utils 0.0.0-next.c58b405b44c71a93df733024d1f15c069cb6bdca → 0.0.0-next.c5f067b5650921187770124e9b6c8186e805e242
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/.eslintignore +1 -0
- package/index.d.ts +5 -0
- package/jest.config.js +1 -0
- package/lib/__tests__/generators/schemas/attributes.test.js +147 -95
- package/lib/__tests__/generators/schemas/imports.test.js +18 -16
- package/lib/__tests__/generators/schemas/utils.test.js +5 -59
- package/lib/admin/create-tsconfig-file.js +2 -16
- package/lib/compile.js +2 -6
- package/lib/compilers/basic.js +12 -4
- package/lib/compilers/index.js +0 -2
- package/lib/generators/{schemas → common}/imports.js +8 -7
- package/lib/generators/common/index.js +9 -0
- package/lib/generators/{schemas → common/models}/attributes.js +62 -36
- package/lib/generators/common/models/index.js +15 -0
- package/lib/generators/{schemas → common/models}/mappers.js +27 -24
- package/lib/generators/{schemas → common/models}/schema.js +10 -6
- package/lib/generators/{schemas → common/models}/utils.js +29 -16
- package/lib/generators/components/index.js +58 -0
- package/lib/generators/constants.js +6 -0
- package/lib/generators/content-types/index.js +58 -0
- package/lib/generators/index.js +118 -3
- package/lib/generators/utils.js +211 -0
- package/package.json +11 -5
- package/tsconfigs/admin.json +18 -19
- package/tsconfigs/server.json +17 -16
- package/lib/__tests__/generators/schemas/global.test.js +0 -108
- package/lib/compilers/watch.js +0 -37
- package/lib/generators/schemas/global.js +0 -70
- package/lib/generators/schemas/index.js +0 -185
package/.eslintignore
CHANGED
package/index.d.ts
ADDED
package/jest.config.js
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
jest.mock('../../../generators/
|
|
3
|
+
jest.mock('../../../generators/common/imports', () => ({ addImport: jest.fn() }));
|
|
4
4
|
|
|
5
5
|
const consoleWarnMock = jest.spyOn(console, 'warn').mockImplementation();
|
|
6
6
|
|
|
7
7
|
const ts = require('typescript');
|
|
8
8
|
|
|
9
|
-
const attributeToPropertySignature = require('../../../generators/
|
|
9
|
+
const attributeToPropertySignature = require('../../../generators/common/models/attributes');
|
|
10
10
|
const {
|
|
11
11
|
getAttributeType,
|
|
12
12
|
getAttributeModifiers,
|
|
13
|
-
} = require('../../../generators/
|
|
14
|
-
const { addImport } = require('../../../generators/
|
|
13
|
+
} = require('../../../generators/common/models/attributes');
|
|
14
|
+
const { addImport } = require('../../../generators/common/imports');
|
|
15
15
|
|
|
16
16
|
// TODO: emit definition (to a string) & also check snapshots based on that. It would allow checking both the structure & the output.
|
|
17
17
|
describe('Attributes', () => {
|
|
18
18
|
afterEach(() => {
|
|
19
|
-
jest.
|
|
19
|
+
jest.clearAllMocks();
|
|
20
20
|
});
|
|
21
21
|
|
|
22
22
|
describe('Attribute to Property Signature', () => {
|
|
@@ -48,7 +48,7 @@ describe('Attributes', () => {
|
|
|
48
48
|
|
|
49
49
|
expect(prop.type.types).toHaveLength(1);
|
|
50
50
|
expect(prop.type.types[0].kind).toBe(ts.SyntaxKind.TypeReference);
|
|
51
|
-
expect(prop.type.types[0].typeName.escapedText).toBe('
|
|
51
|
+
expect(prop.type.types[0].typeName.escapedText).toBe('Attribute.String');
|
|
52
52
|
expect(prop.type.types[0].typeArguments).toBeUndefined();
|
|
53
53
|
});
|
|
54
54
|
|
|
@@ -60,7 +60,7 @@ describe('Attributes', () => {
|
|
|
60
60
|
|
|
61
61
|
expect(prop.type.types).toHaveLength(1);
|
|
62
62
|
expect(prop.type.types[0].kind).toBe(ts.SyntaxKind.TypeReference);
|
|
63
|
-
expect(prop.type.types[0].typeName.escapedText).toBe('
|
|
63
|
+
expect(prop.type.types[0].typeName.escapedText).toBe('Attribute.Component');
|
|
64
64
|
expect(prop.type.types[0].typeArguments).toHaveLength(1);
|
|
65
65
|
expect(prop.type.types[0].typeArguments[0].kind).toBe(ts.SyntaxKind.StringLiteral);
|
|
66
66
|
expect(prop.type.types[0].typeArguments[0].text).toBe('default.comp');
|
|
@@ -82,14 +82,14 @@ describe('Attributes', () => {
|
|
|
82
82
|
const [attributeType, requiredOptionType] = prop.type.types;
|
|
83
83
|
|
|
84
84
|
expect(attributeType.kind).toBe(ts.SyntaxKind.TypeReference);
|
|
85
|
-
expect(attributeType.typeName.escapedText).toBe('
|
|
85
|
+
expect(attributeType.typeName.escapedText).toBe('Attribute.Enumeration');
|
|
86
86
|
expect(attributeType.typeArguments).toHaveLength(1);
|
|
87
87
|
expect(attributeType.typeArguments[0].kind).toBe(ts.SyntaxKind.TupleType);
|
|
88
88
|
expect(attributeType.typeArguments[0].elements[0].text).toBe('a');
|
|
89
89
|
expect(attributeType.typeArguments[0].elements[1].text).toBe('b');
|
|
90
90
|
|
|
91
91
|
expect(requiredOptionType.kind).toBe(ts.SyntaxKind.TypeReference);
|
|
92
|
-
expect(requiredOptionType.typeName.escapedText).toBe('DefaultTo');
|
|
92
|
+
expect(requiredOptionType.typeName.escapedText).toBe('Attribute.DefaultTo');
|
|
93
93
|
expect(requiredOptionType.typeArguments).toHaveLength(1);
|
|
94
94
|
expect(requiredOptionType.typeArguments[0].kind).toBe(ts.SyntaxKind.StringLiteral);
|
|
95
95
|
expect(requiredOptionType.typeArguments[0].text).toBe('b');
|
|
@@ -108,22 +108,22 @@ describe('Attributes', () => {
|
|
|
108
108
|
});
|
|
109
109
|
|
|
110
110
|
test.each([
|
|
111
|
-
['string', '
|
|
112
|
-
['text', '
|
|
113
|
-
['richtext', '
|
|
114
|
-
['password', '
|
|
115
|
-
['email', '
|
|
116
|
-
['date', '
|
|
117
|
-
['time', '
|
|
118
|
-
['datetime', '
|
|
119
|
-
['timestamp', '
|
|
120
|
-
['integer', '
|
|
121
|
-
['biginteger', '
|
|
122
|
-
['float', '
|
|
123
|
-
['decimal', '
|
|
124
|
-
['boolean', '
|
|
125
|
-
['json', '
|
|
126
|
-
['media', '
|
|
111
|
+
['string', 'Attribute.String'],
|
|
112
|
+
['text', 'Attribute.Text'],
|
|
113
|
+
['richtext', 'Attribute.RichText'],
|
|
114
|
+
['password', 'Attribute.Password'],
|
|
115
|
+
['email', 'Attribute.Email'],
|
|
116
|
+
['date', 'Attribute.Date'],
|
|
117
|
+
['time', 'Attribute.Time'],
|
|
118
|
+
['datetime', 'Attribute.DateTime'],
|
|
119
|
+
['timestamp', 'Attribute.Timestamp'],
|
|
120
|
+
['integer', 'Attribute.Integer'],
|
|
121
|
+
['biginteger', 'Attribute.BigInteger'],
|
|
122
|
+
['float', 'Attribute.Float'],
|
|
123
|
+
['decimal', 'Attribute.Decimal'],
|
|
124
|
+
['boolean', 'Attribute.Boolean'],
|
|
125
|
+
['json', 'Attribute.JSON'],
|
|
126
|
+
['media', 'Attribute.Media'],
|
|
127
127
|
])('Basic %p attribute should map to a %p type', (type, expectedType) => {
|
|
128
128
|
const typeNode = getAttributeType('foo', { type });
|
|
129
129
|
|
|
@@ -134,7 +134,7 @@ describe('Attributes', () => {
|
|
|
134
134
|
expect(typeNode.typeArguments).toBeUndefined();
|
|
135
135
|
|
|
136
136
|
expect(consoleWarnMock).not.toHaveBeenCalled();
|
|
137
|
-
expect(addImport).toHaveBeenCalledWith(
|
|
137
|
+
expect(addImport).toHaveBeenCalledWith('Attribute');
|
|
138
138
|
});
|
|
139
139
|
|
|
140
140
|
describe('Complex types (with generic type parameters)', () => {
|
|
@@ -145,7 +145,7 @@ describe('Attributes', () => {
|
|
|
145
145
|
expect(typeNode.typeName.escapedText).toBe(typeName);
|
|
146
146
|
|
|
147
147
|
expect(consoleWarnMock).not.toHaveBeenCalled();
|
|
148
|
-
expect(addImport).toHaveBeenCalledWith(
|
|
148
|
+
expect(addImport).toHaveBeenCalledWith('Attribute');
|
|
149
149
|
};
|
|
150
150
|
|
|
151
151
|
describe('Enumeration', () => {
|
|
@@ -153,7 +153,7 @@ describe('Attributes', () => {
|
|
|
153
153
|
const attribute = { type: 'enumeration', enum: ['a', 'b', 'c'] };
|
|
154
154
|
const typeNode = getAttributeType('foo', attribute);
|
|
155
155
|
|
|
156
|
-
defaultAssertions(typeNode, '
|
|
156
|
+
defaultAssertions(typeNode, 'Attribute.Enumeration');
|
|
157
157
|
|
|
158
158
|
expect(typeNode.typeArguments).toHaveLength(1);
|
|
159
159
|
expect(typeNode.typeArguments[0].kind).toBe(ts.SyntaxKind.TupleType);
|
|
@@ -174,7 +174,7 @@ describe('Attributes', () => {
|
|
|
174
174
|
const attribute = { type: 'uid' };
|
|
175
175
|
const typeNode = getAttributeType('foo', attribute);
|
|
176
176
|
|
|
177
|
-
defaultAssertions(typeNode, '
|
|
177
|
+
defaultAssertions(typeNode, 'Attribute.UID');
|
|
178
178
|
|
|
179
179
|
expect(typeNode.typeArguments).toBeUndefined();
|
|
180
180
|
});
|
|
@@ -183,7 +183,7 @@ describe('Attributes', () => {
|
|
|
183
183
|
const attribute = { type: 'uid', targetField: 'bar' };
|
|
184
184
|
const typeNode = getAttributeType('foo', attribute, 'api::bar.bar');
|
|
185
185
|
|
|
186
|
-
defaultAssertions(typeNode, '
|
|
186
|
+
defaultAssertions(typeNode, 'Attribute.UID');
|
|
187
187
|
|
|
188
188
|
expect(typeNode.typeArguments).not.toBeUndefined();
|
|
189
189
|
expect(typeNode.typeArguments).toHaveLength(2);
|
|
@@ -199,7 +199,7 @@ describe('Attributes', () => {
|
|
|
199
199
|
const attribute = { type: 'uid', options: { separator: '_' } };
|
|
200
200
|
const typeNode = getAttributeType('foo', attribute);
|
|
201
201
|
|
|
202
|
-
defaultAssertions(typeNode, '
|
|
202
|
+
defaultAssertions(typeNode, 'Attribute.UID');
|
|
203
203
|
|
|
204
204
|
expect(typeNode.typeArguments).toHaveLength(3);
|
|
205
205
|
|
|
@@ -224,7 +224,7 @@ describe('Attributes', () => {
|
|
|
224
224
|
const attribute = { type: 'uid', options: { separator: '_' }, targetField: 'bar' };
|
|
225
225
|
const typeNode = getAttributeType('foo', attribute, 'api::bar.bar');
|
|
226
226
|
|
|
227
|
-
defaultAssertions(typeNode, '
|
|
227
|
+
defaultAssertions(typeNode, 'Attribute.UID');
|
|
228
228
|
|
|
229
229
|
expect(typeNode.typeArguments).toHaveLength(3);
|
|
230
230
|
|
|
@@ -254,7 +254,7 @@ describe('Attributes', () => {
|
|
|
254
254
|
const attribute = { type: 'relation', relation: 'oneToOne', target: 'api::bar.bar' };
|
|
255
255
|
const typeNode = getAttributeType('foo', attribute, 'api::foo.foo');
|
|
256
256
|
|
|
257
|
-
defaultAssertions(typeNode, '
|
|
257
|
+
defaultAssertions(typeNode, 'Attribute.Relation');
|
|
258
258
|
|
|
259
259
|
expect(typeNode.typeArguments).toHaveLength(3);
|
|
260
260
|
|
|
@@ -272,7 +272,7 @@ describe('Attributes', () => {
|
|
|
272
272
|
const attribute = { type: 'relation', relation: 'morphMany' };
|
|
273
273
|
const typeNode = getAttributeType('foo', attribute, 'api::foo.foo');
|
|
274
274
|
|
|
275
|
-
defaultAssertions(typeNode, '
|
|
275
|
+
defaultAssertions(typeNode, 'Attribute.Relation');
|
|
276
276
|
|
|
277
277
|
expect(typeNode.typeArguments).toHaveLength(2);
|
|
278
278
|
|
|
@@ -289,7 +289,7 @@ describe('Attributes', () => {
|
|
|
289
289
|
const attribute = { type: 'component', component: 'default.comp', repeatable: true };
|
|
290
290
|
const typeNode = getAttributeType('foo', attribute);
|
|
291
291
|
|
|
292
|
-
defaultAssertions(typeNode, '
|
|
292
|
+
defaultAssertions(typeNode, 'Attribute.Component');
|
|
293
293
|
|
|
294
294
|
expect(typeNode.typeArguments).toHaveLength(2);
|
|
295
295
|
|
|
@@ -303,7 +303,7 @@ describe('Attributes', () => {
|
|
|
303
303
|
const attribute = { type: 'component', component: 'default.comp' };
|
|
304
304
|
const typeNode = getAttributeType('foo', attribute);
|
|
305
305
|
|
|
306
|
-
defaultAssertions(typeNode, '
|
|
306
|
+
defaultAssertions(typeNode, 'Attribute.Component');
|
|
307
307
|
|
|
308
308
|
expect(typeNode.typeArguments).toHaveLength(1);
|
|
309
309
|
|
|
@@ -317,7 +317,7 @@ describe('Attributes', () => {
|
|
|
317
317
|
const attribute = { type: 'dynamiczone', components: ['default.comp1', 'default.comp2'] };
|
|
318
318
|
const typeNode = getAttributeType('foo', attribute);
|
|
319
319
|
|
|
320
|
-
defaultAssertions(typeNode, '
|
|
320
|
+
defaultAssertions(typeNode, 'Attribute.DynamicZone');
|
|
321
321
|
|
|
322
322
|
expect(typeNode.typeArguments).toHaveLength(1);
|
|
323
323
|
|
|
@@ -359,7 +359,7 @@ describe('Attributes', () => {
|
|
|
359
359
|
|
|
360
360
|
expect(modifiers).toHaveLength(1);
|
|
361
361
|
expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
|
|
362
|
-
expect(modifiers[0].typeName.escapedText).toBe('
|
|
362
|
+
expect(modifiers[0].typeName.escapedText).toBe('Attribute.Required');
|
|
363
363
|
});
|
|
364
364
|
});
|
|
365
365
|
|
|
@@ -384,7 +384,7 @@ describe('Attributes', () => {
|
|
|
384
384
|
|
|
385
385
|
expect(modifiers).toHaveLength(1);
|
|
386
386
|
expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
|
|
387
|
-
expect(modifiers[0].typeName.escapedText).toBe('
|
|
387
|
+
expect(modifiers[0].typeName.escapedText).toBe('Attribute.Private');
|
|
388
388
|
});
|
|
389
389
|
});
|
|
390
390
|
|
|
@@ -409,7 +409,7 @@ describe('Attributes', () => {
|
|
|
409
409
|
|
|
410
410
|
expect(modifiers).toHaveLength(1);
|
|
411
411
|
expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
|
|
412
|
-
expect(modifiers[0].typeName.escapedText).toBe('
|
|
412
|
+
expect(modifiers[0].typeName.escapedText).toBe('Attribute.Unique');
|
|
413
413
|
});
|
|
414
414
|
});
|
|
415
415
|
|
|
@@ -434,7 +434,7 @@ describe('Attributes', () => {
|
|
|
434
434
|
|
|
435
435
|
expect(modifiers).toHaveLength(1);
|
|
436
436
|
expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
|
|
437
|
-
expect(modifiers[0].typeName.escapedText).toBe('
|
|
437
|
+
expect(modifiers[0].typeName.escapedText).toBe('Attribute.Configurable');
|
|
438
438
|
});
|
|
439
439
|
});
|
|
440
440
|
|
|
@@ -455,7 +455,7 @@ describe('Attributes', () => {
|
|
|
455
455
|
|
|
456
456
|
expect(modifiers).toHaveLength(1);
|
|
457
457
|
expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
|
|
458
|
-
expect(modifiers[0].typeName.escapedText).toBe('CustomField');
|
|
458
|
+
expect(modifiers[0].typeName.escapedText).toBe('Attribute.CustomField');
|
|
459
459
|
expect(modifiers[0].typeArguments).toHaveLength(1);
|
|
460
460
|
expect(modifiers[0].typeArguments[0].kind).toBe(ts.SyntaxKind.StringLiteral);
|
|
461
461
|
expect(modifiers[0].typeArguments[0].text).toBe('plugin::color-picker.color');
|
|
@@ -473,7 +473,7 @@ describe('Attributes', () => {
|
|
|
473
473
|
|
|
474
474
|
expect(modifiers).toHaveLength(1);
|
|
475
475
|
expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
|
|
476
|
-
expect(modifiers[0].typeName.escapedText).toBe('CustomField');
|
|
476
|
+
expect(modifiers[0].typeName.escapedText).toBe('Attribute.CustomField');
|
|
477
477
|
expect(modifiers[0].typeArguments).toHaveLength(2);
|
|
478
478
|
expect(modifiers[0].typeArguments[0].kind).toBe(ts.SyntaxKind.StringLiteral);
|
|
479
479
|
expect(modifiers[0].typeArguments[0].text).toBe('plugin::color-picker.color');
|
|
@@ -507,7 +507,7 @@ describe('Attributes', () => {
|
|
|
507
507
|
|
|
508
508
|
expect(modifiers).toHaveLength(1);
|
|
509
509
|
expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
|
|
510
|
-
expect(modifiers[0].typeName.escapedText).toBe('SetPluginOptions');
|
|
510
|
+
expect(modifiers[0].typeName.escapedText).toBe('Attribute.SetPluginOptions');
|
|
511
511
|
expect(modifiers[0].typeArguments).toHaveLength(1);
|
|
512
512
|
expect(modifiers[0].typeArguments[0].kind).toBe(ts.SyntaxKind.TypeLiteral);
|
|
513
513
|
expect(modifiers[0].typeArguments[0].members).toHaveLength(1);
|
|
@@ -546,21 +546,29 @@ describe('Attributes', () => {
|
|
|
546
546
|
expect(modifiers).toHaveLength(1);
|
|
547
547
|
|
|
548
548
|
expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
|
|
549
|
-
expect(modifiers[0].typeName.escapedText).toBe('SetMinMax');
|
|
549
|
+
expect(modifiers[0].typeName.escapedText).toBe('Attribute.SetMinMax');
|
|
550
550
|
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
551
|
+
const [setMinMax] = modifiers;
|
|
552
|
+
const { typeArguments } = setMinMax;
|
|
553
|
+
|
|
554
|
+
expect(typeArguments).toBeDefined();
|
|
555
|
+
expect(typeArguments).toHaveLength(2);
|
|
556
|
+
|
|
557
|
+
const [definition, typeofMinMax] = typeArguments;
|
|
554
558
|
|
|
555
559
|
// Min
|
|
556
|
-
expect(
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
);
|
|
563
|
-
expect(
|
|
560
|
+
expect(definition.kind).toBe(ts.SyntaxKind.TypeLiteral);
|
|
561
|
+
expect(definition.members).toHaveLength(1);
|
|
562
|
+
|
|
563
|
+
const [min] = definition.members;
|
|
564
|
+
|
|
565
|
+
expect(min.kind).toBe(ts.SyntaxKind.PropertyDeclaration);
|
|
566
|
+
expect(min.name.escapedText).toBe('min');
|
|
567
|
+
expect(min.type.kind).toBe(ts.SyntaxKind.NumericLiteral);
|
|
568
|
+
expect(min.type.text).toBe('2');
|
|
569
|
+
|
|
570
|
+
// Check for number keyword on the second typeArgument
|
|
571
|
+
expect(typeofMinMax.kind).toBe(ts.SyntaxKind.NumberKeyword);
|
|
564
572
|
});
|
|
565
573
|
|
|
566
574
|
test('No Min, Max: 3', () => {
|
|
@@ -570,21 +578,29 @@ describe('Attributes', () => {
|
|
|
570
578
|
expect(modifiers).toHaveLength(1);
|
|
571
579
|
|
|
572
580
|
expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
|
|
573
|
-
expect(modifiers[0].typeName.escapedText).toBe('SetMinMax');
|
|
581
|
+
expect(modifiers[0].typeName.escapedText).toBe('Attribute.SetMinMax');
|
|
574
582
|
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
expect(modifiers[0].typeArguments[0].members).toHaveLength(1);
|
|
583
|
+
const [setMinMax] = modifiers;
|
|
584
|
+
const { typeArguments } = setMinMax;
|
|
578
585
|
|
|
579
|
-
|
|
580
|
-
expect(
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
);
|
|
587
|
-
|
|
586
|
+
expect(typeArguments).toBeDefined();
|
|
587
|
+
expect(typeArguments).toHaveLength(2);
|
|
588
|
+
|
|
589
|
+
const [definition, typeofMinMax] = typeArguments;
|
|
590
|
+
|
|
591
|
+
// Max
|
|
592
|
+
expect(definition.kind).toBe(ts.SyntaxKind.TypeLiteral);
|
|
593
|
+
expect(definition.members).toHaveLength(1);
|
|
594
|
+
|
|
595
|
+
const [max] = definition.members;
|
|
596
|
+
|
|
597
|
+
expect(max.kind).toBe(ts.SyntaxKind.PropertyDeclaration);
|
|
598
|
+
expect(max.name.escapedText).toBe('max');
|
|
599
|
+
expect(max.type.kind).toBe(ts.SyntaxKind.NumericLiteral);
|
|
600
|
+
expect(max.type.text).toBe('3');
|
|
601
|
+
|
|
602
|
+
// Check for number keyword on the second typeArgument
|
|
603
|
+
expect(typeofMinMax.kind).toBe(ts.SyntaxKind.NumberKeyword);
|
|
588
604
|
});
|
|
589
605
|
|
|
590
606
|
test('Min: 4, Max: 12', () => {
|
|
@@ -594,30 +610,66 @@ describe('Attributes', () => {
|
|
|
594
610
|
expect(modifiers).toHaveLength(1);
|
|
595
611
|
|
|
596
612
|
expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
|
|
597
|
-
expect(modifiers[0].typeName.escapedText).toBe('SetMinMax');
|
|
613
|
+
expect(modifiers[0].typeName.escapedText).toBe('Attribute.SetMinMax');
|
|
598
614
|
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
expect(modifiers[0].typeArguments[0].members).toHaveLength(2);
|
|
615
|
+
const [setMinMax] = modifiers;
|
|
616
|
+
const { typeArguments } = setMinMax;
|
|
602
617
|
|
|
603
|
-
|
|
604
|
-
expect(
|
|
605
|
-
ts.SyntaxKind.PropertyDeclaration
|
|
606
|
-
);
|
|
607
|
-
expect(modifiers[0].typeArguments[0].members[0].name.escapedText).toBe('min');
|
|
608
|
-
expect(modifiers[0].typeArguments[0].members[0].type.kind).toBe(
|
|
609
|
-
ts.SyntaxKind.NumericLiteral
|
|
610
|
-
);
|
|
611
|
-
expect(modifiers[0].typeArguments[0].members[0].type.text).toBe('4');
|
|
618
|
+
expect(typeArguments).toBeDefined();
|
|
619
|
+
expect(typeArguments).toHaveLength(2);
|
|
612
620
|
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
expect(
|
|
617
|
-
expect(
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
+
const [definition, typeofMinMax] = typeArguments;
|
|
622
|
+
|
|
623
|
+
// Min/Max
|
|
624
|
+
expect(definition.kind).toBe(ts.SyntaxKind.TypeLiteral);
|
|
625
|
+
expect(definition.members).toHaveLength(2);
|
|
626
|
+
|
|
627
|
+
const [min, max] = definition.members;
|
|
628
|
+
|
|
629
|
+
expect(min.kind).toBe(ts.SyntaxKind.PropertyDeclaration);
|
|
630
|
+
expect(min.name.escapedText).toBe('min');
|
|
631
|
+
expect(min.type.kind).toBe(ts.SyntaxKind.NumericLiteral);
|
|
632
|
+
expect(min.type.text).toBe('4');
|
|
633
|
+
|
|
634
|
+
expect(max.kind).toBe(ts.SyntaxKind.PropertyDeclaration);
|
|
635
|
+
expect(max.name.escapedText).toBe('max');
|
|
636
|
+
expect(max.type.kind).toBe(ts.SyntaxKind.NumericLiteral);
|
|
637
|
+
expect(max.type.text).toBe('12');
|
|
638
|
+
|
|
639
|
+
// Check for number keyword on the second typeArgument
|
|
640
|
+
expect(typeofMinMax.kind).toBe(ts.SyntaxKind.NumberKeyword);
|
|
641
|
+
});
|
|
642
|
+
|
|
643
|
+
test('Min: "1"', () => {
|
|
644
|
+
const attribute = { min: '1' };
|
|
645
|
+
const modifiers = getAttributeModifiers(attribute);
|
|
646
|
+
|
|
647
|
+
expect(modifiers).toHaveLength(1);
|
|
648
|
+
|
|
649
|
+
expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
|
|
650
|
+
expect(modifiers[0].typeName.escapedText).toBe('Attribute.SetMinMax');
|
|
651
|
+
|
|
652
|
+
const [setMinMax] = modifiers;
|
|
653
|
+
const { typeArguments } = setMinMax;
|
|
654
|
+
|
|
655
|
+
expect(typeArguments).toBeDefined();
|
|
656
|
+
expect(typeArguments).toHaveLength(2);
|
|
657
|
+
|
|
658
|
+
const [definition, typeofMinMax] = typeArguments;
|
|
659
|
+
|
|
660
|
+
// Min/Max
|
|
661
|
+
expect(definition.kind).toBe(ts.SyntaxKind.TypeLiteral);
|
|
662
|
+
expect(definition.members).toHaveLength(1);
|
|
663
|
+
|
|
664
|
+
const [min] = definition.members;
|
|
665
|
+
|
|
666
|
+
expect(min.kind).toBe(ts.SyntaxKind.PropertyDeclaration);
|
|
667
|
+
expect(min.name.escapedText).toBe('min');
|
|
668
|
+
expect(min.type.kind).toBe(ts.SyntaxKind.StringLiteral);
|
|
669
|
+
expect(min.type.text).toBe('1');
|
|
670
|
+
|
|
671
|
+
// Check for string keyword on the second typeArgument
|
|
672
|
+
expect(typeofMinMax.kind).toBe(ts.SyntaxKind.StringKeyword);
|
|
621
673
|
});
|
|
622
674
|
});
|
|
623
675
|
|
|
@@ -636,7 +688,7 @@ describe('Attributes', () => {
|
|
|
636
688
|
expect(modifiers).toHaveLength(1);
|
|
637
689
|
|
|
638
690
|
expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
|
|
639
|
-
expect(modifiers[0].typeName.escapedText).toBe('SetMinMaxLength');
|
|
691
|
+
expect(modifiers[0].typeName.escapedText).toBe('Attribute.SetMinMaxLength');
|
|
640
692
|
|
|
641
693
|
expect(modifiers[0].typeArguments).toHaveLength(1);
|
|
642
694
|
expect(modifiers[0].typeArguments[0].kind).toBe(ts.SyntaxKind.TypeLiteral);
|
|
@@ -660,7 +712,7 @@ describe('Attributes', () => {
|
|
|
660
712
|
expect(modifiers).toHaveLength(1);
|
|
661
713
|
|
|
662
714
|
expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
|
|
663
|
-
expect(modifiers[0].typeName.escapedText).toBe('SetMinMaxLength');
|
|
715
|
+
expect(modifiers[0].typeName.escapedText).toBe('Attribute.SetMinMaxLength');
|
|
664
716
|
|
|
665
717
|
expect(modifiers[0].typeArguments).toHaveLength(1);
|
|
666
718
|
expect(modifiers[0].typeArguments[0].kind).toBe(ts.SyntaxKind.TypeLiteral);
|
|
@@ -684,7 +736,7 @@ describe('Attributes', () => {
|
|
|
684
736
|
expect(modifiers).toHaveLength(1);
|
|
685
737
|
|
|
686
738
|
expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
|
|
687
|
-
expect(modifiers[0].typeName.escapedText).toBe('SetMinMaxLength');
|
|
739
|
+
expect(modifiers[0].typeName.escapedText).toBe('Attribute.SetMinMaxLength');
|
|
688
740
|
|
|
689
741
|
expect(modifiers[0].typeArguments).toHaveLength(1);
|
|
690
742
|
expect(modifiers[0].typeArguments[0].kind).toBe(ts.SyntaxKind.TypeLiteral);
|
|
@@ -726,7 +778,7 @@ describe('Attributes', () => {
|
|
|
726
778
|
expect(modifiers).toHaveLength(1);
|
|
727
779
|
|
|
728
780
|
expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
|
|
729
|
-
expect(modifiers[0].typeName.escapedText).toBe('DefaultTo');
|
|
781
|
+
expect(modifiers[0].typeName.escapedText).toBe('Attribute.DefaultTo');
|
|
730
782
|
|
|
731
783
|
expect(modifiers[0].typeArguments).toHaveLength(1);
|
|
732
784
|
expect(modifiers[0].typeArguments[0].kind).toBe(ts.SyntaxKind.TrueKeyword);
|
|
@@ -739,7 +791,7 @@ describe('Attributes', () => {
|
|
|
739
791
|
expect(modifiers).toHaveLength(1);
|
|
740
792
|
|
|
741
793
|
expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
|
|
742
|
-
expect(modifiers[0].typeName.escapedText).toBe('DefaultTo');
|
|
794
|
+
expect(modifiers[0].typeName.escapedText).toBe('Attribute.DefaultTo');
|
|
743
795
|
|
|
744
796
|
expect(modifiers[0].typeArguments).toHaveLength(1);
|
|
745
797
|
expect(modifiers[0].typeArguments[0].kind).toBe(ts.SyntaxKind.TypeLiteral);
|
|
@@ -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,71 +4,18 @@ 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(
|
|
71
|
-
undefined,
|
|
72
19
|
undefined,
|
|
73
20
|
factory.createIdentifier('Foo'),
|
|
74
21
|
undefined,
|
|
@@ -79,7 +26,6 @@ describe('Utils', () => {
|
|
|
79
26
|
|
|
80
27
|
const createPropertyDeclaration = (name, type) => {
|
|
81
28
|
return factory.createPropertyDeclaration(
|
|
82
|
-
undefined,
|
|
83
29
|
undefined,
|
|
84
30
|
factory.createIdentifier(name),
|
|
85
31
|
undefined,
|
|
@@ -174,10 +120,10 @@ describe('Utils', () => {
|
|
|
174
120
|
|
|
175
121
|
describe('Get Schema Extends Type Name', () => {
|
|
176
122
|
test.each([
|
|
177
|
-
[{ modelType: 'component', kind: null }, '
|
|
178
|
-
[{ modelType: 'contentType', kind: 'singleType' }, '
|
|
179
|
-
[{ modelType: 'contentType', kind: 'collectionType' }, '
|
|
180
|
-
[{ modelType: 'invalidType', kind: 'foo' },
|
|
123
|
+
[{ modelType: 'component', kind: null }, 'Schema.Component'],
|
|
124
|
+
[{ modelType: 'contentType', kind: 'singleType' }, 'Schema.SingleType'],
|
|
125
|
+
[{ modelType: 'contentType', kind: 'collectionType' }, 'Schema.CollectionType'],
|
|
126
|
+
[{ modelType: 'invalidType', kind: 'foo' }, null],
|
|
181
127
|
])("Expect %p to generate %p as the base type for a schema's interface", (schema, expected) => {
|
|
182
128
|
expect(getSchemaExtendsTypeName(schema)).toBe(expected);
|
|
183
129
|
});
|