@strapi/typescript-utils 0.0.0-next.c443fb2cf1a0b330fbf8f4bf6b967c3002ccbd92 → 0.0.0-next.c511e0f2d5c8549e150e6a69dda1a37589419a1b

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 +285 -140
  3. package/lib/__tests__/generators/schemas/imports.test.js +18 -16
  4. package/lib/__tests__/generators/schemas/utils.test.js +42 -88
  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 +36 -12
  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 -8
  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
@@ -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
@@ -17,13 +17,10 @@ const {
17
17
  propEq,
18
18
  } = require('lodash/fp');
19
19
 
20
- /**
21
- * Get all components and content-types in a Strapi application
22
- *
23
- * @param {Strapi} strapi
24
- * @returns {object}
25
- */
26
- const getAllStrapiSchemas = (strapi) => ({ ...strapi.contentTypes, ...strapi.components });
20
+ const NAMESPACES = {
21
+ Struct: 'Struct',
22
+ Schema: 'Schema',
23
+ };
27
24
 
28
25
  /**
29
26
  * Extract a valid interface name from a schema uid
@@ -53,12 +50,16 @@ const getSchemaModelType = (schema) => {
53
50
  * Get the parent type name to extend based on the schema's nature
54
51
  *
55
52
  * @param {object} schema
56
- * @returns {string}
53
+ * @returns {string|null}
57
54
  */
58
55
  const getSchemaExtendsTypeName = (schema) => {
59
56
  const base = getSchemaModelType(schema);
60
57
 
61
- return `${upperFirst(base)}Schema`;
58
+ if (base === null) {
59
+ return null;
60
+ }
61
+
62
+ return `${NAMESPACES.Struct}.${upperFirst(base)}Schema`;
62
63
  };
63
64
 
