@opra/cli 1.0.0-alpha.11 → 1.0.0-alpha.13
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/generators/clean-directory.js +35 -0
- package/cjs/ts-generator/generators/generate-data-type.js +258 -0
- package/cjs/ts-generator/generators/generate-document.js +60 -0
- package/cjs/ts-generator/generators/generate-http-api.js +45 -0
- package/cjs/ts-generator/generators/generate-http-controller.js +228 -0
- 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 +16 -16
- package/esm/ts-generator/generators/clean-directory.js +30 -0
- package/esm/ts-generator/generators/generate-data-type.js +247 -0
- package/esm/ts-generator/generators/generate-document.js +55 -0
- package/esm/ts-generator/generators/generate-http-api.js +40 -0
- package/esm/ts-generator/generators/generate-http-controller.js +223 -0
- 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 +16 -16
- package/package.json +4 -3
- package/types/ts-generator/generators/clean-directory.d.ts +2 -0
- package/types/ts-generator/generators/generate-data-type.d.ts +41 -0
- package/types/ts-generator/generators/generate-document.d.ts +8 -0
- package/types/ts-generator/generators/generate-http-api.d.ts +3 -0
- package/types/ts-generator/generators/generate-http-controller.d.ts +3 -0
- package/types/ts-generator/processors/process-data-types.d.ts +19 -8
- package/types/ts-generator/ts-generator.d.ts +15 -15
|
@@ -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
|
}
|
|
@@ -8,11 +8,11 @@ const node_path_1 = tslib_1.__importDefault(require("node:path"));
|
|
|
8
8
|
const node_process_1 = tslib_1.__importDefault(require("node:process"));
|
|
9
9
|
const chalk_1 = tslib_1.__importDefault(require("chalk"));
|
|
10
10
|
const file_writer_js_1 = require("../file-writer.js");
|
|
11
|
-
const clean_directory_js_1 = require("./
|
|
12
|
-
const
|
|
13
|
-
const
|
|
14
|
-
const
|
|
15
|
-
const
|
|
11
|
+
const clean_directory_js_1 = require("./generators/clean-directory.js");
|
|
12
|
+
const generate_data_type_js_1 = require("./generators/generate-data-type.js");
|
|
13
|
+
const generate_document_js_1 = require("./generators/generate-document.js");
|
|
14
|
+
const generate_http_api_js_1 = require("./generators/generate-http-api.js");
|
|
15
|
+
const generate_http_controller_js_1 = require("./generators/generate-http-controller.js");
|
|
16
16
|
const ts_file_js_1 = require("./ts-file.js");
|
|
17
17
|
/**
|
|
18
18
|
* @class TsGenerator
|
|
@@ -49,7 +49,7 @@ class TsGenerator extends node_events_1.EventEmitter {
|
|
|
49
49
|
this.emit('log', chalk_1.default.cyan('Removing old files..'));
|
|
50
50
|
this.cleanDirectory(this.outDir);
|
|
51
51
|
this._apiPath = '/api';
|
|
52
|
-
await this.
|
|
52
|
+
await this.generateDocument();
|
|
53
53
|
const { importExt } = this.options;
|
|
54
54
|
// Write files
|
|
55
55
|
for (const file of Object.values(this._files)) {
|
|
@@ -95,14 +95,14 @@ class TsGenerator extends node_events_1.EventEmitter {
|
|
|
95
95
|
exports.TsGenerator = TsGenerator;
|
|
96
96
|
(() => {
|
|
97
97
|
TsGenerator.prototype.cleanDirectory = clean_directory_js_1.cleanDirectory;
|
|
98
|
-
TsGenerator.prototype.
|
|
99
|
-
TsGenerator.prototype.
|
|
100
|
-
TsGenerator.prototype.
|
|
101
|
-
TsGenerator.prototype.
|
|
102
|
-
TsGenerator.prototype.
|
|
103
|
-
TsGenerator.prototype.
|
|
104
|
-
TsGenerator.prototype.
|
|
105
|
-
TsGenerator.prototype.
|
|
106
|
-
TsGenerator.prototype.
|
|
107
|
-
TsGenerator.prototype.
|
|
98
|
+
TsGenerator.prototype.generateDocument = generate_document_js_1.generateDocument;
|
|
99
|
+
TsGenerator.prototype.generateDataType = generate_data_type_js_1.generateDataType;
|
|
100
|
+
TsGenerator.prototype._generateTypeCode = generate_data_type_js_1._generateTypeCode;
|
|
101
|
+
TsGenerator.prototype._generateEnumTypeCode = generate_data_type_js_1._generateEnumTypeCode;
|
|
102
|
+
TsGenerator.prototype._generateComplexTypeCode = generate_data_type_js_1._generateComplexTypeCode;
|
|
103
|
+
TsGenerator.prototype._generateSimpleTypeCode = generate_data_type_js_1._generateSimpleTypeCode;
|
|
104
|
+
TsGenerator.prototype._generateMappedTypeCode = generate_data_type_js_1._generateMappedTypeCode;
|
|
105
|
+
TsGenerator.prototype._generateMixinTypeCode = generate_data_type_js_1._generateMixinTypeCode;
|
|
106
|
+
TsGenerator.prototype.generateHttpApi = generate_http_api_js_1.generateHttpApi;
|
|
107
|
+
TsGenerator.prototype.generateHttpController = generate_http_controller_js_1.generateHttpController;
|
|
108
108
|
})();
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import chalk from 'chalk';
|
|
4
|
+
export function cleanDirectory(dirname) {
|
|
5
|
+
const rootDir = dirname;
|
|
6
|
+
const _cleanDirectory = (targetDir) => {
|
|
7
|
+
if (!fs.existsSync(targetDir))
|
|
8
|
+
return;
|
|
9
|
+
const files = fs.readdirSync(targetDir);
|
|
10
|
+
for (const f of files) {
|
|
11
|
+
const absolutePath = path.join(targetDir, f);
|
|
12
|
+
if (fs.statSync(absolutePath).isDirectory()) {
|
|
13
|
+
_cleanDirectory(absolutePath);
|
|
14
|
+
if (!fs.readdirSync(absolutePath).length) {
|
|
15
|
+
this.emit('verbose', chalk.cyan(`Removing directory ${path.relative(absolutePath, rootDir)}`));
|
|
16
|
+
fs.rmdirSync(absolutePath);
|
|
17
|
+
}
|
|
18
|
+
continue;
|
|
19
|
+
}
|
|
20
|
+
if (path.extname(f) === '.ts') {
|
|
21
|
+
const contents = fs.readFileSync(absolutePath, 'utf-8');
|
|
22
|
+
if (contents.includes('#!oprimp_auto_generated!#')) {
|
|
23
|
+
this.emit('verbose', chalk.cyan(`Removing file ${path.relative(absolutePath, rootDir)}`));
|
|
24
|
+
fs.unlinkSync(absolutePath);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
_cleanDirectory(dirname);
|
|
30
|
+
}
|