@strapi/typescript-utils 0.0.0-next.e9bb5ccdc459f4c6b6717a2d5d86359b7a47d47d → 0.0.0-next.f7babb775ed9a7e18d8351cb7f74c63e016323c4

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.
@@ -1,22 +1,22 @@
1
1
  'use strict';
2
2
 
3
- jest.mock('../../../generators/schemas/imports', () => ({ addImport: jest.fn() }));
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/schemas/attributes');
9
+ const attributeToPropertySignature = require('../../../generators/common/models/attributes');
10
10
  const {
11
11
  getAttributeType,
12
12
  getAttributeModifiers,
13
- } = require('../../../generators/schemas/attributes');
14
- const { addImport } = require('../../../generators/schemas/imports');
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.resetAllMocks();
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('StringAttribute');
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('ComponentAttribute');
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('EnumerationAttribute');
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', 'StringAttribute'],
112
- ['text', 'TextAttribute'],
113
- ['richtext', 'RichTextAttribute'],
114
- ['password', 'PasswordAttribute'],
115
- ['email', 'EmailAttribute'],
116
- ['date', 'DateAttribute'],
117
- ['time', 'TimeAttribute'],
118
- ['datetime', 'DateTimeAttribute'],
119
- ['timestamp', 'TimestampAttribute'],
120
- ['integer', 'IntegerAttribute'],
121
- ['biginteger', 'BigIntegerAttribute'],
122
- ['float', 'FloatAttribute'],
123
- ['decimal', 'DecimalAttribute'],
124
- ['boolean', 'BooleanAttribute'],
125
- ['json', 'JSONAttribute'],
126
- ['media', 'MediaAttribute'],
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(expectedType);
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(typeName);
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, 'EnumerationAttribute');
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, 'UIDAttribute');
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, 'UIDAttribute');
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, 'UIDAttribute');
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, 'UIDAttribute');
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, 'RelationAttribute');
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, 'RelationAttribute');
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, 'ComponentAttribute');
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, 'ComponentAttribute');
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, 'DynamicZoneAttribute');
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('RequiredAttribute');
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('PrivateAttribute');
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('UniqueAttribute');
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('ConfigurableAttribute');
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,7 +546,7 @@ 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
551
  expect(modifiers[0].typeArguments).toHaveLength(1);
552
552
  expect(modifiers[0].typeArguments[0].kind).toBe(ts.SyntaxKind.TypeLiteral);