64
65
  /**
@@ -91,7 +92,12 @@ const toTypeLiteral = (data) => {
91
92
  }
92
93
 
93
94
  if (isNumber(data)) {
94
- return factory.createNumericLiteral(data);
95
+ return data < 0
96
+ ? factory.createPrefixUnaryExpression(
97
+ ts.SyntaxKind.MinusToken,
98
+ factory.createNumericLiteral(-data)
99
+ )
100
+ : factory.createNumericLiteral(data);
95
101
  }
96
102
 
97
103
  if (isBoolean(data)) {
@@ -110,7 +116,7 @@ const toTypeLiteral = (data) => {
110
116
  throw new Error(`Cannot convert to object literal. Unknown type "${typeof data}"`);
111
117
  }
112
118
 
113
- const entries = Object.entries(data);
119
+ const entries = Object.entries(data).sort((a, b) => a[0].localeCompare(b[0]));
114
120
 
115
121
  const props = entries.reduce((acc, [key, value]) => {
116
122
  // Handle keys such as content-type-builder & co.
@@ -143,8 +149,26 @@ const getDefinitionAttributesCount = (definition) => {
143
149
  return attributesNode.type.members.length;
144
150
  };
145
151
 
152
+ /**
153
+ * Add the Schema.Attribute namespace before the typename
154
+ *
155
+ * @param {string} typeName
156
+ * @returns {string}
157
+ */
158
+ const withAttributeNamespace = (typeName) => `${NAMESPACES.Schema}.Attribute.${typeName}`;
159
+
160
+ /**
161
+ * Add the schema namespace before the typename
162
+ *
163
+ * @param {string} typeName
164
+ * @returns {string}
165
+ */
166
+ const withSchemaNamespace = (typeName) => `${NAMESPACES.schema}.${typeName}`;
167
+
146
168
  module.exports = {
147
- getAllStrapiSchemas,
169
+ NAMESPACES,
170
+ withAttributeNamespace,
171
+ withSchemaNamespace,
148
172
  getSchemaInterfaceName,
149
173
  getSchemaExtendsTypeName,
150
174
  getSchemaModelType,
@@ -0,0 +1,74 @@
1
+ 'use strict';
2
+
3
+ const { factory } = require('typescript');
4
+ const { pipe, values, sortBy, map } = require('lodash/fp');
5
+
6
+ const { models } = require('../common');
7
+ const { emitDefinitions, format, generateSharedExtensionDefinition } = require('../utils');
8
+
9
+ const NO_COMPONENT_PLACEHOLDER_COMMENT = `/*
10
+ * The app doesn't have any components yet.
11
+ */
12
+ `;
13
+
14
+ /**
15
+ * Generate type definitions for Strapi Components
16
+ *
17
+ * @param {object} [options]
18
+ * @param {object} options.strapi
19
+ * @param {object} options.logger
20
+ * @param {string} options.pwd
21
+ */
22
+ const generateComponentsDefinitions = async (options = {}) => {
23
+ const { strapi } = options;
24
+
25
+ const { components } = strapi;
26
+
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
+ }
41
+
42
+ const formattedSchemasDefinitions = componentsDefinitions.reduce((acc, def) => {
43
+ acc.push(
44
+ // Definition
45
+ def.definition,
46
+
47
+ // Add a newline between each interface declaration
48
+ factory.createIdentifier('\n')
49
+ );
50
+
51
+ return acc;
52
+ }, []);
53
+
54
+ const allDefinitions = [
55
+ // Imports
56
+ ...models.imports.generateImportDefinition(),
57
+
58
+ // Add a newline after the import statement
59
+ factory.createIdentifier('\n'),
60
+
61
+ // Schemas
62
+ ...formattedSchemasDefinitions,
63
+
64
+ // Global
65
+ generateSharedExtensionDefinition('ComponentSchemas', componentsDefinitions),
66
+ ];
67
+
68
+ const output = emitDefinitions(allDefinitions);
69
+ const formattedOutput = await format(output);
70
+
71
+ return { output: formattedOutput, stats: {} };
72
+ };
73
+
74
+ module.exports = generateComponentsDefinitions;
@@ -0,0 +1,6 @@
1
+ 'use strict';
2
+
3
+ const TYPES_ROOT_DIR = 'types';
4
+ const GENERATED_OUT_DIR = 'generated';
5
+
6
+ module.exports = { GENERATED_OUT_DIR, TYPES_ROOT_DIR };
@@ -0,0 +1,74 @@
1
+ 'use strict';
2
+
3
+ const { factory } = require('typescript');
4
+ const { values, pipe, map, sortBy } = require('lodash/fp');
5
+
6
+ const { models } = require('../common');
7
+ const { emitDefinitions, format, generateSharedExtensionDefinition } = require('../utils');
8
+
9
+ const NO_CONTENT_TYPE_PLACEHOLDER_COMMENT = `/*
10
+ * The app doesn't have any content-types yet.
11
+ */
12
+ `;
13
+
14
+ /**
15
+ * Generate type definitions for Strapi Content-Types
16
+ *
17
+ * @param {object} [options]
18
+ * @param {object} options.strapi
19
+ * @param {object} options.logger
20
+ * @param {string} options.pwd
21
+ */
22
+ const generateContentTypesDefinitions = async (options = {}) => {
23
+ const { strapi } = options;
24
+
25
+ const { contentTypes } = strapi;
26
+
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
+ }
41
+
42
+ const formattedSchemasDefinitions = contentTypesDefinitions.reduce((acc, def) => {
43
+ acc.push(
44
+ // Definition
45
+ def.definition,
46
+
47
+ // Add a newline between each interface declaration
48
+ factory.createIdentifier('\n')
49
+ );
50
+
51
+ return acc;
52
+ }, []);
53
+
54
+ const allDefinitions = [
55
+ // Imports
56
+ ...models.imports.generateImportDefinition(),
57
+
58
+ // Add a newline after the import statement
59
+ factory.createIdentifier('\n'),
60
+
61
+ // Schemas
62
+ ...formattedSchemasDefinitions,
63
+
64
+ // Global
65
+ generateSharedExtensionDefinition('ContentTypeSchemas', contentTypesDefinitions),
66
+ ];
67
+
68
+ const output = emitDefinitions(allDefinitions);
69
+ const formattedOutput = await format(output);
70
+
71
+ return { output: formattedOutput, stats: {} };
72
+ };
73
+
74
+ module.exports = generateContentTypesDefinitions;
@@ -1,7 +1,122 @@
1
1
  'use strict';
2
2
 
3
- const generateSchemasDefinitions = require('./schemas');
3
+ const path = require('path');
4
+ const chalk = require('chalk');
4
5
 
