@strapi/typescript-utils 5.0.0-beta.0 → 5.0.0-beta.10
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/__tests__/generators/schemas/attributes.test.js +115 -49
- package/lib/__tests__/generators/schemas/utils.test.js +3 -3
- package/lib/generators/common/models/attributes.js +3 -5
- package/lib/generators/common/models/mappers.js +20 -2
- package/lib/generators/common/models/schema.js +2 -2
- package/lib/generators/common/models/utils.js +6 -6
- package/lib/generators/components/index.js +1 -1
- package/lib/generators/content-types/index.js +1 -1
- package/lib/generators/index.js +1 -1
- package/lib/generators/utils.js +8 -3
- package/lib/index.js +0 -3
- package/package.json +7 -4
- package/tsconfigs/server.json +1 -0
- package/lib/admin/create-tsconfig-file.js +0 -23
- package/lib/admin/index.js +0 -5
- package/tsconfigs/admin.json +0 -19
|
@@ -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('Attribute.String');
|
|
51
|
+
expect(prop.type.types[0].typeName.escapedText).toBe('Schema.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('Attribute.Component');
|
|
63
|
+
expect(prop.type.types[0].typeName.escapedText).toBe('Schema.Attribute.Component');
|
|
64
64
|
expect(prop.type.types[0].typeArguments).toHaveLength(2);
|
|
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');
|
|
@@ -83,14 +83,14 @@ describe('Attributes', () => {
|
|
|
83
83
|
const [attributeType, requiredOptionType] = prop.type.types;
|
|
84
84
|
|
|
85
85
|
expect(attributeType.kind).toBe(ts.SyntaxKind.TypeReference);
|
|
86
|
-
expect(attributeType.typeName.escapedText).toBe('Attribute.Enumeration');
|
|
86
|
+
expect(attributeType.typeName.escapedText).toBe('Schema.Attribute.Enumeration');
|
|
87
87
|
expect(attributeType.typeArguments).toHaveLength(1);
|
|
88
88
|
expect(attributeType.typeArguments[0].kind).toBe(ts.SyntaxKind.TupleType);
|
|
89
89
|
expect(attributeType.typeArguments[0].elements[0].text).toBe('a');
|
|
90
90
|
expect(attributeType.typeArguments[0].elements[1].text).toBe('b');
|
|
91
91
|
|
|
92
92
|
expect(requiredOptionType.kind).toBe(ts.SyntaxKind.TypeReference);
|
|
93
|
-
expect(requiredOptionType.typeName.escapedText).toBe('Attribute.DefaultTo');
|
|
93
|
+
expect(requiredOptionType.typeName.escapedText).toBe('Schema.Attribute.DefaultTo');
|
|
94
94
|
expect(requiredOptionType.typeArguments).toHaveLength(1);
|
|
95
95
|
expect(requiredOptionType.typeArguments[0].kind).toBe(ts.SyntaxKind.StringLiteral);
|
|
96
96
|
expect(requiredOptionType.typeArguments[0].text).toBe('b');
|
|
@@ -109,22 +109,22 @@ describe('Attributes', () => {
|
|
|
109
109
|
});
|
|
110
110
|
|
|
111
111
|
test.each([
|
|
112
|
-
['string', 'Attribute.String'],
|
|
113
|
-
['text', 'Attribute.Text'],
|
|
114
|
-
['richtext', 'Attribute.RichText'],
|
|
115
|
-
['password', 'Attribute.Password'],
|
|
116
|
-
['email', 'Attribute.Email'],
|
|
117
|
-
['date', 'Attribute.Date'],
|
|
118
|
-
['time', 'Attribute.Time'],
|
|
119
|
-
['datetime', 'Attribute.DateTime'],
|
|
120
|
-
['timestamp', 'Attribute.Timestamp'],
|
|
121
|
-
['integer', 'Attribute.Integer'],
|
|
122
|
-
['biginteger', 'Attribute.BigInteger'],
|
|
123
|
-
['float', 'Attribute.Float'],
|
|
124
|
-
['decimal', 'Attribute.Decimal'],
|
|
125
|
-
['boolean', 'Attribute.Boolean'],
|
|
126
|
-
['json', 'Attribute.JSON'],
|
|
127
|
-
['media', 'Attribute.Media'],
|
|
112
|
+
['string', 'Schema.Attribute.String'],
|
|
113
|
+
['text', 'Schema.Attribute.Text'],
|
|
114
|
+
['richtext', 'Schema.Attribute.RichText'],
|
|
115
|
+
['password', 'Schema.Attribute.Password'],
|
|
116
|
+
['email', 'Schema.Attribute.Email'],
|
|
117
|
+
['date', 'Schema.Attribute.Date'],
|
|
118
|
+
['time', 'Schema.Attribute.Time'],
|
|
119
|
+
['datetime', 'Schema.Attribute.DateTime'],
|
|
120
|
+
['timestamp', 'Schema.Attribute.Timestamp'],
|
|
121
|
+
['integer', 'Schema.Attribute.Integer'],
|
|
122
|
+
['biginteger', 'Schema.Attribute.BigInteger'],
|
|
123
|
+
['float', 'Schema.Attribute.Float'],
|
|
124
|
+
['decimal', 'Schema.Attribute.Decimal'],
|
|
125
|
+
['boolean', 'Schema.Attribute.Boolean'],
|
|
126
|
+
['json', 'Schema.Attribute.JSON'],
|
|
127
|
+
['media', 'Schema.Attribute.Media'],
|
|
128
128
|
])('Basic %p attribute should map to a %p type', (type, expectedType) => {
|
|
129
129
|
const typeNode = getAttributeType('foo', { type });
|
|
130
130
|
|
|
@@ -135,7 +135,7 @@ describe('Attributes', () => {
|
|
|
135
135
|
expect(typeNode.typeArguments).toBeUndefined();
|
|
136
136
|
|
|
137
137
|
expect(consoleWarnMock).not.toHaveBeenCalled();
|
|
138
|
-
expect(addImport).toHaveBeenCalledWith('
|
|
138
|
+
expect(addImport).toHaveBeenCalledWith('Schema');
|
|
139
139
|
});
|
|
140
140
|
|
|
141
141
|
describe('Complex types (with generic type parameters)', () => {
|
|
@@ -146,15 +146,81 @@ describe('Attributes', () => {
|
|
|
146
146
|
expect(typeNode.typeName.escapedText).toBe(typeName);
|
|
147
147
|
|
|
148
148
|
expect(consoleWarnMock).not.toHaveBeenCalled();
|
|
149
|
-
expect(addImport).toHaveBeenCalledWith('
|
|
149
|
+
expect(addImport).toHaveBeenCalledWith('Schema');
|
|
150
150
|
};
|
|
151
151
|
|
|
152
|
+
describe('Media', () => {
|
|
153
|
+
test('Media with multiple and with no allowedTypes', () => {
|
|
154
|
+
const attribute = { type: 'media', multiple: true };
|
|
155
|
+
const typeNode = getAttributeType('foo', attribute);
|
|
156
|
+
|
|
157
|
+
defaultAssertions(typeNode, 'Schema.Attribute.Media');
|
|
158
|
+
|
|
159
|
+
expect(typeNode.typeArguments).toHaveLength(2);
|
|
160
|
+
|
|
161
|
+
expect(typeNode.typeArguments[0].kind).toBe(ts.SyntaxKind.UndefinedKeyword);
|
|
162
|
+
|
|
163
|
+
expect(typeNode.typeArguments[1].kind).toBe(ts.SyntaxKind.TrueKeyword);
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
test('Media without multiple with allowedTypes', () => {
|
|
167
|
+
const attribute = { type: 'media', allowedTypes: ['images', 'videos'] };
|
|
168
|
+
const typeNode = getAttributeType('foo', attribute);
|
|
169
|
+
|
|
170
|
+
defaultAssertions(typeNode, 'Schema.Attribute.Media');
|
|
171
|
+
|
|
172
|
+
expect(typeNode.typeArguments).toHaveLength(1);
|
|
173
|
+
|
|
174
|
+
expect(typeNode.typeArguments[0].kind).toBe(ts.SyntaxKind.UnionType);
|
|
175
|
+
|
|
176
|
+
const unionTypes = typeNode.typeArguments[0].types;
|
|
177
|
+
|
|
178
|
+
attribute.allowedTypes.forEach((value, index) => {
|
|
179
|
+
const element = unionTypes[index];
|
|
180
|
+
|
|
181
|
+
expect(element.kind).toBe(ts.SyntaxKind.StringLiteral);
|
|
182
|
+
expect(element.text).toBe(value);
|
|
183
|
+
});
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
test('Media with multiple and with allowedTypes', () => {
|
|
187
|
+
const attribute = { type: 'media', multiple: true, allowedTypes: ['images', 'videos'] };
|
|
188
|
+
const typeNode = getAttributeType('foo', attribute);
|
|
189
|
+
|
|
190
|
+
defaultAssertions(typeNode, 'Schema.Attribute.Media');
|
|
191
|
+
|
|
192
|
+
expect(typeNode.typeArguments).toHaveLength(2);
|
|
193
|
+
|
|
194
|
+
expect(typeNode.typeArguments[0].kind).toBe(ts.SyntaxKind.UnionType);
|
|
195
|
+
|
|
196
|
+
const unionTypes = typeNode.typeArguments[0].types;
|
|
197
|
+
|
|
198
|
+
attribute.allowedTypes.forEach((value, index) => {
|
|
199
|
+
const element = unionTypes[index];
|
|
200
|
+
|
|
201
|
+
expect(element.kind).toBe(ts.SyntaxKind.StringLiteral);
|
|
202
|
+
expect(element.text).toBe(value);
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
expect(typeNode.typeArguments[1].kind).toBe(ts.SyntaxKind.TrueKeyword);
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
test('Media without multiple and with no allowedTypes', () => {
|
|
209
|
+
const attribute = { type: 'media' };
|
|
210
|
+
const typeNode = getAttributeType('foo', attribute);
|
|
211
|
+
|
|
212
|
+
defaultAssertions(typeNode, 'Schema.Attribute.Media');
|
|
213
|
+
|
|
214
|
+
expect(typeNode.typeArguments).toBeUndefined();
|
|
215
|
+
});
|
|
216
|
+
});
|
|
217
|
+
|
|
152
218
|
describe('Enumeration', () => {
|
|
153
219
|
test('Enumeration with an enum property', () => {
|
|
154
220
|
const attribute = { type: 'enumeration', enum: ['a', 'b', 'c'] };
|
|
155
221
|
const typeNode = getAttributeType('foo', attribute);
|
|
156
222
|
|
|
157
|
-
defaultAssertions(typeNode, 'Attribute.Enumeration');
|
|
223
|
+
defaultAssertions(typeNode, 'Schema.Attribute.Enumeration');
|
|
158
224
|
|
|
159
225
|
expect(typeNode.typeArguments).toHaveLength(1);
|
|
160
226
|
expect(typeNode.typeArguments[0].kind).toBe(ts.SyntaxKind.TupleType);
|
|
@@ -175,7 +241,7 @@ describe('Attributes', () => {
|
|
|
175
241
|
const attribute = { type: 'uid' };
|
|
176
242
|
const typeNode = getAttributeType('foo', attribute);
|
|
177
243
|
|
|
178
|
-
defaultAssertions(typeNode, 'Attribute.UID');
|
|
244
|
+
defaultAssertions(typeNode, 'Schema.Attribute.UID');
|
|
179
245
|
|
|
180
246
|
expect(typeNode.typeArguments).toBeUndefined();
|
|
181
247
|
});
|
|
@@ -184,7 +250,7 @@ describe('Attributes', () => {
|
|
|
184
250
|
const attribute = { type: 'uid', targetField: 'bar' };
|
|
185
251
|
const typeNode = getAttributeType('foo', attribute, 'api::bar.bar');
|
|
186
252
|
|
|
187
|
-
defaultAssertions(typeNode, 'Attribute.UID');
|
|
253
|
+
defaultAssertions(typeNode, 'Schema.Attribute.UID');
|
|
188
254
|
|
|
189
255
|
expect(typeNode.typeArguments).not.toBeUndefined();
|
|
190
256
|
expect(typeNode.typeArguments).toHaveLength(1);
|
|
@@ -197,7 +263,7 @@ describe('Attributes', () => {
|
|
|
197
263
|
const attribute = { type: 'uid', options: { separator: '_' } };
|
|
198
264
|
const typeNode = getAttributeType('foo', attribute, 'api::foo.foo');
|
|
199
265
|
|
|
200
|
-
defaultAssertions(typeNode, 'Attribute.UID');
|
|
266
|
+
defaultAssertions(typeNode, 'Schema.Attribute.UID');
|
|
201
267
|
|
|
202
268
|
expect(typeNode.typeArguments).toHaveLength(2);
|
|
203
269
|
|
|
@@ -221,7 +287,7 @@ describe('Attributes', () => {
|
|
|
221
287
|
const attribute = { type: 'uid', options: { separator: '_' }, targetField: 'bar' };
|
|
222
288
|
const typeNode = getAttributeType('foo', attribute, 'api::bar.bar');
|
|
223
289
|
|
|
224
|
-
defaultAssertions(typeNode, 'Attribute.UID');
|
|
290
|
+
defaultAssertions(typeNode, 'Schema.Attribute.UID');
|
|
225
291
|
|
|
226
292
|
expect(typeNode.typeArguments).toHaveLength(2);
|
|
227
293
|
|
|
@@ -248,7 +314,7 @@ describe('Attributes', () => {
|
|
|
248
314
|
const attribute = { type: 'relation', relation: 'oneToOne', target: 'api::bar.bar' };
|
|
249
315
|
const typeNode = getAttributeType('foo', attribute, 'api::foo.foo');
|
|
250
316
|
|
|
251
|
-
defaultAssertions(typeNode, 'Attribute.Relation');
|
|
317
|
+
defaultAssertions(typeNode, 'Schema.Attribute.Relation');
|
|
252
318
|
|
|
253
319
|
expect(typeNode.typeArguments).toHaveLength(2);
|
|
254
320
|
|
|
@@ -263,7 +329,7 @@ describe('Attributes', () => {
|
|
|
263
329
|
const attribute = { type: 'relation', relation: 'morphMany' };
|
|
264
330
|
const typeNode = getAttributeType('foo', attribute, 'api::foo.foo');
|
|
265
331
|
|
|
266
|
-
defaultAssertions(typeNode, 'Attribute.Relation');
|
|
332
|
+
defaultAssertions(typeNode, 'Schema.Attribute.Relation');
|
|
267
333
|
|
|
268
334
|
expect(typeNode.typeArguments).toHaveLength(1);
|
|
269
335
|
|
|
@@ -277,7 +343,7 @@ describe('Attributes', () => {
|
|
|
277
343
|
const attribute = { type: 'component', component: 'default.comp', repeatable: true };
|
|
278
344
|
const typeNode = getAttributeType('foo', attribute);
|
|
279
345
|
|
|
280
|
-
defaultAssertions(typeNode, 'Attribute.Component');
|
|
346
|
+
defaultAssertions(typeNode, 'Schema.Attribute.Component');
|
|
281
347
|
|
|
282
348
|
expect(typeNode.typeArguments).toHaveLength(2);
|
|
283
349
|
|
|
@@ -291,7 +357,7 @@ describe('Attributes', () => {
|
|
|
291
357
|
const attribute = { type: 'component', component: 'default.comp' };
|
|
292
358
|
const typeNode = getAttributeType('foo', attribute);
|
|
293
359
|
|
|
294
|
-
defaultAssertions(typeNode, 'Attribute.Component');
|
|
360
|
+
defaultAssertions(typeNode, 'Schema.Attribute.Component');
|
|
295
361
|
|
|
296
362
|
expect(typeNode.typeArguments).toHaveLength(2);
|
|
297
363
|
|
|
@@ -307,7 +373,7 @@ describe('Attributes', () => {
|
|
|
307
373
|
const attribute = { type: 'dynamiczone', components: ['default.comp1', 'default.comp2'] };
|
|
308
374
|
const typeNode = getAttributeType('foo', attribute);
|
|
309
375
|
|
|
310
|
-
defaultAssertions(typeNode, 'Attribute.DynamicZone');
|
|
376
|
+
defaultAssertions(typeNode, 'Schema.Attribute.DynamicZone');
|
|
311
377
|
|
|
312
378
|
expect(typeNode.typeArguments).toHaveLength(1);
|
|
313
379
|
|
|
@@ -349,7 +415,7 @@ describe('Attributes', () => {
|
|
|
349
415
|
|
|
350
416
|
expect(modifiers).toHaveLength(1);
|
|
351
417
|
expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
|
|
352
|
-
expect(modifiers[0].typeName.escapedText).toBe('Attribute.Required');
|
|
418
|
+
expect(modifiers[0].typeName.escapedText).toBe('Schema.Attribute.Required');
|
|
353
419
|
});
|
|
354
420
|
});
|
|
355
421
|
|
|
@@ -374,7 +440,7 @@ describe('Attributes', () => {
|
|
|
374
440
|
|
|
375
441
|
expect(modifiers).toHaveLength(1);
|
|
376
442
|
expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
|
|
377
|
-
expect(modifiers[0].typeName.escapedText).toBe('Attribute.Private');
|
|
443
|
+
expect(modifiers[0].typeName.escapedText).toBe('Schema.Attribute.Private');
|
|
378
444
|
});
|
|
379
445
|
});
|
|
380
446
|
|
|
@@ -399,7 +465,7 @@ describe('Attributes', () => {
|
|
|
399
465
|
|
|
400
466
|
expect(modifiers).toHaveLength(1);
|
|
401
467
|
expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
|
|
402
|
-
expect(modifiers[0].typeName.escapedText).toBe('Attribute.Unique');
|
|
468
|
+
expect(modifiers[0].typeName.escapedText).toBe('Schema.Attribute.Unique');
|
|
403
469
|
});
|
|
404
470
|
});
|
|
405
471
|
|
|
@@ -424,7 +490,7 @@ describe('Attributes', () => {
|
|
|
424
490
|
|
|
425
491
|
expect(modifiers).toHaveLength(1);
|
|
426
492
|
expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
|
|
427
|
-
expect(modifiers[0].typeName.escapedText).toBe('Attribute.Configurable');
|
|
493
|
+
expect(modifiers[0].typeName.escapedText).toBe('Schema.Attribute.Configurable');
|
|
428
494
|
});
|
|
429
495
|
});
|
|
430
496
|
|
|
@@ -445,7 +511,7 @@ describe('Attributes', () => {
|
|
|
445
511
|
|
|
446
512
|
expect(modifiers).toHaveLength(1);
|
|
447
513
|
expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
|
|
448
|
-
expect(modifiers[0].typeName.escapedText).toBe('Attribute.CustomField');
|
|
514
|
+
expect(modifiers[0].typeName.escapedText).toBe('Schema.Attribute.CustomField');
|
|
449
515
|
expect(modifiers[0].typeArguments).toHaveLength(1);
|
|
450
516
|
expect(modifiers[0].typeArguments[0].kind).toBe(ts.SyntaxKind.StringLiteral);
|
|
451
517
|
expect(modifiers[0].typeArguments[0].text).toBe('plugin::color-picker.color');
|
|
@@ -463,7 +529,7 @@ describe('Attributes', () => {
|
|
|
463
529
|
|
|
464
530
|
expect(modifiers).toHaveLength(1);
|
|
465
531
|
expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
|
|
466
|
-
expect(modifiers[0].typeName.escapedText).toBe('Attribute.CustomField');
|
|
532
|
+
expect(modifiers[0].typeName.escapedText).toBe('Schema.Attribute.CustomField');
|
|
467
533
|
expect(modifiers[0].typeArguments).toHaveLength(2);
|
|
468
534
|
expect(modifiers[0].typeArguments[0].kind).toBe(ts.SyntaxKind.StringLiteral);
|
|
469
535
|
expect(modifiers[0].typeArguments[0].text).toBe('plugin::color-picker.color');
|
|
@@ -497,7 +563,7 @@ describe('Attributes', () => {
|
|
|
497
563
|
|
|
498
564
|
expect(modifiers).toHaveLength(1);
|
|
499
565
|
expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
|
|
500
|
-
expect(modifiers[0].typeName.escapedText).toBe('Attribute.SetPluginOptions');
|
|
566
|
+
expect(modifiers[0].typeName.escapedText).toBe('Schema.Attribute.SetPluginOptions');
|
|
501
567
|
expect(modifiers[0].typeArguments).toHaveLength(1);
|
|
502
568
|
expect(modifiers[0].typeArguments[0].kind).toBe(ts.SyntaxKind.TypeLiteral);
|
|
503
569
|
expect(modifiers[0].typeArguments[0].members).toHaveLength(1);
|
|
@@ -536,7 +602,7 @@ describe('Attributes', () => {
|
|
|
536
602
|
expect(modifiers).toHaveLength(1);
|
|
537
603
|
|
|
538
604
|
expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
|
|
539
|
-
expect(modifiers[0].typeName.escapedText).toBe('Attribute.SetMinMax');
|
|
605
|
+
expect(modifiers[0].typeName.escapedText).toBe('Schema.Attribute.SetMinMax');
|
|
540
606
|
|
|
541
607
|
const [setMinMax] = modifiers;
|
|
542
608
|
const { typeArguments } = setMinMax;
|
|
@@ -568,7 +634,7 @@ describe('Attributes', () => {
|
|
|
568
634
|
expect(modifiers).toHaveLength(1);
|
|
569
635
|
|
|
570
636
|
expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
|
|
571
|
-
expect(modifiers[0].typeName.escapedText).toBe('Attribute.SetMinMax');
|
|
637
|
+
expect(modifiers[0].typeName.escapedText).toBe('Schema.Attribute.SetMinMax');
|
|
572
638
|
|
|
573
639
|
const [setMinMax] = modifiers;
|
|
574
640
|
const { typeArguments } = setMinMax;
|
|
@@ -600,7 +666,7 @@ describe('Attributes', () => {
|
|
|
600
666
|
expect(modifiers).toHaveLength(1);
|
|
601
667
|
|
|
602
668
|
expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
|
|
603
|
-
expect(modifiers[0].typeName.escapedText).toBe('Attribute.SetMinMax');
|
|
669
|
+
expect(modifiers[0].typeName.escapedText).toBe('Schema.Attribute.SetMinMax');
|
|
604
670
|
|
|
605
671
|
const [setMinMax] = modifiers;
|
|
606
672
|
const { typeArguments } = setMinMax;
|
|
@@ -637,7 +703,7 @@ describe('Attributes', () => {
|
|
|
637
703
|
expect(modifiers).toHaveLength(1);
|
|
638
704
|
|
|
639
705
|
expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
|
|
640
|
-
expect(modifiers[0].typeName.escapedText).toBe('Attribute.SetMinMax');
|
|
706
|
+
expect(modifiers[0].typeName.escapedText).toBe('Schema.Attribute.SetMinMax');
|
|
641
707
|
|
|
642
708
|
const [setMinMax] = modifiers;
|
|
643
709
|
const { typeArguments } = setMinMax;
|
|
@@ -669,7 +735,7 @@ describe('Attributes', () => {
|
|
|
669
735
|
expect(modifiers).toHaveLength(1);
|
|
670
736
|
|
|
671
737
|
expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
|
|
672
|
-
expect(modifiers[0].typeName.escapedText).toBe('Attribute.SetMinMax');
|
|
738
|
+
expect(modifiers[0].typeName.escapedText).toBe('Schema.Attribute.SetMinMax');
|
|
673
739
|
|
|
674
740
|
const [setMinMax] = modifiers;
|
|
675
741
|
const { typeArguments } = setMinMax;
|
|
@@ -710,7 +776,7 @@ describe('Attributes', () => {
|
|
|
710
776
|
expect(modifiers).toHaveLength(1);
|
|
711
777
|
|
|
712
778
|
expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
|
|
713
|
-
expect(modifiers[0].typeName.escapedText).toBe('Attribute.SetMinMaxLength');
|
|
779
|
+
expect(modifiers[0].typeName.escapedText).toBe('Schema.Attribute.SetMinMaxLength');
|
|
714
780
|
|
|
715
781
|
expect(modifiers[0].typeArguments).toHaveLength(1);
|
|
716
782
|
expect(modifiers[0].typeArguments[0].kind).toBe(ts.SyntaxKind.TypeLiteral);
|
|
@@ -734,7 +800,7 @@ describe('Attributes', () => {
|
|
|
734
800
|
expect(modifiers).toHaveLength(1);
|
|
735
801
|
|
|
736
802
|
expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
|
|
737
|
-
expect(modifiers[0].typeName.escapedText).toBe('Attribute.SetMinMaxLength');
|
|
803
|
+
expect(modifiers[0].typeName.escapedText).toBe('Schema.Attribute.SetMinMaxLength');
|
|
738
804
|
|
|
739
805
|
expect(modifiers[0].typeArguments).toHaveLength(1);
|
|
740
806
|
expect(modifiers[0].typeArguments[0].kind).toBe(ts.SyntaxKind.TypeLiteral);
|
|
@@ -758,7 +824,7 @@ describe('Attributes', () => {
|
|
|
758
824
|
expect(modifiers).toHaveLength(1);
|
|
759
825
|
|
|
760
826
|
expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
|
|
761
|
-
expect(modifiers[0].typeName.escapedText).toBe('Attribute.SetMinMaxLength');
|
|
827
|
+
expect(modifiers[0].typeName.escapedText).toBe('Schema.Attribute.SetMinMaxLength');
|
|
762
828
|
|
|
763
829
|
expect(modifiers[0].typeArguments).toHaveLength(1);
|
|
764
830
|
expect(modifiers[0].typeArguments[0].kind).toBe(ts.SyntaxKind.TypeLiteral);
|
|
@@ -800,7 +866,7 @@ describe('Attributes', () => {
|
|
|
800
866
|
expect(modifiers).toHaveLength(1);
|
|
801
867
|
|
|
802
868
|
expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
|
|
803
|
-
expect(modifiers[0].typeName.escapedText).toBe('Attribute.DefaultTo');
|
|
869
|
+
expect(modifiers[0].typeName.escapedText).toBe('Schema.Attribute.DefaultTo');
|
|
804
870
|
|
|
805
871
|
expect(modifiers[0].typeArguments).toHaveLength(1);
|
|
806
872
|
expect(modifiers[0].typeArguments[0].kind).toBe(ts.SyntaxKind.TrueKeyword);
|
|
@@ -813,7 +879,7 @@ describe('Attributes', () => {
|
|
|
813
879
|
expect(modifiers).toHaveLength(1);
|
|
814
880
|
|
|
815
881
|
expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
|
|
816
|
-
expect(modifiers[0].typeName.escapedText).toBe('Attribute.DefaultTo');
|
|
882
|
+
expect(modifiers[0].typeName.escapedText).toBe('Schema.Attribute.DefaultTo');
|
|
817
883
|
|
|
818
884
|
expect(modifiers[0].typeArguments).toHaveLength(1);
|
|
819
885
|
expect(modifiers[0].typeArguments[0].kind).toBe(ts.SyntaxKind.TypeLiteral);
|
|
@@ -120,9 +120,9 @@ describe('Utils', () => {
|
|
|
120
120
|
|
|
121
121
|
describe('Get Schema Extends Type Name', () => {
|
|
122
122
|
test.each([
|
|
123
|
-
[{ modelType: 'component', kind: null }, '
|
|
124
|
-
[{ modelType: 'contentType', kind: 'singleType' }, '
|
|
125
|
-
[{ modelType: 'contentType', kind: 'collectionType' }, '
|
|
123
|
+
[{ modelType: 'component', kind: null }, 'Struct.ComponentSchema'],
|
|
124
|
+
[{ modelType: 'contentType', kind: 'singleType' }, 'Struct.SingleTypeSchema'],
|
|
125
|
+
[{ modelType: 'contentType', kind: 'collectionType' }, 'Struct.CollectionTypeSchema'],
|
|
126
126
|
[{ modelType: 'invalidType', kind: 'foo' }, null],
|
|
127
127
|
])("Expect %p to generate %p as the base type for a schema's interface", (schema, expected) => {
|
|
128
128
|
expect(getSchemaExtendsTypeName(schema)).toBe(expected);
|
|
@@ -28,8 +28,8 @@ const getAttributeType = (attributeName, attribute, uid) => {
|
|
|
28
28
|
|
|
29
29
|
const [attributeType, typeParams] = mappers[attribute.type]({ uid, attribute, attributeName });
|
|
30
30
|
|
|
31
|
-
// Make sure the
|
|
32
|
-
addImport(NAMESPACES.
|
|
31
|
+
// Make sure the schema namespace is imported
|
|
32
|
+
addImport(NAMESPACES.Schema);
|
|
33
33
|
|
|
34
34
|
return getTypeNode(attributeType, typeParams);
|
|
35
35
|
};
|
|
@@ -195,6 +195,4 @@ const attributeToPropertySignature = (schema, attributeName, attribute) => {
|
|
|
195
195
|
|
|
196
196
|
module.exports = attributeToPropertySignature;
|
|
197
197
|
|
|
198
|
-
module.exports
|
|
199
|
-
module.exports.getAttributeType = getAttributeType;
|
|
200
|
-
module.exports.getAttributeModifiers = getAttributeModifiers;
|
|
198
|
+
Object.assign(module.exports, { mappers, getAttributeModifiers, getAttributeType });
|
|
@@ -87,8 +87,26 @@ module.exports = {
|
|
|
87
87
|
blocks() {
|
|
88
88
|
return [withAttributeNamespace('Blocks')];
|
|
89
89
|
},
|
|
90
|
-
media() {
|
|
91
|
-
|
|
90
|
+
media({ attribute }) {
|
|
91
|
+
const { allowedTypes, multiple } = attribute;
|
|
92
|
+
|
|
93
|
+
const params = [];
|
|
94
|
+
|
|
95
|
+
const typesParam = allowedTypes
|
|
96
|
+
? factory.createUnionTypeNode(
|
|
97
|
+
allowedTypes.map((allowedType) => factory.createStringLiteral(allowedType))
|
|
98
|
+
)
|
|
99
|
+
: factory.createKeywordTypeNode(ts.SyntaxKind.UndefinedKeyword);
|
|
100
|
+
|
|
101
|
+
if (allowedTypes || multiple) {
|
|
102
|
+
params.push(typesParam);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (multiple) {
|
|
106
|
+
params.push(factory.createTrue());
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return [withAttributeNamespace('Media'), params];
|
|
92
110
|
},
|
|
93
111
|
relation({ attribute }) {
|
|
94
112
|
const { relation, target } = attribute;
|
|
@@ -56,8 +56,8 @@ const generateSchemaDefinition = (schema) => {
|
|
|
56
56
|
const interfaceName = getSchemaInterfaceName(uid);
|
|
57
57
|
const parentType = getSchemaExtendsTypeName(schema);
|
|
58
58
|
|
|
59
|
-
// Make sure the
|
|
60
|
-
addImport(NAMESPACES.
|
|
59
|
+
// Make sure the Struct namespace is imported
|
|
60
|
+
addImport(NAMESPACES.Struct);
|
|
61
61
|
|
|
62
62
|
// Properties whose values can be mapped to a literal type expression
|
|
63
63
|
const literalPropertiesDefinitions = ['collectionName', 'info', 'options', 'pluginOptions']
|
|
@@ -18,8 +18,8 @@ const {
|
|
|
18
18
|
} = require('lodash/fp');
|
|
19
19
|
|
|
20
20
|
const NAMESPACES = {
|
|
21
|
-
|
|
22
|
-
|
|
21
|
+
Struct: 'Struct',
|
|
22
|
+
Schema: 'Schema',
|
|
23
23
|
};
|
|
24
24
|
|
|
25
25
|
/**
|
|
@@ -50,7 +50,7 @@ const getSchemaModelType = (schema) => {
|
|
|
50
50
|
* Get the parent type name to extend based on the schema's nature
|
|
51
51
|
*
|
|
52
52
|
* @param {object} schema
|
|
53
|
-
* @returns {string}
|
|
53
|
+
* @returns {string|null}
|
|
54
54
|
*/
|
|
55
55
|
const getSchemaExtendsTypeName = (schema) => {
|
|
56
56
|
const base = getSchemaModelType(schema);
|
|
@@ -59,7 +59,7 @@ const getSchemaExtendsTypeName = (schema) => {
|
|
|
59
59
|
return null;
|
|
60
60
|
}
|
|
61
61
|
|
|
62
|
-
return `${NAMESPACES.
|
|
62
|
+
return `${NAMESPACES.Struct}.${upperFirst(base)}Schema`;
|
|
63
63
|
};
|
|
64
64
|
|
|
65
65
|
/**
|
|
@@ -145,12 +145,12 @@ const getDefinitionAttributesCount = (definition) => {
|
|
|
145
145
|
};
|
|
146
146
|
|
|
147
147
|
/**
|
|
148
|
-
* Add the
|
|
148
|
+
* Add the Schema.Attribute namespace before the typename
|
|
149
149
|
*
|
|
150
150
|
* @param {string} typeName
|
|
151
151
|
* @returns {string}
|
|
152
152
|
*/
|
|
153
|
-
const withAttributeNamespace = (typeName) => `${NAMESPACES.
|
|
153
|
+
const withAttributeNamespace = (typeName) => `${NAMESPACES.Schema}.Attribute.${typeName}`;
|
|
154
154
|
|
|
155
155
|
/**
|
|
156
156
|
* Add the schema namespace before the typename
|
|
@@ -46,7 +46,7 @@ const generateComponentsDefinitions = async (options = {}) => {
|
|
|
46
46
|
...formattedSchemasDefinitions,
|
|
47
47
|
|
|
48
48
|
// Global
|
|
49
|
-
generateSharedExtensionDefinition('
|
|
49
|
+
generateSharedExtensionDefinition('ComponentSchemas', componentsDefinitions),
|
|
50
50
|
];
|
|
51
51
|
|
|
52
52
|
const output = emitDefinitions(allDefinitions);
|
|
@@ -46,7 +46,7 @@ const generateContentTypesDefinitions = async (options = {}) => {
|
|
|
46
46
|
...formattedSchemasDefinitions,
|
|
47
47
|
|
|
48
48
|
// Global
|
|
49
|
-
generateSharedExtensionDefinition('
|
|
49
|
+
generateSharedExtensionDefinition('ContentTypeSchemas', contentTypesDefinitions),
|
|
50
50
|
];
|
|
51
51
|
|
|
52
52
|
const output = emitDefinitions(allDefinitions);
|
package/lib/generators/index.js
CHANGED
|
@@ -100,7 +100,7 @@ const generate = async (config = {}) => {
|
|
|
100
100
|
|
|
101
101
|
try {
|
|
102
102
|
const outPath = await saveDefinitionToFileSystem(registryPwd, filename, report.output);
|
|
103
|
-
const relativeOutPath = path.relative(
|
|
103
|
+
const relativeOutPath = path.relative(process.cwd(), outPath);
|
|
104
104
|
|
|
105
105
|
artifactFsTimer.end();
|
|
106
106
|
|
package/lib/generators/utils.js
CHANGED
|
@@ -3,12 +3,14 @@
|
|
|
3
3
|
const path = require('path');
|
|
4
4
|
const assert = require('assert');
|
|
5
5
|
const ts = require('typescript');
|
|
6
|
-
const prettier = require('prettier');
|
|
7
6
|
const fse = require('fs-extra');
|
|
8
7
|
const chalk = require('chalk');
|
|
9
8
|
|
|
10
9
|
const { factory } = ts;
|
|
11
10
|
|
|
11
|
+
const MODULE_DECLARATION = '@strapi/strapi';
|
|
12
|
+
const PUBLIC_NAMESPACE = 'Public';
|
|
13
|
+
|
|
12
14
|
/**
|
|
13
15
|
* Aggregate the given TypeScript nodes into a single string
|
|
14
16
|
*
|
|
@@ -58,6 +60,9 @@ const saveDefinitionToFileSystem = async (dir, file, content) => {
|
|
|
58
60
|
* @returns {Promise<string>}
|
|
59
61
|
*/
|
|
60
62
|
const format = async (content) => {
|
|
63
|
+
// eslint-disable-next-line node/no-unsupported-features/es-syntax
|
|
64
|
+
const prettier = await import('prettier'); // ESM-only
|
|
65
|
+
|
|
61
66
|
const configFile = await prettier.resolveConfigFile();
|
|
62
67
|
const config = configFile
|
|
63
68
|
? await prettier.resolveConfig(configFile)
|
|
@@ -92,11 +97,11 @@ const generateSharedExtensionDefinition = (registry, definitions) => {
|
|
|
92
97
|
|
|
93
98
|
return factory.createModuleDeclaration(
|
|
94
99
|
[factory.createModifier(ts.SyntaxKind.DeclareKeyword)],
|
|
95
|
-
factory.createStringLiteral(
|
|
100
|
+
factory.createStringLiteral(MODULE_DECLARATION, true),
|
|
96
101
|
factory.createModuleBlock([
|
|
97
102
|
factory.createModuleDeclaration(
|
|
98
103
|
[factory.createModifier(ts.SyntaxKind.ExportKeyword)],
|
|
99
|
-
factory.createIdentifier(
|
|
104
|
+
factory.createIdentifier(PUBLIC_NAMESPACE),
|
|
100
105
|
factory.createModuleBlock(
|
|
101
106
|
properties.length > 0
|
|
102
107
|
? [
|
package/lib/index.js
CHANGED
|
@@ -2,15 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
const compile = require('./compile');
|
|
4
4
|
const compilers = require('./compilers');
|
|
5
|
-
const admin = require('./admin');
|
|
6
5
|
const utils = require('./utils');
|
|
7
6
|
const generators = require('./generators');
|
|
8
7
|
|
|
9
8
|
module.exports = {
|
|
10
9
|
compile,
|
|
11
10
|
compilers,
|
|
12
|
-
admin,
|
|
13
11
|
generators,
|
|
14
|
-
|
|
15
12
|
...utils,
|
|
16
13
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@strapi/typescript-utils",
|
|
3
|
-
"version": "5.0.0-beta.
|
|
3
|
+
"version": "5.0.0-beta.10",
|
|
4
4
|
"description": "Typescript support for Strapi",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"strapi",
|
|
@@ -37,14 +37,17 @@
|
|
|
37
37
|
"dependencies": {
|
|
38
38
|
"chalk": "4.1.2",
|
|
39
39
|
"cli-table3": "0.6.2",
|
|
40
|
-
"fs-extra": "
|
|
40
|
+
"fs-extra": "11.2.0",
|
|
41
41
|
"lodash": "4.17.21",
|
|
42
|
-
"prettier": "2.
|
|
42
|
+
"prettier": "3.2.5",
|
|
43
43
|
"typescript": "5.3.2"
|
|
44
44
|
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@types/fs-extra": "11.0.4"
|
|
47
|
+
},
|
|
45
48
|
"engines": {
|
|
46
49
|
"node": ">=18.0.0 <=20.x.x",
|
|
47
50
|
"npm": ">=6.0.0"
|
|
48
51
|
},
|
|
49
|
-
"gitHead": "
|
|
52
|
+
"gitHead": "d4542f0dccfa2eecb55026dbd795042e7d0004ba"
|
|
50
53
|
}
|
package/tsconfigs/server.json
CHANGED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const path = require('path');
|
|
4
|
-
const fs = require('fs-extra');
|
|
5
|
-
const adminTsConfig = require('../../tsconfigs/admin.json');
|
|
6
|
-
|
|
7
|
-
module.exports = async (dest) => {
|
|
8
|
-
const tsConfig = {
|
|
9
|
-
...adminTsConfig,
|
|
10
|
-
include: ['../../../src/admin/*', '../../../src/**/**/admin/src/*'],
|
|
11
|
-
exclude: ['node_modules', '**/*.test.js', '*.js'],
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
const filePath = path.join(dest, 'admin', 'src', 'tsconfig.json');
|
|
15
|
-
|
|
16
|
-
try {
|
|
17
|
-
await fs.ensureFile(filePath);
|
|
18
|
-
|
|
19
|
-
await fs.writeJSON(filePath, tsConfig, { spaces: 2 });
|
|
20
|
-
} catch (err) {
|
|
21
|
-
console.log(err);
|
|
22
|
-
}
|
|
23
|
-
};
|
package/lib/admin/index.js
DELETED
package/tsconfigs/admin.json
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"$schema": "https://json.schemastore.org/tsconfig",
|
|
3
|
-
"compilerOptions": {
|
|
4
|
-
"target": "ESNext",
|
|
5
|
-
"module": "ESNext",
|
|
6
|
-
"moduleResolution": "Bundler",
|
|
7
|
-
"useDefineForClassFields": true,
|
|
8
|
-
"lib": ["DOM", "DOM.Iterable", "ESNext"],
|
|
9
|
-
"allowJs": false,
|
|
10
|
-
"skipLibCheck": true,
|
|
11
|
-
"esModuleInterop": true,
|
|
12
|
-
"allowSyntheticDefaultImports": true,
|
|
13
|
-
"strict": true,
|
|
14
|
-
"forceConsistentCasingInFileNames": true,
|
|
15
|
-
"resolveJsonModule": true,
|
|
16
|
-
"noEmit": true,
|
|
17
|
-
"jsx": "react-jsx"
|
|
18
|
-
}
|
|
19
|
-
}
|