@strapi/typescript-utils 0.0.0-experimental.6340da17068c3bed7fb006e7bcc554c71129e282 → 0.0.0-experimental.63624e21eb5b0713345a0cf2f68f1c40eab427bc
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/dist/compile.d.ts +5 -0
- package/dist/compile.d.ts.map +1 -0
- package/dist/compile.js +12 -0
- package/dist/compile.js.map +1 -0
- package/dist/compile.mjs +10 -0
- package/dist/compile.mjs.map +1 -0
- package/dist/compilers/basic.d.ts +11 -0
- package/dist/compilers/basic.d.ts.map +1 -0
- package/dist/compilers/basic.js +58 -0
- package/dist/compilers/basic.js.map +1 -0
- package/dist/compilers/basic.mjs +36 -0
- package/dist/compilers/basic.mjs.map +1 -0
- package/dist/compilers/index.d.ts +3 -0
- package/dist/compilers/index.d.ts.map +1 -0
- package/dist/compilers/index.js +8 -0
- package/dist/compilers/index.js.map +1 -0
- package/dist/compilers/index.mjs +3 -0
- package/dist/compilers/index.mjs.map +1 -0
- package/dist/generators/common/compare.d.ts +14 -0
- package/dist/generators/common/compare.d.ts.map +1 -0
- package/dist/generators/common/compare.js +25 -0
- package/dist/generators/common/compare.js.map +1 -0
- package/dist/generators/common/compare.mjs +22 -0
- package/dist/generators/common/compare.mjs.map +1 -0
- package/dist/generators/common/imports.d.ts +5 -0
- package/dist/generators/common/imports.d.ts.map +1 -0
- package/dist/generators/common/imports.js +42 -0
- package/dist/generators/common/imports.js.map +1 -0
- package/dist/generators/common/imports.mjs +19 -0
- package/dist/generators/common/imports.mjs.map +1 -0
- package/dist/generators/common/index.d.ts +4 -0
- package/dist/generators/common/index.d.ts.map +1 -0
- package/dist/generators/common/interface-names.d.ts +16 -0
- package/dist/generators/common/interface-names.d.ts.map +1 -0
- package/dist/generators/common/interface-names.js +54 -0
- package/dist/generators/common/interface-names.js.map +1 -0
- package/dist/generators/common/interface-names.mjs +52 -0
- package/dist/generators/common/interface-names.mjs.map +1 -0
- package/dist/generators/common/models/attributes.d.ts +38 -0
- package/dist/generators/common/models/attributes.d.ts.map +1 -0
- package/dist/generators/common/models/attributes.js +205 -0
- package/dist/generators/common/models/attributes.js.map +1 -0
- package/dist/generators/common/models/attributes.mjs +176 -0
- package/dist/generators/common/models/attributes.mjs.map +1 -0
- package/dist/generators/common/models/index.d.ts +7 -0
- package/dist/generators/common/models/index.d.ts.map +1 -0
- package/dist/generators/common/models/mappers.d.ts +9 -0
- package/dist/generators/common/models/mappers.d.ts.map +1 -0
- package/dist/generators/common/models/mappers.js +227 -0
- package/dist/generators/common/models/mappers.js.map +1 -0
- package/dist/generators/common/models/mappers.mjs +202 -0
- package/dist/generators/common/models/mappers.mjs.map +1 -0
- package/dist/generators/common/models/schema.d.ts +8 -0
- package/dist/generators/common/models/schema.d.ts.map +1 -0
- package/dist/generators/common/models/schema.js +78 -0
- package/dist/generators/common/models/schema.js.map +1 -0
- package/dist/generators/common/models/schema.mjs +56 -0
- package/dist/generators/common/models/schema.mjs.map +1 -0
- package/dist/generators/common/models/utils.d.ts +57 -0
- package/dist/generators/common/models/utils.d.ts.map +1 -0
- package/dist/generators/common/models/utils.js +110 -0
- package/dist/generators/common/models/utils.js.map +1 -0
- package/dist/generators/common/models/utils.mjs +82 -0
- package/dist/generators/common/models/utils.mjs.map +1 -0
- package/dist/generators/common/sources.d.ts +133 -0
- package/dist/generators/common/sources.d.ts.map +1 -0
- package/dist/generators/common/sources.js +354 -0
- package/dist/generators/common/sources.js.map +1 -0
- package/dist/generators/common/sources.mjs +318 -0
- package/dist/generators/common/sources.mjs.map +1 -0
- package/dist/generators/components/index.d.ts +9 -0
- package/dist/generators/components/index.d.ts.map +1 -0
- package/dist/generators/components/index.js +84 -0
- package/dist/generators/components/index.js.map +1 -0
- package/dist/generators/components/index.mjs +62 -0
- package/dist/generators/components/index.mjs.map +1 -0
- package/dist/generators/constants.d.ts +3 -0
- package/dist/generators/constants.d.ts.map +1 -0
- package/dist/generators/constants.js +8 -0
- package/dist/generators/constants.js.map +1 -0
- package/dist/generators/constants.mjs +5 -0
- package/dist/generators/constants.mjs.map +1 -0
- package/dist/generators/content-types/index.d.ts +9 -0
- package/dist/generators/content-types/index.d.ts.map +1 -0
- package/dist/generators/content-types/index.js +84 -0
- package/dist/generators/content-types/index.js.map +1 -0
- package/dist/generators/content-types/index.mjs +62 -0
- package/dist/generators/content-types/index.mjs.map +1 -0
- package/dist/generators/controllers/index.d.ts +35 -0
- package/dist/generators/controllers/index.d.ts.map +1 -0
- package/dist/generators/controllers/index.js +88 -0
- package/dist/generators/controllers/index.js.map +1 -0
- package/dist/generators/controllers/index.mjs +86 -0
- package/dist/generators/controllers/index.mjs.map +1 -0
- package/dist/generators/index.d.ts +31 -0
- package/dist/generators/index.d.ts.map +1 -0
- package/dist/generators/index.js +150 -0
- package/dist/generators/index.js.map +1 -0
- package/dist/generators/index.mjs +142 -0
- package/dist/generators/index.mjs.map +1 -0
- package/dist/generators/middlewares/index.d.ts +30 -0
- package/dist/generators/middlewares/index.d.ts.map +1 -0
- package/dist/generators/middlewares/index.js +163 -0
- package/dist/generators/middlewares/index.js.map +1 -0
- package/dist/generators/middlewares/index.mjs +141 -0
- package/dist/generators/middlewares/index.mjs.map +1 -0
- package/dist/generators/plugins/index.d.ts +9 -0
- package/dist/generators/plugins/index.d.ts.map +1 -0
- package/dist/generators/plugins/index.js +225 -0
- package/dist/generators/plugins/index.js.map +1 -0
- package/dist/generators/plugins/index.mjs +200 -0
- package/dist/generators/plugins/index.mjs.map +1 -0
- package/dist/generators/policies/index.d.ts +29 -0
- package/dist/generators/policies/index.d.ts.map +1 -0
- package/dist/generators/policies/index.js +135 -0
- package/dist/generators/policies/index.js.map +1 -0
- package/dist/generators/policies/index.mjs +113 -0
- package/dist/generators/policies/index.mjs.map +1 -0
- package/dist/generators/services/index.d.ts +35 -0
- package/dist/generators/services/index.d.ts.map +1 -0
- package/dist/generators/services/index.js +87 -0
- package/dist/generators/services/index.js.map +1 -0
- package/dist/generators/services/index.mjs +85 -0
- package/dist/generators/services/index.mjs.map +1 -0
- package/dist/generators/strict/index.d.ts +13 -0
- package/dist/generators/strict/index.d.ts.map +1 -0
- package/dist/generators/strict/index.js +24 -0
- package/dist/generators/strict/index.js.map +1 -0
- package/dist/generators/strict/index.mjs +22 -0
- package/dist/generators/strict/index.mjs.map +1 -0
- package/dist/generators/utils.d.ts +68 -0
- package/dist/generators/utils.d.ts.map +1 -0
- package/dist/generators/utils.js +201 -0
- package/dist/generators/utils.js.map +1 -0
- package/dist/generators/utils.mjs +166 -0
- package/dist/generators/utils.mjs.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +28 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +14 -0
- package/dist/index.mjs.map +1 -0
- package/dist/utils/format-host.d.ts +3 -0
- package/dist/utils/format-host.d.ts.map +1 -0
- package/dist/utils/format-host.js +33 -0
- package/dist/utils/format-host.js.map +1 -0
- package/dist/utils/format-host.mjs +11 -0
- package/dist/utils/format-host.mjs.map +1 -0
- package/dist/utils/get-config-path.d.ts +10 -0
- package/dist/utils/get-config-path.d.ts.map +1 -0
- package/dist/utils/get-config-path.js +43 -0
- package/dist/utils/get-config-path.js.map +1 -0
- package/dist/utils/get-config-path.mjs +18 -0
- package/dist/utils/get-config-path.mjs.map +1 -0
- package/dist/utils/index.d.ts +9 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/is-using-typescript-sync.d.ts +5 -0
- package/dist/utils/is-using-typescript-sync.d.ts.map +1 -0
- package/dist/utils/is-using-typescript-sync.js +20 -0
- package/dist/utils/is-using-typescript-sync.js.map +1 -0
- package/dist/utils/is-using-typescript-sync.mjs +14 -0
- package/dist/utils/is-using-typescript-sync.mjs.map +1 -0
- package/dist/utils/is-using-typescript.d.ts +5 -0
- package/dist/utils/is-using-typescript.d.ts.map +1 -0
- package/dist/utils/is-using-typescript.js +20 -0
- package/dist/utils/is-using-typescript.js.map +1 -0
- package/dist/utils/is-using-typescript.mjs +14 -0
- package/dist/utils/is-using-typescript.mjs.map +1 -0
- package/dist/utils/report-diagnostics.d.ts +6 -0
- package/dist/utils/report-diagnostics.d.ts.map +1 -0
- package/dist/utils/report-diagnostics.js +37 -0
- package/dist/utils/report-diagnostics.js.map +1 -0
- package/dist/utils/report-diagnostics.mjs +15 -0
- package/dist/utils/report-diagnostics.mjs.map +1 -0
- package/dist/utils/resolve-config-options.d.ts +12 -0
- package/dist/utils/resolve-config-options.d.ts.map +1 -0
- package/dist/utils/resolve-config-options.js +39 -0
- package/dist/utils/resolve-config-options.js.map +1 -0
- package/dist/utils/resolve-config-options.mjs +17 -0
- package/dist/utils/resolve-config-options.mjs.map +1 -0
- package/dist/utils/resolve-outdir-sync.d.ts +5 -0
- package/dist/utils/resolve-outdir-sync.d.ts.map +1 -0
- package/dist/utils/resolve-outdir-sync.js +19 -0
- package/dist/utils/resolve-outdir-sync.js.map +1 -0
- package/dist/utils/resolve-outdir-sync.mjs +13 -0
- package/dist/utils/resolve-outdir-sync.mjs.map +1 -0
- package/dist/utils/resolve-outdir.d.ts +5 -0
- package/dist/utils/resolve-outdir.d.ts.map +1 -0
- package/dist/utils/resolve-outdir.js +19 -0
- package/dist/utils/resolve-outdir.js.map +1 -0
- package/dist/utils/resolve-outdir.mjs +13 -0
- package/dist/utils/resolve-outdir.mjs.map +1 -0
- package/package.json +27 -12
- package/.eslintrc.cjs +0 -16
- package/index.d.ts +0 -5
- package/jest.config.js +0 -6
- package/lib/__tests__/generators/schemas/attributes.test.js +0 -903
- package/lib/__tests__/generators/schemas/imports.test.js +0 -56
- package/lib/__tests__/generators/schemas/utils.test.js +0 -314
- package/lib/compile.js +0 -10
- package/lib/compilers/basic.js +0 -50
- package/lib/compilers/index.js +0 -7
- package/lib/generators/common/imports.js +0 -34
- package/lib/generators/common/index.js +0 -9
- package/lib/generators/common/models/attributes.js +0 -198
- package/lib/generators/common/models/index.js +0 -15
- package/lib/generators/common/models/mappers.js +0 -144
- package/lib/generators/common/models/schema.js +0 -93
- package/lib/generators/common/models/utils.js +0 -178
- package/lib/generators/components/index.js +0 -74
- package/lib/generators/constants.js +0 -6
- package/lib/generators/content-types/index.js +0 -74
- package/lib/generators/index.js +0 -122
- package/lib/generators/utils.js +0 -216
- package/lib/index.js +0 -13
- package/lib/utils/format-host.js +0 -15
- package/lib/utils/get-config-path.js +0 -29
- package/lib/utils/index.js +0 -21
- package/lib/utils/is-using-typescript-sync.js +0 -19
- package/lib/utils/is-using-typescript.js +0 -17
- package/lib/utils/report-diagnostics.js +0 -19
- package/lib/utils/resolve-config-options.js +0 -23
- package/lib/utils/resolve-outdir-sync.js +0 -18
- package/lib/utils/resolve-outdir.js +0 -18
- package/lint-staged.config.mjs +0 -1
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import * as ts from 'typescript';
|
|
2
|
+
export declare const NAMESPACES: {
|
|
3
|
+
Struct: string;
|
|
4
|
+
Schema: string;
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* Shallow representation of a content-type/component attribute definition.
|
|
8
|
+
* Only the fields read by the generators are typed; the rest is left open.
|
|
9
|
+
*/
|
|
10
|
+
export interface Attribute {
|
|
11
|
+
type: string;
|
|
12
|
+
conditions?: {
|
|
13
|
+
visible?: unknown;
|
|
14
|
+
};
|
|
15
|
+
[key: string]: any;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Shallow representation of a content-type/component schema definition.
|
|
19
|
+
* Only the fields read by the generators are typed; the rest is left open.
|
|
20
|
+
*/
|
|
21
|
+
export interface Schema {
|
|
22
|
+
uid: string;
|
|
23
|
+
modelType?: string;
|
|
24
|
+
kind?: string;
|
|
25
|
+
attributes: Record<string, Attribute>;
|
|
26
|
+
[key: string]: any;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Extract a valid interface name from a schema uid
|
|
30
|
+
*/
|
|
31
|
+
export declare const getSchemaInterfaceName: (uid: string) => string;
|
|
32
|
+
export declare const getSchemaModelType: (schema: Schema) => string | null | undefined;
|
|
33
|
+
/**
|
|
34
|
+
* Get the parent type name to extend based on the schema's nature
|
|
35
|
+
*/
|
|
36
|
+
export declare const getSchemaExtendsTypeName: (schema: Schema) => string | null;
|
|
37
|
+
/**
|
|
38
|
+
* Get a type node based on a type and its params
|
|
39
|
+
*/
|
|
40
|
+
export declare const getTypeNode: (typeName: string, params?: ts.TypeNode[]) => ts.TypeReferenceNode;
|
|
41
|
+
/**
|
|
42
|
+
* Transform a regular JavaScript object or scalar value into a literal expression
|
|
43
|
+
*/
|
|
44
|
+
export declare const toTypeLiteral: (data: any) => any;
|
|
45
|
+
/**
|
|
46
|
+
* Get the number of attributes generated for a given schema definition
|
|
47
|
+
*/
|
|
48
|
+
export declare const getDefinitionAttributesCount: (definition: ts.InterfaceDeclaration) => number | null;
|
|
49
|
+
/**
|
|
50
|
+
* Add the Schema.Attribute namespace before the typename
|
|
51
|
+
*/
|
|
52
|
+
export declare const withAttributeNamespace: (typeName: string) => string;
|
|
53
|
+
/**
|
|
54
|
+
* Add the schema namespace before the typename
|
|
55
|
+
*/
|
|
56
|
+
export declare const withSchemaNamespace: (typeName: string) => string;
|
|
57
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../../src/generators/common/models/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,YAAY,CAAC;AAcjC,eAAO,MAAM,UAAU;;;CAGtB,CAAC;AAEF;;;GAGG;AACH,MAAM,WAAW,SAAS;IACxB,IAAI,EAAE,MAAM,CAAC;IACb,UAAU,CAAC,EAAE;QAAE,OAAO,CAAC,EAAE,OAAO,CAAA;KAAE,CAAC;IACnC,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAC;CACpB;AAED;;;GAGG;AACH,MAAM,WAAW,MAAM;IACrB,GAAG,EAAE,MAAM,CAAC;IACZ,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC;IACtC,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAC;CACpB;AAED;;GAEG;AACH,eAAO,MAAM,sBAAsB,GAAI,KAAK,MAAM,KAAG,MACJ,CAAC;AAElD,eAAO,MAAM,kBAAkB,GAAI,QAAQ,MAAM,KAAG,MAAM,GAAG,IAAI,GAAG,SAcnE,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,wBAAwB,GAAI,QAAQ,MAAM,KAAG,MAAM,GAAG,IAQlE,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,WAAW,GAAI,UAAU,MAAM,EAAE,SAAQ,EAAE,CAAC,QAAQ,EAAO,KAAG,EAAE,CAAC,iBAE7E,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,aAAa,GAAI,MAAM,GAAG,KAAG,GA2DzC,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,4BAA4B,GACvC,YAAY,EAAE,CAAC,oBAAoB,KAClC,MAAM,GAAG,IAUX,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,sBAAsB,GAAI,UAAU,MAAM,KAAG,MACZ,CAAC;AAE/C;;GAEG;AACH,eAAO,MAAM,mBAAmB,GAAI,UAAU,MAAM,KAAG,MACV,CAAC"}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var ts = require('typescript');
|
|
4
|
+
var _ = require('lodash');
|
|
5
|
+
var compare = require('../compare.js');
|
|
6
|
+
|
|
7
|
+
function _interopNamespace(e) {
|
|
8
|
+
if (e && e.__esModule) return e;
|
|
9
|
+
var n = Object.create(null);
|
|
10
|
+
if (e) {
|
|
11
|
+
Object.keys(e).forEach(function (k) {
|
|
12
|
+
if (k !== 'default') {
|
|
13
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
14
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
15
|
+
enumerable: true,
|
|
16
|
+
get: function () { return e[k]; }
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
n.default = e;
|
|
22
|
+
return Object.freeze(n);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
var ts__namespace = /*#__PURE__*/_interopNamespace(ts);
|
|
26
|
+
|
|
27
|
+
const { factory } = ts__namespace;
|
|
28
|
+
const NAMESPACES = {
|
|
29
|
+
Struct: 'Struct',
|
|
30
|
+
Schema: 'Schema'
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Extract a valid interface name from a schema uid
|
|
34
|
+
*/ const getSchemaInterfaceName = (uid)=>_.upperFirst(_.camelCase(uid.replace(/(:.)/, ' ')));
|
|
35
|
+
const getSchemaModelType = (schema)=>{
|
|
36
|
+
const { modelType, kind } = schema;
|
|
37
|
+
// Components
|
|
38
|
+
if (modelType === 'component') {
|
|
39
|
+
return 'component';
|
|
40
|
+
}
|
|
41
|
+
// Content-Types
|
|
42
|
+
if (modelType === 'contentType') {
|
|
43
|
+
return kind;
|
|
44
|
+
}
|
|
45
|
+
return null;
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* Get the parent type name to extend based on the schema's nature
|
|
49
|
+
*/ const getSchemaExtendsTypeName = (schema)=>{
|
|
50
|
+
const base = getSchemaModelType(schema);
|
|
51
|
+
if (base === null) {
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
return `${NAMESPACES.Struct}.${_.upperFirst(base)}Schema`;
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* Get a type node based on a type and its params
|
|
58
|
+
*/ const getTypeNode = (typeName, params = [])=>{
|
|
59
|
+
return factory.createTypeReferenceNode(factory.createIdentifier(typeName), params);
|
|
60
|
+
};
|
|
61
|
+
/**
|
|
62
|
+
* Transform a regular JavaScript object or scalar value into a literal expression
|
|
63
|
+
*/ const toTypeLiteral = (data)=>{
|
|
64
|
+
if (data === undefined) {
|
|
65
|
+
return factory.createLiteralTypeNode(ts__namespace.SyntaxKind.UndefinedKeyword);
|
|
66
|
+
}
|
|
67
|
+
if (data === null) {
|
|
68
|
+
return factory.createLiteralTypeNode(ts__namespace.SyntaxKind.NullKeyword);
|
|
69
|
+
}
|
|
70
|
+
if (_.isString(data)) {
|
|
71
|
+
return factory.createStringLiteral(data, true);
|
|
72
|
+
}
|
|
73
|
+
if (_.isNumber(data)) {
|
|
74
|
+
return data < 0 ? factory.createPrefixUnaryExpression(ts__namespace.SyntaxKind.MinusToken, factory.createNumericLiteral(-data)) : factory.createNumericLiteral(data);
|
|
75
|
+
}
|
|
76
|
+
if (_.isBoolean(data)) {
|
|
77
|
+
return data ? factory.createTrue() : factory.createFalse();
|
|
78
|
+
}
|
|
79
|
+
if (Array.isArray(data)) {
|
|
80
|
+
return factory.createTupleTypeNode(data.map((item)=>toTypeLiteral(item)));
|
|
81
|
+
}
|
|
82
|
+
if (_.isDate(data)) {
|
|
83
|
+
return factory.createStringLiteral(data.toISOString());
|
|
84
|
+
}
|
|
85
|
+
if (typeof data !== 'object') {
|
|
86
|
+
throw new Error(`Cannot convert to object literal. Unknown type "${typeof data}"`);
|
|
87
|
+
}
|
|
88
|
+
const entries = Object.entries(data).sort((a, b)=>compare.compareEnglish(a[0], b[0]));
|
|
89
|
+
const props = entries.reduce((acc, [key, value])=>{
|
|
90
|
+
// Handle keys such as content-type-builder & co.
|
|
91
|
+
const identifier = key.includes('-') ? factory.createStringLiteral(key, true) : factory.createIdentifier(key);
|
|
92
|
+
return [
|
|
93
|
+
...acc,
|
|
94
|
+
factory.createPropertyDeclaration(undefined, identifier, undefined, toTypeLiteral(value), undefined)
|
|
95
|
+
];
|
|
96
|
+
}, []);
|
|
97
|
+
return factory.createTypeLiteralNode(props);
|
|
98
|
+
};
|
|
99
|
+
/**
|
|
100
|
+
* Add the Schema.Attribute namespace before the typename
|
|
101
|
+
*/ const withAttributeNamespace = (typeName)=>`${NAMESPACES.Schema}.Attribute.${typeName}`;
|
|
102
|
+
|
|
103
|
+
exports.NAMESPACES = NAMESPACES;
|
|
104
|
+
exports.getSchemaExtendsTypeName = getSchemaExtendsTypeName;
|
|
105
|
+
exports.getSchemaInterfaceName = getSchemaInterfaceName;
|
|
106
|
+
exports.getSchemaModelType = getSchemaModelType;
|
|
107
|
+
exports.getTypeNode = getTypeNode;
|
|
108
|
+
exports.toTypeLiteral = toTypeLiteral;
|
|
109
|
+
exports.withAttributeNamespace = withAttributeNamespace;
|
|
110
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sources":["../../../../src/generators/common/models/utils.ts"],"sourcesContent":["import * as ts from 'typescript';\nimport {\n camelCase,\n isBoolean,\n isDate,\n isNumber,\n isString,\n matchesProperty,\n upperFirst,\n} from 'lodash';\nimport { compareEnglish } from '../compare';\n\nconst { factory } = ts;\n\nexport const NAMESPACES = {\n Struct: 'Struct',\n Schema: 'Schema',\n};\n\n/**\n * Shallow representation of a content-type/component attribute definition.\n * Only the fields read by the generators are typed; the rest is left open.\n */\nexport interface Attribute {\n type: string;\n conditions?: { visible?: unknown };\n [key: string]: any;\n}\n\n/**\n * Shallow representation of a content-type/component schema definition.\n * Only the fields read by the generators are typed; the rest is left open.\n */\nexport interface Schema {\n uid: string;\n modelType?: string;\n kind?: string;\n attributes: Record<string, Attribute>;\n [key: string]: any;\n}\n\n/**\n * Extract a valid interface name from a schema uid\n */\nexport const getSchemaInterfaceName = (uid: string): string =>\n upperFirst(camelCase(uid.replace(/(:.)/, ' ')));\n\nexport const getSchemaModelType = (schema: Schema): string | null | undefined => {\n const { modelType, kind } = schema;\n\n // Components\n if (modelType === 'component') {\n return 'component';\n }\n\n // Content-Types\n if (modelType === 'contentType') {\n return kind;\n }\n\n return null;\n};\n\n/**\n * Get the parent type name to extend based on the schema's nature\n */\nexport const getSchemaExtendsTypeName = (schema: Schema): string | null => {\n const base = getSchemaModelType(schema);\n\n if (base === null) {\n return null;\n }\n\n return `${NAMESPACES.Struct}.${upperFirst(base as string)}Schema`;\n};\n\n/**\n * Get a type node based on a type and its params\n */\nexport const getTypeNode = (typeName: string, params: ts.TypeNode[] = []): ts.TypeReferenceNode => {\n return factory.createTypeReferenceNode(factory.createIdentifier(typeName), params);\n};\n\n/**\n * Transform a regular JavaScript object or scalar value into a literal expression\n */\nexport const toTypeLiteral = (data: any): any => {\n if (data === undefined) {\n return factory.createLiteralTypeNode(ts.SyntaxKind.UndefinedKeyword as any);\n }\n\n if (data === null) {\n return factory.createLiteralTypeNode(ts.SyntaxKind.NullKeyword as any);\n }\n\n if (isString(data)) {\n return factory.createStringLiteral(data, true);\n }\n\n if (isNumber(data)) {\n return data < 0\n ? factory.createPrefixUnaryExpression(\n ts.SyntaxKind.MinusToken,\n factory.createNumericLiteral(-data)\n )\n : factory.createNumericLiteral(data);\n }\n\n if (isBoolean(data)) {\n return data ? factory.createTrue() : factory.createFalse();\n }\n\n if (Array.isArray(data)) {\n return factory.createTupleTypeNode(data.map((item) => toTypeLiteral(item)));\n }\n\n if (isDate(data)) {\n return factory.createStringLiteral(data.toISOString());\n }\n\n if (typeof data !== 'object') {\n throw new Error(`Cannot convert to object literal. Unknown type \"${typeof data}\"`);\n }\n\n const entries = Object.entries(data).sort((a, b) => compareEnglish(a[0], b[0]));\n\n const props = entries.reduce<any[]>((acc, [key, value]) => {\n // Handle keys such as content-type-builder & co.\n const identifier = key.includes('-')\n ? factory.createStringLiteral(key, true)\n : factory.createIdentifier(key);\n\n return [\n ...acc,\n factory.createPropertyDeclaration(\n undefined,\n identifier,\n undefined,\n toTypeLiteral(value),\n undefined\n ),\n ];\n }, []);\n\n return factory.createTypeLiteralNode(props);\n};\n\n/**\n * Get the number of attributes generated for a given schema definition\n */\nexport const getDefinitionAttributesCount = (\n definition: ts.InterfaceDeclaration\n): number | null => {\n const attributesNode = definition.members.find(\n matchesProperty('name.escapedText', 'attributes')\n ) as any;\n\n if (!attributesNode) {\n return null;\n }\n\n return attributesNode.type.members.length;\n};\n\n/**\n * Add the Schema.Attribute namespace before the typename\n */\nexport const withAttributeNamespace = (typeName: string): string =>\n `${NAMESPACES.Schema}.Attribute.${typeName}`;\n\n/**\n * Add the schema namespace before the typename\n */\nexport const withSchemaNamespace = (typeName: string): string =>\n `${(NAMESPACES as any).schema}.${typeName}`;\n"],"names":["factory","ts","NAMESPACES","Struct","Schema","getSchemaInterfaceName","uid","upperFirst","camelCase","replace","getSchemaModelType","schema","modelType","kind","getSchemaExtendsTypeName","base","getTypeNode","typeName","params","createTypeReferenceNode","createIdentifier","toTypeLiteral","data","undefined","createLiteralTypeNode","SyntaxKind","UndefinedKeyword","NullKeyword","isString","createStringLiteral","isNumber","createPrefixUnaryExpression","MinusToken","createNumericLiteral","isBoolean","createTrue","createFalse","Array","isArray","createTupleTypeNode","map","item","isDate","toISOString","Error","entries","Object","sort","a","b","compareEnglish","props","reduce","acc","key","value","identifier","includes","createPropertyDeclaration","createTypeLiteralNode","withAttributeNamespace"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAYA,MAAM,EAAEA,OAAO,EAAE,GAAGC,aAAAA;MAEPC,UAAAA,GAAa;IACxBC,MAAAA,EAAQ,QAAA;IACRC,MAAAA,EAAQ;AACV;AAwBA;;IAGO,MAAMC,sBAAAA,GAAyB,CAACC,GAAAA,GACrCC,YAAAA,CAAWC,WAAAA,CAAUF,GAAAA,CAAIG,OAAO,CAAC,MAAA,EAAQ,GAAA,CAAA,CAAA;AAEpC,MAAMC,qBAAqB,CAACC,MAAAA,GAAAA;AACjC,IAAA,MAAM,EAAEC,SAAS,EAAEC,IAAI,EAAE,GAAGF,MAAAA;;AAG5B,IAAA,IAAIC,cAAc,WAAA,EAAa;QAC7B,OAAO,WAAA;AACT,IAAA;;AAGA,IAAA,IAAIA,cAAc,aAAA,EAAe;QAC/B,OAAOC,I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,yBAAyB,CAC/BnC,SAAAA,EACAiC,UAAAA,EACAjC,SAAAA,EACAF,cAAckC,KAAAA,CAAAA,EACdhC,SAAAA;AAEH,SAAA;AACH,IAAA,CAAA,EAAG,EAAE,CAAA;IAEL,OAAOvB,OAAAA,CAAQ2D,qBAAqB,CAACR,KAAAA,CAAAA;AACvC;AAmBA;;AAEC,IACM,MAAMS,sBAAAA,GAAyB,CAAC3C,QAAAA,GACrC,CAAA,EAAGf,UAAAA,CAAWE,MAAM,CAAC,WAAW,EAAEa,QAAAA,CAAAA;;;;;;;;;;"}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import * as ts from 'typescript';
|
|
2
|
+
import { upperFirst, camelCase, isString, isNumber, isBoolean, isDate } from 'lodash';
|
|
3
|
+
import { compareEnglish } from '../compare.mjs';
|
|
4
|
+
|
|
5
|
+
const { factory } = ts;
|
|
6
|
+
const NAMESPACES = {
|
|
7
|
+
Struct: 'Struct',
|
|
8
|
+
Schema: 'Schema'
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Extract a valid interface name from a schema uid
|
|
12
|
+
*/ const getSchemaInterfaceName = (uid)=>upperFirst(camelCase(uid.replace(/(:.)/, ' ')));
|
|
13
|
+
const getSchemaModelType = (schema)=>{
|
|
14
|
+
const { modelType, kind } = schema;
|
|
15
|
+
// Components
|
|
16
|
+
if (modelType === 'component') {
|
|
17
|
+
return 'component';
|
|
18
|
+
}
|
|
19
|
+
// Content-Types
|
|
20
|
+
if (modelType === 'contentType') {
|
|
21
|
+
return kind;
|
|
22
|
+
}
|
|
23
|
+
return null;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Get the parent type name to extend based on the schema's nature
|
|
27
|
+
*/ const getSchemaExtendsTypeName = (schema)=>{
|
|
28
|
+
const base = getSchemaModelType(schema);
|
|
29
|
+
if (base === null) {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
return `${NAMESPACES.Struct}.${upperFirst(base)}Schema`;
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* Get a type node based on a type and its params
|
|
36
|
+
*/ const getTypeNode = (typeName, params = [])=>{
|
|
37
|
+
return factory.createTypeReferenceNode(factory.createIdentifier(typeName), params);
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* Transform a regular JavaScript object or scalar value into a literal expression
|
|
41
|
+
*/ const toTypeLiteral = (data)=>{
|
|
42
|
+
if (data === undefined) {
|
|
43
|
+
return factory.createLiteralTypeNode(ts.SyntaxKind.UndefinedKeyword);
|
|
44
|
+
}
|
|
45
|
+
if (data === null) {
|
|
46
|
+
return factory.createLiteralTypeNode(ts.SyntaxKind.NullKeyword);
|
|
47
|
+
}
|
|
48
|
+
if (isString(data)) {
|
|
49
|
+
return factory.createStringLiteral(data, true);
|
|
50
|
+
}
|
|
51
|
+
if (isNumber(data)) {
|
|
52
|
+
return data < 0 ? factory.createPrefixUnaryExpression(ts.SyntaxKind.MinusToken, factory.createNumericLiteral(-data)) : factory.createNumericLiteral(data);
|
|
53
|
+
}
|
|
54
|
+
if (isBoolean(data)) {
|
|
55
|
+
return data ? factory.createTrue() : factory.createFalse();
|
|
56
|
+
}
|
|
57
|
+
if (Array.isArray(data)) {
|
|
58
|
+
return factory.createTupleTypeNode(data.map((item)=>toTypeLiteral(item)));
|
|
59
|
+
}
|
|
60
|
+
if (isDate(data)) {
|
|
61
|
+
return factory.createStringLiteral(data.toISOString());
|
|
62
|
+
}
|
|
63
|
+
if (typeof data !== 'object') {
|
|
64
|
+
throw new Error(`Cannot convert to object literal. Unknown type "${typeof data}"`);
|
|
65
|
+
}
|
|
66
|
+
const entries = Object.entries(data).sort((a, b)=>compareEnglish(a[0], b[0]));
|
|
67
|
+
const props = entries.reduce((acc, [key, value])=>{
|
|
68
|
+
// Handle keys such as content-type-builder & co.
|
|
69
|
+
const identifier = key.includes('-') ? factory.createStringLiteral(key, true) : factory.createIdentifier(key);
|
|
70
|
+
return [
|
|
71
|
+
...acc,
|
|
72
|
+
factory.createPropertyDeclaration(undefined, identifier, undefined, toTypeLiteral(value), undefined)
|
|
73
|
+
];
|
|
74
|
+
}, []);
|
|
75
|
+
return factory.createTypeLiteralNode(props);
|
|
76
|
+
};
|
|
77
|
+
/**
|
|
78
|
+
* Add the Schema.Attribute namespace before the typename
|
|
79
|
+
*/ const withAttributeNamespace = (typeName)=>`${NAMESPACES.Schema}.Attribute.${typeName}`;
|
|
80
|
+
|
|
81
|
+
export { NAMESPACES, getSchemaExtendsTypeName, getSchemaInterfaceName, getSchemaModelType, getTypeNode, toTypeLiteral, withAttributeNamespace };
|
|
82
|
+
//# sourceMappingURL=utils.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.mjs","sources":["../../../../src/generators/common/models/utils.ts"],"sourcesContent":["import * as ts from 'typescript';\nimport {\n camelCase,\n isBoolean,\n isDate,\n isNumber,\n isString,\n matchesProperty,\n upperFirst,\n} from 'lodash';\nimport { compareEnglish } from '../compare';\n\nconst { factory } = ts;\n\nexport const NAMESPACES = {\n Struct: 'Struct',\n Schema: 'Schema',\n};\n\n/**\n * Shallow representation of a content-type/component attribute definition.\n * Only the fields read by the generators are typed; the rest is left open.\n */\nexport interface Attribute {\n type: string;\n conditions?: { visible?: unknown };\n [key: string]: any;\n}\n\n/**\n * Shallow representation of a content-type/component schema definition.\n * Only the fields read by the generators are typed; the rest is left open.\n */\nexport interface Schema {\n uid: string;\n modelType?: string;\n kind?: string;\n attributes: Record<string, Attribute>;\n [key: string]: any;\n}\n\n/**\n * Extract a valid interface name from a schema uid\n */\nexport const getSchemaInterfaceName = (uid: string): string =>\n upperFirst(camelCase(uid.replace(/(:.)/, ' ')));\n\nexport const getSchemaModelType = (schema: Schema): string | null | undefined => {\n const { modelType, kind } = schema;\n\n // Components\n if (modelType === 'component') {\n return 'component';\n }\n\n // Content-Types\n if (modelType === 'contentType') {\n return kind;\n }\n\n return null;\n};\n\n/**\n * Get the parent type name to extend based on the schema's nature\n */\nexport const getSchemaExtendsTypeName = (schema: Schema): string | null => {\n const base = getSchemaModelType(schema);\n\n if (base === null) {\n return null;\n }\n\n return `${NAMESPACES.Struct}.${upperFirst(base as string)}Schema`;\n};\n\n/**\n * Get a type node based on a type and its params\n */\nexport const getTypeNode = (typeName: string, params: ts.TypeNode[] = []): ts.TypeReferenceNode => {\n return factory.createTypeReferenceNode(factory.createIdentifier(typeName), params);\n};\n\n/**\n * Transform a regular JavaScript object or scalar value into a literal expression\n */\nexport const toTypeLiteral = (data: any): any => {\n if (data === undefined) {\n return factory.createLiteralTypeNode(ts.SyntaxKind.UndefinedKeyword as any);\n }\n\n if (data === null) {\n return factory.createLiteralTypeNode(ts.SyntaxKind.NullKeyword as any);\n }\n\n if (isString(data)) {\n return factory.createStringLiteral(data, true);\n }\n\n if (isNumber(data)) {\n return data < 0\n ? factory.createPrefixUnaryExpression(\n ts.SyntaxKind.MinusToken,\n factory.createNumericLiteral(-data)\n )\n : factory.createNumericLiteral(data);\n }\n\n if (isBoolean(data)) {\n return data ? factory.createTrue() : factory.createFalse();\n }\n\n if (Array.isArray(data)) {\n return factory.createTupleTypeNode(data.map((item) => toTypeLiteral(item)));\n }\n\n if (isDate(data)) {\n return factory.createStringLiteral(data.toISOString());\n }\n\n if (typeof data !== 'object') {\n throw new Error(`Cannot convert to object literal. Unknown type \"${typeof data}\"`);\n }\n\n const entries = Object.entries(data).sort((a, b) => compareEnglish(a[0], b[0]));\n\n const props = entries.reduce<any[]>((acc, [key, value]) => {\n // Handle keys such as content-type-builder & co.\n const identifier = key.includes('-')\n ? factory.createStringLiteral(key, true)\n : factory.createIdentifier(key);\n\n return [\n ...acc,\n factory.createPropertyDeclaration(\n undefined,\n identifier,\n undefined,\n toTypeLiteral(value),\n undefined\n ),\n ];\n }, []);\n\n return factory.createTypeLiteralNode(props);\n};\n\n/**\n * Get the number of attributes generated for a given schema definition\n */\nexport const getDefinitionAttributesCount = (\n definition: ts.InterfaceDeclaration\n): number | null => {\n const attributesNode = definition.members.find(\n matchesProperty('name.escapedText', 'attributes')\n ) as any;\n\n if (!attributesNode) {\n return null;\n }\n\n return attributesNode.type.members.length;\n};\n\n/**\n * Add the Schema.Attribute namespace before the typename\n */\nexport const withAttributeNamespace = (typeName: string): string =>\n `${NAMESPACES.Schema}.Attribute.${typeName}`;\n\n/**\n * Add the schema namespace before the typename\n */\nexport const withSchemaNamespace = (typeName: string): string =>\n `${(NAMESPACES as any).schema}.${typeName}`;\n"],"names":["factory","ts","NAMESPACES","Struct","Schema","getSchemaInterfaceName","uid","upperFirst","camelCase","replace","getSchemaModelType","schema","modelType","kind","getSchemaExtendsTypeName","base","getTypeNode","typeName","params","createTypeReferenceNode","createIdentifier","toTypeLiteral","data","undefined","createLiteralTypeNode","SyntaxKind","UndefinedKeyword","NullKeyword","isString","createStringLiteral","isNumber","createPrefixUnaryExpression","MinusToken","createNumericLiteral","isBoolean","createTrue","createFalse","Array","isArray","createTupleTypeNode","map","item","isDate","toISOString","Error","entries","Object","sort","a","b","compareEnglish","props","reduce","acc","key","value","identifier","includes","createPropertyDeclaration","createTypeLiteralNode","withAttributeNamespace"],"mappings":";;;;AAYA,MAAM,EAAEA,OAAO,EAAE,GAAGC,EAAAA;MAEPC,UAAAA,GAAa;IACxBC,MAAAA,EAAQ,QAAA;IACRC,MAAAA,EAAQ;AACV;AAwBA;;IAGO,MAAMC,sBAAAA,GAAyB,CAACC,GAAAA,GACrCC,UAAAA,CAAWC,SAAAA,CAAUF,GAAAA,CAAIG,OAAO,CAAC,MAAA,EAAQ,GAAA,CAAA,CAAA;AAEpC,MAAMC,qBAAqB,CAACC,MAAAA,GAAAA;AACjC,IAAA,MAAM,EAAEC,SAAS,EAAEC,IAAI,EAAE,GAAGF,MAAAA;;AAG5B,IAAA,IAAIC,cAAc,WAAA,EAAa;QAC7B,OAAO,WAAA;AACT,IAAA;;AAGA,IAAA,IAAIA,cAAc,aAAA,EAAe;QAC/B,OAAOC,IAAAA;AACT,I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/BnC,SAAAA,EACAiC,UAAAA,EACAjC,SAAAA,EACAF,cAAckC,KAAAA,CAAAA,EACdhC,SAAAA;AAEH,SAAA;AACH,IAAA,CAAA,EAAG,EAAE,CAAA;IAEL,OAAOvB,OAAAA,CAAQ2D,qBAAqB,CAACR,KAAAA,CAAAA;AACvC;AAmBA;;AAEC,IACM,MAAMS,sBAAAA,GAAyB,CAAC3C,QAAAA,GACrC,CAAA,EAAGf,UAAAA,CAAWE,MAAM,CAAC,WAAW,EAAEa,QAAAA,CAAAA;;;;"}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import * as ts from 'typescript';
|
|
2
|
+
import type { Logger } from '../utils';
|
|
3
|
+
export type AppModuleSource = {
|
|
4
|
+
uid: string;
|
|
5
|
+
file: string;
|
|
6
|
+
specifier: string;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Directory of each module kind in an API (src/api/<api>/<kind>), and in src/ for the kinds the
|
|
10
|
+
* loaders also register globally (policies, middlewares)
|
|
11
|
+
*/
|
|
12
|
+
export type AppModuleKind = 'services' | 'controllers' | 'policies' | 'middlewares';
|
|
13
|
+
/**
|
|
14
|
+
* Collect the global source files of a kind when `dirs.global` is given (src/<kind>/<name>.{ts,js}
|
|
15
|
+
* → global::<name>), then the ones of every API (src/api/<api>/<kind>/<name>.{ts,js} →
|
|
16
|
+
* api::<api>.<name>). The first file to resolve to a uid wins.
|
|
17
|
+
*/
|
|
18
|
+
export declare const collectAppSources: (kind: AppModuleKind, dirs: {
|
|
19
|
+
global?: string;
|
|
20
|
+
api: string;
|
|
21
|
+
}, outDir: string, logger: Logger) => Promise<AppModuleSource[]>;
|
|
22
|
+
/**
|
|
23
|
+
* Keep the sources whose uid the running app registered, sorted by uid
|
|
24
|
+
*/
|
|
25
|
+
export declare const filterRegisteredSources: (sources: AppModuleSource[], registered: Record<string, unknown>, logger: Logger) => AppModuleSource[];
|
|
26
|
+
/**
|
|
27
|
+
* Application tsconfigs do not enable `allowJs`, so the import of a JS source does not resolve and is
|
|
28
|
+
* an error type. Unlike `any`, conditional types cannot map it to `unknown`.
|
|
29
|
+
*/
|
|
30
|
+
export declare const isTypedSource: (source: AppModuleSource) => boolean;
|
|
31
|
+
/**
|
|
32
|
+
* The uids of the TypeScript sources whose module exports a type named `name`, such as a service's
|
|
33
|
+
* `ServiceContract`.
|
|
34
|
+
*
|
|
35
|
+
* Each source is parsed on its own, without a program: type generation runs on every `strapi develop`
|
|
36
|
+
* start, before the compilation, and a program would need the application's tsconfig and its whole
|
|
37
|
+
* dependency graph. Only top-level declarations and export specifiers are read (see
|
|
38
|
+
* `exportsTypeNamed`). `export *` is not followed, and a source that cannot be read is skipped with a
|
|
39
|
+
* warning: both keep the type the source registers without a contract, rather than stopping the
|
|
40
|
+
* generation.
|
|
41
|
+
*
|
|
42
|
+
* JS sources are skipped: their import does not resolve without `allowJs` (see `isTypedSource`).
|
|
43
|
+
*/
|
|
44
|
+
export declare const findTypeExports: (sources: AppModuleSource[], name: string, logger: Logger) => Promise<Set<string>>;
|
|
45
|
+
/**
|
|
46
|
+
* A type export that replaces the inferred type of the sources that have it, such as a service's
|
|
47
|
+
* `ServiceContract`.
|
|
48
|
+
*/
|
|
49
|
+
export type AppModuleContract = {
|
|
50
|
+
exportName: string;
|
|
51
|
+
helper: string;
|
|
52
|
+
uids: Set<string>;
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* The type a service or controller source registers:
|
|
56
|
+
* - `<contract helper><import('<source>').<contract export>>` when the module exports the contract;
|
|
57
|
+
* - `<helper><typeof import('<source>')>` otherwise, which infers it from the default export;
|
|
58
|
+
* - `unknown` for a JS source, whose import is an error type without `allowJs` (see `isTypedSource`).
|
|
59
|
+
*
|
|
60
|
+
* The contract does not depend on the default export, so the factory can be checked against its own
|
|
61
|
+
* uid (`Core.ServiceFactory<uid>`). The inferred type does: the generators list those uids in a
|
|
62
|
+
* marker registry (see `isInferredSource`), which the factory types reject.
|
|
63
|
+
*/
|
|
64
|
+
export declare const instanceTypeOf: (helper: string, logger: Logger, contract?: AppModuleContract) => (source: AppModuleSource) => ts.TypeNode;
|
|
65
|
+
/**
|
|
66
|
+
* Whether a source registers the type its module provides (see `instanceTypeOf`): a TypeScript
|
|
67
|
+
* source whose module does not export the contract.
|
|
68
|
+
*/
|
|
69
|
+
export declare const isInferredSource: (contract: AppModuleContract) => (source: AppModuleSource) => boolean;
|
|
70
|
+
/**
|
|
71
|
+
* typeof import('<relative path to the source>')
|
|
72
|
+
*/
|
|
73
|
+
export declare const typeofImport: (source: AppModuleSource) => ts.ImportTypeNode;
|
|
74
|
+
export declare const withDocComment: <TNode extends ts.Node>(node: TNode, comment: string) => TNode;
|
|
75
|
+
/**
|
|
76
|
+
* type <name><TModule> = TModule extends { default: infer TExport }
|
|
77
|
+
* ? 0 extends 1 & TExport
|
|
78
|
+
* ? unknown
|
|
79
|
+
* : TExport extends (...args: any[]) => infer TInstance ? TInstance : TExport
|
|
80
|
+
* : unknown;
|
|
81
|
+
*
|
|
82
|
+
* Emitted in each file so that services.ts and controllers.ts stand alone, and only when a source
|
|
83
|
+
* registers its inferred type.
|
|
84
|
+
*
|
|
85
|
+
* Resolves the type registered for a service or a controller from the type of its module: the return
|
|
86
|
+
* type of the default export when it is a factory, the default export itself otherwise. A module
|
|
87
|
+
* without a default export registers nothing usable at runtime; with strict types enabled, an
|
|
88
|
+
* unregistered uid resolves to `unknown`, so the helper does the same rather than claiming a
|
|
89
|
+
* permissive contract for it. A module typed `any` and a default export typed `any` resolve to
|
|
90
|
+
* `unknown` too: `any` would match the factory branch and register `any`.
|
|
91
|
+
*
|
|
92
|
+
* An import that no longer resolves (the source moved after the generation) is an error type, which
|
|
93
|
+
* no conditional type maps to `unknown`. The generated file is a `.ts` file, so `tsc` reports the
|
|
94
|
+
* TS2307 there, `skipLibCheck` or not, until the next generation drops the entry.
|
|
95
|
+
*/
|
|
96
|
+
export declare const generateInstanceHelperDefinition: (name: string, comment: string) => ts.TypeAliasDeclaration;
|
|
97
|
+
/**
|
|
98
|
+
* type <name><TContract> = 0 extends 1 & TContract ? unknown : TContract;
|
|
99
|
+
*
|
|
100
|
+
* Emitted only when at least one source exports a contract: an application without contracts does
|
|
101
|
+
* not get it.
|
|
102
|
+
*
|
|
103
|
+
* A contract typed `any` registers `unknown`, as for an unregistered uid, rather than turning every
|
|
104
|
+
* lookup of the uid into `any`.
|
|
105
|
+
*
|
|
106
|
+
* A contract reference that no longer resolves to a type (the export was renamed after the
|
|
107
|
+
* generation, or a specifier names a value) is an error type, which no conditional type can map to
|
|
108
|
+
* `unknown` (an alias instantiated with the error type stays the error type). The generated file is a
|
|
109
|
+
* `.ts` file, so `tsc` reports the TS2694/TS2749 there, `skipLibCheck` or not. The next generation,
|
|
110
|
+
* on every `strapi develop` start, registers the source without its contract again.
|
|
111
|
+
*/
|
|
112
|
+
export declare const generateContractHelperDefinition: (name: string, comment: string) => ts.TypeAliasDeclaration;
|
|
113
|
+
/**
|
|
114
|
+
* A registry that lists uids without typing them, such as the uids registered with their inferred
|
|
115
|
+
* type. Each uid maps to `true`, which nothing reads: the marker never depends on a source.
|
|
116
|
+
*/
|
|
117
|
+
export type AppModuleMarker = {
|
|
118
|
+
registry: string;
|
|
119
|
+
uids: string[];
|
|
120
|
+
};
|
|
121
|
+
/**
|
|
122
|
+
* export {};
|
|
123
|
+
*
|
|
124
|
+
* <helpers>
|
|
125
|
+
*
|
|
126
|
+
* declare global { … <registry> … <markers> … }
|
|
127
|
+
*
|
|
128
|
+
* The module marker turns the generated file into a module: the global augmentation is only allowed
|
|
129
|
+
* in one. The helpers come first so that the file stands alone. A marker without uids is not
|
|
130
|
+
* emitted, so that the output of an application without such uids does not change.
|
|
131
|
+
*/
|
|
132
|
+
export declare const emitRegistryDefinitions: (registry: string, helpers: ts.Node[], sources: AppModuleSource[], toValue: (source: AppModuleSource) => ts.TypeNode, markers?: AppModuleMarker[]) => Promise<string>;
|
|
133
|
+
//# sourceMappingURL=sources.d.ts.map
|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