@strapi/typescript-utils 0.0.0-next.fd9757603c653ca239c45d6e28ab536d2dae0b39 → 0.0.0-next.fdac61dd05ca665168f51f655f1d165b55ec4231
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 +99 -104
- package/lib/__tests__/generators/schemas/utils.test.js +31 -31
- package/lib/generators/common/imports.js +3 -3
- package/lib/generators/common/models/attributes.js +5 -7
- package/lib/generators/common/models/mappers.js +12 -20
- package/lib/generators/common/models/schema.js +7 -5
- package/lib/generators/common/models/utils.js +7 -7
- package/lib/generators/components/index.js +21 -5
- package/lib/generators/content-types/index.js +21 -5
- package/lib/generators/index.js +1 -1
- package/lib/generators/utils.js +8 -3
- 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 +9 -7
- package/tsconfigs/server.json +1 -0
- package/lib/admin/create-tsconfig-file.js +0 -23
- package/lib/admin/index.js +0 -5
|
@@ -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,10 +60,11 @@ 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');
|
|
64
|
-
expect(prop.type.types[0].typeArguments).toHaveLength(
|
|
63
|
+
expect(prop.type.types[0].typeName.escapedText).toBe('Schema.Attribute.Component');
|
|
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');
|
|
67
|
+
expect(prop.type.types[0].typeArguments[1].kind).toBe(ts.SyntaxKind.FalseKeyword);
|
|
67
68
|
});
|
|
68
69
|
|
|
69
70
|
test('Attribute with type argument and options', () => {
|
|
@@ -82,14 +83,14 @@ describe('Attributes', () => {
|
|
|
82
83
|
const [attributeType, requiredOptionType] = prop.type.types;
|
|
83
84
|
|
|
84
85
|
expect(attributeType.kind).toBe(ts.SyntaxKind.TypeReference);
|
|
85
|
-
expect(attributeType.typeName.escapedText).toBe('Attribute.Enumeration');
|
|
86
|
+
expect(attributeType.typeName.escapedText).toBe('Schema.Attribute.Enumeration');
|
|
86
87
|
expect(attributeType.typeArguments).toHaveLength(1);
|
|
87
88
|
expect(attributeType.typeArguments[0].kind).toBe(ts.SyntaxKind.TupleType);
|
|
88
89
|
expect(attributeType.typeArguments[0].elements[0].text).toBe('a');
|
|
89
90
|
expect(attributeType.typeArguments[0].elements[1].text).toBe('b');
|
|
90
91
|
|
|
91
92
|
expect(requiredOptionType.kind).toBe(ts.SyntaxKind.TypeReference);
|
|
92
|
-
expect(requiredOptionType.typeName.escapedText).toBe('Attribute.DefaultTo');
|
|
93
|
+
expect(requiredOptionType.typeName.escapedText).toBe('Schema.Attribute.DefaultTo');
|
|
93
94
|
expect(requiredOptionType.typeArguments).toHaveLength(1);
|
|
94
95
|
expect(requiredOptionType.typeArguments[0].kind).toBe(ts.SyntaxKind.StringLiteral);
|
|
95
96
|
expect(requiredOptionType.typeArguments[0].text).toBe('b');
|
|
@@ -108,22 +109,22 @@ describe('Attributes', () => {
|
|
|
108
109
|
});
|
|
109
110
|
|
|
110
111
|
test.each([
|
|
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'],
|
|
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'],
|
|
127
128
|
])('Basic %p attribute should map to a %p type', (type, expectedType) => {
|
|
128
129
|
const typeNode = getAttributeType('foo', { type });
|
|
129
130
|
|
|
@@ -134,7 +135,7 @@ describe('Attributes', () => {
|
|
|
134
135
|
expect(typeNode.typeArguments).toBeUndefined();
|
|
135
136
|
|
|
136
137
|
expect(consoleWarnMock).not.toHaveBeenCalled();
|
|
137
|
-
expect(addImport).toHaveBeenCalledWith('
|
|
138
|
+
expect(addImport).toHaveBeenCalledWith('Schema');
|
|
138
139
|
});
|
|
139
140
|
|
|
140
141
|
describe('Complex types (with generic type parameters)', () => {
|
|
@@ -145,7 +146,7 @@ describe('Attributes', () => {
|
|
|
145
146
|
expect(typeNode.typeName.escapedText).toBe(typeName);
|
|
146
147
|
|
|
147
148
|
expect(consoleWarnMock).not.toHaveBeenCalled();
|
|
148
|
-
expect(addImport).toHaveBeenCalledWith('
|
|
149
|
+
expect(addImport).toHaveBeenCalledWith('Schema');
|
|
149
150
|
};
|
|
150
151
|
|
|
151
152
|
describe('Media', () => {
|
|
@@ -153,7 +154,7 @@ describe('Attributes', () => {
|
|
|
153
154
|
const attribute = { type: 'media', multiple: true };
|
|
154
155
|
const typeNode = getAttributeType('foo', attribute);
|
|
155
156
|
|
|
156
|
-
defaultAssertions(typeNode, 'Attribute.Media');
|
|
157
|
+
defaultAssertions(typeNode, 'Schema.Attribute.Media');
|
|
157
158
|
|
|
158
159
|
expect(typeNode.typeArguments).toHaveLength(2);
|
|
159
160
|
|
|
@@ -166,7 +167,7 @@ describe('Attributes', () => {
|
|
|
166
167
|
const attribute = { type: 'media', allowedTypes: ['images', 'videos'] };
|
|
167
168
|
const typeNode = getAttributeType('foo', attribute);
|
|
168
169
|
|
|
169
|
-
defaultAssertions(typeNode, 'Attribute.Media');
|
|
170
|
+
defaultAssertions(typeNode, 'Schema.Attribute.Media');
|
|
170
171
|
|
|
171
172
|
expect(typeNode.typeArguments).toHaveLength(1);
|
|
172
173
|
|
|
@@ -186,7 +187,7 @@ describe('Attributes', () => {
|
|
|
186
187
|
const attribute = { type: 'media', multiple: true, allowedTypes: ['images', 'videos'] };
|
|
187
188
|
const typeNode = getAttributeType('foo', attribute);
|
|
188
189
|
|
|
189
|
-
defaultAssertions(typeNode, 'Attribute.Media');
|
|
190
|
+
defaultAssertions(typeNode, 'Schema.Attribute.Media');
|
|
190
191
|
|
|
191
192
|
expect(typeNode.typeArguments).toHaveLength(2);
|
|
192
193
|
|
|
@@ -208,7 +209,7 @@ describe('Attributes', () => {
|
|
|
208
209
|
const attribute = { type: 'media' };
|
|
209
210
|
const typeNode = getAttributeType('foo', attribute);
|
|
210
211
|
|
|
211
|
-
defaultAssertions(typeNode, 'Attribute.Media');
|
|
212
|
+
defaultAssertions(typeNode, 'Schema.Attribute.Media');
|
|
212
213
|
|
|
213
214
|
expect(typeNode.typeArguments).toBeUndefined();
|
|
214
215
|
});
|
|
@@ -219,7 +220,7 @@ describe('Attributes', () => {
|
|
|
219
220
|
const attribute = { type: 'enumeration', enum: ['a', 'b', 'c'] };
|
|
220
221
|
const typeNode = getAttributeType('foo', attribute);
|
|
221
222
|
|
|
222
|
-
defaultAssertions(typeNode, 'Attribute.Enumeration');
|
|
223
|
+
defaultAssertions(typeNode, 'Schema.Attribute.Enumeration');
|
|
223
224
|
|
|
224
225
|
expect(typeNode.typeArguments).toHaveLength(1);
|
|
225
226
|
expect(typeNode.typeArguments[0].kind).toBe(ts.SyntaxKind.TupleType);
|
|
@@ -240,7 +241,7 @@ describe('Attributes', () => {
|
|
|
240
241
|
const attribute = { type: 'uid' };
|
|
241
242
|
const typeNode = getAttributeType('foo', attribute);
|
|
242
243
|
|
|
243
|
-
defaultAssertions(typeNode, 'Attribute.UID');
|
|
244
|
+
defaultAssertions(typeNode, 'Schema.Attribute.UID');
|
|
244
245
|
|
|
245
246
|
expect(typeNode.typeArguments).toBeUndefined();
|
|
246
247
|
});
|
|
@@ -249,30 +250,26 @@ describe('Attributes', () => {
|
|
|
249
250
|
const attribute = { type: 'uid', targetField: 'bar' };
|
|
250
251
|
const typeNode = getAttributeType('foo', attribute, 'api::bar.bar');
|
|
251
252
|
|
|
252
|
-
defaultAssertions(typeNode, 'Attribute.UID');
|
|
253
|
+
defaultAssertions(typeNode, 'Schema.Attribute.UID');
|
|
253
254
|
|
|
254
255
|
expect(typeNode.typeArguments).not.toBeUndefined();
|
|
255
|
-
expect(typeNode.typeArguments).toHaveLength(
|
|
256
|
+
expect(typeNode.typeArguments).toHaveLength(1);
|
|
256
257
|
|
|
257
258
|
expect(typeNode.typeArguments[0].kind).toBe(ts.SyntaxKind.StringLiteral);
|
|
258
|
-
expect(typeNode.typeArguments[0].text).toBe('
|
|
259
|
-
|
|
260
|
-
expect(typeNode.typeArguments[1].kind).toBe(ts.SyntaxKind.StringLiteral);
|
|
261
|
-
expect(typeNode.typeArguments[1].text).toBe('bar');
|
|
259
|
+
expect(typeNode.typeArguments[0].text).toBe('bar');
|
|
262
260
|
});
|
|
263
261
|
|
|
264
262
|
test('UID with partial options and no target field', () => {
|
|
265
263
|
const attribute = { type: 'uid', options: { separator: '_' } };
|
|
266
|
-
const typeNode = getAttributeType('foo', attribute);
|
|
264
|
+
const typeNode = getAttributeType('foo', attribute, 'api::foo.foo');
|
|
267
265
|
|
|
268
|
-
defaultAssertions(typeNode, 'Attribute.UID');
|
|
266
|
+
defaultAssertions(typeNode, 'Schema.Attribute.UID');
|
|
269
267
|
|
|
270
|
-
expect(typeNode.typeArguments).toHaveLength(
|
|
268
|
+
expect(typeNode.typeArguments).toHaveLength(2);
|
|
271
269
|
|
|
272
270
|
expect(typeNode.typeArguments[0].kind).toBe(ts.SyntaxKind.UndefinedKeyword);
|
|
273
|
-
expect(typeNode.typeArguments[1].kind).toBe(ts.SyntaxKind.UndefinedKeyword);
|
|
274
271
|
|
|
275
|
-
const optionsLiteralNode = typeNode.typeArguments[
|
|
272
|
+
const optionsLiteralNode = typeNode.typeArguments[1];
|
|
276
273
|
|
|
277
274
|
expect(optionsLiteralNode.kind).toBe(ts.SyntaxKind.TypeLiteral);
|
|
278
275
|
expect(optionsLiteralNode.members).toHaveLength(1);
|
|
@@ -290,17 +287,14 @@ describe('Attributes', () => {
|
|
|
290
287
|
const attribute = { type: 'uid', options: { separator: '_' }, targetField: 'bar' };
|
|
291
288
|
const typeNode = getAttributeType('foo', attribute, 'api::bar.bar');
|
|
292
289
|
|
|
293
|
-
defaultAssertions(typeNode, 'Attribute.UID');
|
|
290
|
+
defaultAssertions(typeNode, 'Schema.Attribute.UID');
|
|
294
291
|
|
|
295
|
-
expect(typeNode.typeArguments).toHaveLength(
|
|
292
|
+
expect(typeNode.typeArguments).toHaveLength(2);
|
|
296
293
|
|
|
297
294
|
expect(typeNode.typeArguments[0].kind).toBe(ts.SyntaxKind.StringLiteral);
|
|
298
|
-
expect(typeNode.typeArguments[0].text).toBe('
|
|
299
|
-
|
|
300
|
-
expect(typeNode.typeArguments[1].kind).toBe(ts.SyntaxKind.StringLiteral);
|
|
301
|
-
expect(typeNode.typeArguments[1].text).toBe('bar');
|
|
295
|
+
expect(typeNode.typeArguments[0].text).toBe('bar');
|
|
302
296
|
|
|
303
|
-
const optionsLiteralNode = typeNode.typeArguments[
|
|
297
|
+
const optionsLiteralNode = typeNode.typeArguments[1];
|
|
304
298
|
|
|
305
299
|
expect(optionsLiteralNode.kind).toBe(ts.SyntaxKind.TypeLiteral);
|
|
306
300
|
expect(optionsLiteralNode.members).toHaveLength(1);
|
|
@@ -320,33 +314,27 @@ describe('Attributes', () => {
|
|
|
320
314
|
const attribute = { type: 'relation', relation: 'oneToOne', target: 'api::bar.bar' };
|
|
321
315
|
const typeNode = getAttributeType('foo', attribute, 'api::foo.foo');
|
|
322
316
|
|
|
323
|
-
defaultAssertions(typeNode, 'Attribute.Relation');
|
|
317
|
+
defaultAssertions(typeNode, 'Schema.Attribute.Relation');
|
|
324
318
|
|
|
325
|
-
expect(typeNode.typeArguments).toHaveLength(
|
|
319
|
+
expect(typeNode.typeArguments).toHaveLength(2);
|
|
326
320
|
|
|
327
321
|
expect(typeNode.typeArguments[0].kind).toBe(ts.SyntaxKind.StringLiteral);
|
|
328
|
-
expect(typeNode.typeArguments[0].text).toBe('
|
|
322
|
+
expect(typeNode.typeArguments[0].text).toBe('oneToOne');
|
|
329
323
|
|
|
330
324
|
expect(typeNode.typeArguments[1].kind).toBe(ts.SyntaxKind.StringLiteral);
|
|
331
|
-
expect(typeNode.typeArguments[1].text).toBe('
|
|
332
|
-
|
|
333
|
-
expect(typeNode.typeArguments[2].kind).toBe(ts.SyntaxKind.StringLiteral);
|
|
334
|
-
expect(typeNode.typeArguments[2].text).toBe('api::bar.bar');
|
|
325
|
+
expect(typeNode.typeArguments[1].text).toBe('api::bar.bar');
|
|
335
326
|
});
|
|
336
327
|
|
|
337
328
|
test('Polymorphic relation', () => {
|
|
338
329
|
const attribute = { type: 'relation', relation: 'morphMany' };
|
|
339
330
|
const typeNode = getAttributeType('foo', attribute, 'api::foo.foo');
|
|
340
331
|
|
|
341
|
-
defaultAssertions(typeNode, 'Attribute.Relation');
|
|
332
|
+
defaultAssertions(typeNode, 'Schema.Attribute.Relation');
|
|
342
333
|
|
|
343
|
-
expect(typeNode.typeArguments).toHaveLength(
|
|
334
|
+
expect(typeNode.typeArguments).toHaveLength(1);
|
|
344
335
|
|
|
345
336
|
expect(typeNode.typeArguments[0].kind).toBe(ts.SyntaxKind.StringLiteral);
|
|
346
|
-
expect(typeNode.typeArguments[0].text).toBe('
|
|
347
|
-
|
|
348
|
-
expect(typeNode.typeArguments[1].kind).toBe(ts.SyntaxKind.StringLiteral);
|
|
349
|
-
expect(typeNode.typeArguments[1].text).toBe('morphMany');
|
|
337
|
+
expect(typeNode.typeArguments[0].text).toBe('morphMany');
|
|
350
338
|
});
|
|
351
339
|
});
|
|
352
340
|
|
|
@@ -355,7 +343,7 @@ describe('Attributes', () => {
|
|
|
355
343
|
const attribute = { type: 'component', component: 'default.comp', repeatable: true };
|
|
356
344
|
const typeNode = getAttributeType('foo', attribute);
|
|
357
345
|
|
|
358
|
-
defaultAssertions(typeNode, 'Attribute.Component');
|
|
346
|
+
defaultAssertions(typeNode, 'Schema.Attribute.Component');
|
|
359
347
|
|
|
360
348
|
expect(typeNode.typeArguments).toHaveLength(2);
|
|
361
349
|
|
|
@@ -369,12 +357,14 @@ describe('Attributes', () => {
|
|
|
369
357
|
const attribute = { type: 'component', component: 'default.comp' };
|
|
370
358
|
const typeNode = getAttributeType('foo', attribute);
|
|
371
359
|
|
|
372
|
-
defaultAssertions(typeNode, 'Attribute.Component');
|
|
360
|
+
defaultAssertions(typeNode, 'Schema.Attribute.Component');
|
|
373
361
|
|
|
374
|
-
expect(typeNode.typeArguments).toHaveLength(
|
|
362
|
+
expect(typeNode.typeArguments).toHaveLength(2);
|
|
375
363
|
|
|
376
364
|
expect(typeNode.typeArguments[0].kind).toBe(ts.SyntaxKind.StringLiteral);
|
|
377
365
|
expect(typeNode.typeArguments[0].text).toBe('default.comp');
|
|
366
|
+
|
|
367
|
+
expect(typeNode.typeArguments[1].kind).toBe(ts.SyntaxKind.FalseKeyword);
|
|
378
368
|
});
|
|
379
369
|
});
|
|
380
370
|
|
|
@@ -383,7 +373,7 @@ describe('Attributes', () => {
|
|
|
383
373
|
const attribute = { type: 'dynamiczone', components: ['default.comp1', 'default.comp2'] };
|
|
384
374
|
const typeNode = getAttributeType('foo', attribute);
|
|
385
375
|
|
|
386
|
-
defaultAssertions(typeNode, 'Attribute.DynamicZone');
|
|
376
|
+
defaultAssertions(typeNode, 'Schema.Attribute.DynamicZone');
|
|
387
377
|
|
|
388
378
|
expect(typeNode.typeArguments).toHaveLength(1);
|
|
389
379
|
|
|
@@ -425,7 +415,7 @@ describe('Attributes', () => {
|
|
|
425
415
|
|
|
426
416
|
expect(modifiers).toHaveLength(1);
|
|
427
417
|
expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
|
|
428
|
-
expect(modifiers[0].typeName.escapedText).toBe('Attribute.Required');
|
|
418
|
+
expect(modifiers[0].typeName.escapedText).toBe('Schema.Attribute.Required');
|
|
429
419
|
});
|
|
430
420
|
});
|
|
431
421
|
|
|
@@ -450,7 +440,7 @@ describe('Attributes', () => {
|
|
|
450
440
|
|
|
451
441
|
expect(modifiers).toHaveLength(1);
|
|
452
442
|
expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
|
|
453
|
-
expect(modifiers[0].typeName.escapedText).toBe('Attribute.Private');
|
|
443
|
+
expect(modifiers[0].typeName.escapedText).toBe('Schema.Attribute.Private');
|
|
454
444
|
});
|
|
455
445
|
});
|
|
456
446
|
|
|
@@ -475,7 +465,7 @@ describe('Attributes', () => {
|
|
|
475
465
|
|
|
476
466
|
expect(modifiers).toHaveLength(1);
|
|
477
467
|
expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
|
|
478
|
-
expect(modifiers[0].typeName.escapedText).toBe('Attribute.Unique');
|
|
468
|
+
expect(modifiers[0].typeName.escapedText).toBe('Schema.Attribute.Unique');
|
|
479
469
|
});
|
|
480
470
|
});
|
|
481
471
|
|
|
@@ -500,7 +490,7 @@ describe('Attributes', () => {
|
|
|
500
490
|
|
|
501
491
|
expect(modifiers).toHaveLength(1);
|
|
502
492
|
expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
|
|
503
|
-
expect(modifiers[0].typeName.escapedText).toBe('Attribute.Configurable');
|
|
493
|
+
expect(modifiers[0].typeName.escapedText).toBe('Schema.Attribute.Configurable');
|
|
504
494
|
});
|
|
505
495
|
});
|
|
506
496
|
|
|
@@ -521,7 +511,7 @@ describe('Attributes', () => {
|
|
|
521
511
|
|
|
522
512
|
expect(modifiers).toHaveLength(1);
|
|
523
513
|
expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
|
|
524
|
-
expect(modifiers[0].typeName.escapedText).toBe('Attribute.CustomField');
|
|
514
|
+
expect(modifiers[0].typeName.escapedText).toBe('Schema.Attribute.CustomField');
|
|
525
515
|
expect(modifiers[0].typeArguments).toHaveLength(1);
|
|
526
516
|
expect(modifiers[0].typeArguments[0].kind).toBe(ts.SyntaxKind.StringLiteral);
|
|
527
517
|
expect(modifiers[0].typeArguments[0].text).toBe('plugin::color-picker.color');
|
|
@@ -539,7 +529,7 @@ describe('Attributes', () => {
|
|
|
539
529
|
|
|
540
530
|
expect(modifiers).toHaveLength(1);
|
|
541
531
|
expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
|
|
542
|
-
expect(modifiers[0].typeName.escapedText).toBe('Attribute.CustomField');
|
|
532
|
+
expect(modifiers[0].typeName.escapedText).toBe('Schema.Attribute.CustomField');
|
|
543
533
|
expect(modifiers[0].typeArguments).toHaveLength(2);
|
|
544
534
|
expect(modifiers[0].typeArguments[0].kind).toBe(ts.SyntaxKind.StringLiteral);
|
|
545
535
|
expect(modifiers[0].typeArguments[0].text).toBe('plugin::color-picker.color');
|
|
@@ -573,7 +563,7 @@ describe('Attributes', () => {
|
|
|
573
563
|
|
|
574
564
|
expect(modifiers).toHaveLength(1);
|
|
575
565
|
expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
|
|
576
|
-
expect(modifiers[0].typeName.escapedText).toBe('Attribute.SetPluginOptions');
|
|
566
|
+
expect(modifiers[0].typeName.escapedText).toBe('Schema.Attribute.SetPluginOptions');
|
|
577
567
|
expect(modifiers[0].typeArguments).toHaveLength(1);
|
|
578
568
|
expect(modifiers[0].typeArguments[0].kind).toBe(ts.SyntaxKind.TypeLiteral);
|
|
579
569
|
expect(modifiers[0].typeArguments[0].members).toHaveLength(1);
|
|
@@ -612,7 +602,7 @@ describe('Attributes', () => {
|
|
|
612
602
|
expect(modifiers).toHaveLength(1);
|
|
613
603
|
|
|
614
604
|
expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
|
|
615
|
-
expect(modifiers[0].typeName.escapedText).toBe('Attribute.SetMinMax');
|
|
605
|
+
expect(modifiers[0].typeName.escapedText).toBe('Schema.Attribute.SetMinMax');
|
|
616
606
|
|
|
617
607
|
const [setMinMax] = modifiers;
|
|
618
608
|
const { typeArguments } = setMinMax;
|
|
@@ -644,7 +634,7 @@ describe('Attributes', () => {
|
|
|
644
634
|
expect(modifiers).toHaveLength(1);
|
|
645
635
|
|
|
646
636
|
expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
|
|
647
|
-
expect(modifiers[0].typeName.escapedText).toBe('Attribute.SetMinMax');
|
|
637
|
+
expect(modifiers[0].typeName.escapedText).toBe('Schema.Attribute.SetMinMax');
|
|
648
638
|
|
|
649
639
|
const [setMinMax] = modifiers;
|
|
650
640
|
const { typeArguments } = setMinMax;
|
|
@@ -676,7 +666,7 @@ describe('Attributes', () => {
|
|
|
676
666
|
expect(modifiers).toHaveLength(1);
|
|
677
667
|
|
|
678
668
|
expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
|
|
679
|
-
expect(modifiers[0].typeName.escapedText).toBe('Attribute.SetMinMax');
|
|
669
|
+
expect(modifiers[0].typeName.escapedText).toBe('Schema.Attribute.SetMinMax');
|
|
680
670
|
|
|
681
671
|
const [setMinMax] = modifiers;
|
|
682
672
|
const { typeArguments } = setMinMax;
|
|
@@ -690,18 +680,18 @@ describe('Attributes', () => {
|
|
|
690
680
|
expect(definition.kind).toBe(ts.SyntaxKind.TypeLiteral);
|
|
691
681
|
expect(definition.members).toHaveLength(2);
|
|
692
682
|
|
|
693
|
-
const [
|
|
694
|
-
|
|
695
|
-
expect(min.kind).toBe(ts.SyntaxKind.PropertyDeclaration);
|
|
696
|
-
expect(min.name.escapedText).toBe('min');
|
|
697
|
-
expect(min.type.kind).toBe(ts.SyntaxKind.NumericLiteral);
|
|
698
|
-
expect(min.type.text).toBe('4');
|
|
683
|
+
const [max, min] = definition.members;
|
|
699
684
|
|
|
700
685
|
expect(max.kind).toBe(ts.SyntaxKind.PropertyDeclaration);
|
|
701
686
|
expect(max.name.escapedText).toBe('max');
|
|
702
687
|
expect(max.type.kind).toBe(ts.SyntaxKind.NumericLiteral);
|
|
703
688
|
expect(max.type.text).toBe('12');
|
|
704
689
|
|
|
690
|
+
expect(min.kind).toBe(ts.SyntaxKind.PropertyDeclaration);
|
|
691
|
+
expect(min.name.escapedText).toBe('min');
|
|
692
|
+
expect(min.type.kind).toBe(ts.SyntaxKind.NumericLiteral);
|
|
693
|
+
expect(min.type.text).toBe('4');
|
|
694
|
+
|
|
705
695
|
// Check for number keyword on the second typeArgument
|
|
706
696
|
expect(typeofMinMax.kind).toBe(ts.SyntaxKind.NumberKeyword);
|
|
707
697
|
});
|
|
@@ -713,7 +703,7 @@ describe('Attributes', () => {
|
|
|
713
703
|
expect(modifiers).toHaveLength(1);
|
|
714
704
|
|
|
715
705
|
expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
|
|
716
|
-
expect(modifiers[0].typeName.escapedText).toBe('Attribute.SetMinMax');
|
|
706
|
+
expect(modifiers[0].typeName.escapedText).toBe('Schema.Attribute.SetMinMax');
|
|
717
707
|
|
|
718
708
|
const [setMinMax] = modifiers;
|
|
719
709
|
const { typeArguments } = setMinMax;
|
|
@@ -745,7 +735,7 @@ describe('Attributes', () => {
|
|
|
745
735
|
expect(modifiers).toHaveLength(1);
|
|
746
736
|
|
|
747
737
|
expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
|
|
748
|
-
expect(modifiers[0].typeName.escapedText).toBe('Attribute.SetMinMax');
|
|
738
|
+
expect(modifiers[0].typeName.escapedText).toBe('Schema.Attribute.SetMinMax');
|
|
749
739
|
|
|
750
740
|
const [setMinMax] = modifiers;
|
|
751
741
|
const { typeArguments } = setMinMax;
|
|
@@ -786,7 +776,7 @@ describe('Attributes', () => {
|
|
|
786
776
|
expect(modifiers).toHaveLength(1);
|
|
787
777
|
|
|
788
778
|
expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
|
|
789
|
-
expect(modifiers[0].typeName.escapedText).toBe('Attribute.SetMinMaxLength');
|
|
779
|
+
expect(modifiers[0].typeName.escapedText).toBe('Schema.Attribute.SetMinMaxLength');
|
|
790
780
|
|
|
791
781
|
expect(modifiers[0].typeArguments).toHaveLength(1);
|
|
792
782
|
expect(modifiers[0].typeArguments[0].kind).toBe(ts.SyntaxKind.TypeLiteral);
|
|
@@ -810,7 +800,7 @@ describe('Attributes', () => {
|
|
|
810
800
|
expect(modifiers).toHaveLength(1);
|
|
811
801
|
|
|
812
802
|
expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
|
|
813
|
-
expect(modifiers[0].typeName.escapedText).toBe('Attribute.SetMinMaxLength');
|
|
803
|
+
expect(modifiers[0].typeName.escapedText).toBe('Schema.Attribute.SetMinMaxLength');
|
|
814
804
|
|
|
815
805
|
expect(modifiers[0].typeArguments).toHaveLength(1);
|
|
816
806
|
expect(modifiers[0].typeArguments[0].kind).toBe(ts.SyntaxKind.TypeLiteral);
|
|
@@ -834,30 +824,25 @@ describe('Attributes', () => {
|
|
|
834
824
|
expect(modifiers).toHaveLength(1);
|
|
835
825
|
|
|
836
826
|
expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
|
|
837
|
-
expect(modifiers[0].typeName.escapedText).toBe('Attribute.SetMinMaxLength');
|
|
827
|
+
expect(modifiers[0].typeName.escapedText).toBe('Schema.Attribute.SetMinMaxLength');
|
|
838
828
|
|
|
839
829
|
expect(modifiers[0].typeArguments).toHaveLength(1);
|
|
840
830
|
expect(modifiers[0].typeArguments[0].kind).toBe(ts.SyntaxKind.TypeLiteral);
|
|
841
831
|
expect(modifiers[0].typeArguments[0].members).toHaveLength(2);
|
|
842
832
|
|
|
843
|
-
|
|
844
|
-
expect(modifiers[0].typeArguments[0].members[0].kind).toBe(
|
|
845
|
-
ts.SyntaxKind.PropertyDeclaration
|
|
846
|
-
);
|
|
847
|
-
expect(modifiers[0].typeArguments[0].members[0].name.escapedText).toBe('minLength');
|
|
848
|
-
expect(modifiers[0].typeArguments[0].members[0].type.kind).toBe(
|
|
849
|
-
ts.SyntaxKind.NumericLiteral
|
|
850
|
-
);
|
|
851
|
-
expect(modifiers[0].typeArguments[0].members[0].type.text).toBe('4');
|
|
833
|
+
const [maxLength, minLength] = modifiers[0].typeArguments[0].members;
|
|
852
834
|
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
);
|
|
856
|
-
expect(
|
|
857
|
-
expect(
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
expect(
|
|
835
|
+
// Max
|
|
836
|
+
expect(maxLength.kind).toBe(ts.SyntaxKind.PropertyDeclaration);
|
|
837
|
+
expect(maxLength.name.escapedText).toBe('maxLength');
|
|
838
|
+
expect(maxLength.type.kind).toBe(ts.SyntaxKind.NumericLiteral);
|
|
839
|
+
expect(maxLength.type.text).toBe('12');
|
|
840
|
+
|
|
841
|
+
// Min
|
|
842
|
+
expect(minLength.kind).toBe(ts.SyntaxKind.PropertyDeclaration);
|
|
843
|
+
expect(minLength.name.escapedText).toBe('minLength');
|
|
844
|
+
expect(minLength.type.kind).toBe(ts.SyntaxKind.NumericLiteral);
|
|
845
|
+
expect(minLength.type.text).toBe('4');
|
|
861
846
|
});
|
|
862
847
|
});
|
|
863
848
|
|
|
@@ -876,7 +861,7 @@ describe('Attributes', () => {
|
|
|
876
861
|
expect(modifiers).toHaveLength(1);
|
|
877
862
|
|
|
878
863
|
expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
|
|
879
|
-
expect(modifiers[0].typeName.escapedText).toBe('Attribute.DefaultTo');
|
|
864
|
+
expect(modifiers[0].typeName.escapedText).toBe('Schema.Attribute.DefaultTo');
|
|
880
865
|
|
|
881
866
|
expect(modifiers[0].typeArguments).toHaveLength(1);
|
|
882
867
|
expect(modifiers[0].typeArguments[0].kind).toBe(ts.SyntaxKind.TrueKeyword);
|
|
@@ -889,7 +874,7 @@ describe('Attributes', () => {
|
|
|
889
874
|
expect(modifiers).toHaveLength(1);
|
|
890
875
|
|
|
891
876
|
expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
|
|
892
|
-
expect(modifiers[0].typeName.escapedText).toBe('Attribute.DefaultTo');
|
|
877
|
+
expect(modifiers[0].typeName.escapedText).toBe('Schema.Attribute.DefaultTo');
|
|
893
878
|
|
|
894
879
|
expect(modifiers[0].typeArguments).toHaveLength(1);
|
|
895
880
|
expect(modifiers[0].typeArguments[0].kind).toBe(ts.SyntaxKind.TypeLiteral);
|
|
@@ -902,6 +887,16 @@ describe('Attributes', () => {
|
|
|
902
887
|
ts.SyntaxKind.TrueKeyword
|
|
903
888
|
);
|
|
904
889
|
});
|
|
890
|
+
|
|
891
|
+
test('Default: <function>', () => {
|
|
892
|
+
const anyFunction = jest.fn();
|
|
893
|
+
const attribute = { default: anyFunction };
|
|
894
|
+
|
|
895
|
+
const modifiers = getAttributeModifiers(attribute);
|
|
896
|
+
|
|
897
|
+
// The default modifier shouldn't be processed when encountering a function
|
|
898
|
+
expect(modifiers).toHaveLength(0);
|
|
899
|
+
});
|
|
905
900
|
});
|
|
906
901
|
});
|
|
907
902
|
});
|
|
@@ -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);
|
|
@@ -247,13 +247,13 @@ describe('Utils', () => {
|
|
|
247
247
|
expect(objectNode.members).toHaveLength(2);
|
|
248
248
|
|
|
249
249
|
expect(objectNode.members[0].kind).toBe(ts.SyntaxKind.PropertyDeclaration);
|
|
250
|
-
expect(objectNode.members[0].name.escapedText).toBe('
|
|
251
|
-
expect(objectNode.members[0].type.kind).toBe(ts.SyntaxKind.
|
|
252
|
-
expect(objectNode.members[0].type.text).toBe('bar');
|
|
250
|
+
expect(objectNode.members[0].name.escapedText).toBe('bar');
|
|
251
|
+
expect(objectNode.members[0].type.kind).toBe(ts.SyntaxKind.TrueKeyword);
|
|
253
252
|
|
|
254
253
|
expect(objectNode.members[1].kind).toBe(ts.SyntaxKind.PropertyDeclaration);
|
|
255
|
-
expect(objectNode.members[1].name.escapedText).toBe('
|
|
256
|
-
expect(objectNode.members[1].type.kind).toBe(ts.SyntaxKind.
|
|
254
|
+
expect(objectNode.members[1].name.escapedText).toBe('foo');
|
|
255
|
+
expect(objectNode.members[1].type.kind).toBe(ts.SyntaxKind.StringLiteral);
|
|
256
|
+
expect(objectNode.members[1].type.text).toBe('bar');
|
|
257
257
|
});
|
|
258
258
|
|
|
259
259
|
test('Object', () => {
|
|
@@ -262,20 +262,20 @@ describe('Utils', () => {
|
|
|
262
262
|
expect(node.kind).toBe(ts.SyntaxKind.TypeLiteral);
|
|
263
263
|
expect(node.members).toHaveLength(2);
|
|
264
264
|
|
|
265
|
-
const [
|
|
265
|
+
const [barMember, fooMember] = node.members;
|
|
266
266
|
|
|
267
|
-
expect(
|
|
268
|
-
expect(
|
|
269
|
-
expect(
|
|
270
|
-
expect(
|
|
271
|
-
expect(firstMember.type.elements[0].kind).toBe(ts.SyntaxKind.StringLiteral);
|
|
272
|
-
expect(firstMember.type.elements[1].kind).toBe(ts.SyntaxKind.TrueKeyword);
|
|
273
|
-
expect(firstMember.type.elements[2].kind).toBe(ts.SyntaxKind.FirstLiteralToken);
|
|
267
|
+
expect(barMember.kind).toBe(ts.SyntaxKind.PropertyDeclaration);
|
|
268
|
+
expect(barMember.name.escapedText).toBe('bar');
|
|
269
|
+
expect(barMember.type.kind).toBe(ts.SyntaxKind.LiteralType);
|
|
270
|
+
expect(barMember.type.literal).toBe(ts.SyntaxKind.NullKeyword);
|
|
274
271
|
|
|
275
|
-
expect(
|
|
276
|
-
expect(
|
|
277
|
-
expect(
|
|
278
|
-
expect(
|
|
272
|
+
expect(fooMember.kind).toBe(ts.SyntaxKind.PropertyDeclaration);
|
|
273
|
+
expect(fooMember.name.escapedText).toBe('foo');
|
|
274
|
+
expect(fooMember.type.kind).toBe(ts.SyntaxKind.TupleType);
|
|
275
|
+
expect(fooMember.type.elements).toHaveLength(3);
|
|
276
|
+
expect(fooMember.type.elements[0].kind).toBe(ts.SyntaxKind.StringLiteral);
|
|
277
|
+
expect(fooMember.type.elements[1].kind).toBe(ts.SyntaxKind.TrueKeyword);
|
|
278
|
+
expect(fooMember.type.elements[2].kind).toBe(ts.SyntaxKind.FirstLiteralToken);
|
|
279
279
|
});
|
|
280
280
|
|
|
281
281
|
test('Object with complex keys', () => {
|
|
@@ -284,19 +284,19 @@ describe('Utils', () => {
|
|
|
284
284
|
expect(node.kind).toBe(ts.SyntaxKind.TypeLiteral);
|
|
285
285
|
expect(node.members).toHaveLength(2);
|
|
286
286
|
|
|
287
|
-
const [
|
|
287
|
+
const [fooBar, fooDashBar] = node.members;
|
|
288
288
|
|
|
289
|
-
expect(
|
|
290
|
-
expect(
|
|
291
|
-
expect(
|
|
292
|
-
expect(
|
|
293
|
-
expect(
|
|
289
|
+
expect(fooBar.kind).toBe(ts.SyntaxKind.PropertyDeclaration);
|
|
290
|
+
expect(fooBar.name.kind).toBe(ts.SyntaxKind.Identifier);
|
|
291
|
+
expect(fooBar.name.escapedText).toBe('foo');
|
|
292
|
+
expect(fooBar.type.kind).toBe(ts.SyntaxKind.StringLiteral);
|
|
293
|
+
expect(fooBar.type.text).toBe('bar');
|
|
294
294
|
|
|
295
|
-
expect(
|
|
296
|
-
expect(
|
|
297
|
-
expect(
|
|
298
|
-
expect(
|
|
299
|
-
expect(
|
|
295
|
+
expect(fooDashBar.kind).toBe(ts.SyntaxKind.PropertyDeclaration);
|
|
296
|
+
expect(fooDashBar.name.kind).toBe(ts.SyntaxKind.StringLiteral);
|
|
297
|
+
expect(fooDashBar.name.text).toBe('foo-bar');
|
|
298
|
+
expect(fooDashBar.type.kind).toBe(ts.SyntaxKind.StringLiteral);
|
|
299
|
+
expect(fooDashBar.type.text).toBe('foobar');
|
|
300
300
|
});
|
|
301
301
|
|
|
302
302
|
test('Invalid data type supplied (function)', () => {
|
|
@@ -18,9 +18,9 @@ module.exports = {
|
|
|
18
18
|
},
|
|
19
19
|
|
|
20
20
|
generateImportDefinition() {
|
|
21
|
-
const formattedImports = imports
|
|
22
|
-
|
|
23
|
-
|
|
21
|
+
const formattedImports = imports
|
|
22
|
+
.sort()
|
|
23
|
+
.map((key) => factory.createImportSpecifier(false, undefined, factory.createIdentifier(key)));
|
|
24
24
|
|
|
25
25
|
return [
|
|
26
26
|
factory.createImportDeclaration(
|
|
@@ -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
|
};
|
|
@@ -151,8 +151,8 @@ const getAttributeModifiers = (attribute) => {
|
|
|
151
151
|
);
|
|
152
152
|
}
|
|
153
153
|
|
|
154
|
-
// Default
|
|
155
|
-
if (!_.isNil(attribute.default)) {
|
|
154
|
+
// Default (ignore if default is a function)
|
|
155
|
+
if (!_.isNil(attribute.default) && !_.isFunction(attribute.default)) {
|
|
156
156
|
const defaultLiteral = toTypeLiteral(attribute.default);
|
|
157
157
|
|
|
158
158
|
modifiers.push(
|
|
@@ -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 });
|
|
@@ -47,7 +47,7 @@ module.exports = {
|
|
|
47
47
|
decimal() {
|
|
48
48
|
return [withAttributeNamespace('Decimal')];
|
|
49
49
|
},
|
|
50
|
-
uid({ attribute
|
|
50
|
+
uid({ attribute }) {
|
|
51
51
|
const { targetField, options } = attribute;
|
|
52
52
|
|
|
53
53
|
// If there are no params to compute, then return the attribute type alone
|
|
@@ -58,18 +58,15 @@ module.exports = {
|
|
|
58
58
|
const params = [];
|
|
59
59
|
|
|
60
60
|
// If the targetField property is defined, then reference it,
|
|
61
|
-
// otherwise, put `undefined` keyword type
|
|
62
|
-
const
|
|
63
|
-
?
|
|
64
|
-
|
|
65
|
-
factory.createKeywordTypeNode(ts.SyntaxKind.UndefinedKeyword),
|
|
66
|
-
]
|
|
67
|
-
: [factory.createStringLiteral(uid), factory.createStringLiteral(targetField)];
|
|
61
|
+
// otherwise, put `undefined` keyword type node as placeholder
|
|
62
|
+
const targetFieldParam = _.isUndefined(targetField)
|
|
63
|
+
? factory.createKeywordTypeNode(ts.SyntaxKind.UndefinedKeyword)
|
|
64
|
+
: factory.createStringLiteral(targetField);
|
|
68
65
|
|
|
69
|
-
params.push(
|
|
66
|
+
params.push(targetFieldParam);
|
|
70
67
|
|
|
71
68
|
// If the options property is defined, transform it to
|
|
72
|
-
// a type
|
|
69
|
+
// a type literal node and add it to the params list
|
|
73
70
|
if (_.isObject(options)) {
|
|
74
71
|
params.push(toTypeLiteral(options));
|
|
75
72
|
}
|
|
@@ -111,25 +108,18 @@ module.exports = {
|
|
|
111
108
|
|
|
112
109
|
return [withAttributeNamespace('Media'), params];
|
|
113
110
|
},
|
|
114
|
-
relation({
|
|
111
|
+
relation({ attribute }) {
|
|
115
112
|
const { relation, target } = attribute;
|
|
116
113
|
|
|
117
114
|
const isMorphRelation = relation.toLowerCase().includes('morph');
|
|
118
115
|
|
|
119
116
|
if (isMorphRelation) {
|
|
120
|
-
return [
|
|
121
|
-
withAttributeNamespace('Relation'),
|
|
122
|
-
[factory.createStringLiteral(uid, true), factory.createStringLiteral(relation, true)],
|
|
123
|
-
];
|
|
117
|
+
return [withAttributeNamespace('Relation'), [factory.createStringLiteral(relation, true)]];
|
|
124
118
|
}
|
|
125
119
|
|
|
126
120
|
return [
|
|
127
121
|
withAttributeNamespace('Relation'),
|
|
128
|
-
[
|
|
129
|
-
factory.createStringLiteral(uid, true),
|
|
130
|
-
factory.createStringLiteral(relation, true),
|
|
131
|
-
factory.createStringLiteral(target, true),
|
|
132
|
-
],
|
|
122
|
+
[factory.createStringLiteral(relation, true), factory.createStringLiteral(target, true)],
|
|
133
123
|
];
|
|
134
124
|
},
|
|
135
125
|
component({ attribute }) {
|
|
@@ -138,6 +128,8 @@ module.exports = {
|
|
|
138
128
|
|
|
139
129
|
if (attribute.repeatable) {
|
|
140
130
|
params.push(factory.createTrue());
|
|
131
|
+
} else {
|
|
132
|
+
params.push(factory.createFalse());
|
|
141
133
|
}
|
|
142
134
|
|
|
143
135
|
return [withAttributeNamespace('Component'), params];
|
|
@@ -22,9 +22,11 @@ const { addImport } = require('../imports');
|
|
|
22
22
|
const generateAttributePropertySignature = (schema) => {
|
|
23
23
|
const { attributes } = schema;
|
|
24
24
|
|
|
25
|
-
const properties = Object.entries(attributes)
|
|
26
|
-
|
|
27
|
-
|
|
25
|
+
const properties = Object.entries(attributes)
|
|
26
|
+
.sort((a, b) => a[0].localeCompare(b[0]))
|
|
27
|
+
.map(([attributeName, attribute]) => {
|
|
28
|
+
return attributeToPropertySignature(schema, attributeName, attribute);
|
|
29
|
+
});
|
|
28
30
|
|
|
29
31
|
return factory.createPropertySignature(
|
|
30
32
|
undefined,
|
|
@@ -56,8 +58,8 @@ const generateSchemaDefinition = (schema) => {
|
|
|
56
58
|
const interfaceName = getSchemaInterfaceName(uid);
|
|
57
59
|
const parentType = getSchemaExtendsTypeName(schema);
|
|
58
60
|
|
|
59
|
-
// Make sure the
|
|
60
|
-
addImport(NAMESPACES.
|
|
61
|
+
// Make sure the Struct namespace is imported
|
|
62
|
+
addImport(NAMESPACES.Struct);
|
|
61
63
|
|
|
62
64
|
// Properties whose values can be mapped to a literal type expression
|
|
63
65
|
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
|
/**
|
|
@@ -111,7 +111,7 @@ const toTypeLiteral = (data) => {
|
|
|
111
111
|
throw new Error(`Cannot convert to object literal. Unknown type "${typeof data}"`);
|
|
112
112
|
}
|
|
113
113
|
|
|
114
|
-
const entries = Object.entries(data);
|
|
114
|
+
const entries = Object.entries(data).sort((a, b) => a[0].localeCompare(b[0]));
|
|
115
115
|
|
|
116
116
|
const props = entries.reduce((acc, [key, value]) => {
|
|
117
117
|
// Handle keys such as content-type-builder & co.
|
|
@@ -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
|
|
@@ -1,10 +1,16 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const { factory } = require('typescript');
|
|
4
|
+
const { pipe, values, sortBy, map } = require('lodash/fp');
|
|
4
5
|
|
|
5
6
|
const { models } = require('../common');
|
|
6
7
|
const { emitDefinitions, format, generateSharedExtensionDefinition } = require('../utils');
|
|
7
8
|
|
|
9
|
+
const NO_COMPONENT_PLACEHOLDER_COMMENT = `/*
|
|
10
|
+
* The app doesn't have any components yet.
|
|
11
|
+
*/
|
|
12
|
+
`;
|
|
13
|
+
|
|
8
14
|
/**
|
|
9
15
|
* Generate type definitions for Strapi Components
|
|
10
16
|
*
|
|
@@ -18,10 +24,20 @@ const generateComponentsDefinitions = async (options = {}) => {
|
|
|
18
24
|
|
|
19
25
|
const { components } = strapi;
|
|
20
26
|
|
|
21
|
-
const componentsDefinitions =
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
27
|
+
const componentsDefinitions = pipe(
|
|
28
|
+
values,
|
|
29
|
+
sortBy('uid'),
|
|
30
|
+
map((component) => ({
|
|
31
|
+
uid: component.uid,
|
|
32
|
+
definition: models.schema.generateSchemaDefinition(component),
|
|
33
|
+
}))
|
|
34
|
+
)(components);
|
|
35
|
+
|
|
36
|
+
options.logger.debug(`Found ${componentsDefinitions.length} components.`);
|
|
37
|
+
|
|
38
|
+
if (componentsDefinitions.length === 0) {
|
|
39
|
+
return { output: NO_COMPONENT_PLACEHOLDER_COMMENT, stats: {} };
|
|
40
|
+
}
|
|
25
41
|
|
|
26
42
|
const formattedSchemasDefinitions = componentsDefinitions.reduce((acc, def) => {
|
|
27
43
|
acc.push(
|
|
@@ -46,7 +62,7 @@ const generateComponentsDefinitions = async (options = {}) => {
|
|
|
46
62
|
...formattedSchemasDefinitions,
|
|
47
63
|
|
|
48
64
|
// Global
|
|
49
|
-
generateSharedExtensionDefinition('
|
|
65
|
+
generateSharedExtensionDefinition('ComponentSchemas', componentsDefinitions),
|
|
50
66
|
];
|
|
51
67
|
|
|
52
68
|
const output = emitDefinitions(allDefinitions);
|
|
@@ -1,10 +1,16 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const { factory } = require('typescript');
|
|
4
|
+
const { values, pipe, map, sortBy } = require('lodash/fp');
|
|
4
5
|
|
|
5
6
|
const { models } = require('../common');
|
|
6
7
|
const { emitDefinitions, format, generateSharedExtensionDefinition } = require('../utils');
|
|
7
8
|
|
|
9
|
+
const NO_CONTENT_TYPE_PLACEHOLDER_COMMENT = `/*
|
|
10
|
+
* The app doesn't have any content-types yet.
|
|
11
|
+
*/
|
|
12
|
+
`;
|
|
13
|
+
|
|
8
14
|
/**
|
|
9
15
|
* Generate type definitions for Strapi Content-Types
|
|
10
16
|
*
|
|
@@ -18,10 +24,20 @@ const generateContentTypesDefinitions = async (options = {}) => {
|
|
|
18
24
|
|
|
19
25
|
const { contentTypes } = strapi;
|
|
20
26
|
|
|
21
|
-
const contentTypesDefinitions =
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
27
|
+
const contentTypesDefinitions = pipe(
|
|
28
|
+
values,
|
|
29
|
+
sortBy('uid'),
|
|
30
|
+
map((contentType) => ({
|
|
31
|
+
uid: contentType.uid,
|
|
32
|
+
definition: models.schema.generateSchemaDefinition(contentType),
|
|
33
|
+
}))
|
|
34
|
+
)(contentTypes);
|
|
35
|
+
|
|
36
|
+
options.logger.debug(`Found ${contentTypesDefinitions.length} content-types.`);
|
|
37
|
+
|
|
38
|
+
if (contentTypesDefinitions.length === 0) {
|
|
39
|
+
return { output: NO_CONTENT_TYPE_PLACEHOLDER_COMMENT, stats: {} };
|
|
40
|
+
}
|
|
25
41
|
|
|
26
42
|
const formattedSchemasDefinitions = contentTypesDefinitions.reduce((acc, def) => {
|
|
27
43
|
acc.push(
|
|
@@ -46,7 +62,7 @@ const generateContentTypesDefinitions = async (options = {}) => {
|
|
|
46
62
|
...formattedSchemasDefinitions,
|
|
47
63
|
|
|
48
64
|
// Global
|
|
49
|
-
generateSharedExtensionDefinition('
|
|
65
|
+
generateSharedExtensionDefinition('ContentTypeSchemas', contentTypesDefinitions),
|
|
50
66
|
];
|
|
51
67
|
|
|
52
68
|
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/lib/utils/index.js
CHANGED
|
@@ -7,6 +7,7 @@ const reportDiagnostics = require('./report-diagnostics');
|
|
|
7
7
|
const resolveConfigOptions = require('./resolve-config-options');
|
|
8
8
|
const formatHost = require('./format-host');
|
|
9
9
|
const resolveOutDir = require('./resolve-outdir');
|
|
10
|
+
const resolveOutDirSync = require('./resolve-outdir-sync');
|
|
10
11
|
|
|
11
12
|
module.exports = {
|
|
12
13
|
isUsingTypeScript,
|
|
@@ -16,4 +17,5 @@ module.exports = {
|
|
|
16
17
|
resolveConfigOptions,
|
|
17
18
|
formatHost,
|
|
18
19
|
resolveOutDir,
|
|
20
|
+
resolveOutDirSync,
|
|
19
21
|
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const path = require('path');
|
|
4
|
+
const resolveConfigOptions = require('./resolve-config-options');
|
|
5
|
+
const isUsingTypescriptSync = require('./is-using-typescript-sync');
|
|
6
|
+
|
|
7
|
+
const DEFAULT_TS_CONFIG_FILENAME = 'tsconfig.json';
|
|
8
|
+
/**
|
|
9
|
+
* Gets the outDir value from config file (tsconfig)
|
|
10
|
+
* @param {string} dir
|
|
11
|
+
* @param {string | undefined} configFilename
|
|
12
|
+
* @returns {string | undefined}
|
|
13
|
+
*/
|
|
14
|
+
module.exports = (dir, configFilename = DEFAULT_TS_CONFIG_FILENAME) => {
|
|
15
|
+
return isUsingTypescriptSync(dir)
|
|
16
|
+
? resolveConfigOptions(path.join(dir, configFilename)).options.outDir
|
|
17
|
+
: undefined;
|
|
18
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@strapi/typescript-utils",
|
|
3
|
-
"version": "0.0.0-next.
|
|
3
|
+
"version": "0.0.0-next.fdac61dd05ca665168f51f655f1d165b55ec4231",
|
|
4
4
|
"description": "Typescript support for Strapi",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"strapi",
|
|
@@ -37,14 +37,16 @@
|
|
|
37
37
|
"dependencies": {
|
|
38
38
|
"chalk": "4.1.2",
|
|
39
39
|
"cli-table3": "0.6.5",
|
|
40
|
-
"fs-extra": "
|
|
40
|
+
"fs-extra": "11.2.0",
|
|
41
41
|
"lodash": "4.17.21",
|
|
42
|
-
"prettier": "
|
|
43
|
-
"typescript": "5.
|
|
42
|
+
"prettier": "3.3.3",
|
|
43
|
+
"typescript": "5.4.4"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@types/fs-extra": "11.0.4"
|
|
44
47
|
},
|
|
45
48
|
"engines": {
|
|
46
|
-
"node": ">=18.0.0 <=
|
|
49
|
+
"node": ">=18.0.0 <=22.x.x",
|
|
47
50
|
"npm": ">=6.0.0"
|
|
48
|
-
}
|
|
49
|
-
"gitHead": "fd9757603c653ca239c45d6e28ab536d2dae0b39"
|
|
51
|
+
}
|
|
50
52
|
}
|
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
|
-
};
|