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

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.
Files changed (33) hide show
  1. package/LICENSE +18 -3
  2. package/lib/__tests__/generators/schemas/attributes.test.js +273 -123
  3. package/lib/__tests__/generators/schemas/imports.test.js +18 -16
  4. package/lib/__tests__/generators/schemas/utils.test.js +5 -57
  5. package/lib/compile.js +2 -6
  6. package/lib/compilers/basic.js +12 -4
  7. package/lib/compilers/index.js +0 -2
  8. package/lib/generators/common/imports.js +34 -0
  9. package/lib/generators/common/index.js +9 -0
  10. package/lib/generators/{schemas → common/models}/attributes.js +65 -41
  11. package/lib/generators/common/models/index.js +15 -0
  12. package/lib/generators/common/models/mappers.js +144 -0
  13. package/lib/generators/{schemas → common/models}/schema.js +15 -8
  14. package/lib/generators/{schemas → common/models}/utils.js +30 -11
  15. package/lib/generators/components/index.js +74 -0
  16. package/lib/generators/constants.js +6 -0
  17. package/lib/generators/content-types/index.js +74 -0
  18. package/lib/generators/index.js +118 -3
  19. package/lib/generators/utils.js +216 -0
  20. package/lib/index.js +0 -3
  21. package/lib/utils/index.js +2 -0
  22. package/lib/utils/resolve-outdir-sync.js +18 -0
  23. package/package.json +15 -7
  24. package/tsconfigs/admin.json +18 -19
  25. package/tsconfigs/server.json +18 -16
  26. package/lib/__tests__/generators/schemas/global.test.js +0 -108
  27. package/lib/admin/create-tsconfig-file.js +0 -37
  28. package/lib/admin/index.js +0 -5
  29. package/lib/compilers/watch.js +0 -37
  30. package/lib/generators/schemas/global.js +0 -67
  31. package/lib/generators/schemas/imports.js +0 -32
  32. package/lib/generators/schemas/index.js +0 -185
  33. package/lib/generators/schemas/mappers.js +0 -131
@@ -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 }, 'Struct.ComponentSchema'],
124
+ [{ modelType: 'contentType', kind: 'singleType' }, 'Struct.SingleTypeSchema'],
125
+ [{ modelType: 'contentType', kind: 'collectionType' }, 'Struct.CollectionTypeSchema'],
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
  });
package/lib/compile.js CHANGED
@@ -3,12 +3,8 @@
3
3
  const compilers = require('./compilers');
4
4
  const getConfigPath = require('./utils/get-config-path');
5
5
 
