@strapi/typescript-utils 0.0.0-next.f45143c5e2a8a9d85691d0abf79a3f42024a0c71 → 0.0.0-next.f4ec69568d980c6fee91ce2ee0f41c138347aa81
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintignore +1 -0
- package/LICENSE +18 -3
- package/index.d.ts +5 -0
- package/jest.config.js +1 -0
- package/lib/__tests__/generators/schemas/attributes.test.js +273 -123
- package/lib/__tests__/generators/schemas/imports.test.js +18 -16
- package/lib/__tests__/generators/schemas/utils.test.js +5 -59
- package/lib/compile.js +2 -6
- package/lib/compilers/basic.js +12 -4
- package/lib/compilers/index.js +0 -2
- package/lib/generators/common/imports.js +34 -0
- package/lib/generators/common/index.js +9 -0
- package/lib/generators/{schemas → common/models}/attributes.js +65 -41
- package/lib/generators/common/models/index.js +15 -0
- package/lib/generators/common/models/mappers.js +144 -0
- package/lib/generators/{schemas → common/models}/schema.js +15 -9
- package/lib/generators/{schemas → common/models}/utils.js +31 -18
- package/lib/generators/components/index.js +74 -0
- package/lib/generators/constants.js +6 -0
- package/lib/generators/content-types/index.js +74 -0
- package/lib/generators/index.js +118 -3
- package/lib/generators/utils.js +216 -0
- package/lib/index.js +0 -3
- package/package.json +16 -7
- package/tsconfigs/admin.json +18 -19
- package/tsconfigs/server.json +18 -16
- package/lib/__tests__/generators/schemas/global.test.js +0 -108
- package/lib/admin/create-tsconfig-file.js +0 -37
- package/lib/admin/index.js +0 -5
- package/lib/compilers/watch.js +0 -37
- package/lib/generators/schemas/global.js +0 -70
- package/lib/generators/schemas/imports.js +0 -33
- package/lib/generators/schemas/index.js +0 -185
- package/lib/generators/schemas/mappers.js +0 -131
package/tsconfigs/server.json
CHANGED
|
@@ -1,19 +1,21 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
"compilerOptions": {
|
|
5
|
-
"module": "CommonJS",
|
|
6
|
-
"moduleResolution": "Node",
|
|
7
|
-
"lib": ["ES2020"],
|
|
8
|
-
"target": "ES2019",
|
|
2
|
+
"$schema": "https://json.schemastore.org/tsconfig",
|
|
9
3
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
4
|
+
"compilerOptions": {
|
|
5
|
+
"module": "CommonJS",
|
|
6
|
+
"moduleResolution": "Node",
|
|
7
|
+
"lib": ["ES2020"],
|
|
8
|
+
"target": "ES2019",
|
|
13
9
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
10
|
+
"strict": false,
|
|
11
|
+
"skipLibCheck": true,
|
|
12
|
+
"forceConsistentCasingInFileNames": true,
|
|
13
|
+
|
|
14
|
+
"tsBuildInfoFile": "./.tsbuildinfo",
|
|
15
|
+
"incremental": true,
|
|
16
|
+
"esModuleInterop": true,
|
|
17
|
+
"resolveJsonModule": true,
|
|
18
|
+
"noEmitOnError": true,
|
|
19
|
+
"noImplicitThis": true
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
jest.mock('../../../generators/schemas/utils', () => ({
|
|
4
|
-
getSchemaInterfaceName: jest.fn(),
|
|
5
|
-
}));
|
|
6
|
-
|
|
7
|
-
const ts = require('typescript');
|
|
8
|
-
const { get } = require('lodash/fp');
|
|
9
|
-
|
|
10
|
-
const { generateGlobalDefinition } = require('../../../generators/schemas/global');
|
|
11
|
-
const { getSchemaInterfaceName } = require('../../../generators/schemas/utils');
|
|
12
|
-
|
|
13
|
-
const getSchemasInterfaceNode = get('body.statements[0].body.statements[0]');
|
|
14
|
-
|
|
15
|
-
describe('Global', () => {
|
|
16
|
-
afterAll(() => {
|
|
17
|
-
jest.resetAllMocks();
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
const assertGlobalNodeStructure = (node) => {
|
|
21
|
-
// "declare global"
|
|
22
|
-
expect(node.kind).toBe(ts.SyntaxKind.ModuleDeclaration);
|
|
23
|
-
expect(node.modifiers).toHaveLength(1);
|
|
24
|
-
expect(node.modifiers[0].kind).toBe(ts.SyntaxKind.DeclareKeyword);
|
|
25
|
-
expect(node.name.originalKeywordKind).toBe(ts.SyntaxKind.GlobalKeyword);
|
|
26
|
-
expect(node.name.escapedText).toBe('global');
|
|
27
|
-
|
|
28
|
-
// "namespace Strapi"
|
|
29
|
-
const [strapiNamespace] = node.body.statements;
|
|
30
|
-
|
|
31
|
-
expect(strapiNamespace.kind).toBe(ts.SyntaxKind.ModuleDeclaration);
|
|
32
|
-
expect(strapiNamespace.name.kind).toBe(ts.SyntaxKind.Identifier);
|
|
33
|
-
expect(strapiNamespace.name.escapedText).toBe('Strapi');
|
|
34
|
-
|
|
35
|
-
// "interface Schemas"
|
|
36
|
-
const [schemasInterface] = strapiNamespace.body.statements;
|
|
37
|
-
|
|
38
|
-
expect(schemasInterface.kind).toBe(ts.SyntaxKind.InterfaceDeclaration);
|
|
39
|
-
expect(schemasInterface.name.escapedText).toBe('Schemas');
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
describe('Generate Global Definition', () => {
|
|
43
|
-
beforeEach(() => {
|
|
44
|
-
jest.resetAllMocks();
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
test('With empty definition', () => {
|
|
48
|
-
const definitions = [];
|
|
49
|
-
|
|
50
|
-
const globalNode = generateGlobalDefinition(definitions);
|
|
51
|
-
|
|
52
|
-
assertGlobalNodeStructure(globalNode);
|
|
53
|
-
|
|
54
|
-
expect(getSchemaInterfaceName).not.toHaveBeenCalled();
|
|
55
|
-
|
|
56
|
-
const schemasNode = getSchemasInterfaceNode(globalNode);
|
|
57
|
-
|
|
58
|
-
expect(schemasNode.members).toHaveLength(0);
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
test('With no definition', () => {
|
|
62
|
-
const globalNode = generateGlobalDefinition();
|
|
63
|
-
|
|
64
|
-
assertGlobalNodeStructure(globalNode);
|
|
65
|
-
|
|
66
|
-
expect(getSchemaInterfaceName).not.toHaveBeenCalled();
|
|
67
|
-
|
|
68
|
-
const schemasNode = getSchemasInterfaceNode(globalNode);
|
|
69
|
-
|
|
70
|
-
expect(schemasNode.members).toHaveLength(0);
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
test('With multiple definitions', () => {
|
|
74
|
-
const definitions = [
|
|
75
|
-
{ schema: { uid: 'api::foo.foo' } },
|
|
76
|
-
{ schema: { uid: 'api::bar.bar' } },
|
|
77
|
-
{ schema: { uid: 'api::foobar.foobar' } },
|
|
78
|
-
{ schema: { uid: 'default.barfoo' } },
|
|
79
|
-
];
|
|
80
|
-
|
|
81
|
-
getSchemaInterfaceName.mockReturnValue('Placeholder');
|
|
82
|
-
|
|
83
|
-
const globalNode = generateGlobalDefinition(definitions);
|
|
84
|
-
|
|
85
|
-
assertGlobalNodeStructure(globalNode);
|
|
86
|
-
|
|
87
|
-
const schemasNode = getSchemasInterfaceNode(globalNode);
|
|
88
|
-
|
|
89
|
-
expect(schemasNode.members).toHaveLength(definitions.length);
|
|
90
|
-
|
|
91
|
-
definitions.forEach(({ schema }, index) => {
|
|
92
|
-
const { uid } = schema;
|
|
93
|
-
const node = schemasNode.members[index];
|
|
94
|
-
|
|
95
|
-
expect(node.kind).toBe(ts.SyntaxKind.PropertySignature);
|
|
96
|
-
|
|
97
|
-
expect(getSchemaInterfaceName).toHaveBeenCalledWith(uid);
|
|
98
|
-
|
|
99
|
-
expect(node.name.kind).toBe(ts.SyntaxKind.StringLiteral);
|
|
100
|
-
expect(node.name.text).toBe(uid);
|
|
101
|
-
expect(node.name.singleQuote).toBeTruthy();
|
|
102
|
-
|
|
103
|
-
expect(node.type.kind).toBe(ts.SyntaxKind.TypeReference);
|
|
104
|
-
expect(node.type.typeName.escapedText).toBe('Placeholder');
|
|
105
|
-
});
|
|
106
|
-
});
|
|
107
|
-
});
|
|
108
|
-
});
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const path = require('path');
|
|
4
|
-
const fs = require('fs-extra');
|
|
5
|
-
|
|
6
|
-
module.exports = async (dest) => {
|
|
7
|
-
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
|
-
},
|
|
24
|
-
include: ['../../../src/admin/*', '../../../src/**/**/admin/src/*'],
|
|
25
|
-
exclude: ['node_modules', '**/*.test.js', '*.js'],
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
const filePath = path.join(dest, 'admin', 'src', 'tsconfig.json');
|
|
29
|
-
|
|
30
|
-
try {
|
|
31
|
-
await fs.ensureFile(filePath);
|
|
32
|
-
|
|
33
|
-
await fs.writeJSON(filePath, tsConfig, { spaces: 2 });
|
|
34
|
-
} catch (err) {
|
|
35
|
-
console.log(err);
|
|
36
|
-
}
|
|
37
|
-
};
|
package/lib/admin/index.js
DELETED
package/lib/compilers/watch.js
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const ts = require('typescript');
|
|
4
|
-
|
|
5
|
-
const reportDiagnostics = require('../utils/report-diagnostics');
|
|
6
|
-
const formatHost = require('../utils/format-host');
|
|
7
|
-
const resolveConfigOptions = require('../utils/resolve-config-options');
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Prints a diagnostic every time the watch status changes.
|
|
11
|
-
* This is mainly for messages like "Starting compilation" or "Compilation completed".
|
|
12
|
-
*/
|
|
13
|
-
const reportWatchStatusChanged = (diagnostic) => {
|
|
14
|
-
console.info(ts.formatDiagnostic(diagnostic, formatHost));
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
module.exports = {
|
|
18
|
-
run(configPath) {
|
|
19
|
-
const createProgram = ts.createSemanticDiagnosticsBuilderProgram;
|
|
20
|
-
|
|
21
|
-
const { fileNames, options, projectReferences, watchOptions } =
|
|
22
|
-
resolveConfigOptions(configPath);
|
|
23
|
-
|
|
24
|
-
const host = ts.createWatchCompilerHost(
|
|
25
|
-
fileNames,
|
|
26
|
-
options,
|
|
27
|
-
ts.sys,
|
|
28
|
-
createProgram,
|
|
29
|
-
reportDiagnostics,
|
|
30
|
-
reportWatchStatusChanged,
|
|
31
|
-
projectReferences,
|
|
32
|
-
watchOptions
|
|
33
|
-
);
|
|
34
|
-
|
|
35
|
-
ts.createWatchProgram(host);
|
|
36
|
-
},
|
|
37
|
-
};
|
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
/* eslint-disable no-bitwise */
|
|
4
|
-
|
|
5
|
-
const ts = require('typescript');
|
|
6
|
-
const { factory } = require('typescript');
|
|
7
|
-
|
|
8
|
-
const { getSchemaInterfaceName } = require('./utils');
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
*
|
|
12
|
-
* @param {object} schemaDefinition
|
|
13
|
-
* @param {ts.InterfaceDeclaration} schemaDefinition.definition
|
|
14
|
-
* @param {object} schemaDefinition.schema
|
|
15
|
-
*/
|
|
16
|
-
const schemaDefinitionToPropertySignature = ({ schema }) => {
|
|
17
|
-
const { uid } = schema;
|
|
18
|
-
|
|
19
|
-
const interfaceTypeName = getSchemaInterfaceName(uid);
|
|
20
|
-
|
|
21
|
-
return factory.createPropertySignature(
|
|
22
|
-
undefined,
|
|
23
|
-
factory.createStringLiteral(uid, true),
|
|
24
|
-
undefined,
|
|
25
|
-
factory.createTypeReferenceNode(factory.createIdentifier(interfaceTypeName))
|
|
26
|
-
);
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Generate the global module augmentation block
|
|
31
|
-
*
|
|
32
|
-
* @param {Array<{ schema: object; definition: ts.TypeNode }>} schemasDefinitions
|
|
33
|
-
* @returns {ts.ModuleDeclaration}
|
|
34
|
-
*/
|
|
35
|
-
const generateGlobalDefinition = (schemasDefinitions = []) => {
|
|
36
|
-
const properties = schemasDefinitions.map(schemaDefinitionToPropertySignature);
|
|
37
|
-
|
|
38
|
-
return factory.createModuleDeclaration(
|
|
39
|
-
undefined,
|
|
40
|
-
[factory.createModifier(ts.SyntaxKind.DeclareKeyword)],
|
|
41
|
-
factory.createIdentifier('global'),
|
|
42
|
-
factory.createModuleBlock([
|
|
43
|
-
factory.createModuleDeclaration(
|
|
44
|
-
undefined,
|
|
45
|
-
undefined,
|
|
46
|
-
factory.createIdentifier('Strapi'),
|
|
47
|
-
factory.createModuleBlock([
|
|
48
|
-
factory.createInterfaceDeclaration(
|
|
49
|
-
undefined,
|
|
50
|
-
undefined,
|
|
51
|
-
factory.createIdentifier('Schemas'),
|
|
52
|
-
undefined,
|
|
53
|
-
undefined,
|
|
54
|
-
properties
|
|
55
|
-
),
|
|
56
|
-
]),
|
|
57
|
-
ts.NodeFlags.Namespace |
|
|
58
|
-
ts.NodeFlags.ExportContext |
|
|
59
|
-
ts.NodeFlags.Ambient |
|
|
60
|
-
ts.NodeFlags.ContextFlags
|
|
61
|
-
),
|
|
62
|
-
]),
|
|
63
|
-
ts.NodeFlags.ExportContext |
|
|
64
|
-
ts.NodeFlags.GlobalAugmentation |
|
|
65
|
-
ts.NodeFlags.Ambient |
|
|
66
|
-
ts.NodeFlags.ContextFlags
|
|
67
|
-
);
|
|
68
|
-
};
|
|
69
|
-
|
|
70
|
-
module.exports = { generateGlobalDefinition };
|
|
@@ -1,33 +0,0 @@
|
|
|
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.map((key) =>
|
|
22
|
-
factory.createImportSpecifier(false, undefined, factory.createIdentifier(key))
|
|
23
|
-
);
|
|
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
|
-
);
|
|
32
|
-
},
|
|
33
|
-
};
|
|
@@ -1,185 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const path = require('path');
|
|
4
|
-
|
|
5
|
-
const ts = require('typescript');
|
|
6
|
-
const { factory } = require('typescript');
|
|
7
|
-
|
|
8
|
-
const fp = require('lodash/fp');
|
|
9
|
-
const fse = require('fs-extra');
|
|
10
|
-
const prettier = require('prettier');
|
|
11
|
-
const chalk = require('chalk');
|
|
12
|
-
const CLITable = require('cli-table3');
|
|
13
|
-
|
|
14
|
-
const { generateImportDefinition } = require('./imports');
|
|
15
|
-
const { generateSchemaDefinition } = require('./schema');
|
|
16
|
-
const { generateGlobalDefinition } = require('./global');
|
|
17
|
-
const {
|
|
18
|
-
getAllStrapiSchemas,
|
|
19
|
-
getSchemaInterfaceName,
|
|
20
|
-
getSchemaModelType,
|
|
21
|
-
getDefinitionAttributesCount,
|
|
22
|
-
} = require('./utils');
|
|
23
|
-
|
|
24
|
-
const DEFAULT_OUT_FILENAME = 'schemas.d.ts';
|
|
25
|
-
|
|
26
|
-
const emitDefinitions = (definitions) => {
|
|
27
|
-
const nodeArray = factory.createNodeArray(definitions);
|
|
28
|
-
|
|
29
|
-
const sourceFile = ts.createSourceFile(
|
|
30
|
-
'placeholder.ts',
|
|
31
|
-
'',
|
|
32
|
-
ts.ScriptTarget.ESNext,
|
|
33
|
-
true,
|
|
34
|
-
ts.ScriptKind.TS
|
|
35
|
-
);
|
|
36
|
-
|
|
37
|
-
const printer = ts.createPrinter({ newLine: true, omitTrailingSemicolon: true });
|
|
38
|
-
|
|
39
|
-
return printer.printList(ts.ListFormat.MultiLine, nodeArray, sourceFile);
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
const saveDefinitionToFileSystem = async (dir, file, content) => {
|
|
43
|
-
const filepath = path.join(dir, file);
|
|
44
|
-
|
|
45
|
-
await fse.writeFile(filepath, content);
|
|
46
|
-
|
|
47
|
-
return filepath;
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Format the given definitions.
|
|
52
|
-
* Uses the existing config if one is defined in the project.
|
|
53
|
-
*
|
|
54
|
-
* @param {string} content
|
|
55
|
-
* @returns {Promise<string>}
|
|
56
|
-
*/
|
|
57
|
-
const format = async (content) => {
|
|
58
|
-
const configFile = await prettier.resolveConfigFile();
|
|
59
|
-
const config = configFile
|
|
60
|
-
? await prettier.resolveConfig(configFile)
|
|
61
|
-
: // Default config
|
|
62
|
-
{
|
|
63
|
-
singleQuote: true,
|
|
64
|
-
useTabs: false,
|
|
65
|
-
tabWidth: 2,
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
Object.assign(config, { parser: 'typescript' });
|
|
69
|
-
|
|
70
|
-
return prettier.format(content, config);
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
const logDebugInformation = (definitions, options = {}) => {
|
|
74
|
-
const { filepath, verbose, silent } = options;
|
|
75
|
-
|
|
76
|
-
if (verbose) {
|
|
77
|
-
const table = new CLITable({
|
|
78
|
-
head: [
|
|
79
|
-
chalk.bold(chalk.green('Model Type')),
|
|
80
|
-
chalk.bold(chalk.blue('UID')),
|
|
81
|
-
chalk.bold(chalk.blue('Type')),
|
|
82
|
-
chalk.bold(chalk.gray('Attributes Count')),
|
|
83
|
-
],
|
|
84
|
-
colAligns: ['center', 'left', 'left', 'center'],
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
const sortedDefinitions = definitions.map((def) => ({
|
|
88
|
-
...def,
|
|
89
|
-
attributesCount: getDefinitionAttributesCount(def.definition),
|
|
90
|
-
}));
|
|
91
|
-
|
|
92
|
-
for (const { schema, attributesCount } of sortedDefinitions) {
|
|
93
|
-
const modelType = fp.upperFirst(getSchemaModelType(schema));
|
|
94
|
-
const interfaceType = getSchemaInterfaceName(schema.uid);
|
|
95
|
-
|
|
96
|
-
table.push([
|
|
97
|
-
chalk.greenBright(modelType),
|
|
98
|
-
chalk.blue(schema.uid),
|
|
99
|
-
chalk.blue(interfaceType),
|
|
100
|
-
chalk.grey(fp.isNil(attributesCount) ? 'N/A' : attributesCount),
|
|
101
|
-
]);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
// Table
|
|
105
|
-
console.log(table.toString());
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
if (!silent) {
|
|
109
|
-
// Metrics
|
|
110
|
-
console.log(
|
|
111
|
-
chalk.greenBright(
|
|
112
|
-
`Generated ${definitions.length} type definition for your Strapi application's schemas.`
|
|
113
|
-
)
|
|
114
|
-
);
|
|
115
|
-
|
|
116
|
-
// Filepath
|
|
117
|
-
const relativePath = path.relative(process.cwd(), filepath);
|
|
118
|
-
|
|
119
|
-
console.log(
|
|
120
|
-
chalk.grey(`The definitions file has been generated here: ${chalk.bold(relativePath)}`)
|
|
121
|
-
);
|
|
122
|
-
}
|
|
123
|
-
};
|
|
124
|
-
|
|
125
|
-
/**
|
|
126
|
-
* Generate type definitions for Strapi schemas
|
|
127
|
-
*
|
|
128
|
-
* @param {object} options
|
|
129
|
-
* @param {Strapi} options.strapi
|
|
130
|
-
* @param {{ distDir: string; appDir: string; }} options.dirs
|
|
131
|
-
* @param {string} [options.outDir]
|
|
132
|
-
* @param {string} [options.file]
|
|
133
|
-
* @param {boolean} [options.verbose]
|
|
134
|
-
*/
|
|
135
|
-
const generateSchemasDefinitions = async (options = {}) => {
|
|
136
|
-
const {
|
|
137
|
-
strapi,
|
|
138
|
-
outDir = process.cwd(),
|
|
139
|
-
file = DEFAULT_OUT_FILENAME,
|
|
140
|
-
verbose = false,
|
|
141
|
-
silent = false,
|
|
142
|
-
} = options;
|
|
143
|
-
|
|
144
|
-
const schemas = getAllStrapiSchemas(strapi);
|
|
145
|
-
|
|
146
|
-
const schemasDefinitions = Object.values(schemas).map((schema) => ({
|
|
147
|
-
schema,
|
|
148
|
-
definition: generateSchemaDefinition(schema),
|
|
149
|
-
}));
|
|
150
|
-
|
|
151
|
-
const formattedSchemasDefinitions = schemasDefinitions.reduce((acc, def) => {
|
|
152
|
-
acc.push(
|
|
153
|
-
// Definition
|
|
154
|
-
def.definition,
|
|
155
|
-
|
|
156
|
-
// Add a newline between each interface declaration
|
|
157
|
-
factory.createIdentifier('\n')
|
|
158
|
-
);
|
|
159
|
-
|
|
160
|
-
return acc;
|
|
161
|
-
}, []);
|
|
162
|
-
|
|
163
|
-
const allDefinitions = [
|
|
164
|
-
// Imports
|
|
165
|
-
generateImportDefinition(),
|
|
166
|
-
|
|
167
|
-
// Add a newline after the import statement
|
|
168
|
-
factory.createIdentifier('\n'),
|
|
169
|
-
|
|
170
|
-
// Schemas
|
|
171
|
-
...formattedSchemasDefinitions,
|
|
172
|
-
|
|
173
|
-
// Global
|
|
174
|
-
generateGlobalDefinition(schemasDefinitions),
|
|
175
|
-
];
|
|
176
|
-
|
|
177
|
-
const output = emitDefinitions(allDefinitions);
|
|
178
|
-
const formattedOutput = await format(output);
|
|
179
|
-
|
|
180
|
-
const definitionFilepath = await saveDefinitionToFileSystem(outDir, file, formattedOutput);
|
|
181
|
-
|
|
182
|
-
logDebugInformation(schemasDefinitions, { filepath: definitionFilepath, verbose, silent });
|
|
183
|
-
};
|
|
184
|
-
|
|
185
|
-
module.exports = generateSchemasDefinitions;
|
|
@@ -1,131 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const ts = require('typescript');
|
|
4
|
-
const _ = require('lodash/fp');
|
|
5
|
-
|
|
6
|
-
const { toTypeLiteral } = require('./utils');
|
|
7
|
-
|
|
8
|
-
const { factory } = ts;
|
|
9
|
-
|
|
10
|
-
module.exports = {
|
|
11
|
-
string() {
|
|
12
|
-
return ['StringAttribute'];
|
|
13
|
-
},
|
|
14
|
-
text() {
|
|
15
|
-
return ['TextAttribute'];
|
|
16
|
-
},
|
|
17
|
-
richtext() {
|
|
18
|
-
return ['RichTextAttribute'];
|
|
19
|
-
},
|
|
20
|
-
password() {
|
|
21
|
-
return ['PasswordAttribute'];
|
|
22
|
-
},
|
|
23
|
-
email() {
|
|
24
|
-
return ['EmailAttribute'];
|
|
25
|
-
},
|
|
26
|
-
date() {
|
|
27
|
-
return ['DateAttribute'];
|
|
28
|
-
},
|
|
29
|
-
time() {
|
|
30
|
-
return ['TimeAttribute'];
|
|
31
|
-
},
|
|
32
|
-
datetime() {
|
|
33
|
-
return ['DateTimeAttribute'];
|
|
34
|
-
},
|
|
35
|
-
timestamp() {
|
|
36
|
-
return ['TimestampAttribute'];
|
|
37
|
-
},
|
|
38
|
-
integer() {
|
|
39
|
-
return ['IntegerAttribute'];
|
|
40
|
-
},
|
|
41
|
-
biginteger() {
|
|
42
|
-
return ['BigIntegerAttribute'];
|
|
43
|
-
},
|
|
44
|
-
float() {
|
|
45
|
-
return ['FloatAttribute'];
|
|
46
|
-
},
|
|
47
|
-
decimal() {
|
|
48
|
-
return ['DecimalAttribute'];
|
|
49
|
-
},
|
|
50
|
-
uid({ attribute, uid }) {
|
|
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 ['UIDAttribute'];
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
const params = [];
|
|
59
|
-
|
|
60
|
-
// If the targetField property is defined, then reference it,
|
|
61
|
-
// otherwise, put `undefined` keyword type nodes as placeholders
|
|
62
|
-
const targetFieldParams = _.isUndefined(targetField)
|
|
63
|
-
? [
|
|
64
|
-
factory.createKeywordTypeNode(ts.SyntaxKind.UndefinedKeyword),
|
|
65
|
-
factory.createKeywordTypeNode(ts.SyntaxKind.UndefinedKeyword),
|
|
66
|
-
]
|
|
67
|
-
: [factory.createStringLiteral(uid), factory.createStringLiteral(targetField)];
|
|
68
|
-
|
|
69
|
-
params.push(...targetFieldParams);
|
|
70
|
-
|
|
71
|
-
// If the options property is defined, transform it to
|
|
72
|
-
// a type literral node and add it to the params list
|
|
73
|
-
if (_.isObject(options)) {
|
|
74
|
-
params.push(toTypeLiteral(options));
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
return ['UIDAttribute', params];
|
|
78
|
-
},
|
|
79
|
-
enumeration({ attribute }) {
|
|
80
|
-
const { enum: enumValues } = attribute;
|
|
81
|
-
|
|
82
|
-
return ['EnumerationAttribute', [toTypeLiteral(enumValues)]];
|
|
83
|
-
},
|
|
84
|
-
boolean() {
|
|
85
|
-
return ['BooleanAttribute'];
|
|
86
|
-
},
|
|
87
|
-
json() {
|
|
88
|
-
return ['JSONAttribute'];
|
|
89
|
-
},
|
|
90
|
-
media() {
|
|
91
|
-
return ['MediaAttribute'];
|
|
92
|
-
},
|
|
93
|
-
relation({ uid, attribute }) {
|
|
94
|
-
const { relation, target } = attribute;
|
|
95
|
-
|
|
96
|
-
const isMorphRelation = relation.toLowerCase().includes('morph');
|
|
97
|
-
|
|
98
|
-
if (isMorphRelation) {
|
|
99
|
-
return [
|
|
100
|
-
'RelationAttribute',
|
|
101
|
-
[factory.createStringLiteral(uid, true), factory.createStringLiteral(relation, true)],
|
|
102
|
-
];
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
return [
|
|
106
|
-
'RelationAttribute',
|
|
107
|
-
[
|
|
108
|
-
factory.createStringLiteral(uid, true),
|
|
109
|
-
factory.createStringLiteral(relation, true),
|
|
110
|
-
factory.createStringLiteral(target, true),
|
|
111
|
-
],
|
|
112
|
-
];
|
|
113
|
-
},
|
|
114
|
-
component({ attribute }) {
|
|
115
|
-
const target = attribute.component;
|
|
116
|
-
const params = [factory.createStringLiteral(target, true)];
|
|
117
|
-
|
|
118
|
-
if (attribute.repeatable) {
|
|
119
|
-
params.push(factory.createTrue());
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
return ['ComponentAttribute', params];
|
|
123
|
-
},
|
|
124
|
-
dynamiczone({ attribute }) {
|
|
125
|
-
const componentsParam = factory.createTupleTypeNode(
|
|
126
|
-
attribute.components.map((component) => factory.createStringLiteral(component))
|
|
127
|
-
);
|
|
128
|
-
|
|
129
|
-
return ['DynamicZoneAttribute', [componentsParam]];
|
|
130
|
-
},
|
|
131
|
-
};
|