5
- module.exports = {
6
- generateSchemasDefinitions,
6
+ const { TYPES_ROOT_DIR, GENERATED_OUT_DIR } = require('./constants');
7
+ const { saveDefinitionToFileSystem, createLogger, timer } = require('./utils');
8
+ const generateContentTypesDefinitions = require('./content-types');
9
+ const generateComponentsDefinitions = require('./components');
10
+
11
+ const GENERATORS = {
12
+ contentTypes: generateContentTypesDefinitions,
13
+ components: generateComponentsDefinitions,
7
14
  };
15
+
16
+ /**
17
+ * @typedef GenerateConfig
18
+ *
19
+ * @property {object} strapi
20
+ * @property {boolean} pwd
21
+ * @property {object} [artifacts]
22
+ * @property {boolean} [artifacts.contentTypes]
23
+ * @property {boolean} [artifacts.components]
24
+ * @property {boolean} [artifacts.services]
25
+ * @property {boolean} [artifacts.controllers]
26
+ * @property {boolean} [artifacts.policies]
27
+ * @property {boolean} [artifacts.middlewares]
28
+ * @property {object} [logger]
29
+ * @property {boolean} [logger.silent]
30
+ * @property {boolean} [logger.debug]
31
+ * @property {boolean} [logger.verbose]
32
+ */
33
+
34
+ /**
35
+ * Generate types definitions based on the given configuration
36
+ *
37
+ * @param {GenerateConfig} [config]
38
+ */
39
+ const generate = async (config = {}) => {
40
+ const { pwd, rootDir = TYPES_ROOT_DIR, strapi, artifacts = {}, logger: loggerConfig } = config;
41
+ const reports = {};
42
+ const logger = createLogger(loggerConfig);
43
+ const psTimer = timer().start();
44
+
45
+ const registryPwd = path.join(pwd, rootDir, GENERATED_OUT_DIR);
46
+ const generatorConfig = { strapi, pwd: registryPwd, logger };
47
+
48
+ const returnWithMessage = () => {
49
+ const nbWarnings = chalk.yellow(`${logger.warnings} warning(s)`);
50
+ const nbErrors = chalk.red(`${logger.errors} error(s)`);
51
+
52
+ const status = logger.errors > 0 ? chalk.red('errored') : chalk.green('completed successfully');
53
+
54
+ psTimer.end();
55
+
56
+ logger.info(`The task ${status} with ${nbWarnings} and ${nbErrors} in ${psTimer.duration}s.`);
57
+
58
+ return reports;
59
+ };
60
+
61
+ const enabledArtifacts = Object.keys(artifacts).filter((p) => artifacts[p] === true);
62
+
63
+ logger.info('Starting the type generation process');
64
+ logger.debug(`Enabled artifacts: ${enabledArtifacts.join(', ')}`);
65
+
66
+ for (const artifact of enabledArtifacts) {
67
+ const boldArtifact = chalk.bold(artifact); // used for log messages
68
+
69
+ logger.info(`Generating types for ${boldArtifact}`);
70
+
71
+ if (artifact in GENERATORS) {
72
+ const generator = GENERATORS[artifact];
73
+
74
+ try {
75
+ const artifactGenTimer = timer().start();
76
+
77
+ reports[artifact] = await generator(generatorConfig);
78
+
79
+ artifactGenTimer.end();
80
+
81
+ logger.debug(`Generated ${boldArtifact} in ${artifactGenTimer.duration}s`);
82
+ } catch (e) {
83
+ logger.error(
84
+ `Failed to generate types for ${boldArtifact}: ${e.message ?? e.toString()}. Exiting`
85
+ );
86
+ return returnWithMessage();
87
+ }
88
+ } else {
89
+ logger.warn(`The types generator for ${boldArtifact} is not implemented, skipping`);
90
+ }
91
+ }
92
+
93
+ for (const artifact of Object.keys(reports)) {
94
+ const boldArtifact = chalk.bold(artifact); // used for log messages
95
+
96
+ const artifactFsTimer = timer().start();
97
+
98
+ const report = reports[artifact];
99
+ const filename = `${artifact}.d.ts`;
100
+
101
+ try {
102
+ const outPath = await saveDefinitionToFileSystem(registryPwd, filename, report.output);
103
+ const relativeOutPath = path.relative(process.cwd(), outPath);
104
+
105
+ artifactFsTimer.end();
106
+
107
+ logger.info(`Saved ${boldArtifact} types in ${chalk.bold(relativeOutPath)}`);
108
+ logger.debug(`Saved ${boldArtifact} in ${artifactFsTimer.duration}s`);
109
+ } catch (e) {
110
+ logger.error(
111
+ `An error occurred while saving ${boldArtifact} types to the filesystem: ${
112
+ e.message ?? e.toString()
113
+ }. Exiting`
114
+ );
115
+ return returnWithMessage();
116
+ }
117
+ }
118
+
119
+ return returnWithMessage();
120
+ };
121
+
122
+ module.exports = { generate };