@vaadin/hilla-generator-plugin-backbone 24.7.0-alpha9 → 24.8.0-alpha1
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/EndpointMethodOperationProcessor.d.ts +8 -11
- package/EndpointMethodOperationProcessor.js +51 -86
- package/EndpointMethodOperationProcessor.js.map +1 -7
- package/EndpointMethodRequestBodyProcessor.d.ts +12 -13
- package/EndpointMethodRequestBodyProcessor.js +55 -81
- package/EndpointMethodRequestBodyProcessor.js.map +1 -7
- package/EndpointMethodResponseProcessor.d.ts +10 -11
- package/EndpointMethodResponseProcessor.js +26 -28
- package/EndpointMethodResponseProcessor.js.map +1 -7
- package/EndpointProcessor.d.ts +8 -9
- package/EndpointProcessor.js +36 -57
- package/EndpointProcessor.js.map +1 -7
- package/EntityProcessor.d.ts +7 -7
- package/EntityProcessor.js +88 -136
- package/EntityProcessor.js.map +1 -7
- package/TypeSchemaProcessor.d.ts +8 -8
- package/TypeSchemaProcessor.js +75 -88
- package/TypeSchemaProcessor.js.map +1 -7
- package/index.d.ts +9 -10
- package/index.js +41 -51
- package/index.js.map +1 -7
- package/package.json +11 -32
- package/utils.d.ts +4 -5
- package/utils.js +27 -38
- package/utils.js.map +1 -7
- package/EndpointMethodOperationProcessor.d.ts.map +0 -1
- package/EndpointMethodRequestBodyProcessor.d.ts.map +0 -1
- package/EndpointMethodResponseProcessor.d.ts.map +0 -1
- package/EndpointProcessor.d.ts.map +0 -1
- package/EntityProcessor.d.ts.map +0 -1
- package/TypeSchemaProcessor.d.ts.map +0 -1
- package/index.d.ts.map +0 -1
- package/utils.d.ts.map +0 -1
package/EndpointProcessor.js
CHANGED
|
@@ -3,61 +3,40 @@ import createSourceFile from "@vaadin/hilla-generator-utils/createSourceFile.js"
|
|
|
3
3
|
import DependencyManager from "@vaadin/hilla-generator-utils/dependencies/DependencyManager.js";
|
|
4
4
|
import PathManager from "@vaadin/hilla-generator-utils/dependencies/PathManager.js";
|
|
5
5
|
import { OpenAPIV3 } from "openapi-types";
|
|
6
|
-
import EndpointMethodOperationProcessor
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
this.#createdFilePaths.createRelativePath(this.#name)
|
|
42
|
-
);
|
|
43
|
-
}
|
|
44
|
-
async #processMethod(method, pathItem) {
|
|
45
|
-
this.#owner.logger.debug(`Processing endpoint method: ${this.#name}.${method}`);
|
|
46
|
-
return (await Promise.all(
|
|
47
|
-
Object.values(OpenAPIV3.HttpMethods).filter((httpMethod) => pathItem[httpMethod]).map(
|
|
48
|
-
async (httpMethod) => EndpointMethodOperationProcessor.createProcessor(
|
|
49
|
-
httpMethod,
|
|
50
|
-
this.#name,
|
|
51
|
-
method,
|
|
52
|
-
pathItem[httpMethod],
|
|
53
|
-
this.#dependencies,
|
|
54
|
-
this.#owner
|
|
55
|
-
)?.process(this.#outputDir)
|
|
56
|
-
)
|
|
57
|
-
)).filter(Boolean);
|
|
58
|
-
}
|
|
6
|
+
import EndpointMethodOperationProcessor from "./EndpointMethodOperationProcessor.js";
|
|
7
|
+
export default class EndpointProcessor {
|
|
8
|
+
static async create(name, methods, storage, owner) {
|
|
9
|
+
const endpoint = new EndpointProcessor(name, methods, storage, owner);
|
|
10
|
+
endpoint.#dependencies.imports.default.add(endpoint.#dependencies.paths.createRelativePath(await ClientPlugin.getClientFileName(storage.outputDir)), "client");
|
|
11
|
+
return endpoint;
|
|
12
|
+
}
|
|
13
|
+
#createdFilePaths = new PathManager({ extension: "ts" });
|
|
14
|
+
#dependencies = new DependencyManager(new PathManager({ extension: ".js" }));
|
|
15
|
+
#methods;
|
|
16
|
+
#name;
|
|
17
|
+
#outputDir;
|
|
18
|
+
#transferTypes;
|
|
19
|
+
#owner;
|
|
20
|
+
constructor(name, methods, storage, owner) {
|
|
21
|
+
this.#name = name;
|
|
22
|
+
this.#owner = owner;
|
|
23
|
+
this.#methods = methods;
|
|
24
|
+
this.#outputDir = storage.outputDir;
|
|
25
|
+
this.#transferTypes = storage.transferTypes;
|
|
26
|
+
}
|
|
27
|
+
async process() {
|
|
28
|
+
this.#owner.logger.debug(`Processing endpoint: ${this.#name}`);
|
|
29
|
+
const statements = (await Promise.all(Array.from(this.#methods, async ([method, pathItem]) => this.#processMethod(method, pathItem)))).flatMap((item) => item);
|
|
30
|
+
const { exports, imports } = this.#dependencies;
|
|
31
|
+
return createSourceFile([
|
|
32
|
+
...imports.toCode(),
|
|
33
|
+
...statements,
|
|
34
|
+
...exports.toCode()
|
|
35
|
+
], this.#createdFilePaths.createRelativePath(this.#name));
|
|
36
|
+
}
|
|
37
|
+
async #processMethod(method, pathItem) {
|
|
38
|
+
this.#owner.logger.debug(`Processing endpoint method: ${this.#name}.${method}`);
|
|
39
|
+
return (await Promise.all(Object.values(OpenAPIV3.HttpMethods).filter((httpMethod) => pathItem[httpMethod]).map(async (httpMethod) => EndpointMethodOperationProcessor.createProcessor(httpMethod, this.#name, method, pathItem[httpMethod], this.#dependencies, this.#transferTypes, this.#owner)?.process(this.#outputDir)))).filter(Boolean);
|
|
40
|
+
}
|
|
59
41
|
}
|
|
60
|
-
|
|
61
|
-
EndpointProcessor as default
|
|
62
|
-
};
|
|
63
|
-
//# sourceMappingURL=EndpointProcessor.js.map
|
|
42
|
+
//# sourceMappingURL=./EndpointProcessor.js.map
|
package/EndpointProcessor.js.map
CHANGED
|
@@ -1,7 +1 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"sources": ["src/EndpointProcessor.ts"],
|
|
4
|
-
"sourcesContent": ["import type Plugin from '@vaadin/hilla-generator-core/Plugin.js';\nimport ClientPlugin from '@vaadin/hilla-generator-plugin-client';\nimport createSourceFile from '@vaadin/hilla-generator-utils/createSourceFile.js';\nimport DependencyManager from '@vaadin/hilla-generator-utils/dependencies/DependencyManager.js';\nimport PathManager from '@vaadin/hilla-generator-utils/dependencies/PathManager.js';\nimport { OpenAPIV3 } from 'openapi-types';\nimport type { ReadonlyDeep } from 'type-fest';\nimport type { SourceFile, Statement } from 'typescript';\nimport EndpointMethodOperationProcessor, {\n HILLA_FRONTEND_NAME,\n INIT_TYPE_NAME,\n} from './EndpointMethodOperationProcessor.js';\n\nexport default class EndpointProcessor {\n static async create(\n name: string,\n owner: Plugin,\n methods: Map<string, ReadonlyDeep<OpenAPIV3.PathItemObject>>,\n outputDir?: string,\n ): Promise<EndpointProcessor> {\n const endpoint = new EndpointProcessor(name, owner, methods, outputDir);\n endpoint.#dependencies.imports.default.add(\n endpoint.#dependencies.paths.createRelativePath(await ClientPlugin.getClientFileName(outputDir)),\n 'client',\n );\n endpoint.#dependencies.imports.named.add(\n endpoint.#dependencies.paths.createBareModulePath(HILLA_FRONTEND_NAME),\n INIT_TYPE_NAME,\n );\n return endpoint;\n }\n\n readonly #createdFilePaths = new PathManager({ extension: 'ts' });\n readonly #dependencies = new DependencyManager(new PathManager({ extension: '.js' }));\n readonly #methods: Map<string, ReadonlyDeep<OpenAPIV3.PathItemObject>>;\n readonly #name: string;\n readonly #outputDir: string | undefined;\n readonly #owner: Plugin;\n\n private constructor(\n name: string,\n owner: Plugin,\n methods: Map<string, ReadonlyDeep<OpenAPIV3.PathItemObject>>,\n outputDir?: string,\n ) {\n this.#name = name;\n this.#owner = owner;\n this.#methods = methods;\n this.#outputDir = outputDir;\n }\n\n async process(): Promise<SourceFile> {\n this.#owner.logger.debug(`Processing endpoint: ${this.#name}`);\n\n const statements = (\n await Promise.all(Array.from(this.#methods, async ([method, pathItem]) => this.#processMethod(method, pathItem)))\n ).flatMap((item) => item);\n\n const { exports, imports } = this.#dependencies;\n\n return createSourceFile(\n [...imports.toCode(), ...statements, ...exports.toCode()],\n this.#createdFilePaths.createRelativePath(this.#name),\n );\n }\n\n async #processMethod(\n method: string,\n pathItem: ReadonlyDeep<OpenAPIV3.PathItemObject>,\n ): Promise<readonly Statement[]> {\n this.#owner.logger.debug(`Processing endpoint method: ${this.#name}.${method}`);\n\n return (\n await Promise.all(\n Object.values(OpenAPIV3.HttpMethods)\n .filter((httpMethod) => pathItem[httpMethod])\n .map(async (httpMethod) =>\n EndpointMethodOperationProcessor.createProcessor(\n httpMethod,\n this.#name,\n method,\n pathItem[httpMethod]!,\n this.#dependencies,\n this.#owner,\n )?.process(this.#outputDir),\n ),\n )\n ).filter(Boolean) as readonly Statement[];\n }\n}\n"],
|
|
5
|
-
"mappings": "AACA,OAAO,kBAAkB;AACzB,OAAO,sBAAsB;AAC7B,OAAO,uBAAuB;AAC9B,OAAO,iBAAiB;AACxB,SAAS,iBAAiB;AAG1B,OAAO;AAAA,EACL;AAAA,EACA;AAAA,OACK;AAEP,MAAO,kBAAgC;AAAA,EACrC,aAAa,OACX,MACA,OACA,SACA,WAC4B;AAC5B,UAAM,WAAW,IAAI,kBAAkB,MAAM,OAAO,SAAS,SAAS;AACtE,aAAS,cAAc,QAAQ,QAAQ;AAAA,MACrC,SAAS,cAAc,MAAM,mBAAmB,MAAM,aAAa,kBAAkB,SAAS,CAAC;AAAA,MAC/F;AAAA,IACF;AACA,aAAS,cAAc,QAAQ,MAAM;AAAA,MACnC,SAAS,cAAc,MAAM,qBAAqB,mBAAmB;AAAA,MACrE;AAAA,IACF;AACA,WAAO;AAAA,EACT;AAAA,EAES,oBAAoB,IAAI,YAAY,EAAE,WAAW,KAAK,CAAC;AAAA,EACvD,gBAAgB,IAAI,kBAAkB,IAAI,YAAY,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CAAC,eAAe,SAAS,YAAY,CAC5C,IAAI,OAAO,eACV,iCAAiC,gBAC/B,YACA,KAAKA,OACL,QACA,SAAS,aACT,KAAKH,eACL,KAAKK,gBACL,KAAKC,OACN,EAAE,QAAQ,KAAKF,WAAW,CAC5B,CACJ,EACD,OAAO,QAAQ;CAClB;AACF","names":["name: string","methods: Map<string, OpenAPIV3.PathItemObject>","storage: SharedStorage","owner: Plugin","#dependencies","#createdFilePaths","#methods","#name","#outputDir","#transferTypes","#owner","#processMethod","method: string","pathItem: OpenAPIV3.PathItemObject"],"sources":["/opt/agent/work/1af72d8adc613024/hilla/packages/ts/generator-plugin-backbone/src/EndpointProcessor.ts"],"sourcesContent":["import type Plugin from '@vaadin/hilla-generator-core/Plugin.js';\nimport type { SharedStorage, TransferTypes } from '@vaadin/hilla-generator-core/SharedStorage.js';\nimport ClientPlugin from '@vaadin/hilla-generator-plugin-client';\nimport createSourceFile from '@vaadin/hilla-generator-utils/createSourceFile.js';\nimport DependencyManager from '@vaadin/hilla-generator-utils/dependencies/DependencyManager.js';\nimport PathManager from '@vaadin/hilla-generator-utils/dependencies/PathManager.js';\nimport { OpenAPIV3 } from 'openapi-types';\nimport type { SourceFile, Statement } from 'typescript';\nimport EndpointMethodOperationProcessor from './EndpointMethodOperationProcessor.js';\n\nexport default class EndpointProcessor {\n static async create(\n name: string,\n methods: Map<string, OpenAPIV3.PathItemObject>,\n storage: SharedStorage,\n owner: Plugin,\n ): Promise<EndpointProcessor> {\n const endpoint = new EndpointProcessor(name, methods, storage, owner);\n endpoint.#dependencies.imports.default.add(\n endpoint.#dependencies.paths.createRelativePath(await ClientPlugin.getClientFileName(storage.outputDir)),\n 'client',\n );\n return endpoint;\n }\n\n readonly #createdFilePaths = new PathManager({ extension: 'ts' });\n readonly #dependencies = new DependencyManager(new PathManager({ extension: '.js' }));\n readonly #methods: Map<string, OpenAPIV3.PathItemObject>;\n readonly #name: string;\n readonly #outputDir: string | undefined;\n readonly #transferTypes: TransferTypes;\n readonly #owner: Plugin;\n\n private constructor(\n name: string,\n methods: Map<string, OpenAPIV3.PathItemObject>,\n storage: SharedStorage,\n owner: Plugin,\n ) {\n this.#name = name;\n this.#owner = owner;\n this.#methods = methods;\n this.#outputDir = storage.outputDir;\n this.#transferTypes = storage.transferTypes;\n }\n\n async process(): Promise<SourceFile> {\n this.#owner.logger.debug(`Processing endpoint: ${this.#name}`);\n\n const statements = (\n await Promise.all(Array.from(this.#methods, async ([method, pathItem]) => this.#processMethod(method, pathItem)))\n ).flatMap((item) => item);\n\n const { exports, imports } = this.#dependencies;\n\n return createSourceFile(\n [...imports.toCode(), ...statements, ...exports.toCode()],\n this.#createdFilePaths.createRelativePath(this.#name),\n );\n }\n\n async #processMethod(method: string, pathItem: OpenAPIV3.PathItemObject): Promise<readonly Statement[]> {\n this.#owner.logger.debug(`Processing endpoint method: ${this.#name}.${method}`);\n\n return (\n await Promise.all(\n Object.values(OpenAPIV3.HttpMethods)\n .filter((httpMethod) => pathItem[httpMethod])\n .map(async (httpMethod) =>\n EndpointMethodOperationProcessor.createProcessor(\n httpMethod,\n this.#name,\n method,\n pathItem[httpMethod]!,\n this.#dependencies,\n this.#transferTypes,\n this.#owner,\n )?.process(this.#outputDir),\n ),\n )\n ).filter(Boolean) as readonly Statement[];\n }\n}\n"],"version":3}
|
package/EntityProcessor.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import type Plugin from
|
|
2
|
-
import { type Schema } from
|
|
3
|
-
import {
|
|
1
|
+
import type Plugin from "@vaadin/hilla-generator-core/Plugin.js";
|
|
2
|
+
import { type Schema } from "@vaadin/hilla-generator-core/Schema.js";
|
|
3
|
+
import type { SharedStorage } from "@vaadin/hilla-generator-core/SharedStorage.js";
|
|
4
|
+
import { type SourceFile } from "typescript";
|
|
4
5
|
export declare class EntityProcessor {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
#private;
|
|
7
|
+
constructor(name: string, component: Schema, storage: SharedStorage, owner: Plugin);
|
|
8
|
+
process(): SourceFile;
|
|
8
9
|
}
|
|
9
|
-
//# sourceMappingURL=EntityProcessor.d.ts.map
|
package/EntityProcessor.js
CHANGED
|
@@ -1,143 +1,95 @@
|
|
|
1
1
|
import { dirname } from "path/posix";
|
|
2
|
-
import {
|
|
3
|
-
convertReferenceSchemaToPath,
|
|
4
|
-
convertReferenceSchemaToSpecifier,
|
|
5
|
-
decomposeSchema,
|
|
6
|
-
isComposedSchema,
|
|
7
|
-
isEmptyObject,
|
|
8
|
-
isEnumSchema,
|
|
9
|
-
isNullableSchema,
|
|
10
|
-
isObjectSchema,
|
|
11
|
-
isReferenceSchema
|
|
12
|
-
} from "@vaadin/hilla-generator-core/Schema.js";
|
|
13
|
-
import {
|
|
14
|
-
convertFullyQualifiedNameToRelativePath,
|
|
15
|
-
simplifyFullyQualifiedName
|
|
16
|
-
} from "@vaadin/hilla-generator-core/utils.js";
|
|
2
|
+
import { convertReferenceSchemaToPath, convertReferenceSchemaToSpecifier, decomposeSchema, isComposedSchema, isEmptyObject, isEnumSchema, isNullableSchema, isObjectSchema, isReferenceSchema, convertFullyQualifiedNameToRelativePath, simplifyFullyQualifiedName } from "@vaadin/hilla-generator-core/Schema.js";
|
|
17
3
|
import createSourceFile from "@vaadin/hilla-generator-utils/createSourceFile.js";
|
|
18
4
|
import DependencyManager from "@vaadin/hilla-generator-utils/dependencies/DependencyManager.js";
|
|
19
5
|
import PathManager from "@vaadin/hilla-generator-utils/dependencies/PathManager.js";
|
|
20
|
-
import ts
|
|
21
|
-
} from "typescript";
|
|
6
|
+
import ts from "typescript";
|
|
22
7
|
import TypeSchemaProcessor from "./TypeSchemaProcessor.js";
|
|
23
8
|
import { findTypeParameters } from "./utils.js";
|
|
24
|
-
class EntityProcessor {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
}
|
|
110
|
-
return this.#processClass(schema);
|
|
111
|
-
}
|
|
112
|
-
#processParentClass(schema) {
|
|
113
|
-
const { imports, paths } = this.#dependencies;
|
|
114
|
-
const specifier = convertReferenceSchemaToSpecifier(schema);
|
|
115
|
-
const path = paths.createRelativePath(convertReferenceSchemaToPath(schema));
|
|
116
|
-
return imports.default.getIdentifier(path) ?? imports.default.add(path, specifier, true);
|
|
117
|
-
}
|
|
118
|
-
#processTypeElements({ properties }) {
|
|
119
|
-
return Object.entries(properties ?? {}).map(([name, schema]) => {
|
|
120
|
-
const [type] = new TypeSchemaProcessor(schema, this.#dependencies).process();
|
|
121
|
-
return ts.factory.createPropertySignature(
|
|
122
|
-
void 0,
|
|
123
|
-
name,
|
|
124
|
-
isNullableSchema(schema) ? ts.factory.createToken(ts.SyntaxKind.QuestionToken) : void 0,
|
|
125
|
-
type
|
|
126
|
-
);
|
|
127
|
-
});
|
|
128
|
-
}
|
|
129
|
-
static #processTypeParameters(schema) {
|
|
130
|
-
return findTypeParameters(schema)?.map(String).map(
|
|
131
|
-
(name) => ts.factory.createTypeParameterDeclaration(
|
|
132
|
-
void 0,
|
|
133
|
-
name,
|
|
134
|
-
void 0,
|
|
135
|
-
ts.factory.createKeywordTypeNode(ts.SyntaxKind.UnknownKeyword)
|
|
136
|
-
)
|
|
137
|
-
);
|
|
138
|
-
}
|
|
9
|
+
export class EntityProcessor {
|
|
10
|
+
#component;
|
|
11
|
+
#dependencies;
|
|
12
|
+
#fullyQualifiedName;
|
|
13
|
+
#name;
|
|
14
|
+
#outputPathManager = new PathManager({ extension: "ts" });
|
|
15
|
+
#transferTypes;
|
|
16
|
+
#owner;
|
|
17
|
+
#path;
|
|
18
|
+
constructor(name, component, storage, owner) {
|
|
19
|
+
this.#component = component;
|
|
20
|
+
this.#owner = owner;
|
|
21
|
+
this.#fullyQualifiedName = name;
|
|
22
|
+
this.#name = simplifyFullyQualifiedName(name);
|
|
23
|
+
this.#path = convertFullyQualifiedNameToRelativePath(name);
|
|
24
|
+
this.#dependencies = new DependencyManager(new PathManager({
|
|
25
|
+
extension: ".js",
|
|
26
|
+
relativeTo: dirname(this.#path)
|
|
27
|
+
}));
|
|
28
|
+
this.#transferTypes = storage.transferTypes;
|
|
29
|
+
}
|
|
30
|
+
get #id() {
|
|
31
|
+
const id = ts.factory.createIdentifier(this.#name);
|
|
32
|
+
this.#dependencies.exports.default.set(id);
|
|
33
|
+
return id;
|
|
34
|
+
}
|
|
35
|
+
process() {
|
|
36
|
+
this.#owner.logger.debug(`Processing entity: ${this.#name}`);
|
|
37
|
+
const declaration = isEnumSchema(this.#component) ? this.#processEnum(this.#component) : this.#processExtendedClass(this.#component);
|
|
38
|
+
const statements = declaration ? [declaration] : [];
|
|
39
|
+
const { exports, imports } = this.#dependencies;
|
|
40
|
+
return createSourceFile([
|
|
41
|
+
...imports.toCode(),
|
|
42
|
+
...statements,
|
|
43
|
+
...exports.toCode()
|
|
44
|
+
], this.#outputPathManager.createRelativePath(this.#path));
|
|
45
|
+
}
|
|
46
|
+
#processClass(schema) {
|
|
47
|
+
const { logger } = this.#owner;
|
|
48
|
+
if (!isObjectSchema(schema)) {
|
|
49
|
+
logger.debug(schema, `Component is not an object: '${this.#fullyQualifiedName}'`);
|
|
50
|
+
return undefined;
|
|
51
|
+
}
|
|
52
|
+
if (isEmptyObject(schema)) {
|
|
53
|
+
logger.debug(`Component has no properties:' ${this.#fullyQualifiedName}'`);
|
|
54
|
+
}
|
|
55
|
+
return ts.factory.createInterfaceDeclaration(undefined, this.#id, EntityProcessor.#processTypeParameters(schema), undefined, this.#processTypeElements(schema));
|
|
56
|
+
}
|
|
57
|
+
#processEnum({ enum: members }) {
|
|
58
|
+
return ts.factory.createEnumDeclaration(undefined, this.#id, members.map((member) => ts.factory.createEnumMember(member, ts.factory.createStringLiteral(member))));
|
|
59
|
+
}
|
|
60
|
+
#processExtendedClass(schema) {
|
|
61
|
+
const { logger } = this.#owner;
|
|
62
|
+
if (isComposedSchema(schema)) {
|
|
63
|
+
const decomposed = decomposeSchema(schema);
|
|
64
|
+
if (decomposed.length > 2) {
|
|
65
|
+
logger.debug(schema, `Schema for '${this.#fullyQualifiedName}' has more than two components. This plugin will ignore it.`);
|
|
66
|
+
return undefined;
|
|
67
|
+
}
|
|
68
|
+
const [parent, child] = decomposed;
|
|
69
|
+
if (!isReferenceSchema(parent)) {
|
|
70
|
+
logger.debug(parent, "Only reference schema allowed for parent class");
|
|
71
|
+
return undefined;
|
|
72
|
+
}
|
|
73
|
+
const declaration = this.#processClass(child);
|
|
74
|
+
const identifier = this.#processParentClass(parent);
|
|
75
|
+
return declaration && ts.factory.updateInterfaceDeclaration(declaration, declaration.modifiers, declaration.name, declaration.typeParameters, [ts.factory.createHeritageClause(ts.SyntaxKind.ExtendsKeyword, [ts.factory.createExpressionWithTypeArguments(identifier, undefined)])], declaration.members);
|
|
76
|
+
}
|
|
77
|
+
return this.#processClass(schema);
|
|
78
|
+
}
|
|
79
|
+
#processParentClass(schema) {
|
|
80
|
+
const { imports, paths } = this.#dependencies;
|
|
81
|
+
const specifier = convertReferenceSchemaToSpecifier(schema);
|
|
82
|
+
const path = paths.createRelativePath(convertReferenceSchemaToPath(schema));
|
|
83
|
+
return imports.default.getIdentifier(path) ?? imports.default.add(path, specifier, true);
|
|
84
|
+
}
|
|
85
|
+
#processTypeElements({ properties }) {
|
|
86
|
+
return Object.entries(properties ?? {}).map(([name, schema]) => {
|
|
87
|
+
const [type] = new TypeSchemaProcessor(schema, this.#dependencies, this.#transferTypes).process();
|
|
88
|
+
return ts.factory.createPropertySignature(undefined, name, isNullableSchema(schema) ? ts.factory.createToken(ts.SyntaxKind.QuestionToken) : undefined, type);
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
static #processTypeParameters(schema) {
|
|
92
|
+
return findTypeParameters(schema)?.map(String).map((name) => ts.factory.createTypeParameterDeclaration(undefined, name, undefined, ts.factory.createKeywordTypeNode(ts.SyntaxKind.UnknownKeyword)));
|
|
93
|
+
}
|
|
139
94
|
}
|
|
140
|
-
|
|
141
|
-
EntityProcessor
|
|
142
|
-
};
|
|
143
|
-
//# sourceMappingURL=EntityProcessor.js.map
|
|
95
|
+
//# sourceMappingURL=./EntityProcessor.js.map
|
package/EntityProcessor.js.map
CHANGED
|
@@ -1,7 +1 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"sources": ["src/EntityProcessor.ts"],
|
|
4
|
-
"sourcesContent": ["import { dirname } from 'path/posix';\nimport type Plugin from '@vaadin/hilla-generator-core/Plugin.js';\nimport {\n type EnumSchema,\n type ReferenceSchema,\n type Schema,\n convertReferenceSchemaToPath,\n convertReferenceSchemaToSpecifier,\n decomposeSchema,\n isComposedSchema,\n isEmptyObject,\n isEnumSchema,\n isNullableSchema,\n isObjectSchema,\n isReferenceSchema,\n type ObjectSchema,\n} from '@vaadin/hilla-generator-core/Schema.js';\nimport {\n convertFullyQualifiedNameToRelativePath,\n simplifyFullyQualifiedName,\n} from '@vaadin/hilla-generator-core/utils.js';\nimport createSourceFile from '@vaadin/hilla-generator-utils/createSourceFile.js';\nimport DependencyManager from '@vaadin/hilla-generator-utils/dependencies/DependencyManager.js';\nimport PathManager from '@vaadin/hilla-generator-utils/dependencies/PathManager.js';\nimport ts, {\n type Identifier,\n type InterfaceDeclaration,\n type SourceFile,\n type Statement,\n type TypeElement,\n} from 'typescript';\nimport TypeSchemaProcessor from './TypeSchemaProcessor.js';\nimport { findTypeParameters } from './utils.js';\n\nexport class EntityProcessor {\n readonly #component: Schema;\n readonly #dependencies;\n readonly #fullyQualifiedName: string;\n readonly #name: string;\n readonly #outputPathManager = new PathManager({ extension: 'ts' });\n readonly #owner: Plugin;\n readonly #path: string;\n\n constructor(name: string, component: Schema, owner: Plugin) {\n this.#component = component;\n this.#owner = owner;\n this.#fullyQualifiedName = name;\n this.#name = simplifyFullyQualifiedName(name);\n this.#path = convertFullyQualifiedNameToRelativePath(name);\n this.#dependencies = new DependencyManager(new PathManager({ extension: '.js', relativeTo: dirname(this.#path) }));\n }\n\n get #id(): Identifier {\n const id = ts.factory.createIdentifier(this.#name);\n\n this.#dependencies.exports.default.set(id);\n\n return id;\n }\n\n process(): SourceFile {\n this.#owner.logger.debug(`Processing entity: ${this.#name}`);\n\n const declaration = isEnumSchema(this.#component)\n ? this.#processEnum(this.#component)\n : this.#processExtendedClass(this.#component);\n\n const statements = declaration ? [declaration] : [];\n\n const { exports, imports } = this.#dependencies;\n\n return createSourceFile(\n [...imports.toCode(), ...statements, ...exports.toCode()],\n this.#outputPathManager.createRelativePath(this.#path),\n );\n }\n\n #processClass(schema: Schema): InterfaceDeclaration | undefined {\n const { logger } = this.#owner;\n\n if (!isObjectSchema(schema)) {\n logger.debug(schema, `Component is not an object: '${this.#fullyQualifiedName}'`);\n return undefined;\n }\n\n if (isEmptyObject(schema)) {\n logger.debug(`Component has no properties:' ${this.#fullyQualifiedName}'`);\n }\n\n return ts.factory.createInterfaceDeclaration(\n undefined,\n this.#id,\n EntityProcessor.#processTypeParameters(schema),\n undefined,\n this.#processTypeElements(schema as ObjectSchema),\n );\n }\n\n #processEnum({ enum: members }: EnumSchema): Statement {\n return ts.factory.createEnumDeclaration(\n undefined,\n this.#id,\n members.map((member) => ts.factory.createEnumMember(member, ts.factory.createStringLiteral(member))),\n );\n }\n\n #processExtendedClass(schema: Schema): Statement | undefined {\n const { logger } = this.#owner;\n\n if (isComposedSchema(schema)) {\n const decomposed = decomposeSchema(schema);\n\n if (decomposed.length > 2) {\n logger.debug(\n schema,\n `Schema for '${this.#fullyQualifiedName}' has more than two components. This plugin will ignore it.`,\n );\n return undefined;\n }\n\n const [parent, child] = decomposed;\n\n if (!isReferenceSchema(parent)) {\n logger.debug(parent, 'Only reference schema allowed for parent class');\n return undefined;\n }\n\n const declaration = this.#processClass(child);\n const identifier = this.#processParentClass(parent);\n\n return (\n declaration &&\n ts.factory.updateInterfaceDeclaration(\n declaration,\n declaration.modifiers,\n declaration.name,\n declaration.typeParameters,\n [\n ts.factory.createHeritageClause(ts.SyntaxKind.ExtendsKeyword, [\n ts.factory.createExpressionWithTypeArguments(identifier, undefined),\n ]),\n ],\n declaration.members,\n )\n );\n }\n\n return this.#processClass(schema);\n }\n\n #processParentClass(schema: ReferenceSchema): Identifier {\n const { imports, paths } = this.#dependencies;\n\n const specifier = convertReferenceSchemaToSpecifier(schema);\n const path = paths.createRelativePath(convertReferenceSchemaToPath(schema));\n\n return imports.default.getIdentifier(path) ?? imports.default.add(path, specifier, true);\n }\n\n #processTypeElements({ properties }: ObjectSchema): readonly TypeElement[] {\n return Object.entries(properties ?? {}).map(([name, schema]) => {\n const [type] = new TypeSchemaProcessor(schema, this.#dependencies).process();\n\n return ts.factory.createPropertySignature(\n undefined,\n name,\n isNullableSchema(schema) ? ts.factory.createToken(ts.SyntaxKind.QuestionToken) : undefined,\n type,\n );\n });\n }\n\n static #processTypeParameters(schema: Schema): readonly ts.TypeParameterDeclaration[] | undefined {\n return findTypeParameters(schema)\n ?.map(String)\n .map((name) =>\n ts.factory.createTypeParameterDeclaration(\n undefined,\n name,\n undefined,\n ts.factory.createKeywordTypeNode(ts.SyntaxKind.UnknownKeyword),\n ),\n );\n }\n}\n"],
|
|
5
|
-
"mappings": "AAAA,SAAS,eAAe;AAExB;AAAA,EAIE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OAEK;AACP;AAAA,EACE;AAAA,EACA;AAAA,OACK;AACP,OAAO,sBAAsB;AAC7B,OAAO,uBAAuB;AAC9B,OAAO,iBAAiB;AACxB,OAAO;AAAA,OAMA;AACP,OAAO,yBAAyB;AAChC,SAAS,0BAA0B;AAE5B,MAAM,gBAAgB;AAAA,EAClB;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA,qBAAqB,IAAI,YAAY,EAAE,WAAW,KAAK,CAAC;AAAA,EACxD;AAAA,EACA;AAAA,EAET,YAAY,MAAc,WAAmB,OAAe;AAC1D,SAAK,aAAa;AAClB,SAAK,SAAS;AACd,SAAK,sBAAsB;AAC3B,SAAK,QAAQ,2BAA2B,IAAI;AAC5C,SAAK,QAAQ,wCAAwC,IAAI;AACzD,SAAK,gBAAgB,IAAI,kBAAkB,IAAI,YAAY,EAAE,WAAW,OAAO,YAAY,QAAQ,KAAK,KAAK,EAAE,CAAC,CAAC;AAAA,EACnH;AAAA,EAEA,IAAI,MAAkB;AACpB,UAAM,KAAK,GAAG,QAAQ,iBAAiB,KAAK,KAA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#path","name: string","component: Schema","storage: SharedStorage","owner: Plugin","#id","#processEnum","#processExtendedClass","schema: Schema","#processTypeParameters","#processTypeElements","#processClass","#processParentClass","schema: ReferenceSchema"],"sources":["/opt/agent/work/1af72d8adc613024/hilla/packages/ts/generator-plugin-backbone/src/EntityProcessor.ts"],"sourcesContent":["import { dirname } from 'path/posix';\nimport type Plugin from '@vaadin/hilla-generator-core/Plugin.js';\nimport {\n type EnumSchema,\n type ReferenceSchema,\n type Schema,\n convertReferenceSchemaToPath,\n convertReferenceSchemaToSpecifier,\n decomposeSchema,\n isComposedSchema,\n isEmptyObject,\n isEnumSchema,\n isNullableSchema,\n isObjectSchema,\n isReferenceSchema,\n type ObjectSchema,\n convertFullyQualifiedNameToRelativePath,\n simplifyFullyQualifiedName,\n} from '@vaadin/hilla-generator-core/Schema.js';\nimport type { SharedStorage, TransferTypes } from '@vaadin/hilla-generator-core/SharedStorage.js';\nimport createSourceFile from '@vaadin/hilla-generator-utils/createSourceFile.js';\nimport DependencyManager from '@vaadin/hilla-generator-utils/dependencies/DependencyManager.js';\nimport PathManager from '@vaadin/hilla-generator-utils/dependencies/PathManager.js';\nimport ts, {\n type Identifier,\n type InterfaceDeclaration,\n type SourceFile,\n type Statement,\n type TypeElement,\n type TypeParameterDeclaration,\n} from 'typescript';\nimport TypeSchemaProcessor from './TypeSchemaProcessor.js';\nimport { findTypeParameters } from './utils.js';\n\nexport class EntityProcessor {\n readonly #component: Schema;\n readonly #dependencies;\n readonly #fullyQualifiedName: string;\n readonly #name: string;\n readonly #outputPathManager = new PathManager({ extension: 'ts' });\n readonly #transferTypes: TransferTypes;\n readonly #owner: Plugin;\n readonly #path: string;\n\n constructor(name: string, component: Schema, storage: SharedStorage, owner: Plugin) {\n this.#component = component;\n this.#owner = owner;\n this.#fullyQualifiedName = name;\n this.#name = simplifyFullyQualifiedName(name);\n this.#path = convertFullyQualifiedNameToRelativePath(name);\n this.#dependencies = new DependencyManager(new PathManager({ extension: '.js', relativeTo: dirname(this.#path) }));\n this.#transferTypes = storage.transferTypes;\n }\n\n get #id(): Identifier {\n const id = ts.factory.createIdentifier(this.#name);\n\n this.#dependencies.exports.default.set(id);\n\n return id;\n }\n\n process(): SourceFile {\n this.#owner.logger.debug(`Processing entity: ${this.#name}`);\n\n const declaration = isEnumSchema(this.#component)\n ? this.#processEnum(this.#component)\n : this.#processExtendedClass(this.#component);\n\n const statements = declaration ? [declaration] : [];\n\n const { exports, imports } = this.#dependencies;\n\n return createSourceFile(\n [...imports.toCode(), ...statements, ...exports.toCode()],\n this.#outputPathManager.createRelativePath(this.#path),\n );\n }\n\n #processClass(schema: Schema): InterfaceDeclaration | undefined {\n const { logger } = this.#owner;\n\n if (!isObjectSchema(schema)) {\n logger.debug(schema, `Component is not an object: '${this.#fullyQualifiedName}'`);\n return undefined;\n }\n\n if (isEmptyObject(schema)) {\n logger.debug(`Component has no properties:' ${this.#fullyQualifiedName}'`);\n }\n\n return ts.factory.createInterfaceDeclaration(\n undefined,\n this.#id,\n EntityProcessor.#processTypeParameters(schema),\n undefined,\n this.#processTypeElements(schema as ObjectSchema),\n );\n }\n\n #processEnum({ enum: members }: EnumSchema): Statement {\n return ts.factory.createEnumDeclaration(\n undefined,\n this.#id,\n members.map((member) => ts.factory.createEnumMember(member, ts.factory.createStringLiteral(member))),\n );\n }\n\n #processExtendedClass(schema: Schema): Statement | undefined {\n const { logger } = this.#owner;\n\n if (isComposedSchema(schema)) {\n const decomposed = decomposeSchema(schema);\n\n if (decomposed.length > 2) {\n logger.debug(\n schema,\n `Schema for '${this.#fullyQualifiedName}' has more than two components. This plugin will ignore it.`,\n );\n return undefined;\n }\n\n const [parent, child] = decomposed;\n\n if (!isReferenceSchema(parent)) {\n logger.debug(parent, 'Only reference schema allowed for parent class');\n return undefined;\n }\n\n const declaration = this.#processClass(child);\n const identifier = this.#processParentClass(parent);\n\n return (\n declaration &&\n ts.factory.updateInterfaceDeclaration(\n declaration,\n declaration.modifiers,\n declaration.name,\n declaration.typeParameters,\n [\n ts.factory.createHeritageClause(ts.SyntaxKind.ExtendsKeyword, [\n ts.factory.createExpressionWithTypeArguments(identifier, undefined),\n ]),\n ],\n declaration.members,\n )\n );\n }\n\n return this.#processClass(schema);\n }\n\n #processParentClass(schema: ReferenceSchema): Identifier {\n const { imports, paths } = this.#dependencies;\n\n const specifier = convertReferenceSchemaToSpecifier(schema);\n const path = paths.createRelativePath(convertReferenceSchemaToPath(schema));\n\n return imports.default.getIdentifier(path) ?? imports.default.add(path, specifier, true);\n }\n\n #processTypeElements({ properties }: ObjectSchema): readonly TypeElement[] {\n return Object.entries(properties ?? {}).map(([name, schema]) => {\n const [type] = new TypeSchemaProcessor(schema, this.#dependencies, this.#transferTypes).process();\n\n return ts.factory.createPropertySignature(\n undefined,\n name,\n isNullableSchema(schema) ? ts.factory.createToken(ts.SyntaxKind.QuestionToken) : undefined,\n type,\n );\n });\n }\n\n static #processTypeParameters(schema: Schema): readonly TypeParameterDeclaration[] | undefined {\n return findTypeParameters(schema)\n ?.map(String)\n .map((name) =>\n ts.factory.createTypeParameterDeclaration(\n undefined,\n name,\n undefined,\n ts.factory.createKeywordTypeNode(ts.SyntaxKind.UnknownKeyword),\n ),\n );\n }\n}\n"],"version":3}
|
package/TypeSchemaProcessor.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { type Schema } from
|
|
2
|
-
import type
|
|
3
|
-
import
|
|
1
|
+
import { type Schema } from "@vaadin/hilla-generator-core/Schema.js";
|
|
2
|
+
import type { TransferTypeMaker } from "@vaadin/hilla-generator-core/SharedStorage.js";
|
|
3
|
+
import type DependencyManager from "@vaadin/hilla-generator-utils/dependencies/DependencyManager.js";
|
|
4
|
+
import { type TypeNode } from "typescript";
|
|
4
5
|
export default class TypeSchemaProcessor {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
#private;
|
|
7
|
+
["constructor"]: typeof TypeSchemaProcessor;
|
|
8
|
+
constructor(schema: Schema, dependencies: DependencyManager, transferTypes: Map<string, TransferTypeMaker>);
|
|
9
|
+
process(): readonly TypeNode[];
|
|
9
10
|
}
|
|
10
|
-
//# sourceMappingURL=TypeSchemaProcessor.d.ts.map
|
package/TypeSchemaProcessor.js
CHANGED
|
@@ -1,102 +1,89 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
convertReferenceSchemaToSpecifier,
|
|
4
|
-
decomposeSchema,
|
|
5
|
-
isArraySchema,
|
|
6
|
-
isBooleanSchema,
|
|
7
|
-
isComposedSchema,
|
|
8
|
-
isIntegerSchema,
|
|
9
|
-
isMapSchema,
|
|
10
|
-
isNullableSchema,
|
|
11
|
-
isNumberSchema,
|
|
12
|
-
isReferenceSchema,
|
|
13
|
-
isStringSchema
|
|
14
|
-
} from "@vaadin/hilla-generator-core/Schema.js";
|
|
15
|
-
import ts, {} from "typescript";
|
|
1
|
+
import { convertFullyQualifiedNameToRelativePath, convertReferenceSchemaToFullyQualifiedName, convertReferenceSchemaToSpecifier, decomposeSchema, isArraySchema, isBooleanSchema, isComposedSchema, isIntegerSchema, isMapSchema, isNullableSchema, isNumberSchema, isReferenceSchema, isStringSchema } from "@vaadin/hilla-generator-core/Schema.js";
|
|
2
|
+
import ts from "typescript";
|
|
16
3
|
import { findTypeArguments, findTypeVariable } from "./utils.js";
|
|
17
4
|
function createBoolean() {
|
|
18
|
-
|
|
5
|
+
return ts.factory.createKeywordTypeNode(ts.SyntaxKind.BooleanKeyword);
|
|
19
6
|
}
|
|
20
7
|
function createNumber() {
|
|
21
|
-
|
|
8
|
+
return ts.factory.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword);
|
|
22
9
|
}
|
|
23
10
|
function createString() {
|
|
24
|
-
|
|
11
|
+
return ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword);
|
|
25
12
|
}
|
|
26
13
|
function createUndefined() {
|
|
27
|
-
|
|
14
|
+
return ts.factory.createKeywordTypeNode(ts.SyntaxKind.UndefinedKeyword);
|
|
28
15
|
}
|
|
29
16
|
function createUnknown() {
|
|
30
|
-
|
|
17
|
+
return ts.factory.createKeywordTypeNode(ts.SyntaxKind.UnknownKeyword);
|
|
31
18
|
}
|
|
32
19
|
function unwrapPossiblyNullableSchema(schema) {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
20
|
+
if (isComposedSchema(schema)) {
|
|
21
|
+
const [result] = decomposeSchema(schema);
|
|
22
|
+
return result;
|
|
23
|
+
}
|
|
24
|
+
return schema;
|
|
38
25
|
}
|
|
39
|
-
class TypeSchemaProcessor {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
26
|
+
export default class TypeSchemaProcessor {
|
|
27
|
+
#dependencies;
|
|
28
|
+
#schema;
|
|
29
|
+
#transferTypes;
|
|
30
|
+
constructor(schema, dependencies, transferTypes) {
|
|
31
|
+
this.#schema = schema;
|
|
32
|
+
this.#dependencies = dependencies;
|
|
33
|
+
this.#transferTypes = transferTypes;
|
|
34
|
+
}
|
|
35
|
+
process() {
|
|
36
|
+
let node;
|
|
37
|
+
const unwrappedSchema = unwrapPossiblyNullableSchema(this.#schema);
|
|
38
|
+
const typeVariable = findTypeVariable(this.#schema);
|
|
39
|
+
if (typeVariable) {
|
|
40
|
+
return [ts.factory.createTypeReferenceNode(typeVariable)];
|
|
41
|
+
}
|
|
42
|
+
if (isReferenceSchema(unwrappedSchema)) {
|
|
43
|
+
const typeArguments = this.#processTypeArguments(this.#schema);
|
|
44
|
+
node = this.#processReference(unwrappedSchema, typeArguments);
|
|
45
|
+
} else if (isArraySchema(unwrappedSchema)) {
|
|
46
|
+
node = this.#processArray(unwrappedSchema);
|
|
47
|
+
} else if (isMapSchema(unwrappedSchema)) {
|
|
48
|
+
node = this.#processMap(unwrappedSchema);
|
|
49
|
+
} else if (isBooleanSchema(unwrappedSchema)) {
|
|
50
|
+
node = createBoolean();
|
|
51
|
+
} else if (isIntegerSchema(unwrappedSchema) || isNumberSchema(unwrappedSchema)) {
|
|
52
|
+
node = createNumber();
|
|
53
|
+
} else if (isStringSchema(unwrappedSchema)) {
|
|
54
|
+
node = createString();
|
|
55
|
+
} else {
|
|
56
|
+
node = createUnknown();
|
|
57
|
+
}
|
|
58
|
+
return isNullableSchema(this.#schema) ? [node, createUndefined()] : [node];
|
|
59
|
+
}
|
|
60
|
+
#processArray(schema) {
|
|
61
|
+
const nodes = new TypeSchemaProcessor(schema.items, this.#dependencies, this.#transferTypes).process();
|
|
62
|
+
return ts.factory.createTypeReferenceNode("Array", [ts.factory.createUnionTypeNode(nodes)]);
|
|
63
|
+
}
|
|
64
|
+
#processMap({ additionalProperties: valuesType }) {
|
|
65
|
+
let valuesTypeNode;
|
|
66
|
+
if (typeof valuesType !== "boolean") {
|
|
67
|
+
const nodes = new TypeSchemaProcessor(valuesType, this.#dependencies, this.#transferTypes).process();
|
|
68
|
+
valuesTypeNode = ts.factory.createUnionTypeNode(nodes);
|
|
69
|
+
} else {
|
|
70
|
+
valuesTypeNode = ts.factory.createKeywordTypeNode(ts.SyntaxKind.UnknownKeyword);
|
|
71
|
+
}
|
|
72
|
+
return ts.factory.createTypeReferenceNode("Record", [ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword), valuesTypeNode]);
|
|
73
|
+
}
|
|
74
|
+
#processTypeArguments(schema) {
|
|
75
|
+
return findTypeArguments(schema)?.allOf.map((s) => new TypeSchemaProcessor(s, this.#dependencies, this.#transferTypes).process()).map((t) => ts.factory.createUnionTypeNode(t));
|
|
76
|
+
}
|
|
77
|
+
#processReference(schema, typeArguments) {
|
|
78
|
+
const { imports, paths } = this.#dependencies;
|
|
79
|
+
const fullyQualifiedName = convertReferenceSchemaToFullyQualifiedName(schema);
|
|
80
|
+
if (this.#transferTypes.has(fullyQualifiedName)) {
|
|
81
|
+
return this.#transferTypes.get(fullyQualifiedName)(typeArguments);
|
|
82
|
+
}
|
|
83
|
+
const specifier = convertReferenceSchemaToSpecifier(schema);
|
|
84
|
+
const path = paths.createRelativePath(convertFullyQualifiedNameToRelativePath(fullyQualifiedName));
|
|
85
|
+
const identifier = imports.default.getIdentifier(path) ?? imports.default.add(path, specifier, true);
|
|
86
|
+
return ts.factory.createTypeReferenceNode(identifier, typeArguments);
|
|
87
|
+
}
|
|
98
88
|
}
|
|
99
|
-
|
|
100
|
-
TypeSchemaProcessor as default
|
|
101
|
-
};
|
|
102
|
-
//# sourceMappingURL=TypeSchemaProcessor.js.map
|
|
89
|
+
//# sourceMappingURL=./TypeSchemaProcessor.js.map
|