6
- module.exports = async (srcDir, { watch = false, configOptions = {} } = {}) => {
7
- // TODO: Use the Strapi debug logger instead or don't log at all
8
- console.log(`Starting the compilation for TypeScript files in ${srcDir}`);
9
-
10
- const compiler = watch ? compilers.watch : compilers.basic;
6
+ module.exports = async (srcDir, { configOptions = {} } = {}) => {
11
7
  const configPath = getConfigPath(srcDir);
12
8
 
13
- compiler.run(configPath, configOptions);
9
+ compilers.basic.run(configPath, configOptions);
14
10
  };
@@ -13,15 +13,23 @@ module.exports = {
13
13
  * @param {Object} configOptions
14
14
  * @param {Array.<string>} configOptions.fileNames
15
15
  * @param {Object} configOptions.options
16
+ * @param {boolean} configOptions.ignoreDiagnostics
16
17
  */
17
18
  run(tsConfigPath, configOptions = {}) {
19
+ const { ignoreDiagnostics = false } = configOptions;
18
20
  // Parse the tsconfig.json file & resolve the configuration options
19
21
  const { fileNames, options, projectReferences } = resolveConfigOptions(tsConfigPath);
20
22
 
23
+ const compilerOptions = merge(options, configOptions.options);
24
+
25
+ if (ignoreDiagnostics) {
26
+ Object.assign(compilerOptions, { noEmit: false, noEmitOnError: false });
27
+ }
28
+
21
29
  const program = ts.createProgram({
22
30
  rootNames: configOptions.fileNames ? configOptions.fileNames : fileNames,
23
31
  projectReferences,
24
- options: merge(options, configOptions.options),
32
+ options: compilerOptions,
25
33
  });
26
34
 
27
35
  const emitResults = program.emit();
@@ -30,12 +38,12 @@ module.exports = {
30
38
  ts.getPreEmitDiagnostics(program).concat(emitResults.diagnostics)
31
39
  );
32
40
 
33
- if (diagnostics.length > 0) {
41
+ if (!ignoreDiagnostics && diagnostics.length > 0) {
34
42
  reportDiagnostics(diagnostics);
35
43
  }
36
44
 
37
- // If the compilation failed, exit early
38
- if (emitResults.emitSkipped) {
45
+ // If the compilation failed and diagnostics are not ignored, exit early
46
+ if (!ignoreDiagnostics && emitResults.emitSkipped) {
39
47
  process.exit(1);
40
48
  }
41
49
  },
@@ -1,9 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  const basic = require('./basic');
4
- const watch = require('./watch');
5
4
 
6
5
  module.exports = {
7
6
  basic,
8
- watch,
9
7
  };
@@ -0,0 +1,34 @@
1
+ 'use strict';
2
+
3
+ const { factory } = require('typescript');
4
+
5
+ const imports = [];
6
+
7
+ module.exports = {
8
+ getImports() {
9
+ return imports;
10
+ },
11
+
12
+ addImport(type) {
13
+ const hasType = imports.includes(type);
14
+
15
+ if (!hasType) {
16
+ imports.push(type);
17
+ }
18
+ },
19
+
20
+ generateImportDefinition() {
21
+ const formattedImports = imports
22
+ .sort()
23
+ .map((key) => factory.createImportSpecifier(false, undefined, factory.createIdentifier(key)));
24
+
25
+ return [
26
+ factory.createImportDeclaration(
27
+ undefined,
28
+ factory.createImportClause(true, undefined, factory.createNamedImports(formattedImports)),
29
+ factory.createStringLiteral('@strapi/strapi'),
30
+ undefined
31
+ ),
32
+ ];
33
+ },
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
+ };
@@ -1,12 +1,14 @@
1
1
  'use strict';
2
2
 
3
- const { factory } = require('typescript');
3
+ const ts = 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
+ const { factory } = ts;
11
+
10
12
  /**
11
13
  * Create the base type node for a given attribute
12
14
  *
@@ -26,7 +28,8 @@ const getAttributeType = (attributeName, attribute, uid) => {
26
28
 
27
29
  const [attributeType, typeParams] = mappers[attribute.type]({ uid, attribute, attributeName });
28
30
 
29
- addImport(attributeType);
31
+ // Make sure the schema namespace is imported
32
+ addImport(NAMESPACES.Schema);
30
33
 
31
34
  return getTypeNode(attributeType, typeParams);
32
35
  };
@@ -42,38 +45,36 @@ const getAttributeModifiers = (attribute) => {
42
45
 
43
46
  // Required
44
47
  if (attribute.required) {
45
- addImport('RequiredAttribute');
46
-
47
- modifiers.push(factory.createTypeReferenceNode(factory.createIdentifier('RequiredAttribute')));
48
+ modifiers.push(
49
+ factory.createTypeReferenceNode(factory.createIdentifier(withAttributeNamespace('Required')))
50
+ );
48
51
  }
49
52
 
50
53
  // Private
51
54
  if (attribute.private) {
52
- addImport('PrivateAttribute');
53
-
54
- modifiers.push(factory.createTypeReferenceNode(factory.createIdentifier('PrivateAttribute')));
55
+ modifiers.push(
56
+ factory.createTypeReferenceNode(factory.createIdentifier(withAttributeNamespace('Private')))
57
+ );
55
58
  }
56
59
 
57
60
  // Unique
58
61
  if (attribute.unique) {
59
- addImport('UniqueAttribute');
60
-
61
- modifiers.push(factory.createTypeReferenceNode(factory.createIdentifier('UniqueAttribute')));
62
+ modifiers.push(
63
+ factory.createTypeReferenceNode(factory.createIdentifier(withAttributeNamespace('Unique')))
64
+ );
62
65
  }
63
66
 
64
67
  // Configurable
65
68
  if (attribute.configurable) {
66
- addImport('ConfigurableAttribute');
67
-
68
69
  modifiers.push(
69
- factory.createTypeReferenceNode(factory.createIdentifier('ConfigurableAttribute'))
70
+ factory.createTypeReferenceNode(
71
+ factory.createIdentifier(withAttributeNamespace('Configurable'))
72
+ )
70
73
  );
71
74
  }
72
75
 
73
76
  // Custom field
74
77
  if (attribute.customField) {
75
- addImport('CustomField');
76
-
77
78
  const customFieldUid = factory.createStringLiteral(attribute.customField);
78
79
  const typeArguments = [customFieldUid];
79
80
 
@@ -82,17 +83,18 @@ const getAttributeModifiers = (attribute) => {
82
83
  }
83
84
 
84
85
  modifiers.push(
85
- factory.createTypeReferenceNode(factory.createIdentifier('CustomField'), typeArguments)
86
+ factory.createTypeReferenceNode(
87
+ factory.createIdentifier(withAttributeNamespace('CustomField')),
88
+ typeArguments
89
+ )
86
90
  );
87
91
  }
88
92
 
89
93
  // Plugin Options
90
94
  if (!_.isEmpty(attribute.pluginOptions)) {
91
- addImport('SetPluginOptions');
92
-
93
95
  modifiers.push(
94
96
  factory.createTypeReferenceNode(
95
- factory.createIdentifier('SetPluginOptions'),
97
+ factory.createIdentifier(withAttributeNamespace('SetPluginOptions')),
96
98
  // Transform the pluginOptions object into an object literal expression
97
99
  [toTypeLiteral(attribute.pluginOptions)]
98
100
  )
@@ -100,40 +102,64 @@ const getAttributeModifiers = (attribute) => {
100
102
  }
101
103
 
102
104
  // Min / Max
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
105
  if (!_.isNil(attribute.min) || !_.isNil(attribute.max)) {
105
- addImport('SetMinMax');
106
-
107
106
  const minMaxProperties = _.pick(['min', 'max'], attribute);
107
+ const { min, max } = minMaxProperties;
108
+
109
+ const typeofMin = typeof min;
110
+ const typeofMax = typeof max;
111
+
112
+ // Throws error if min/max exist but have different types to prevent unexpected behavior
113
+ if (min !== undefined && max !== undefined && typeofMin !== typeofMax) {
114
+ throw new Error('typeof min/max values mismatch');
115
+ }
116
+
117
+ let typeKeyword;
118
+
119
+ // use 'string'
120
+ if (typeofMin === 'string' || typeofMax === 'string') {
121
+ typeKeyword = ts.SyntaxKind.StringKeyword;
122
+ }
123
+ // use 'number'
124
+ else if (typeofMin === 'number' || typeofMax === 'number') {
125
+ typeKeyword = ts.SyntaxKind.NumberKeyword;
126
+ }
127
+ // invalid type
128
+ else {
129
+ throw new Error(
130
+ `Invalid data type for min/max options. Must be string, number or undefined, but found { min: ${min} (${typeofMin}), max: ${max} (${typeofMax}) }`
131
+ );
132
+ }
108
133
 
109
134
  modifiers.push(
110
- factory.createTypeReferenceNode(factory.createIdentifier('SetMinMax'), [
111
- toTypeLiteral(minMaxProperties),
112
- ])
135
+ factory.createTypeReferenceNode(
136
+ factory.createIdentifier(withAttributeNamespace('SetMinMax')),
137
+ [toTypeLiteral(minMaxProperties), factory.createKeywordTypeNode(typeKeyword)]
138
+ )
113
139
  );
114
140
  }
115
141
 
116
142
  // Min length / Max length
117
143
  if (!_.isNil(attribute.minLength) || !_.isNil(attribute.maxLength)) {
118
- addImport('SetMinMaxLength');
119
-
120
144
  const minMaxProperties = _.pick(['minLength', 'maxLength'], attribute);
121
145
 
122
146
  modifiers.push(
123
- factory.createTypeReferenceNode(factory.createIdentifier('SetMinMaxLength'), [
124
- toTypeLiteral(minMaxProperties),
125
- ])
147
+ factory.createTypeReferenceNode(
148
+ factory.createIdentifier(withAttributeNamespace('SetMinMaxLength')),
149
+ [toTypeLiteral(minMaxProperties)]
150
+ )
126
151
  );
127
152
  }
128
153
 
129
- // Default
130
- if (!_.isNil(attribute.default)) {
131
- addImport('DefaultTo');
132
-
154
+ // Default (ignore if default is a function)
155
+ if (!_.isNil(attribute.default) && !_.isFunction(attribute.default)) {
133
156
  const defaultLiteral = toTypeLiteral(attribute.default);
134
157
 
135
158
  modifiers.push(
136
- factory.createTypeReferenceNode(factory.createIdentifier('DefaultTo'), [defaultLiteral])
159
+ factory.createTypeReferenceNode(
160
+ factory.createIdentifier(withAttributeNamespace('DefaultTo')),
161
+ [defaultLiteral]
162
+ )
137
163
  );
138
164
  }
139
165
 
@@ -169,6 +195,4 @@ const attributeToPropertySignature = (schema, attributeName, attribute) => {
169
195
 
170
196
  module.exports = attributeToPropertySignature;
171
197
 
172
- module.exports.mappers = mappers;
173
- module.exports.getAttributeType = getAttributeType;
174
- module.exports.getAttributeModifiers = getAttributeModifiers;
198
+ Object.assign(module.exports, { mappers, getAttributeModifiers, getAttributeType });
@@ -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
+ };
@@ -0,0 +1,144 @@
1
+ 'use strict';
2
+
3
+ const ts = require('typescript');
4
+ const _ = require('lodash/fp');
5
+
6
+ const { toTypeLiteral, withAttributeNamespace } = require('./utils');
7
+
8
+ const { factory } = ts;
9
+
10
+ module.exports = {
11
+ string() {
12
+ return [withAttributeNamespace('String')];
13
+ },
14
+ text() {
15
+ return [withAttributeNamespace('Text')];
16
+ },
17
+ richtext() {
18
+ return [withAttributeNamespace('RichText')];
19
+ },
20
+ password() {
21
+ return [withAttributeNamespace('Password')];
22
+ },
23
+ email() {
24
+ return [withAttributeNamespace('Email')];
25
+ },
26
+ date() {
27
+ return [withAttributeNamespace('Date')];
28
+ },
29
+ time() {
30
+ return [withAttributeNamespace('Time')];
31
+ },
32
+ datetime() {
33
+ return [withAttributeNamespace('DateTime')];
34
+ },
35
+ timestamp() {
36
+ return [withAttributeNamespace('Timestamp')];
37
+ },
38
+ integer() {
39
+ return [withAttributeNamespace('Integer')];
40
+ },
41
+ biginteger() {
42
+ return [withAttributeNamespace('BigInteger')];
43
+ },
44
+ float() {
45
+ return [withAttributeNamespace('Float')];
46
+ },
47
+ decimal() {
48
+ return [withAttributeNamespace('Decimal')];
49
+ },
50
+ uid({ attribute }) {
51
+ const { targetField, options } = attribute;
52
+
53
+ // If there are no params to compute, then return the attribute type alone
54
+ if (targetField === undefined && options === undefined) {
55
+ return [withAttributeNamespace('UID')];
56
+ }
57
+
58
+ const params = [];
59
+
60
+ // If the targetField property is defined, then reference it,
61
+ // otherwise, put `undefined` keyword type node as placeholder
62
+ const targetFieldParam = _.isUndefined(targetField)
63
+ ? factory.createKeywordTypeNode(ts.SyntaxKind.UndefinedKeyword)
64
+ : factory.createStringLiteral(targetField);
65
+
66
+ params.push(targetFieldParam);
67
+
68
+ // If the options property is defined, transform it to
69
+ // a type literal node and add it to the params list
70
+ if (_.isObject(options)) {
71
+ params.push(toTypeLiteral(options));
72
+ }
73
+
74
+ return [withAttributeNamespace('UID'), params];
75
+ },
76
+ enumeration({ attribute }) {
77
+ const { enum: enumValues } = attribute;
78
+
79
+ return [withAttributeNamespace('Enumeration'), [toTypeLiteral(enumValues)]];
80
+ },
81
+ boolean() {
82
+ return [withAttributeNamespace('Boolean')];
83
+ },
84
+ json() {
85
+ return [withAttributeNamespace('JSON')];
86
+ },
87
+ blocks() {
88
+ return [withAttributeNamespace('Blocks')];
89
+ },
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];
110
+ },
111
+ relation({ attribute }) {
112
+ const { relation, target } = attribute;
113
+
114
+ const isMorphRelation = relation.toLowerCase().includes('morph');
115
+
116
+ if (isMorphRelation) {
117
+ return [withAttributeNamespace('Relation'), [factory.createStringLiteral(relation, true)]];
118
+ }
119
+
120
+ return [
121
+ withAttributeNamespace('Relation'),
122
+ [factory.createStringLiteral(relation, true), factory.createStringLiteral(target, true)],
123
+ ];
124
+ },
125
+ component({ attribute }) {
126
+ const target = attribute.component;
127
+ const params = [factory.createStringLiteral(target, true)];
128
+
129
+ if (attribute.repeatable) {
130
+ params.push(factory.createTrue());
131
+ } else {
132
+ params.push(factory.createFalse());
133
+ }
134
+
135
+ return [withAttributeNamespace('Component'), params];
136
+ },
137
+ dynamiczone({ attribute }) {
138
+ const componentsParam = factory.createTupleTypeNode(
139
+ attribute.components.map((component) => factory.createStringLiteral(component))
140
+ );
141
+
142
+ return [withAttributeNamespace('DynamicZone'), [componentsParam]];
143
+ },
144
+ };
@@ -4,9 +4,14 @@ const ts = require('typescript');
4
4
  const { factory } = require('typescript');
5
5
  const { isEmpty } = require('lodash/fp');
6
6
 
7
- const { getSchemaExtendsTypeName, getSchemaInterfaceName, toTypeLiteral } = require('./utils');
7
+ const {
8
+ getSchemaExtendsTypeName,
9
+ getSchemaInterfaceName,
10
+ toTypeLiteral,
11
+ NAMESPACES,
12
+ } = require('./utils');
8
13
  const attributeToPropertySignature = require('./attributes');
9
- const { addImport } = require('./imports');
14
+ const { addImport } = require('../imports');
10
15
 
11
16
  /**
12
17
  * Generate a property signature for the schema's `attributes` field
@@ -17,9 +22,11 @@ const { addImport } = require('./imports');
17
22
  const generateAttributePropertySignature = (schema) => {
18
23
  const { attributes } = schema;
19
24
 
20
- const properties = Object.entries(attributes).map(([attributeName, attribute]) => {
21
- return attributeToPropertySignature(schema, attributeName, attribute);
22
- });
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
+ });
23
30
 
24
31
  return factory.createPropertySignature(
25
32
  undefined,
@@ -51,11 +58,11 @@ const generateSchemaDefinition = (schema) => {
51
58
  const interfaceName = getSchemaInterfaceName(uid);
52
59
  const parentType = getSchemaExtendsTypeName(schema);
53
60
 
54
- // Make sure the extended interface are imported
55
- addImport(parentType);
61
+ // Make sure the Struct namespace is imported
62
+ addImport(NAMESPACES.Struct);
56
63
 
57
64
  // Properties whose values can be mapped to a literal type expression
58
- const literalPropertiesDefinitions = ['info', 'options', 'pluginOptions']
65
+ const literalPropertiesDefinitions = ['collectionName', 'info', 'options', 'pluginOptions']
59
66
  // Ignore non-existent or empty declarations
60
67
  .filter((key) => !isEmpty(schema[key]))
61
68
  // Generate literal definition for each property