@strapi/typescript-utils 0.0.0-next.f45143c5e2a8a9d85691d0abf79a3f42024a0c71 → 0.0.0-next.f4ff842a3cb7b83db540bee67554b704e042b042

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.
@@ -4,71 +4,18 @@ const ts = require('typescript');
4
4
  const { factory } = require('typescript');
5
5
 
6
6
  const {
7
- getAllStrapiSchemas,
8
7
  getDefinitionAttributesCount,
9
8
  getSchemaExtendsTypeName,
10
9
  getSchemaInterfaceName,
11
10
  getSchemaModelType,
12
11
  getTypeNode,
13
12
  toTypeLiteral,
14
- } = require('../../../generators/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(
71
- undefined,
72
19
  undefined,
73
20
  factory.createIdentifier('Foo'),
74
21
  undefined,
@@ -79,7 +26,6 @@ describe('Utils', () => {
79
26
 
80
27
  const createPropertyDeclaration = (name, type) => {
81
28
  return factory.createPropertyDeclaration(
82
- undefined,
83
29
  undefined,
84
30
  factory.createIdentifier(name),
85
31
  undefined,
@@ -174,10 +120,10 @@ describe('Utils', () => {
174
120
 
175
121
  describe('Get Schema Extends Type Name', () => {
176
122
  test.each([
177
- [{ modelType: 'component', kind: null }, 'ComponentSchema'],
178
- [{ modelType: 'contentType', kind: 'singleType' }, 'SingleTypeSchema'],
179
- [{ modelType: 'contentType', kind: 'collectionType' }, 'CollectionTypeSchema'],
180
- [{ 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],
181
127
  ])("Expect %p to generate %p as the base type for a schema's interface", (schema, expected) => {
182
128
  expect(getSchemaExtendsTypeName(schema)).toBe(expected);
183
129
  });
@@ -2,25 +2,11 @@
2
2
 
3
3
  const path = require('path');
4
4
  const fs = require('fs-extra');
5
+ const adminTsConfig = require('../../tsconfigs/admin.json');
5
6
 
6
7
  module.exports = async (dest) => {
7
8
  const tsConfig = {
8
- compilerOptions: {
9
- lib: ['es2019', 'es2020.promise', 'es2020.bigint', 'es2020.string', 'DOM'],
10
- noImplicitAny: false,
11
- module: 'es2020',
12
- target: 'es5',
13
- jsx: 'react',
14
- allowJs: true,
15
- strict: true,
16
- moduleResolution: 'node',
17
- skipLibCheck: true,
18
- esModuleInterop: true,
19
- allowSyntheticDefaultImports: true,
20
- resolveJsonModule: true,
21
- noEmit: false,
22
- incremental: true,
23
- },
9
+ ...adminTsConfig,
24
10
  include: ['../../../src/admin/*', '../../../src/**/**/admin/src/*'],
25
11
  exclude: ['node_modules', '**/*.test.js', '*.js'],
26
12
  };
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
  };
@@ -22,12 +22,13 @@ module.exports = {
22
22
  factory.createImportSpecifier(false, undefined, factory.createIdentifier(key))
23
23
  );
24
24
 
25
- return factory.createImportDeclaration(
26
- undefined,
27
- undefined,
28
- factory.createImportClause(false, undefined, factory.createNamedImports(formattedImports)),
29
- factory.createStringLiteral('@strapi/strapi'),
30
- undefined
31
- );
25
+ return [
26
+ factory.createImportDeclaration(
27
+ undefined,
28
+ factory.createImportClause(true, undefined, factory.createNamedImports(formattedImports)),
29
+ factory.createStringLiteral('@strapi/strapi'),
30
+ undefined
31
+ ),
32
+ ];
32
33
  },
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 attribute namespace is imported
32
+ addImport(NAMESPACES.attribute);
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
154
  // Default
130
155
  if (!_.isNil(attribute.default)) {
131
- addImport('DefaultTo');
132
-
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
 
@@ -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
+ };
@@ -3,56 +3,56 @@
3
3
  const ts = require('typescript');
4
4
  const _ = require('lodash/fp');
5
5
 
6
- const { toTypeLiteral } = require('./utils');
6
+ const { toTypeLiteral, withAttributeNamespace } = require('./utils');
7
7
 
8
8
  const { factory } = ts;
9
9
 
10
10
  module.exports = {
11
11
  string() {
12
- return ['StringAttribute'];
12
+ return [withAttributeNamespace('String')];
13
13
  },
14
14
  text() {
15
- return ['TextAttribute'];
15
+ return [withAttributeNamespace('Text')];
16
16
  },
17
17
  richtext() {
18
- return ['RichTextAttribute'];
18
+ return [withAttributeNamespace('RichText')];
19
19
  },
20
20
  password() {
21
- return ['PasswordAttribute'];
21
+ return [withAttributeNamespace('Password')];
22
22
  },
23
23
  email() {
24
- return ['EmailAttribute'];
24
+ return [withAttributeNamespace('Email')];
25
25
  },
26
26
  date() {
27
- return ['DateAttribute'];
27
+ return [withAttributeNamespace('Date')];
28
28
  },
29
29
  time() {
30
- return ['TimeAttribute'];
30
+ return [withAttributeNamespace('Time')];
31
31
  },
32
32
  datetime() {
33
- return ['DateTimeAttribute'];
33
+ return [withAttributeNamespace('DateTime')];
34
34
  },
35
35
  timestamp() {
36
- return ['TimestampAttribute'];
36
+ return [withAttributeNamespace('Timestamp')];
37
37
  },
38
38
  integer() {
39
- return ['IntegerAttribute'];
39
+ return [withAttributeNamespace('Integer')];
40
40
  },
41
41
  biginteger() {
42
- return ['BigIntegerAttribute'];
42
+ return [withAttributeNamespace('BigInteger')];
43
43
  },
44
44
  float() {
45
- return ['FloatAttribute'];
45
+ return [withAttributeNamespace('Float')];
46
46
  },
47
47
  decimal() {
48
- return ['DecimalAttribute'];
48
+ return [withAttributeNamespace('Decimal')];
49
49
  },
50
50
  uid({ attribute, uid }) {
51
51
  const { targetField, options } = attribute;
52
52
 
53
53
  // If there are no params to compute, then return the attribute type alone
54
54
  if (targetField === undefined && options === undefined) {
55
- return ['UIDAttribute'];
55
+ return [withAttributeNamespace('UID')];
56
56
  }
57
57
 
58
58
  const params = [];
@@ -74,21 +74,42 @@ module.exports = {
74
74
  params.push(toTypeLiteral(options));
75
75
  }
76
76
 
77
- return ['UIDAttribute', params];
77
+ return [withAttributeNamespace('UID'), params];
78
78
  },
79
79
  enumeration({ attribute }) {
80
80
  const { enum: enumValues } = attribute;
81
81
 
82
- return ['EnumerationAttribute', [toTypeLiteral(enumValues)]];
82
+ return [withAttributeNamespace('Enumeration'), [toTypeLiteral(enumValues)]];
83
83
  },
84
84
  boolean() {
85
- return ['BooleanAttribute'];
85
+ return [withAttributeNamespace('Boolean')];
86
86
  },
87
87
  json() {
88
- return ['JSONAttribute'];
88
+ return [withAttributeNamespace('JSON')];
89
89
  },
90
- media() {
91
- return ['MediaAttribute'];
90
+ blocks() {
91
+ return [withAttributeNamespace('Blocks')];
92
+ },
93
+ media({ attribute }) {
94
+ const { allowedTypes, multiple } = attribute;
95
+
96
+ const params = [];
97
+
98
+ const typesParam = allowedTypes
99
+ ? factory.createUnionTypeNode(
100
+ allowedTypes.map((allowedType) => factory.createStringLiteral(allowedType))
101
+ )
102
+ : factory.createKeywordTypeNode(ts.SyntaxKind.UndefinedKeyword);
103
+
104
+ if (allowedTypes || multiple) {
105
+ params.push(typesParam);
106
+ }
107
+
108
+ if (multiple) {
109
+ params.push(factory.createTrue());
110
+ }
111
+
112
+ return [withAttributeNamespace('Media'), params];
92
113
  },
93
114
  relation({ uid, attribute }) {
94
115
  const { relation, target } = attribute;
@@ -97,13 +118,13 @@ module.exports = {
97
118
 
98
119
  if (isMorphRelation) {
99
120
  return [
100
- 'RelationAttribute',
121
+ withAttributeNamespace('Relation'),
101
122
  [factory.createStringLiteral(uid, true), factory.createStringLiteral(relation, true)],
102
123
  ];
103
124
  }
104
125
 
105
126
  return [
106
- 'RelationAttribute',
127
+ withAttributeNamespace('Relation'),
107
128
  [
108
129
  factory.createStringLiteral(uid, true),
109
130
  factory.createStringLiteral(relation, true),
@@ -119,13 +140,13 @@ module.exports = {
119
140
  params.push(factory.createTrue());
120
141
  }
121
142
 
122
- return ['ComponentAttribute', params];
143
+ return [withAttributeNamespace('Component'), params];
123
144
  },
124
145
  dynamiczone({ attribute }) {
125
146
  const componentsParam = factory.createTupleTypeNode(
126
147
  attribute.components.map((component) => factory.createStringLiteral(component))
127
148
  );
128
149
 
129
- return ['DynamicZoneAttribute', [componentsParam]];
150
+ return [withAttributeNamespace('DynamicZone'), [componentsParam]];
130
151
  },
131
152
  };
@@ -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
@@ -51,11 +56,11 @@ const generateSchemaDefinition = (schema) => {
51
56
  const interfaceName = getSchemaInterfaceName(uid);
52
57
  const parentType = getSchemaExtendsTypeName(schema);
53
58
 
54
- // Make sure the extended interface are imported
55
- addImport(parentType);
59
+ // Make sure the Schema namespace is imported
60
+ addImport(NAMESPACES.schema);
56
61
 
57
62
  // Properties whose values can be mapped to a literal type expression
58
- const literalPropertiesDefinitions = ['info', 'options', 'pluginOptions']
63
+ const literalPropertiesDefinitions = ['collectionName', 'info', 'options', 'pluginOptions']
59
64
  // Ignore non-existent or empty declarations
60
65
  .filter((key) => !isEmpty(schema[key]))
61
66
  // Generate literal definition for each property
@@ -69,7 +74,6 @@ const generateSchemaDefinition = (schema) => {
69
74
 
70
75
  // Generate the schema's interface declaration
71
76
  const schemaType = factory.createInterfaceDeclaration(
72
- undefined,
73
77
  [factory.createModifier(ts.SyntaxKind.ExportKeyword)],
74
78
  factory.createIdentifier(interfaceName),
75
79
  undefined,