@opra/cli 1.0.0-alpha.10 → 1.0.0-alpha.12
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/cjs/ts-generator/processors/process-data-types.js +116 -120
- package/cjs/ts-generator/processors/process-http-controller.js +38 -25
- package/cjs/ts-generator/ts-generator.js +2 -1
- package/esm/ts-generator/processors/process-data-types.js +113 -117
- package/esm/ts-generator/processors/process-http-controller.js +38 -25
- package/esm/ts-generator/ts-generator.js +3 -2
- package/package.json +3 -3
- package/types/ts-generator/processors/process-data-types.d.ts +19 -8
- package/types/ts-generator/ts-generator.d.ts +4 -4
|
@@ -1,116 +1,128 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.generateMappedTypeDefinition = exports.generateMixinTypeDefinition = exports.generateSimpleTypeDefinition = exports.generateComplexTypeDefinition = exports.generateEnumTypeDefinition = exports.generateTypeDefinition = exports.processDataType = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
5
|
const common_1 = require("@opra/common");
|
|
6
6
|
const path_1 = tslib_1.__importDefault(require("path"));
|
|
7
7
|
const code_block_js_1 = require("../../code-block.js");
|
|
8
8
|
const string_utils_js_1 = require("../utils/string-utils.js");
|
|
9
9
|
const internalTypeNames = ['any', 'boolean', 'bigint', 'number', 'null', 'string', 'object'];
|
|
10
|
-
async function processDataType(dataType) {
|
|
10
|
+
async function processDataType(dataType, currentFile, intent) {
|
|
11
11
|
const doc = dataType.node.getDocument();
|
|
12
12
|
if (doc.id !== this._document?.id) {
|
|
13
13
|
const { generator } = await this.processDocument(doc);
|
|
14
|
-
return await generator.processDataType(dataType);
|
|
14
|
+
return await generator.processDataType(dataType, currentFile, intent);
|
|
15
15
|
}
|
|
16
16
|
const typeName = dataType.name;
|
|
17
|
-
if (typeName
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
17
|
+
if (typeName) {
|
|
18
|
+
if (internalTypeNames.includes(typeName))
|
|
19
|
+
return { kind: 'internal', typeName: dataType.name };
|
|
20
|
+
let file = this._filesMap.get(dataType);
|
|
21
|
+
if (file) {
|
|
22
|
+
if (currentFile)
|
|
23
|
+
currentFile.addImport(file.filename, [typeName]);
|
|
24
|
+
return { kind: 'named', file, typeName: dataType.name };
|
|
25
|
+
}
|
|
26
|
+
if (dataType instanceof common_1.SimpleType)
|
|
27
|
+
file = this.addFile(path_1.default.join(this._documentRoot, '/simple-types.ts'), true);
|
|
28
|
+
else if (dataType instanceof common_1.EnumType)
|
|
28
29
|
file = this.addFile(path_1.default.join(this._typesRoot, 'enums', typeName + '.ts'));
|
|
29
30
|
else
|
|
30
31
|
file = this.addFile(path_1.default.join(this._typesRoot, 'types', typeName + '.ts'));
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
codeBlock.head +=
|
|
44
|
-
codeBlock.head += `
|
|
32
|
+
this._filesMap.set(dataType, file);
|
|
33
|
+
if (file.exportTypes.includes(typeName)) {
|
|
34
|
+
if (currentFile)
|
|
35
|
+
currentFile.addImport(file.filename, [typeName]);
|
|
36
|
+
return { kind: 'named', file, typeName: dataType.name };
|
|
37
|
+
}
|
|
38
|
+
file.exportTypes.push(typeName);
|
|
39
|
+
const typesIndexTs = this.addFile(path_1.default.join(this._typesRoot, 'index.ts'), true);
|
|
40
|
+
const indexTs = this.addFile('/index.ts', true);
|
|
41
|
+
indexTs.addExport(typesIndexTs.filename);
|
|
42
|
+
const codeBlock = (file.code['type_' + typeName] = new code_block_js_1.CodeBlock());
|
|
43
|
+
codeBlock.head = `/**\n * ${(0, string_utils_js_1.wrapJSDocString)(dataType.description || '')}\n *`;
|
|
44
|
+
codeBlock.head += `
|
|
45
45
|
* @url ${path_1.default.posix.join(doc.url || this.serviceUrl, '$schema', '#types/' + typeName)}
|
|
46
46
|
*/
|
|
47
47
|
export `;
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
48
|
+
codeBlock.typeDef = (await this.generateTypeDefinition(file, dataType, 'root')) + '\n\n';
|
|
49
|
+
typesIndexTs.addExport(file.filename);
|
|
50
|
+
if (currentFile)
|
|
51
|
+
currentFile.addImport(file.filename, [typeName]);
|
|
52
|
+
return { kind: 'named', file, typeName };
|
|
53
|
+
}
|
|
54
|
+
if (!currentFile)
|
|
55
|
+
throw new TypeError(`You must provide currentFile to generate data type`);
|
|
56
|
+
const code = await this.generateTypeDefinition(currentFile, dataType, intent);
|
|
57
|
+
return { kind: 'embedded', code };
|
|
58
|
+
}
|
|
59
|
+
exports.processDataType = processDataType;
|
|
60
|
+
/**
|
|
61
|
+
*
|
|
62
|
+
*/
|
|
63
|
+
async function generateTypeDefinition(currentFile, dataType, intent) {
|
|
64
|
+
if (intent === 'root' && !dataType.name) {
|
|
65
|
+
throw new TypeError(`Name required to generate data type code to root intent`);
|
|
66
|
+
}
|
|
67
|
+
if (dataType instanceof common_1.EnumType) {
|
|
68
|
+
return await this.generateEnumTypeDefinition(currentFile, dataType, intent);
|
|
69
|
+
}
|
|
70
|
+
if (dataType instanceof common_1.ComplexType) {
|
|
71
|
+
return await this.generateComplexTypeDefinition(currentFile, dataType, intent);
|
|
52
72
|
}
|
|
53
|
-
|
|
54
|
-
|
|
73
|
+
if (dataType instanceof common_1.SimpleType) {
|
|
74
|
+
return await this.generateSimpleTypeDefinition(currentFile, dataType, intent);
|
|
55
75
|
}
|
|
56
|
-
|
|
57
|
-
|
|
76
|
+
if (dataType instanceof common_1.MappedType) {
|
|
77
|
+
return await this.generateMappedTypeDefinition(currentFile, dataType, intent);
|
|
58
78
|
}
|
|
59
|
-
|
|
60
|
-
|
|
79
|
+
if (dataType instanceof common_1.MixinType) {
|
|
80
|
+
return await this.generateMixinTypeDefinition(currentFile, dataType, intent);
|
|
61
81
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
typesIndexTs.addExport(file.filename);
|
|
65
|
-
return file;
|
|
82
|
+
/* istanbul ignore next */
|
|
83
|
+
throw new TypeError(`${dataType.kind} data types can not be directly exported`);
|
|
66
84
|
}
|
|
67
|
-
exports.
|
|
85
|
+
exports.generateTypeDefinition = generateTypeDefinition;
|
|
68
86
|
/**
|
|
69
87
|
*
|
|
70
88
|
*/
|
|
71
|
-
async function generateEnumTypeDefinition(dataType, intent) {
|
|
72
|
-
if (intent === '
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
if (dataType.attributes[value].description)
|
|
88
|
-
jsDoc += ` * ${dataType.attributes[value].description}\n`;
|
|
89
|
-
if (jsDoc)
|
|
90
|
-
out += `/**\n${jsDoc} */\n`;
|
|
91
|
-
out +=
|
|
92
|
-
`${info.alias || value} = ` + (typeof value === 'number' ? value : "'" + String(value).replace("'", "\\'") + "'");
|
|
93
|
-
out += ',\n\n';
|
|
89
|
+
async function generateEnumTypeDefinition(currentFile, dataType, intent) {
|
|
90
|
+
if (intent === 'root') {
|
|
91
|
+
let out = `enum ${dataType.name} {\n\t`;
|
|
92
|
+
for (const [value, info] of Object.entries(dataType.attributes)) {
|
|
93
|
+
// Print JSDoc
|
|
94
|
+
let jsDoc = '';
|
|
95
|
+
if (dataType.attributes[value].description)
|
|
96
|
+
jsDoc += ` * ${dataType.attributes[value].description}\n`;
|
|
97
|
+
if (jsDoc)
|
|
98
|
+
out += `/**\n${jsDoc} */\n`;
|
|
99
|
+
out +=
|
|
100
|
+
`${info.alias || value} = ` +
|
|
101
|
+
(typeof value === 'number' ? value : "'" + String(value).replace("'", "\\'") + "'");
|
|
102
|
+
out += ',\n\n';
|
|
103
|
+
}
|
|
104
|
+
return out + '\b}';
|
|
94
105
|
}
|
|
95
|
-
return
|
|
106
|
+
return ('(' +
|
|
107
|
+
Object.keys(dataType.attributes)
|
|
108
|
+
.map(t => `'${t}'`)
|
|
109
|
+
.join(' | ') +
|
|
110
|
+
')');
|
|
96
111
|
}
|
|
97
112
|
exports.generateEnumTypeDefinition = generateEnumTypeDefinition;
|
|
98
113
|
/**
|
|
99
114
|
*
|
|
100
115
|
*/
|
|
101
|
-
async function generateComplexTypeDefinition(
|
|
102
|
-
|
|
103
|
-
throw new TypeError(`Name required to generate ComplexType for "${intent}" intent`);
|
|
104
|
-
}
|
|
105
|
-
let out = intent === 'scope' ? `interface ${dataType.name} ` : '';
|
|
116
|
+
async function generateComplexTypeDefinition(currentFile, dataType, intent) {
|
|
117
|
+
let out = intent === 'root' ? `interface ${dataType.name} ` : '';
|
|
106
118
|
const ownFields = [...dataType.fields.values()].filter(f => f.origin === dataType);
|
|
107
119
|
if (dataType.base) {
|
|
108
|
-
const base = await this.
|
|
109
|
-
|
|
110
|
-
const
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
if (intent === '
|
|
120
|
+
const base = await this.processDataType(dataType.base, currentFile, 'extends');
|
|
121
|
+
let baseDef = base.kind === 'embedded' ? base.code : base.typeName;
|
|
122
|
+
const omitBaseFields = ownFields.filter(f => dataType.base.fields.has(f.name));
|
|
123
|
+
if (omitBaseFields.length)
|
|
124
|
+
baseDef = `Omit<${baseDef}, ${omitBaseFields.map(x => "'" + x.name + "'").join(' | ')}>`;
|
|
125
|
+
if (intent === 'root')
|
|
114
126
|
out += `extends ${baseDef} `;
|
|
115
127
|
else {
|
|
116
128
|
out += baseDef;
|
|
@@ -149,7 +161,8 @@ async function generateComplexTypeDefinition(dataType, file, intent) {
|
|
|
149
161
|
out += `${t === 'number' || t === 'boolean' || t === 'bigint' ? field.fixed : "'" + field.fixed + "'"}\n`;
|
|
150
162
|
}
|
|
151
163
|
else {
|
|
152
|
-
|
|
164
|
+
const x = await this.processDataType(field.type, currentFile);
|
|
165
|
+
out += (x.kind === 'embedded' ? x.code : x.typeName) + `${field.isArray ? '[]' : ''};\n`;
|
|
153
166
|
}
|
|
154
167
|
}
|
|
155
168
|
if (dataType.additionalFields)
|
|
@@ -160,29 +173,39 @@ exports.generateComplexTypeDefinition = generateComplexTypeDefinition;
|
|
|
160
173
|
/**
|
|
161
174
|
*
|
|
162
175
|
*/
|
|
163
|
-
async function generateSimpleTypeDefinition(dataType, intent) {
|
|
164
|
-
|
|
165
|
-
throw new TypeError(`Name required to generate SimpleType for "${intent}" intent`);
|
|
166
|
-
}
|
|
167
|
-
let out = intent === 'scope' ? `type ${dataType.name} = ` : '';
|
|
176
|
+
async function generateSimpleTypeDefinition(currentFile, dataType, intent) {
|
|
177
|
+
let out = intent === 'root' ? `type ${dataType.name} = ` : '';
|
|
168
178
|
out += dataType.nameMappings.js || 'any';
|
|
169
|
-
return intent === '
|
|
179
|
+
return intent === 'root' ? out + ';' : out;
|
|
170
180
|
}
|
|
171
181
|
exports.generateSimpleTypeDefinition = generateSimpleTypeDefinition;
|
|
172
182
|
/**
|
|
173
183
|
*
|
|
174
184
|
*/
|
|
175
|
-
async function generateMixinTypeDefinition(
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
185
|
+
async function generateMixinTypeDefinition(currentFile, dataType, intent) {
|
|
186
|
+
const outArray = [];
|
|
187
|
+
for (const t of dataType.types) {
|
|
188
|
+
const x = await this.processDataType(t, currentFile);
|
|
189
|
+
if (x.kind === 'embedded') {
|
|
190
|
+
outArray.push(x.code.includes('|') ? '(' + x.code + ')' : x.code);
|
|
191
|
+
}
|
|
192
|
+
else
|
|
193
|
+
outArray.push(x.typeName);
|
|
194
|
+
}
|
|
195
|
+
if (intent === 'root')
|
|
196
|
+
return `type ${dataType.name} = ${outArray.join(' & ')}`;
|
|
197
|
+
if (intent === 'extends')
|
|
198
|
+
return outArray.join(', ');
|
|
199
|
+
return outArray.join(' & ');
|
|
179
200
|
}
|
|
180
201
|
exports.generateMixinTypeDefinition = generateMixinTypeDefinition;
|
|
181
202
|
/**
|
|
182
203
|
*
|
|
183
204
|
*/
|
|
184
|
-
async function generateMappedTypeDefinition(
|
|
185
|
-
|
|
205
|
+
async function generateMappedTypeDefinition(currentFile, dataType, intent) {
|
|
206
|
+
let out = intent === 'root' ? `type ${dataType.name} = ` : '';
|
|
207
|
+
const base = await this.processDataType(dataType.base, currentFile);
|
|
208
|
+
const typeDef = base.kind === 'embedded' ? base.code : base.typeName;
|
|
186
209
|
const pick = dataType.pick?.length ? dataType.pick : undefined;
|
|
187
210
|
const omit = !pick && dataType.omit?.length ? dataType.omit : undefined;
|
|
188
211
|
const partial = dataType.partial === true || (Array.isArray(dataType.partial) && dataType.partial.length > 0)
|
|
@@ -193,18 +216,17 @@ async function generateMappedTypeDefinition(dataType, file, intent) {
|
|
|
193
216
|
: undefined;
|
|
194
217
|
if (!(pick || omit || partial || required))
|
|
195
218
|
return typeDef;
|
|
196
|
-
let out = '';
|
|
197
219
|
if (partial === true)
|
|
198
220
|
out += 'Partial<';
|
|
199
221
|
else if (partial) {
|
|
200
222
|
out += 'PartialSome<';
|
|
201
|
-
|
|
223
|
+
currentFile.addExport('ts-gems', ['PartialSome']);
|
|
202
224
|
}
|
|
203
225
|
if (required === true)
|
|
204
226
|
out += 'Partial<';
|
|
205
227
|
else if (required) {
|
|
206
228
|
out += 'RequiredSome<';
|
|
207
|
-
|
|
229
|
+
currentFile.addExport('ts-gems', ['RequiredSome']);
|
|
208
230
|
}
|
|
209
231
|
if (pick)
|
|
210
232
|
out += 'Pick<';
|
|
@@ -234,29 +256,3 @@ async function generateMappedTypeDefinition(dataType, file, intent) {
|
|
|
234
256
|
return out;
|
|
235
257
|
}
|
|
236
258
|
exports.generateMappedTypeDefinition = generateMappedTypeDefinition;
|
|
237
|
-
/**
|
|
238
|
-
*
|
|
239
|
-
*/
|
|
240
|
-
async function resolveTypeNameOrDef(dataType, file, intent) {
|
|
241
|
-
if (dataType.name && !dataType.embedded) {
|
|
242
|
-
if (internalTypeNames.includes(dataType.name))
|
|
243
|
-
return dataType.name;
|
|
244
|
-
const f = await this.processDataType(dataType);
|
|
245
|
-
if (!f)
|
|
246
|
-
return '';
|
|
247
|
-
file.addImport(f.filename, [dataType.name], true);
|
|
248
|
-
return dataType.name;
|
|
249
|
-
}
|
|
250
|
-
if (dataType instanceof common_1.SimpleType)
|
|
251
|
-
return this.generateSimpleTypeDefinition(dataType, intent);
|
|
252
|
-
if (dataType instanceof common_1.EnumType)
|
|
253
|
-
return this.generateEnumTypeDefinition(dataType, intent);
|
|
254
|
-
if (dataType instanceof common_1.MixinType)
|
|
255
|
-
return this.generateMixinTypeDefinition(dataType, file, intent);
|
|
256
|
-
if (dataType instanceof common_1.MappedType)
|
|
257
|
-
return this.generateMappedTypeDefinition(dataType, file, intent);
|
|
258
|
-
if (dataType instanceof common_1.ComplexType)
|
|
259
|
-
return this.generateComplexTypeDefinition(dataType, file, intent);
|
|
260
|
-
return '';
|
|
261
|
-
}
|
|
262
|
-
exports.resolveTypeNameOrDef = resolveTypeNameOrDef;
|
|
@@ -90,8 +90,13 @@ async function processHttpController(controller) {
|
|
|
90
90
|
}
|
|
91
91
|
let argIndex = 0;
|
|
92
92
|
for (const prm of pathParams) {
|
|
93
|
-
|
|
94
|
-
|
|
93
|
+
let typeName;
|
|
94
|
+
if (prm.type) {
|
|
95
|
+
const xt = await this.processDataType(prm.type, file);
|
|
96
|
+
typeName = xt.kind === 'embedded' ? xt.code : xt.typeName;
|
|
97
|
+
}
|
|
98
|
+
else
|
|
99
|
+
typeName = `any`;
|
|
95
100
|
if (argIndex++ > 0)
|
|
96
101
|
operationBlock.head += ', ';
|
|
97
102
|
operationBlock.head += `${prm.name}: ${typeName}`;
|
|
@@ -103,8 +108,10 @@ async function processHttpController(controller) {
|
|
|
103
108
|
let typeArr = [];
|
|
104
109
|
for (const content of operation.requestBody.content) {
|
|
105
110
|
if (content.type) {
|
|
106
|
-
const
|
|
111
|
+
const xt = await this.processDataType(content.type, file);
|
|
112
|
+
const typeName = xt.kind === 'embedded' ? xt.code : xt.typeName;
|
|
107
113
|
typeArr.push(typeName);
|
|
114
|
+
continue;
|
|
108
115
|
}
|
|
109
116
|
typeArr = [];
|
|
110
117
|
break;
|
|
@@ -131,18 +138,15 @@ async function processHttpController(controller) {
|
|
|
131
138
|
operationBlock.head += ', ';
|
|
132
139
|
operationBlock.head += '\n\t$params' + (isHeadersRequired || isQueryRequired ? '' : '?') + ': {\n\t';
|
|
133
140
|
for (const prm of queryParams) {
|
|
134
|
-
const type = (0, locate_named_type_js_1.locateNamedType)(prm.type);
|
|
135
141
|
operationBlock.head += `/**\n * ${prm.description || ''}\n */\n`;
|
|
136
142
|
operationBlock.head += `${prm.name}${prm.required ? '' : '?'}: `;
|
|
137
|
-
if (type
|
|
138
|
-
const
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
operationBlock.head += `${type.name};\n`;
|
|
142
|
-
continue;
|
|
143
|
-
}
|
|
143
|
+
if (prm.type) {
|
|
144
|
+
const xt = await this.processDataType(prm.type, file);
|
|
145
|
+
const typeDef = xt.kind === 'embedded' ? xt.code : xt.typeName;
|
|
146
|
+
operationBlock.head += `${typeDef};\n`;
|
|
144
147
|
}
|
|
145
|
-
|
|
148
|
+
else
|
|
149
|
+
operationBlock.head += `any;\n`;
|
|
146
150
|
}
|
|
147
151
|
operationBlock.head += '\b}\b';
|
|
148
152
|
}
|
|
@@ -152,30 +156,39 @@ async function processHttpController(controller) {
|
|
|
152
156
|
operationBlock.head += ', \n';
|
|
153
157
|
operationBlock.head += '\t$headers' + (isHeadersRequired ? '' : '?') + ': {\n\t';
|
|
154
158
|
for (const prm of headerParams) {
|
|
155
|
-
const type = (0, locate_named_type_js_1.locateNamedType)(prm.type);
|
|
156
159
|
operationBlock.head += `/**\n * ${prm.description || ''}\n */\n`;
|
|
157
160
|
operationBlock.head += `${prm.name}${prm.required ? '' : '?'}: `;
|
|
158
|
-
if (type
|
|
159
|
-
const
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
operationBlock.head += `${type.name};\n`;
|
|
163
|
-
continue;
|
|
164
|
-
}
|
|
161
|
+
if (prm.type) {
|
|
162
|
+
const xt = await this.processDataType(prm.type, file);
|
|
163
|
+
const typeDef = xt.kind === 'embedded' ? xt.code : xt.typeName;
|
|
164
|
+
operationBlock.head += `${typeDef};\n`;
|
|
165
165
|
}
|
|
166
|
-
|
|
166
|
+
else
|
|
167
|
+
operationBlock.head += `any;\n`;
|
|
167
168
|
}
|
|
168
169
|
operationBlock.head += '\b}\b';
|
|
169
170
|
}
|
|
171
|
+
/* Determine return type */
|
|
172
|
+
// let returnType = '';
|
|
173
|
+
// for (const resp of operation.responses) {
|
|
174
|
+
// if (resp.type) {
|
|
175
|
+
// const typeFile = await this.processDataType(resp.type);
|
|
176
|
+
// // if (typeFile) {
|
|
177
|
+
// // file.addImport(typeFile.filename, [resp.type.name!]);
|
|
178
|
+
// // operationBlock.head += `${type.name};\n`;
|
|
179
|
+
// // continue;
|
|
180
|
+
// // }
|
|
181
|
+
// }
|
|
182
|
+
// }
|
|
170
183
|
operationBlock.head += `\n): HttpRequestObservable<any>{`;
|
|
171
184
|
operationBlock.body = `\n\t`;
|
|
172
185
|
operationBlock.body +=
|
|
173
186
|
`const url = this._prepareUrl('${operation.getFullUrl()}', {` + pathParams.map(p => p.name).join(', ') + '});';
|
|
174
187
|
operationBlock.body +=
|
|
175
|
-
`\nreturn this[kClient].request(url, {` +
|
|
176
|
-
(hasBody ? ' body: $body
|
|
177
|
-
(queryParams.length ? ' params: $params as any
|
|
178
|
-
(headerParams.length ? ' headers: $headers as any
|
|
188
|
+
`\nreturn this[kClient].request(url, { method: '${operation.method}'` +
|
|
189
|
+
(hasBody ? ', body: $body' : '') +
|
|
190
|
+
(queryParams.length ? ', params: $params as any' : '') +
|
|
191
|
+
(headerParams.length ? ', headers: $headers as any' : '') +
|
|
179
192
|
'});';
|
|
180
193
|
operationBlock.tail = `\b\n};\n`;
|
|
181
194
|
}
|
|
@@ -99,10 +99,11 @@ exports.TsGenerator = TsGenerator;
|
|
|
99
99
|
TsGenerator.prototype.processDataType = process_data_types_js_1.processDataType;
|
|
100
100
|
TsGenerator.prototype.processHttpApi = process_http_api_js_1.processHttpApi;
|
|
101
101
|
TsGenerator.prototype.processHttpController = process_http_controller_js_1.processHttpController;
|
|
102
|
+
TsGenerator.prototype.generateTypeDefinition = process_data_types_js_1.generateTypeDefinition;
|
|
102
103
|
TsGenerator.prototype.generateEnumTypeDefinition = process_data_types_js_1.generateEnumTypeDefinition;
|
|
103
104
|
TsGenerator.prototype.generateComplexTypeDefinition = process_data_types_js_1.generateComplexTypeDefinition;
|
|
104
105
|
TsGenerator.prototype.generateSimpleTypeDefinition = process_data_types_js_1.generateSimpleTypeDefinition;
|
|
105
106
|
TsGenerator.prototype.generateMappedTypeDefinition = process_data_types_js_1.generateMappedTypeDefinition;
|
|
106
107
|
TsGenerator.prototype.generateMixinTypeDefinition = process_data_types_js_1.generateMixinTypeDefinition;
|
|
107
|
-
TsGenerator.prototype.resolveTypeNameOrDef =
|
|
108
|
+
// TsGenerator.prototype.resolveTypeNameOrDef = resolveTypeNameOrDef;
|
|
108
109
|
})();
|
|
@@ -3,108 +3,119 @@ import path from 'path';
|
|
|
3
3
|
import { CodeBlock } from '../../code-block.js';
|
|
4
4
|
import { wrapJSDocString } from '../utils/string-utils.js';
|
|
5
5
|
const internalTypeNames = ['any', 'boolean', 'bigint', 'number', 'null', 'string', 'object'];
|
|
6
|
-
export async function processDataType(dataType) {
|
|
6
|
+
export async function processDataType(dataType, currentFile, intent) {
|
|
7
7
|
const doc = dataType.node.getDocument();
|
|
8
8
|
if (doc.id !== this._document?.id) {
|
|
9
9
|
const { generator } = await this.processDocument(doc);
|
|
10
|
-
return await generator.processDataType(dataType);
|
|
10
|
+
return await generator.processDataType(dataType, currentFile, intent);
|
|
11
11
|
}
|
|
12
12
|
const typeName = dataType.name;
|
|
13
|
-
if (typeName
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
13
|
+
if (typeName) {
|
|
14
|
+
if (internalTypeNames.includes(typeName))
|
|
15
|
+
return { kind: 'internal', typeName: dataType.name };
|
|
16
|
+
let file = this._filesMap.get(dataType);
|
|
17
|
+
if (file) {
|
|
18
|
+
if (currentFile)
|
|
19
|
+
currentFile.addImport(file.filename, [typeName]);
|
|
20
|
+
return { kind: 'named', file, typeName: dataType.name };
|
|
21
|
+
}
|
|
22
|
+
if (dataType instanceof SimpleType)
|
|
23
|
+
file = this.addFile(path.join(this._documentRoot, '/simple-types.ts'), true);
|
|
24
|
+
else if (dataType instanceof EnumType)
|
|
24
25
|
file = this.addFile(path.join(this._typesRoot, 'enums', typeName + '.ts'));
|
|
25
26
|
else
|
|
26
27
|
file = this.addFile(path.join(this._typesRoot, 'types', typeName + '.ts'));
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
codeBlock.head +=
|
|
40
|
-
codeBlock.head += `
|
|
28
|
+
this._filesMap.set(dataType, file);
|
|
29
|
+
if (file.exportTypes.includes(typeName)) {
|
|
30
|
+
if (currentFile)
|
|
31
|
+
currentFile.addImport(file.filename, [typeName]);
|
|
32
|
+
return { kind: 'named', file, typeName: dataType.name };
|
|
33
|
+
}
|
|
34
|
+
file.exportTypes.push(typeName);
|
|
35
|
+
const typesIndexTs = this.addFile(path.join(this._typesRoot, 'index.ts'), true);
|
|
36
|
+
const indexTs = this.addFile('/index.ts', true);
|
|
37
|
+
indexTs.addExport(typesIndexTs.filename);
|
|
38
|
+
const codeBlock = (file.code['type_' + typeName] = new CodeBlock());
|
|
39
|
+
codeBlock.head = `/**\n * ${wrapJSDocString(dataType.description || '')}\n *`;
|
|
40
|
+
codeBlock.head += `
|
|
41
41
|
* @url ${path.posix.join(doc.url || this.serviceUrl, '$schema', '#types/' + typeName)}
|
|
42
42
|
*/
|
|
43
43
|
export `;
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
44
|
+
codeBlock.typeDef = (await this.generateTypeDefinition(file, dataType, 'root')) + '\n\n';
|
|
45
|
+
typesIndexTs.addExport(file.filename);
|
|
46
|
+
if (currentFile)
|
|
47
|
+
currentFile.addImport(file.filename, [typeName]);
|
|
48
|
+
return { kind: 'named', file, typeName };
|
|
49
|
+
}
|
|
50
|
+
if (!currentFile)
|
|
51
|
+
throw new TypeError(`You must provide currentFile to generate data type`);
|
|
52
|
+
const code = await this.generateTypeDefinition(currentFile, dataType, intent);
|
|
53
|
+
return { kind: 'embedded', code };
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
*
|
|
57
|
+
*/
|
|
58
|
+
export async function generateTypeDefinition(currentFile, dataType, intent) {
|
|
59
|
+
if (intent === 'root' && !dataType.name) {
|
|
60
|
+
throw new TypeError(`Name required to generate data type code to root intent`);
|
|
61
|
+
}
|
|
62
|
+
if (dataType instanceof EnumType) {
|
|
63
|
+
return await this.generateEnumTypeDefinition(currentFile, dataType, intent);
|
|
64
|
+
}
|
|
65
|
+
if (dataType instanceof ComplexType) {
|
|
66
|
+
return await this.generateComplexTypeDefinition(currentFile, dataType, intent);
|
|
48
67
|
}
|
|
49
|
-
|
|
50
|
-
|
|
68
|
+
if (dataType instanceof SimpleType) {
|
|
69
|
+
return await this.generateSimpleTypeDefinition(currentFile, dataType, intent);
|
|
51
70
|
}
|
|
52
|
-
|
|
53
|
-
|
|
71
|
+
if (dataType instanceof MappedType) {
|
|
72
|
+
return await this.generateMappedTypeDefinition(currentFile, dataType, intent);
|
|
54
73
|
}
|
|
55
|
-
|
|
56
|
-
|
|
74
|
+
if (dataType instanceof MixinType) {
|
|
75
|
+
return await this.generateMixinTypeDefinition(currentFile, dataType, intent);
|
|
57
76
|
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
typesIndexTs.addExport(file.filename);
|
|
61
|
-
return file;
|
|
77
|
+
/* istanbul ignore next */
|
|
78
|
+
throw new TypeError(`${dataType.kind} data types can not be directly exported`);
|
|
62
79
|
}
|
|
63
80
|
/**
|
|
64
81
|
*
|
|
65
82
|
*/
|
|
66
|
-
export async function generateEnumTypeDefinition(dataType, intent) {
|
|
67
|
-
if (intent === '
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
if (dataType.attributes[value].description)
|
|
83
|
-
jsDoc += ` * ${dataType.attributes[value].description}\n`;
|
|
84
|
-
if (jsDoc)
|
|
85
|
-
out += `/**\n${jsDoc} */\n`;
|
|
86
|
-
out +=
|
|
87
|
-
`${info.alias || value} = ` + (typeof value === 'number' ? value : "'" + String(value).replace("'", "\\'") + "'");
|
|
88
|
-
out += ',\n\n';
|
|
83
|
+
export async function generateEnumTypeDefinition(currentFile, dataType, intent) {
|
|
84
|
+
if (intent === 'root') {
|
|
85
|
+
let out = `enum ${dataType.name} {\n\t`;
|
|
86
|
+
for (const [value, info] of Object.entries(dataType.attributes)) {
|
|
87
|
+
// Print JSDoc
|
|
88
|
+
let jsDoc = '';
|
|
89
|
+
if (dataType.attributes[value].description)
|
|
90
|
+
jsDoc += ` * ${dataType.attributes[value].description}\n`;
|
|
91
|
+
if (jsDoc)
|
|
92
|
+
out += `/**\n${jsDoc} */\n`;
|
|
93
|
+
out +=
|
|
94
|
+
`${info.alias || value} = ` +
|
|
95
|
+
(typeof value === 'number' ? value : "'" + String(value).replace("'", "\\'") + "'");
|
|
96
|
+
out += ',\n\n';
|
|
97
|
+
}
|
|
98
|
+
return out + '\b}';
|
|
89
99
|
}
|
|
90
|
-
return
|
|
100
|
+
return ('(' +
|
|
101
|
+
Object.keys(dataType.attributes)
|
|
102
|
+
.map(t => `'${t}'`)
|
|
103
|
+
.join(' | ') +
|
|
104
|
+
')');
|
|
91
105
|
}
|
|
92
106
|
/**
|
|
93
107
|
*
|
|
94
108
|
*/
|
|
95
|
-
export async function generateComplexTypeDefinition(
|
|
96
|
-
|
|
97
|
-
throw new TypeError(`Name required to generate ComplexType for "${intent}" intent`);
|
|
98
|
-
}
|
|
99
|
-
let out = intent === 'scope' ? `interface ${dataType.name} ` : '';
|
|
109
|
+
export async function generateComplexTypeDefinition(currentFile, dataType, intent) {
|
|
110
|
+
let out = intent === 'root' ? `interface ${dataType.name} ` : '';
|
|
100
111
|
const ownFields = [...dataType.fields.values()].filter(f => f.origin === dataType);
|
|
101
112
|
if (dataType.base) {
|
|
102
|
-
const base = await this.
|
|
103
|
-
|
|
104
|
-
const
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
if (intent === '
|
|
113
|
+
const base = await this.processDataType(dataType.base, currentFile, 'extends');
|
|
114
|
+
let baseDef = base.kind === 'embedded' ? base.code : base.typeName;
|
|
115
|
+
const omitBaseFields = ownFields.filter(f => dataType.base.fields.has(f.name));
|
|
116
|
+
if (omitBaseFields.length)
|
|
117
|
+
baseDef = `Omit<${baseDef}, ${omitBaseFields.map(x => "'" + x.name + "'").join(' | ')}>`;
|
|
118
|
+
if (intent === 'root')
|
|
108
119
|
out += `extends ${baseDef} `;
|
|
109
120
|
else {
|
|
110
121
|
out += baseDef;
|
|
@@ -143,7 +154,8 @@ export async function generateComplexTypeDefinition(dataType, file, intent) {
|
|
|
143
154
|
out += `${t === 'number' || t === 'boolean' || t === 'bigint' ? field.fixed : "'" + field.fixed + "'"}\n`;
|
|
144
155
|
}
|
|
145
156
|
else {
|
|
146
|
-
|
|
157
|
+
const x = await this.processDataType(field.type, currentFile);
|
|
158
|
+
out += (x.kind === 'embedded' ? x.code : x.typeName) + `${field.isArray ? '[]' : ''};\n`;
|
|
147
159
|
}
|
|
148
160
|
}
|
|
149
161
|
if (dataType.additionalFields)
|
|
@@ -153,27 +165,37 @@ export async function generateComplexTypeDefinition(dataType, file, intent) {
|
|
|
153
165
|
/**
|
|
154
166
|
*
|
|
155
167
|
*/
|
|
156
|
-
export async function generateSimpleTypeDefinition(dataType, intent) {
|
|
157
|
-
|
|
158
|
-
throw new TypeError(`Name required to generate SimpleType for "${intent}" intent`);
|
|
159
|
-
}
|
|
160
|
-
let out = intent === 'scope' ? `type ${dataType.name} = ` : '';
|
|
168
|
+
export async function generateSimpleTypeDefinition(currentFile, dataType, intent) {
|
|
169
|
+
let out = intent === 'root' ? `type ${dataType.name} = ` : '';
|
|
161
170
|
out += dataType.nameMappings.js || 'any';
|
|
162
|
-
return intent === '
|
|
171
|
+
return intent === 'root' ? out + ';' : out;
|
|
163
172
|
}
|
|
164
173
|
/**
|
|
165
174
|
*
|
|
166
175
|
*/
|
|
167
|
-
export async function generateMixinTypeDefinition(
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
176
|
+
export async function generateMixinTypeDefinition(currentFile, dataType, intent) {
|
|
177
|
+
const outArray = [];
|
|
178
|
+
for (const t of dataType.types) {
|
|
179
|
+
const x = await this.processDataType(t, currentFile);
|
|
180
|
+
if (x.kind === 'embedded') {
|
|
181
|
+
outArray.push(x.code.includes('|') ? '(' + x.code + ')' : x.code);
|
|
182
|
+
}
|
|
183
|
+
else
|
|
184
|
+
outArray.push(x.typeName);
|
|
185
|
+
}
|
|
186
|
+
if (intent === 'root')
|
|
187
|
+
return `type ${dataType.name} = ${outArray.join(' & ')}`;
|
|
188
|
+
if (intent === 'extends')
|
|
189
|
+
return outArray.join(', ');
|
|
190
|
+
return outArray.join(' & ');
|
|
171
191
|
}
|
|
172
192
|
/**
|
|
173
193
|
*
|
|
174
194
|
*/
|
|
175
|
-
export async function generateMappedTypeDefinition(
|
|
176
|
-
|
|
195
|
+
export async function generateMappedTypeDefinition(currentFile, dataType, intent) {
|
|
196
|
+
let out = intent === 'root' ? `type ${dataType.name} = ` : '';
|
|
197
|
+
const base = await this.processDataType(dataType.base, currentFile);
|
|
198
|
+
const typeDef = base.kind === 'embedded' ? base.code : base.typeName;
|
|
177
199
|
const pick = dataType.pick?.length ? dataType.pick : undefined;
|
|
178
200
|
const omit = !pick && dataType.omit?.length ? dataType.omit : undefined;
|
|
179
201
|
const partial = dataType.partial === true || (Array.isArray(dataType.partial) && dataType.partial.length > 0)
|
|
@@ -184,18 +206,17 @@ export async function generateMappedTypeDefinition(dataType, file, intent) {
|
|
|
184
206
|
: undefined;
|
|
185
207
|
if (!(pick || omit || partial || required))
|
|
186
208
|
return typeDef;
|
|
187
|
-
let out = '';
|
|
188
209
|
if (partial === true)
|
|
189
210
|
out += 'Partial<';
|
|
190
211
|
else if (partial) {
|
|
191
212
|
out += 'PartialSome<';
|
|
192
|
-
|
|
213
|
+
currentFile.addExport('ts-gems', ['PartialSome']);
|
|
193
214
|
}
|
|
194
215
|
if (required === true)
|
|
195
216
|
out += 'Partial<';
|
|
196
217
|
else if (required) {
|
|
197
218
|
out += 'RequiredSome<';
|
|
198
|
-
|
|
219
|
+
currentFile.addExport('ts-gems', ['RequiredSome']);
|
|
199
220
|
}
|
|
200
221
|
if (pick)
|
|
201
222
|
out += 'Pick<';
|
|
@@ -224,28 +245,3 @@ export async function generateMappedTypeDefinition(dataType, file, intent) {
|
|
|
224
245
|
}
|
|
225
246
|
return out;
|
|
226
247
|
}
|
|
227
|
-
/**
|
|
228
|
-
*
|
|
229
|
-
*/
|
|
230
|
-
export async function resolveTypeNameOrDef(dataType, file, intent) {
|
|
231
|
-
if (dataType.name && !dataType.embedded) {
|
|
232
|
-
if (internalTypeNames.includes(dataType.name))
|
|
233
|
-
return dataType.name;
|
|
234
|
-
const f = await this.processDataType(dataType);
|
|
235
|
-
if (!f)
|
|
236
|
-
return '';
|
|
237
|
-
file.addImport(f.filename, [dataType.name], true);
|
|
238
|
-
return dataType.name;
|
|
239
|
-
}
|
|
240
|
-
if (dataType instanceof SimpleType)
|
|
241
|
-
return this.generateSimpleTypeDefinition(dataType, intent);
|
|
242
|
-
if (dataType instanceof EnumType)
|
|
243
|
-
return this.generateEnumTypeDefinition(dataType, intent);
|
|
244
|
-
if (dataType instanceof MixinType)
|
|
245
|
-
return this.generateMixinTypeDefinition(dataType, file, intent);
|
|
246
|
-
if (dataType instanceof MappedType)
|
|
247
|
-
return this.generateMappedTypeDefinition(dataType, file, intent);
|
|
248
|
-
if (dataType instanceof ComplexType)
|
|
249
|
-
return this.generateComplexTypeDefinition(dataType, file, intent);
|
|
250
|
-
return '';
|
|
251
|
-
}
|
|
@@ -86,8 +86,13 @@ export async function processHttpController(controller) {
|
|
|
86
86
|
}
|
|
87
87
|
let argIndex = 0;
|
|
88
88
|
for (const prm of pathParams) {
|
|
89
|
-
|
|
90
|
-
|
|
89
|
+
let typeName;
|
|
90
|
+
if (prm.type) {
|
|
91
|
+
const xt = await this.processDataType(prm.type, file);
|
|
92
|
+
typeName = xt.kind === 'embedded' ? xt.code : xt.typeName;
|
|
93
|
+
}
|
|
94
|
+
else
|
|
95
|
+
typeName = `any`;
|
|
91
96
|
if (argIndex++ > 0)
|
|
92
97
|
operationBlock.head += ', ';
|
|
93
98
|
operationBlock.head += `${prm.name}: ${typeName}`;
|
|
@@ -99,8 +104,10 @@ export async function processHttpController(controller) {
|
|
|
99
104
|
let typeArr = [];
|
|
100
105
|
for (const content of operation.requestBody.content) {
|
|
101
106
|
if (content.type) {
|
|
102
|
-
const
|
|
107
|
+
const xt = await this.processDataType(content.type, file);
|
|
108
|
+
const typeName = xt.kind === 'embedded' ? xt.code : xt.typeName;
|
|
103
109
|
typeArr.push(typeName);
|
|
110
|
+
continue;
|
|
104
111
|
}
|
|
105
112
|
typeArr = [];
|
|
106
113
|
break;
|
|
@@ -127,18 +134,15 @@ export async function processHttpController(controller) {
|
|
|
127
134
|
operationBlock.head += ', ';
|
|
128
135
|
operationBlock.head += '\n\t$params' + (isHeadersRequired || isQueryRequired ? '' : '?') + ': {\n\t';
|
|
129
136
|
for (const prm of queryParams) {
|
|
130
|
-
const type = locateNamedType(prm.type);
|
|
131
137
|
operationBlock.head += `/**\n * ${prm.description || ''}\n */\n`;
|
|
132
138
|
operationBlock.head += `${prm.name}${prm.required ? '' : '?'}: `;
|
|
133
|
-
if (type
|
|
134
|
-
const
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
operationBlock.head += `${type.name};\n`;
|
|
138
|
-
continue;
|
|
139
|
-
}
|
|
139
|
+
if (prm.type) {
|
|
140
|
+
const xt = await this.processDataType(prm.type, file);
|
|
141
|
+
const typeDef = xt.kind === 'embedded' ? xt.code : xt.typeName;
|
|
142
|
+
operationBlock.head += `${typeDef};\n`;
|
|
140
143
|
}
|
|
141
|
-
|
|
144
|
+
else
|
|
145
|
+
operationBlock.head += `any;\n`;
|
|
142
146
|
}
|
|
143
147
|
operationBlock.head += '\b}\b';
|
|
144
148
|
}
|
|
@@ -148,30 +152,39 @@ export async function processHttpController(controller) {
|
|
|
148
152
|
operationBlock.head += ', \n';
|
|
149
153
|
operationBlock.head += '\t$headers' + (isHeadersRequired ? '' : '?') + ': {\n\t';
|
|
150
154
|
for (const prm of headerParams) {
|
|
151
|
-
const type = locateNamedType(prm.type);
|
|
152
155
|
operationBlock.head += `/**\n * ${prm.description || ''}\n */\n`;
|
|
153
156
|
operationBlock.head += `${prm.name}${prm.required ? '' : '?'}: `;
|
|
154
|
-
if (type
|
|
155
|
-
const
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
operationBlock.head += `${type.name};\n`;
|
|
159
|
-
continue;
|
|
160
|
-
}
|
|
157
|
+
if (prm.type) {
|
|
158
|
+
const xt = await this.processDataType(prm.type, file);
|
|
159
|
+
const typeDef = xt.kind === 'embedded' ? xt.code : xt.typeName;
|
|
160
|
+
operationBlock.head += `${typeDef};\n`;
|
|
161
161
|
}
|
|
162
|
-
|
|
162
|
+
else
|
|
163
|
+
operationBlock.head += `any;\n`;
|
|
163
164
|
}
|
|
164
165
|
operationBlock.head += '\b}\b';
|
|
165
166
|
}
|
|
167
|
+
/* Determine return type */
|
|
168
|
+
// let returnType = '';
|
|
169
|
+
// for (const resp of operation.responses) {
|
|
170
|
+
// if (resp.type) {
|
|
171
|
+
// const typeFile = await this.processDataType(resp.type);
|
|
172
|
+
// // if (typeFile) {
|
|
173
|
+
// // file.addImport(typeFile.filename, [resp.type.name!]);
|
|
174
|
+
// // operationBlock.head += `${type.name};\n`;
|
|
175
|
+
// // continue;
|
|
176
|
+
// // }
|
|
177
|
+
// }
|
|
178
|
+
// }
|
|
166
179
|
operationBlock.head += `\n): HttpRequestObservable<any>{`;
|
|
167
180
|
operationBlock.body = `\n\t`;
|
|
168
181
|
operationBlock.body +=
|
|
169
182
|
`const url = this._prepareUrl('${operation.getFullUrl()}', {` + pathParams.map(p => p.name).join(', ') + '});';
|
|
170
183
|
operationBlock.body +=
|
|
171
|
-
`\nreturn this[kClient].request(url, {` +
|
|
172
|
-
(hasBody ? ' body: $body
|
|
173
|
-
(queryParams.length ? ' params: $params as any
|
|
174
|
-
(headerParams.length ? ' headers: $headers as any
|
|
184
|
+
`\nreturn this[kClient].request(url, { method: '${operation.method}'` +
|
|
185
|
+
(hasBody ? ', body: $body' : '') +
|
|
186
|
+
(queryParams.length ? ', params: $params as any' : '') +
|
|
187
|
+
(headerParams.length ? ', headers: $headers as any' : '') +
|
|
175
188
|
'});';
|
|
176
189
|
operationBlock.tail = `\b\n};\n`;
|
|
177
190
|
}
|
|
@@ -5,7 +5,7 @@ import process from 'node:process';
|
|
|
5
5
|
import chalk from 'chalk';
|
|
6
6
|
import { FileWriter } from '../file-writer.js';
|
|
7
7
|
import { cleanDirectory } from './processors/clean-directory.js';
|
|
8
|
-
import { generateComplexTypeDefinition, generateEnumTypeDefinition, generateMappedTypeDefinition, generateMixinTypeDefinition, generateSimpleTypeDefinition,
|
|
8
|
+
import { generateComplexTypeDefinition, generateEnumTypeDefinition, generateMappedTypeDefinition, generateMixinTypeDefinition, generateSimpleTypeDefinition, generateTypeDefinition, processDataType, } from './processors/process-data-types.js';
|
|
9
9
|
import { processDocument } from './processors/process-document.js';
|
|
10
10
|
import { processHttpApi } from './processors/process-http-api.js';
|
|
11
11
|
import { processHttpController } from './processors/process-http-controller.js';
|
|
@@ -94,10 +94,11 @@ export class TsGenerator extends EventEmitter {
|
|
|
94
94
|
TsGenerator.prototype.processDataType = processDataType;
|
|
95
95
|
TsGenerator.prototype.processHttpApi = processHttpApi;
|
|
96
96
|
TsGenerator.prototype.processHttpController = processHttpController;
|
|
97
|
+
TsGenerator.prototype.generateTypeDefinition = generateTypeDefinition;
|
|
97
98
|
TsGenerator.prototype.generateEnumTypeDefinition = generateEnumTypeDefinition;
|
|
98
99
|
TsGenerator.prototype.generateComplexTypeDefinition = generateComplexTypeDefinition;
|
|
99
100
|
TsGenerator.prototype.generateSimpleTypeDefinition = generateSimpleTypeDefinition;
|
|
100
101
|
TsGenerator.prototype.generateMappedTypeDefinition = generateMappedTypeDefinition;
|
|
101
102
|
TsGenerator.prototype.generateMixinTypeDefinition = generateMixinTypeDefinition;
|
|
102
|
-
TsGenerator.prototype.resolveTypeNameOrDef = resolveTypeNameOrDef;
|
|
103
|
+
// TsGenerator.prototype.resolveTypeNameOrDef = resolveTypeNameOrDef;
|
|
103
104
|
})();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opra/cli",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.12",
|
|
4
4
|
"description": "Opra CLI tools",
|
|
5
5
|
"author": "Panates",
|
|
6
6
|
"license": "MIT",
|
|
@@ -31,8 +31,8 @@
|
|
|
31
31
|
"clean:cover": "rimraf ../../coverage/client"
|
|
32
32
|
},
|
|
33
33
|
"dependencies": {
|
|
34
|
-
"@opra/client": "^1.0.0-alpha.
|
|
35
|
-
"@opra/common": "^1.0.0-alpha.
|
|
34
|
+
"@opra/client": "^1.0.0-alpha.12",
|
|
35
|
+
"@opra/common": "^1.0.0-alpha.12",
|
|
36
36
|
"chalk": "^5.3.0",
|
|
37
37
|
"commander": "^12.0.0",
|
|
38
38
|
"js-string-escape": "^1.0.1",
|
|
@@ -1,30 +1,41 @@
|
|
|
1
1
|
import { ComplexType, DataType, EnumType, MappedType, MixinType, SimpleType } from '@opra/common';
|
|
2
2
|
import { TsFile } from '../ts-file.js';
|
|
3
3
|
import type { TsGenerator } from '../ts-generator';
|
|
4
|
-
type Intent = '
|
|
5
|
-
export
|
|
4
|
+
type Intent = 'root' | 'extends' | 'property';
|
|
5
|
+
export type ProcessDataTypeResult = {
|
|
6
|
+
kind: 'internal';
|
|
7
|
+
typeName: string;
|
|
8
|
+
} | {
|
|
9
|
+
kind: 'named';
|
|
10
|
+
typeName: string;
|
|
11
|
+
file: TsFile;
|
|
12
|
+
} | {
|
|
13
|
+
kind: 'embedded';
|
|
14
|
+
code: string;
|
|
15
|
+
};
|
|
16
|
+
export declare function processDataType(this: TsGenerator, dataType: DataType, currentFile?: TsFile, intent?: Intent): Promise<ProcessDataTypeResult>;
|
|
6
17
|
/**
|
|
7
18
|
*
|
|
8
19
|
*/
|
|
9
|
-
export declare function
|
|
20
|
+
export declare function generateTypeDefinition(this: TsGenerator, currentFile: TsFile, dataType: DataType, intent?: Intent): Promise<string>;
|
|
10
21
|
/**
|
|
11
22
|
*
|
|
12
23
|
*/
|
|
13
|
-
export declare function
|
|
24
|
+
export declare function generateEnumTypeDefinition(this: TsGenerator, currentFile: TsFile, dataType: EnumType, intent?: Intent): Promise<string>;
|
|
14
25
|
/**
|
|
15
26
|
*
|
|
16
27
|
*/
|
|
17
|
-
export declare function
|
|
28
|
+
export declare function generateComplexTypeDefinition(this: TsGenerator, currentFile: TsFile, dataType: ComplexType, intent?: Intent): Promise<string>;
|
|
18
29
|
/**
|
|
19
30
|
*
|
|
20
31
|
*/
|
|
21
|
-
export declare function
|
|
32
|
+
export declare function generateSimpleTypeDefinition(this: TsGenerator, currentFile: TsFile, dataType: SimpleType, intent?: Intent): Promise<string>;
|
|
22
33
|
/**
|
|
23
34
|
*
|
|
24
35
|
*/
|
|
25
|
-
export declare function
|
|
36
|
+
export declare function generateMixinTypeDefinition(this: TsGenerator, currentFile: TsFile, dataType: MixinType, intent?: Intent): Promise<string>;
|
|
26
37
|
/**
|
|
27
38
|
*
|
|
28
39
|
*/
|
|
29
|
-
export declare function
|
|
40
|
+
export declare function generateMappedTypeDefinition(this: TsGenerator, currentFile: TsFile, dataType: MappedType, intent?: Intent): Promise<string>;
|
|
30
41
|
export {};
|
|
@@ -4,7 +4,7 @@ import { ApiDocument } from '@opra/common';
|
|
|
4
4
|
import { IFileWriter } from '../interfaces/file-writer.interface.js';
|
|
5
5
|
import { ILogger } from '../interfaces/logger.interface.js';
|
|
6
6
|
import { cleanDirectory } from './processors/clean-directory.js';
|
|
7
|
-
import { generateComplexTypeDefinition, generateEnumTypeDefinition, generateMappedTypeDefinition, generateMixinTypeDefinition, generateSimpleTypeDefinition,
|
|
7
|
+
import { generateComplexTypeDefinition, generateEnumTypeDefinition, generateMappedTypeDefinition, generateMixinTypeDefinition, generateSimpleTypeDefinition, generateTypeDefinition, processDataType } from './processors/process-data-types.js';
|
|
8
8
|
import { processDocument } from './processors/process-document.js';
|
|
9
9
|
import { processHttpApi } from './processors/process-http-api.js';
|
|
10
10
|
import { processHttpController } from './processors/process-http-controller.js';
|
|
@@ -30,14 +30,14 @@ export declare class TsGenerator extends EventEmitter {
|
|
|
30
30
|
protected cleanDirectory: typeof cleanDirectory;
|
|
31
31
|
protected processDocument: typeof processDocument;
|
|
32
32
|
protected processDataType: typeof processDataType;
|
|
33
|
-
protected
|
|
34
|
-
protected processHttpController: typeof processHttpController;
|
|
33
|
+
protected generateTypeDefinition: typeof generateTypeDefinition;
|
|
35
34
|
protected generateEnumTypeDefinition: typeof generateEnumTypeDefinition;
|
|
36
35
|
protected generateComplexTypeDefinition: typeof generateComplexTypeDefinition;
|
|
37
36
|
protected generateSimpleTypeDefinition: typeof generateSimpleTypeDefinition;
|
|
38
37
|
protected generateMappedTypeDefinition: typeof generateMappedTypeDefinition;
|
|
39
38
|
protected generateMixinTypeDefinition: typeof generateMixinTypeDefinition;
|
|
40
|
-
protected
|
|
39
|
+
protected processHttpApi: typeof processHttpApi;
|
|
40
|
+
protected processHttpController: typeof processHttpController;
|
|
41
41
|
protected _started: boolean;
|
|
42
42
|
protected _document?: ApiDocument;
|
|
43
43
|
protected _documentRoot: string;
|