@@ -570,7 +570,7 @@ describe('Attributes', () => {
570
570
  expect(modifiers).toHaveLength(1);
571
571
 
572
572
  expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
573
- expect(modifiers[0].typeName.escapedText).toBe('SetMinMax');
573
+ expect(modifiers[0].typeName.escapedText).toBe('Attribute.SetMinMax');
574
574
 
575
575
  expect(modifiers[0].typeArguments).toHaveLength(1);
576
576
  expect(modifiers[0].typeArguments[0].kind).toBe(ts.SyntaxKind.TypeLiteral);
@@ -594,7 +594,7 @@ describe('Attributes', () => {
594
594
  expect(modifiers).toHaveLength(1);
595
595
 
596
596
  expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
597
- expect(modifiers[0].typeName.escapedText).toBe('SetMinMax');
597
+ expect(modifiers[0].typeName.escapedText).toBe('Attribute.SetMinMax');
598
598
 
599
599
  expect(modifiers[0].typeArguments).toHaveLength(1);
600
600
  expect(modifiers[0].typeArguments[0].kind).toBe(ts.SyntaxKind.TypeLiteral);
@@ -636,7 +636,7 @@ describe('Attributes', () => {
636
636
  expect(modifiers).toHaveLength(1);
637
637
 
638
638
  expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
639
- expect(modifiers[0].typeName.escapedText).toBe('SetMinMaxLength');
639
+ expect(modifiers[0].typeName.escapedText).toBe('Attribute.SetMinMaxLength');
640
640
 
641
641
  expect(modifiers[0].typeArguments).toHaveLength(1);
642
642
  expect(modifiers[0].typeArguments[0].kind).toBe(ts.SyntaxKind.TypeLiteral);
@@ -660,7 +660,7 @@ describe('Attributes', () => {
660
660
  expect(modifiers).toHaveLength(1);
661
661
 
662
662
  expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
663
- expect(modifiers[0].typeName.escapedText).toBe('SetMinMaxLength');
663
+ expect(modifiers[0].typeName.escapedText).toBe('Attribute.SetMinMaxLength');
664
664
 
665
665
  expect(modifiers[0].typeArguments).toHaveLength(1);
666
666
  expect(modifiers[0].typeArguments[0].kind).toBe(ts.SyntaxKind.TypeLiteral);
@@ -684,7 +684,7 @@ describe('Attributes', () => {
684
684
  expect(modifiers).toHaveLength(1);
685
685
 
686
686
  expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
687
- expect(modifiers[0].typeName.escapedText).toBe('SetMinMaxLength');
687
+ expect(modifiers[0].typeName.escapedText).toBe('Attribute.SetMinMaxLength');
688
688
 
689
689
  expect(modifiers[0].typeArguments).toHaveLength(1);
690
690
  expect(modifiers[0].typeArguments[0].kind).toBe(ts.SyntaxKind.TypeLiteral);
@@ -726,7 +726,7 @@ describe('Attributes', () => {
726
726
  expect(modifiers).toHaveLength(1);
727
727
 
728
728
  expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
729
- expect(modifiers[0].typeName.escapedText).toBe('DefaultTo');
729
+ expect(modifiers[0].typeName.escapedText).toBe('Attribute.DefaultTo');
730
730
 
731
731
  expect(modifiers[0].typeArguments).toHaveLength(1);
732
732
  expect(modifiers[0].typeArguments[0].kind).toBe(ts.SyntaxKind.TrueKeyword);
@@ -739,7 +739,7 @@ describe('Attributes', () => {
739
739
  expect(modifiers).toHaveLength(1);
740
740
 
741
741
  expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
742
- expect(modifiers[0].typeName.escapedText).toBe('DefaultTo');
742
+ expect(modifiers[0].typeName.escapedText).toBe('Attribute.DefaultTo');
743
743
 
744
744
  expect(modifiers[0].typeArguments).toHaveLength(1);
745
745
  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/schemas/imports');
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 def = generateImportDefinition();
30
+ const defs = generateImportDefinition();
31
31
 
32
- expect(def.kind).toBe(ts.SyntaxKind.ImportDeclaration);
32
+ defs.forEach((def) => {
33
+ expect(def.kind).toBe(ts.SyntaxKind.ImportDeclaration);
33
34
 
34
- // Module specifier
35
- expect(def.moduleSpecifier.kind).toBe(ts.SyntaxKind.StringLiteral);
36
- expect(def.moduleSpecifier.text).toBe('@strapi/strapi');
35
+ // Module specifier
36
+ expect(def.moduleSpecifier.kind).toBe(ts.SyntaxKind.StringLiteral);
37
+ expect(def.moduleSpecifier.text).toBe('@strapi/strapi');
37
38
 
38
- // Import clause (should be named imports)
39
- expect(def.importClause.kind).toBe(ts.SyntaxKind.ImportClause);
39
+ // Import clause (should be named imports)
40
+ expect(def.importClause.kind).toBe(ts.SyntaxKind.ImportClause);
40
41
 
41
- const { elements } = def.importClause.namedBindings;
42
+ const { elements } = def.importClause.namedBindings;
42
43
 
43
- expect(elements).toHaveLength(2);
44
+ expect(elements).toHaveLength(2);
44
45
 
45
- // Import clauses
46
- getImports().forEach((namedImport, index) => {
47
- const element = elements[index];
46
+ // Import clauses
47
+ getImports().forEach((namedImport, index) => {
48
+ const element = elements[index];
48
49
 
49
- expect(element.kind).toBe(ts.SyntaxKind.ImportSpecifier);
50
- expect(element.name.kind).toBe(ts.SyntaxKind.Identifier);
51
- expect(element.name.escapedText).toBe(namedImport);
50
+ expect(element.kind).toBe(ts.SyntaxKind.ImportSpecifier);
51
+ expect(element.name.kind).toBe(ts.SyntaxKind.Identifier);
52
+ expect(element.name.escapedText).toBe(namedImport);
53
+ });
52
54
  });
53
55
  });
54
56
  });
@@ -4,67 +4,15 @@ const ts = require('typescript');
4
4
  const { factory } = require('typescript');
5
5
 
6
6
  const {
7
- getAllStrapiSchemas,
8
7
  getDefinitionAttributesCount,
9
8
  getSchemaExtendsTypeName,
10
9
  getSchemaInterfaceName,
11
10
  getSchemaModelType,
12
11
  getTypeNode,
13
12
  toTypeLiteral,
14
- } = require('../../../generators/schemas/utils');
13
+ } = require('../../../generators/common/models/utils');
15
14
 
16
15
  describe('Utils', () => {
17
- describe('Get All Strapi Schemas', () => {
18
- test('Get both components and content types', () => {
19
- const strapi = {
20
- contentTypes: {
21
- ctA: {},
22
- ctB: {},
23
- },
24
- components: {
25
- comp1: {},
26
- comp2: {},
27
- comp3: {},
28
- },
29
- };
30
-
31
- const schemas = getAllStrapiSchemas(strapi);
32
-
33
- expect(schemas).toMatchObject({ ctA: {}, ctB: {}, comp1: {}, comp2: {}, comp3: {} });
34
- });
35
-
36
- test('Get only components if there is no content type', () => {
37
- const strapi = {
38
- contentTypes: {},
39
-
40
- components: {
41
- comp1: {},
42
- comp2: {},
43
- comp3: {},
44
- },
45
- };
46
-
47
- const schemas = getAllStrapiSchemas(strapi);
48
-
49
- expect(schemas).toMatchObject({ comp1: {}, comp2: {}, comp3: {} });
50
- });
51
-
52
- test('Get only content types if there is no component', () => {
53
- const strapi = {
54
- contentTypes: {
55
- ctA: {},
56
- ctB: {},
57
- },
58
-
59
- components: {},
60
- };
61
-
62
- const schemas = getAllStrapiSchemas(strapi);
63
-
64
- expect(schemas).toMatchObject({ ctA: {}, ctB: {} });
65
- });
66
- });
67
-
68
16
  describe('Get Definition Attributes Count', () => {
69
17
  const createMainNode = (members = []) => {
70
18
  return factory.createInterfaceDeclaration(
@@ -172,10 +120,10 @@ describe('Utils', () => {
172
120
 
173
121
  describe('Get Schema Extends Type Name', () => {
174
122
  test.each([
175
- [{ modelType: 'component', kind: null }, 'ComponentSchema'],
176
- [{ modelType: 'contentType', kind: 'singleType' }, 'SingleTypeSchema'],
177
- [{ modelType: 'contentType', kind: 'collectionType' }, 'CollectionTypeSchema'],
178
- [{ modelType: 'invalidType', kind: 'foo' }, 'Schema'],
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],
179
127
  ])("Expect %p to generate %p as the base type for a schema's interface", (schema, expected) => {
180
128
  expect(getSchemaExtendsTypeName(schema)).toBe(expected);
181
129
  });
@@ -22,11 +22,13 @@ module.exports = {
22
22
  factory.createImportSpecifier(false, undefined, factory.createIdentifier(key))
23
23
  );
24
24
 
25
- return factory.createImportDeclaration(
26
- undefined,
27
- factory.createImportClause(false, undefined, factory.createNamedImports(formattedImports)),
28
- factory.createStringLiteral('@strapi/strapi'),
29
- undefined
30
- );
25
+ return [
26
+ factory.createImportDeclaration(
27
+ undefined,
28
+ factory.createImportClause(true, undefined, factory.createNamedImports(formattedImports)),
29
+ factory.createStringLiteral('@strapi/strapi'),
30
+ undefined
31
+ ),
32
+ ];
31
33
  },
32
34
  };
@@ -0,0 +1,9 @@
1
+ 'use strict';
2
+
3
+ const models = require('./models');
4
+ const imports = require('./imports');
5
+
6
+ module.exports = {
7
+ models,
8
+ imports,
9
+ };
@@ -3,8 +3,8 @@
3
3
  const { factory } = require('typescript');
4
4
  const _ = require('lodash/fp');
5
5
 
6
- const { addImport } = require('./imports');
7
- const { getTypeNode, toTypeLiteral } = require('./utils');
6
+ const { addImport } = require('../imports');
7
+ const { getTypeNode, toTypeLiteral, withAttributeNamespace, NAMESPACES } = require('./utils');
8
8
  const mappers = require('./mappers');
9
9
 
10
10
  /**
@@ -26,7 +26,8 @@ const getAttributeType = (attributeName, attribute, uid) => {
26
26
 
27
27
  const [attributeType, typeParams] = mappers[attribute.type]({ uid, attribute, attributeName });
28
28
 
29
- addImport(attributeType);
29
+ // Make sure the attribute namespace is imported
30
+ addImport(NAMESPACES.attribute);
30
31
 
31
32
  return getTypeNode(attributeType, typeParams);
32
33
  };
@@ -42,38 +43,36 @@ const getAttributeModifiers = (attribute) => {
42
43
 
43
44
  // Required
44
45
  if (attribute.required) {
45
- addImport('RequiredAttribute');
46
-
47
- modifiers.push(factory.createTypeReferenceNode(factory.createIdentifier('RequiredAttribute')));
46
+ modifiers.push(
47
+ factory.createTypeReferenceNode(factory.createIdentifier(withAttributeNamespace('Required')))
48
+ );
48
49
  }
49
50
 
50
51
  // Private
51
52
  if (attribute.private) {
52
- addImport('PrivateAttribute');
53
-
54
- modifiers.push(factory.createTypeReferenceNode(factory.createIdentifier('PrivateAttribute')));
53
+ modifiers.push(
54
+ factory.createTypeReferenceNode(factory.createIdentifier(withAttributeNamespace('Private')))
55
+ );
55
56
  }
56
57
 
57
58
  // Unique
58
59
  if (attribute.unique) {
59
- addImport('UniqueAttribute');
60
-
61
- modifiers.push(factory.createTypeReferenceNode(factory.createIdentifier('UniqueAttribute')));
60
+ modifiers.push(
61
+ factory.createTypeReferenceNode(factory.createIdentifier(withAttributeNamespace('Unique')))
62
+ );
62
63
  }
63
64
 
64
65
  // Configurable
65
66
  if (attribute.configurable) {
66
- addImport('ConfigurableAttribute');
67
-
68
67
  modifiers.push(
69
- factory.createTypeReferenceNode(factory.createIdentifier('ConfigurableAttribute'))
68
+ factory.createTypeReferenceNode(
69
+ factory.createIdentifier(withAttributeNamespace('Configurable'))
70
+ )
70
71
  );
71
72
  }
72
73
 
73
74
  // Custom field
74
75
  if (attribute.customField) {
75
- addImport('CustomField');
76
-
77
76
  const customFieldUid = factory.createStringLiteral(attribute.customField);
78
77
  const typeArguments = [customFieldUid];
79
78
 
@@ -82,17 +81,18 @@ const getAttributeModifiers = (attribute) => {
82
81
  }
83
82
 
84
83
  modifiers.push(
85
- factory.createTypeReferenceNode(factory.createIdentifier('CustomField'), typeArguments)
84
+ factory.createTypeReferenceNode(
85
+ factory.createIdentifier(withAttributeNamespace('CustomField')),
86
+ typeArguments
87
+ )
86
88
  );
87
89
  }
88
90
 
89
91
  // Plugin Options
90
92
  if (!_.isEmpty(attribute.pluginOptions)) {
91
- addImport('SetPluginOptions');
92
-
93
93
  modifiers.push(
94
94
  factory.createTypeReferenceNode(
95
- factory.createIdentifier('SetPluginOptions'),
95
+ factory.createIdentifier(withAttributeNamespace('SetPluginOptions')),
96
96
  // Transform the pluginOptions object into an object literal expression
97
97
  [toTypeLiteral(attribute.pluginOptions)]
98
98
  )
@@ -102,38 +102,37 @@ const getAttributeModifiers = (attribute) => {
102
102
  // Min / Max
103
103
  // TODO: Always provide a second type argument for min/max (ie: resolve the attribute scalar type with a `GetAttributeType<${mappers[attribute][0]}>` (useful for biginter (string values)))
104
104
  if (!_.isNil(attribute.min) || !_.isNil(attribute.max)) {
105
- addImport('SetMinMax');
106
-
107
105
  const minMaxProperties = _.pick(['min', 'max'], attribute);
108
106
 
109
107
  modifiers.push(
110
- factory.createTypeReferenceNode(factory.createIdentifier('SetMinMax'), [
111
- toTypeLiteral(minMaxProperties),
112
- ])
108
+ factory.createTypeReferenceNode(
109
+ factory.createIdentifier(withAttributeNamespace('SetMinMax')),
110
+ [toTypeLiteral(minMaxProperties)]
111
+ )
113
112
  );
114
113
  }
115
114
 
116
115
  // Min length / Max length
117
116
  if (!_.isNil(attribute.minLength) || !_.isNil(attribute.maxLength)) {
118
- addImport('SetMinMaxLength');
119
-
120
117
  const minMaxProperties = _.pick(['minLength', 'maxLength'], attribute);
121
118
 
122
119
  modifiers.push(
123
- factory.createTypeReferenceNode(factory.createIdentifier('SetMinMaxLength'), [
124
- toTypeLiteral(minMaxProperties),
125
- ])
120
+ factory.createTypeReferenceNode(
121
+ factory.createIdentifier(withAttributeNamespace('SetMinMaxLength')),
122
+ [toTypeLiteral(minMaxProperties)]
123
+ )
126
124
  );
127
125
  }
128
126
 
129
127
  // Default
130
128
  if (!_.isNil(attribute.default)) {
131
- addImport('DefaultTo');
132
-
133
129
  const defaultLiteral = toTypeLiteral(attribute.default);
134
130
 
135
131
  modifiers.push(
136
- factory.createTypeReferenceNode(factory.createIdentifier('DefaultTo'), [defaultLiteral])
132
+ factory.createTypeReferenceNode(
133
+ factory.createIdentifier(withAttributeNamespace('DefaultTo')),
134
+ [defaultLiteral]
135
+ )
137
136
  );
138
137
  }
139
138
 
@@ -0,0 +1,15 @@
1
+ 'use strict';
2
+
3
+ const schema = require('./schema');
4
+ const attributes = require('./attributes');
5
+ const mappers = require('./mappers');
6
+ const utils = require('./utils');
7
+ const imports = require('../imports');
8
+
9
+ module.exports = {
10
+ schema,
11
+ attributes,
12
+ mappers,
13
+ utils,
14
+ imports,
15
+ };