swagger-typescript-api 13.0.3 → 13.0.4
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/README.md +310 -369
- package/cli/constants.js +3 -3
- package/cli/execute.js +12 -12
- package/cli/index.js +16 -16
- package/cli/operations/display-help.js +25 -25
- package/cli/parse-args.js +3 -3
- package/cli/process-option.js +11 -11
- package/index.d.ts +140 -45
- package/index.js +110 -111
- package/package.json +93 -108
- package/src/code-formatter.js +6 -6
- package/src/code-gen-process.js +45 -45
- package/src/commands/generate-templates/configuration.js +2 -2
- package/src/commands/generate-templates/index.js +1 -1
- package/src/commands/generate-templates/templates-gen-process.js +21 -21
- package/src/component-type-name-resolver.js +4 -4
- package/src/configuration.js +107 -107
- package/src/constants.js +26 -26
- package/src/index.js +3 -3
- package/src/schema-components-map.js +3 -3
- package/src/schema-parser/base-schema-parsers/array.js +3 -3
- package/src/schema-parser/base-schema-parsers/complex.js +5 -5
- package/src/schema-parser/base-schema-parsers/discriminator.js +12 -12
- package/src/schema-parser/base-schema-parsers/enum.js +12 -12
- package/src/schema-parser/base-schema-parsers/object.js +8 -8
- package/src/schema-parser/base-schema-parsers/primitive.js +3 -3
- package/src/schema-parser/complex-schema-parsers/all-of.js +2 -2
- package/src/schema-parser/complex-schema-parsers/any-of.js +2 -2
- package/src/schema-parser/complex-schema-parsers/not.js +1 -1
- package/src/schema-parser/complex-schema-parsers/one-of.js +2 -2
- package/src/schema-parser/mono-schema-parser.js +1 -1
- package/src/schema-parser/schema-formatters.js +14 -14
- package/src/schema-parser/schema-parser-fabric.js +5 -5
- package/src/schema-parser/schema-parser.js +24 -24
- package/src/schema-parser/schema-utils.js +21 -21
- package/src/schema-parser/util/enum-key-resolver.js +2 -2
- package/src/schema-routes/schema-routes.js +68 -68
- package/src/schema-routes/util/specific-arg-name-resolver.js +2 -2
- package/src/schema-walker.js +7 -7
- package/src/swagger-schema-resolver.js +16 -16
- package/src/templates-worker.js +21 -18
- package/src/translators/javascript.js +7 -7
- package/src/translators/translator.js +1 -1
- package/src/type-name-formatter.js +16 -16
- package/src/util/file-system.js +13 -14
- package/src/util/id.js +2 -2
- package/src/util/internal-case.js +1 -1
- package/src/util/logger.js +27 -27
- package/src/util/name-resolver.js +5 -5
- package/src/util/object-assign.js +2 -2
- package/src/util/pascal-case.js +1 -1
- package/src/util/request.js +15 -10
- package/templates/README.md +15 -14
- package/templates/base/README.md +4 -5
- package/templates/base/http-clients/fetch-http-client.ejs +1 -1
- package/templates/base/route-name.ejs +4 -4
- package/templates/default/README.md +4 -4
- package/templates/modular/README.md +4 -4
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
const _ = require(
|
|
2
|
-
const { generateId } = require(
|
|
1
|
+
const _ = require("lodash");
|
|
2
|
+
const { generateId } = require("../util/id.js");
|
|
3
3
|
const {
|
|
4
4
|
SpecificArgNameResolver,
|
|
5
|
-
} = require(
|
|
5
|
+
} = require("./util/specific-arg-name-resolver");
|
|
6
6
|
const {
|
|
7
7
|
DEFAULT_BODY_ARG_NAME,
|
|
8
8
|
RESERVED_BODY_ARG_NAMES,
|
|
9
9
|
RESERVED_HEADER_ARG_NAMES,
|
|
10
10
|
RESERVED_PATH_ARG_NAMES,
|
|
11
11
|
RESERVED_QUERY_ARG_NAMES,
|
|
12
|
-
} = require(
|
|
13
|
-
const { camelCase } = require(
|
|
12
|
+
} = require("../constants.js");
|
|
13
|
+
const { camelCase } = require("lodash");
|
|
14
14
|
|
|
15
15
|
const CONTENT_KIND = {
|
|
16
|
-
JSON:
|
|
17
|
-
URL_ENCODED:
|
|
18
|
-
FORM_DATA:
|
|
19
|
-
IMAGE:
|
|
20
|
-
OTHER:
|
|
21
|
-
TEXT:
|
|
16
|
+
JSON: "JSON",
|
|
17
|
+
URL_ENCODED: "URL_ENCODED",
|
|
18
|
+
FORM_DATA: "FORM_DATA",
|
|
19
|
+
IMAGE: "IMAGE",
|
|
20
|
+
OTHER: "OTHER",
|
|
21
|
+
TEXT: "TEXT",
|
|
22
22
|
};
|
|
23
23
|
|
|
24
24
|
class SchemaRoutes {
|
|
@@ -75,20 +75,20 @@ class SchemaRoutes {
|
|
|
75
75
|
this.templatesWorker = templatesWorker;
|
|
76
76
|
|
|
77
77
|
this.FORM_DATA_TYPES = _.uniq([
|
|
78
|
-
this.schemaUtils.getSchemaType({ type:
|
|
79
|
-
this.schemaUtils.getSchemaType({ type:
|
|
78
|
+
this.schemaUtils.getSchemaType({ type: "string", format: "file" }),
|
|
79
|
+
this.schemaUtils.getSchemaType({ type: "string", format: "binary" }),
|
|
80
80
|
]);
|
|
81
81
|
}
|
|
82
82
|
|
|
83
83
|
createRequestsMap = (routeInfoByMethodsMap) => {
|
|
84
|
-
const parameters = _.get(routeInfoByMethodsMap,
|
|
84
|
+
const parameters = _.get(routeInfoByMethodsMap, "parameters");
|
|
85
85
|
|
|
86
86
|
return _.reduce(
|
|
87
87
|
routeInfoByMethodsMap,
|
|
88
88
|
(acc, requestInfo, method) => {
|
|
89
89
|
if (
|
|
90
|
-
_.startsWith(method,
|
|
91
|
-
[
|
|
90
|
+
_.startsWith(method, "x-") ||
|
|
91
|
+
["parameters", "$ref"].includes(method)
|
|
92
92
|
) {
|
|
93
93
|
return acc;
|
|
94
94
|
}
|
|
@@ -109,7 +109,7 @@ class SchemaRoutes {
|
|
|
109
109
|
this.config.hooks.onPreBuildRoutePath(originalRouteName) ||
|
|
110
110
|
originalRouteName;
|
|
111
111
|
|
|
112
|
-
const pathParamMatches = (routeName ||
|
|
112
|
+
const pathParamMatches = (routeName || "").match(
|
|
113
113
|
/({(([A-z]){1}([a-zA-Z0-9]-?_?\.?)+)([0-9]+)?})|(:(([A-z]){1}([a-zA-Z0-9]-?_?\.?)+)([0-9]+)?:?)/g,
|
|
114
114
|
);
|
|
115
115
|
|
|
@@ -117,24 +117,24 @@ class SchemaRoutes {
|
|
|
117
117
|
const pathParams = _.reduce(
|
|
118
118
|
pathParamMatches,
|
|
119
119
|
(pathParams, match) => {
|
|
120
|
-
const paramName = _.replace(match, /\{|\}|:/g,
|
|
120
|
+
const paramName = _.replace(match, /\{|\}|:/g, "");
|
|
121
121
|
|
|
122
122
|
if (!paramName) return pathParams;
|
|
123
123
|
|
|
124
|
-
if (_.includes(paramName,
|
|
125
|
-
this.logger.warn(
|
|
124
|
+
if (_.includes(paramName, "-")) {
|
|
125
|
+
this.logger.warn("wrong path param name", paramName);
|
|
126
126
|
}
|
|
127
127
|
|
|
128
128
|
pathParams.push({
|
|
129
129
|
$match: match,
|
|
130
130
|
name: _.camelCase(paramName),
|
|
131
131
|
required: true,
|
|
132
|
-
type:
|
|
133
|
-
description:
|
|
132
|
+
type: "string",
|
|
133
|
+
description: "",
|
|
134
134
|
schema: {
|
|
135
|
-
type:
|
|
135
|
+
type: "string",
|
|
136
136
|
},
|
|
137
|
-
in:
|
|
137
|
+
in: "path",
|
|
138
138
|
});
|
|
139
139
|
|
|
140
140
|
return pathParams;
|
|
@@ -154,7 +154,7 @@ class SchemaRoutes {
|
|
|
154
154
|
) || pathParam.name;
|
|
155
155
|
return _.replace(fixedRoute, pathParam.$match, `\${${insertion}}`);
|
|
156
156
|
},
|
|
157
|
-
routeName ||
|
|
157
|
+
routeName || "",
|
|
158
158
|
);
|
|
159
159
|
|
|
160
160
|
const queryParamMatches = fixedRoute.match(/(\{\?.*\})/g);
|
|
@@ -162,35 +162,35 @@ class SchemaRoutes {
|
|
|
162
162
|
|
|
163
163
|
if (queryParamMatches && queryParamMatches.length) {
|
|
164
164
|
queryParamMatches.forEach((match) => {
|
|
165
|
-
fixedRoute = fixedRoute.replace(match,
|
|
165
|
+
fixedRoute = fixedRoute.replace(match, "");
|
|
166
166
|
});
|
|
167
167
|
|
|
168
168
|
_.uniq(
|
|
169
169
|
queryParamMatches
|
|
170
|
-
.join(
|
|
171
|
-
.replace(/(\{\?)|(\})|\s/g,
|
|
172
|
-
.split(
|
|
170
|
+
.join(",")
|
|
171
|
+
.replace(/(\{\?)|(\})|\s/g, "")
|
|
172
|
+
.split(","),
|
|
173
173
|
).forEach((paramName) => {
|
|
174
|
-
if (_.includes(paramName,
|
|
175
|
-
this.logger.warn(
|
|
174
|
+
if (_.includes(paramName, "-")) {
|
|
175
|
+
this.logger.warn("wrong query param name", paramName);
|
|
176
176
|
}
|
|
177
177
|
|
|
178
178
|
queryParams.push({
|
|
179
179
|
$match: paramName,
|
|
180
180
|
name: _.camelCase(paramName),
|
|
181
181
|
required: true,
|
|
182
|
-
type:
|
|
183
|
-
description:
|
|
182
|
+
type: "string",
|
|
183
|
+
description: "",
|
|
184
184
|
schema: {
|
|
185
|
-
type:
|
|
185
|
+
type: "string",
|
|
186
186
|
},
|
|
187
|
-
in:
|
|
187
|
+
in: "query",
|
|
188
188
|
});
|
|
189
189
|
});
|
|
190
190
|
}
|
|
191
191
|
|
|
192
192
|
const result = {
|
|
193
|
-
originalRoute: originalRouteName ||
|
|
193
|
+
originalRoute: originalRouteName || "",
|
|
194
194
|
route: fixedRoute,
|
|
195
195
|
pathParams,
|
|
196
196
|
queryParams,
|
|
@@ -246,7 +246,7 @@ class SchemaRoutes {
|
|
|
246
246
|
};
|
|
247
247
|
}
|
|
248
248
|
|
|
249
|
-
if (routeParam.in ===
|
|
249
|
+
if (routeParam.in === "path") {
|
|
250
250
|
if (!routeParam.name) return;
|
|
251
251
|
|
|
252
252
|
routeParam.name = _.camelCase(routeParam.name);
|
|
@@ -300,29 +300,29 @@ class SchemaRoutes {
|
|
|
300
300
|
getContentKind = (contentTypes) => {
|
|
301
301
|
if (
|
|
302
302
|
_.some(contentTypes, (contentType) =>
|
|
303
|
-
_.startsWith(contentType,
|
|
303
|
+
_.startsWith(contentType, "application/json"),
|
|
304
304
|
) ||
|
|
305
|
-
_.some(contentTypes, (contentType) => _.endsWith(contentType,
|
|
305
|
+
_.some(contentTypes, (contentType) => _.endsWith(contentType, "+json"))
|
|
306
306
|
) {
|
|
307
307
|
return CONTENT_KIND.JSON;
|
|
308
308
|
}
|
|
309
309
|
|
|
310
|
-
if (contentTypes.includes(
|
|
310
|
+
if (contentTypes.includes("application/x-www-form-urlencoded")) {
|
|
311
311
|
return CONTENT_KIND.URL_ENCODED;
|
|
312
312
|
}
|
|
313
313
|
|
|
314
|
-
if (contentTypes.includes(
|
|
314
|
+
if (contentTypes.includes("multipart/form-data")) {
|
|
315
315
|
return CONTENT_KIND.FORM_DATA;
|
|
316
316
|
}
|
|
317
317
|
|
|
318
318
|
if (
|
|
319
|
-
_.some(contentTypes, (contentType) => _.includes(contentType,
|
|
319
|
+
_.some(contentTypes, (contentType) => _.includes(contentType, "image/"))
|
|
320
320
|
) {
|
|
321
321
|
return CONTENT_KIND.IMAGE;
|
|
322
322
|
}
|
|
323
323
|
|
|
324
324
|
if (
|
|
325
|
-
_.some(contentTypes, (contentType) => _.startsWith(contentType,
|
|
325
|
+
_.some(contentTypes, (contentType) => _.startsWith(contentType, "text/"))
|
|
326
326
|
) {
|
|
327
327
|
return CONTENT_KIND.TEXT;
|
|
328
328
|
}
|
|
@@ -331,13 +331,13 @@ class SchemaRoutes {
|
|
|
331
331
|
};
|
|
332
332
|
|
|
333
333
|
isSuccessStatus = (status) =>
|
|
334
|
-
(this.config.defaultResponseAsSuccess && status ===
|
|
334
|
+
(this.config.defaultResponseAsSuccess && status === "default") ||
|
|
335
335
|
(+status >= this.config.successResponseStatusRange[0] &&
|
|
336
336
|
+status <= this.config.successResponseStatusRange[1]) ||
|
|
337
|
-
status ===
|
|
337
|
+
status === "2xx";
|
|
338
338
|
|
|
339
339
|
getSchemaFromRequestType = (requestInfo) => {
|
|
340
|
-
const content = _.get(requestInfo,
|
|
340
|
+
const content = _.get(requestInfo, "content");
|
|
341
341
|
|
|
342
342
|
if (!content) return null;
|
|
343
343
|
|
|
@@ -398,7 +398,7 @@ class SchemaRoutes {
|
|
|
398
398
|
const typeNameWithoutOpId = _.replace(
|
|
399
399
|
refTypeInfo.typeName,
|
|
400
400
|
operationId,
|
|
401
|
-
|
|
401
|
+
"",
|
|
402
402
|
);
|
|
403
403
|
if (
|
|
404
404
|
_.find(parsedSchemas, (schema) => schema.name === typeNameWithoutOpId)
|
|
@@ -407,10 +407,10 @@ class SchemaRoutes {
|
|
|
407
407
|
}
|
|
408
408
|
|
|
409
409
|
switch (refTypeInfo.componentName) {
|
|
410
|
-
case
|
|
410
|
+
case "schemas":
|
|
411
411
|
return this.typeNameFormatter.format(refTypeInfo.typeName);
|
|
412
|
-
case
|
|
413
|
-
case
|
|
412
|
+
case "responses":
|
|
413
|
+
case "requestBodies":
|
|
414
414
|
return this.schemaParserFabric.getInlineParseContent(
|
|
415
415
|
this.getSchemaFromRequestType(refTypeInfo.rawTypeData),
|
|
416
416
|
refTypeInfo.typeName || null,
|
|
@@ -456,7 +456,7 @@ class SchemaRoutes {
|
|
|
456
456
|
),
|
|
457
457
|
description:
|
|
458
458
|
this.schemaParserFabric.schemaFormatters.formatDescription(
|
|
459
|
-
requestInfo.description ||
|
|
459
|
+
requestInfo.description || "",
|
|
460
460
|
true,
|
|
461
461
|
),
|
|
462
462
|
status: _.isNaN(+status) ? status : +status,
|
|
@@ -472,7 +472,7 @@ class SchemaRoutes {
|
|
|
472
472
|
|
|
473
473
|
const contentTypes = this.getContentTypes(responses, [
|
|
474
474
|
...(produces || []),
|
|
475
|
-
routeInfo[
|
|
475
|
+
routeInfo["x-accepts"],
|
|
476
476
|
]);
|
|
477
477
|
|
|
478
478
|
const responseInfos = this.getRequestInfoTypes({
|
|
@@ -492,7 +492,7 @@ class SchemaRoutes {
|
|
|
492
492
|
|
|
493
493
|
const handleResponseHeaders = (src) => {
|
|
494
494
|
if (!src) {
|
|
495
|
-
return
|
|
495
|
+
return "headers: {},";
|
|
496
496
|
}
|
|
497
497
|
const headerTypes = Object.fromEntries(
|
|
498
498
|
Object.entries(src).map(([k, v]) => {
|
|
@@ -501,7 +501,7 @@ class SchemaRoutes {
|
|
|
501
501
|
);
|
|
502
502
|
const r = `headers: { ${Object.entries(headerTypes)
|
|
503
503
|
.map(([k, v]) => `"${k}": ${v}`)
|
|
504
|
-
.join(
|
|
504
|
+
.join(",")} },`;
|
|
505
505
|
return r;
|
|
506
506
|
};
|
|
507
507
|
|
|
@@ -545,7 +545,7 @@ class SchemaRoutes {
|
|
|
545
545
|
|
|
546
546
|
let usageName = `${schemaPart.name}`;
|
|
547
547
|
|
|
548
|
-
if (usageName.includes(
|
|
548
|
+
if (usageName.includes(".")) {
|
|
549
549
|
usageName = camelCase(usageName);
|
|
550
550
|
}
|
|
551
551
|
|
|
@@ -564,7 +564,7 @@ class SchemaRoutes {
|
|
|
564
564
|
},
|
|
565
565
|
{
|
|
566
566
|
properties: {},
|
|
567
|
-
type:
|
|
567
|
+
type: "object",
|
|
568
568
|
},
|
|
569
569
|
);
|
|
570
570
|
};
|
|
@@ -576,7 +576,7 @@ class SchemaRoutes {
|
|
|
576
576
|
|
|
577
577
|
const contentTypes = this.getContentTypes(
|
|
578
578
|
[requestBody],
|
|
579
|
-
[...(consumes || []), routeInfo[
|
|
579
|
+
[...(consumes || []), routeInfo["x-contentType"]],
|
|
580
580
|
);
|
|
581
581
|
let contentKind = this.getContentKind(contentTypes);
|
|
582
582
|
|
|
@@ -650,7 +650,7 @@ class SchemaRoutes {
|
|
|
650
650
|
type: content,
|
|
651
651
|
required:
|
|
652
652
|
requestBody &&
|
|
653
|
-
(typeof requestBody.required ===
|
|
653
|
+
(typeof requestBody.required === "undefined" || !!requestBody.required),
|
|
654
654
|
};
|
|
655
655
|
};
|
|
656
656
|
|
|
@@ -669,7 +669,7 @@ class SchemaRoutes {
|
|
|
669
669
|
if (pathArgSchema.name) {
|
|
670
670
|
acc[pathArgSchema.name] = {
|
|
671
671
|
...pathArgSchema,
|
|
672
|
-
in:
|
|
672
|
+
in: "path",
|
|
673
673
|
};
|
|
674
674
|
}
|
|
675
675
|
|
|
@@ -679,12 +679,12 @@ class SchemaRoutes {
|
|
|
679
679
|
);
|
|
680
680
|
|
|
681
681
|
const fixedQueryParams = _.reduce(
|
|
682
|
-
_.get(queryObjectSchema,
|
|
682
|
+
_.get(queryObjectSchema, "properties", {}),
|
|
683
683
|
(acc, property, name) => {
|
|
684
684
|
if (name && _.isObject(property)) {
|
|
685
685
|
acc[name] = {
|
|
686
686
|
...property,
|
|
687
|
-
in:
|
|
687
|
+
in: "query",
|
|
688
688
|
};
|
|
689
689
|
}
|
|
690
690
|
|
|
@@ -784,17 +784,17 @@ class SchemaRoutes {
|
|
|
784
784
|
title: errorSchemas
|
|
785
785
|
.map((schema) => schema.title)
|
|
786
786
|
.filter(Boolean)
|
|
787
|
-
.join(
|
|
787
|
+
.join(" "),
|
|
788
788
|
description: errorSchemas
|
|
789
789
|
.map((schema) => schema.description)
|
|
790
790
|
.filter(Boolean)
|
|
791
|
-
.join(
|
|
791
|
+
.join("\n"),
|
|
792
792
|
},
|
|
793
793
|
null,
|
|
794
794
|
[routeInfo.operationId],
|
|
795
795
|
);
|
|
796
796
|
const component = this.schemaComponentsMap.createComponent(
|
|
797
|
-
this.schemaComponentsMap.createRef([
|
|
797
|
+
this.schemaComponentsMap.createRef(["components", "schemas", typeName]),
|
|
798
798
|
{ ...schema },
|
|
799
799
|
);
|
|
800
800
|
responseBodyInfo.error.schemas = [component];
|
|
@@ -843,7 +843,7 @@ class SchemaRoutes {
|
|
|
843
843
|
const duplicates = routeNameDuplicatesMap.get(duplicateIdentifier);
|
|
844
844
|
|
|
845
845
|
const routeNameInfo = {
|
|
846
|
-
usage: routeName + (duplicates > 1 ? duplicates :
|
|
846
|
+
usage: routeName + (duplicates > 1 ? duplicates : ""),
|
|
847
847
|
original: routeName,
|
|
848
848
|
duplicate: duplicates > 1,
|
|
849
849
|
};
|
|
@@ -891,7 +891,7 @@ class SchemaRoutes {
|
|
|
891
891
|
const moduleName =
|
|
892
892
|
moduleNameFirstTag && firstTag
|
|
893
893
|
? _.camelCase(firstTag)
|
|
894
|
-
: _.camelCase(_.compact(_.split(route,
|
|
894
|
+
: _.camelCase(_.compact(_.split(route, "/"))[moduleNameIndex]);
|
|
895
895
|
let hasSecurity = !!(globalSecurity && globalSecurity.length);
|
|
896
896
|
if (security) {
|
|
897
897
|
hasSecurity = security.length > 0;
|
|
@@ -1059,7 +1059,7 @@ class SchemaRoutes {
|
|
|
1059
1059
|
|
|
1060
1060
|
return {
|
|
1061
1061
|
id: routeId,
|
|
1062
|
-
namespace: _.replace(moduleName, /^(\d)/,
|
|
1062
|
+
namespace: _.replace(moduleName, /^(\d)/, "v$1"),
|
|
1063
1063
|
routeName,
|
|
1064
1064
|
routeParams,
|
|
1065
1065
|
requestBodyInfo,
|
|
@@ -1156,7 +1156,7 @@ class SchemaRoutes {
|
|
|
1156
1156
|
const routeGroups = _.reduce(
|
|
1157
1157
|
groupedRoutes,
|
|
1158
1158
|
(acc, routesGroup, moduleName) => {
|
|
1159
|
-
if (moduleName ===
|
|
1159
|
+
if (moduleName === "$outOfModule") {
|
|
1160
1160
|
acc.outOfModule = routesGroup;
|
|
1161
1161
|
} else {
|
|
1162
1162
|
if (!acc.combined) acc.combined = [];
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const { NameResolver } = require(
|
|
1
|
+
const { NameResolver } = require("../../util/name-resolver");
|
|
2
2
|
|
|
3
3
|
class SpecificArgNameResolver extends NameResolver {
|
|
4
4
|
counter = 1;
|
|
@@ -13,7 +13,7 @@ class SpecificArgNameResolver extends NameResolver {
|
|
|
13
13
|
(variants[0] && `${variants[0]}${this.counter++}`) ||
|
|
14
14
|
`${this.config.specificArgNameResolverName}${this.counter++}`;
|
|
15
15
|
this.logger.debug(
|
|
16
|
-
|
|
16
|
+
"generated fallback type name for specific arg - ",
|
|
17
17
|
generatedVariant,
|
|
18
18
|
);
|
|
19
19
|
return generatedVariant;
|
package/src/schema-walker.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const _ = require(
|
|
1
|
+
const _ = require("lodash");
|
|
2
2
|
|
|
3
3
|
// TODO: WIP
|
|
4
4
|
// this class will be needed to walk by schema everywhere
|
|
@@ -32,7 +32,7 @@ class SchemaWalker {
|
|
|
32
32
|
* @returns {any}
|
|
33
33
|
*/
|
|
34
34
|
findByRef = (ref) => {
|
|
35
|
-
this.logger.debug(
|
|
35
|
+
this.logger.debug("try to resolve ref by path", ref);
|
|
36
36
|
|
|
37
37
|
if (this.caches.has(ref)) {
|
|
38
38
|
return this.caches.get(ref);
|
|
@@ -47,10 +47,10 @@ class SchemaWalker {
|
|
|
47
47
|
}
|
|
48
48
|
}
|
|
49
49
|
} else if (this._isRemoteRef(ref)) {
|
|
50
|
-
this.logger.debug(
|
|
50
|
+
this.logger.debug("remote refs not supported", ref);
|
|
51
51
|
return null;
|
|
52
52
|
} else {
|
|
53
|
-
const [address, path] = path.split(
|
|
53
|
+
const [address, path] = path.split("#");
|
|
54
54
|
let swaggerSchemaObject;
|
|
55
55
|
|
|
56
56
|
if (this.schemas.has(address)) {
|
|
@@ -71,15 +71,15 @@ class SchemaWalker {
|
|
|
71
71
|
};
|
|
72
72
|
|
|
73
73
|
_isLocalRef = (ref) => {
|
|
74
|
-
return ref.startsWith(
|
|
74
|
+
return ref.startsWith("#");
|
|
75
75
|
};
|
|
76
76
|
|
|
77
77
|
_isRemoteRef = (ref) => {
|
|
78
|
-
return ref.startsWith(
|
|
78
|
+
return ref.startsWith("http://") || ref.startsWith("https://");
|
|
79
79
|
};
|
|
80
80
|
|
|
81
81
|
_getRefDataFromSchema = (schema, ref) => {
|
|
82
|
-
const path = ref.replace(
|
|
82
|
+
const path = ref.replace("#", "").split("/");
|
|
83
83
|
const refData = _.get(schema, path);
|
|
84
84
|
if (refData) {
|
|
85
85
|
this.caches.set(ref, refData);
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
const _ = require(
|
|
2
|
-
const converter = require(
|
|
3
|
-
const yaml = require(
|
|
4
|
-
const { Request } = require(
|
|
1
|
+
const _ = require("lodash");
|
|
2
|
+
const converter = require("swagger2openapi");
|
|
3
|
+
const yaml = require("js-yaml");
|
|
4
|
+
const { Request } = require("./util/request");
|
|
5
5
|
|
|
6
6
|
class SwaggerSchemaResolver {
|
|
7
7
|
/**
|
|
@@ -70,8 +70,8 @@ class SwaggerSchemaResolver {
|
|
|
70
70
|
const result = _.cloneDeep(swaggerSchema);
|
|
71
71
|
result.info = _.merge(
|
|
72
72
|
{
|
|
73
|
-
title:
|
|
74
|
-
version:
|
|
73
|
+
title: "No title",
|
|
74
|
+
version: "",
|
|
75
75
|
},
|
|
76
76
|
result.info,
|
|
77
77
|
);
|
|
@@ -84,14 +84,14 @@ class SwaggerSchemaResolver {
|
|
|
84
84
|
{
|
|
85
85
|
...converterOptions,
|
|
86
86
|
warnOnly: true,
|
|
87
|
-
refSiblings:
|
|
88
|
-
rbname:
|
|
87
|
+
refSiblings: "preserve",
|
|
88
|
+
rbname: "requestBodyName",
|
|
89
89
|
},
|
|
90
90
|
(err, options) => {
|
|
91
91
|
const parsedSwaggerSchema = _.get(
|
|
92
92
|
err,
|
|
93
|
-
|
|
94
|
-
_.get(options,
|
|
93
|
+
"options.openapi",
|
|
94
|
+
_.get(options, "openapi"),
|
|
95
95
|
);
|
|
96
96
|
if (!parsedSwaggerSchema && err) {
|
|
97
97
|
throw new Error(err);
|
|
@@ -138,7 +138,7 @@ class SwaggerSchemaResolver {
|
|
|
138
138
|
}
|
|
139
139
|
|
|
140
140
|
processSwaggerSchemaFile(file) {
|
|
141
|
-
if (typeof file !==
|
|
141
|
+
if (typeof file !== "string") return file;
|
|
142
142
|
|
|
143
143
|
try {
|
|
144
144
|
return JSON.parse(file);
|
|
@@ -148,8 +148,8 @@ class SwaggerSchemaResolver {
|
|
|
148
148
|
}
|
|
149
149
|
|
|
150
150
|
fixSwaggerSchema({ usageSchema, originalSchema }) {
|
|
151
|
-
const usagePaths = _.get(usageSchema,
|
|
152
|
-
const originalPaths = _.get(originalSchema,
|
|
151
|
+
const usagePaths = _.get(usageSchema, "paths");
|
|
152
|
+
const originalPaths = _.get(originalSchema, "paths");
|
|
153
153
|
|
|
154
154
|
// walk by routes
|
|
155
155
|
_.each(usagePaths, (usagePathObject, route) => {
|
|
@@ -158,10 +158,10 @@ class SwaggerSchemaResolver {
|
|
|
158
158
|
// walk by methods
|
|
159
159
|
_.each(usagePathObject, (usageRouteInfo, methodName) => {
|
|
160
160
|
const originalRouteInfo = _.get(originalPathObject, methodName);
|
|
161
|
-
const usageRouteParams = _.get(usageRouteInfo,
|
|
162
|
-
const originalRouteParams = _.get(originalRouteInfo,
|
|
161
|
+
const usageRouteParams = _.get(usageRouteInfo, "parameters", []);
|
|
162
|
+
const originalRouteParams = _.get(originalRouteInfo, "parameters", []);
|
|
163
163
|
|
|
164
|
-
if (typeof usageRouteInfo ===
|
|
164
|
+
if (typeof usageRouteInfo === "object") {
|
|
165
165
|
usageRouteInfo.consumes = _.uniq(
|
|
166
166
|
_.compact([
|
|
167
167
|
...(usageRouteInfo.consumes || []),
|
package/src/templates-worker.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
const { resolve } = require(
|
|
2
|
-
const _ = require(
|
|
3
|
-
const Eta = require(
|
|
4
|
-
const path = require(
|
|
1
|
+
const { resolve } = require("node:path");
|
|
2
|
+
const _ = require("lodash");
|
|
3
|
+
const Eta = require("eta");
|
|
4
|
+
const path = require("node:path");
|
|
5
5
|
|
|
6
6
|
class TemplatesWorker {
|
|
7
7
|
/**
|
|
@@ -34,9 +34,9 @@ class TemplatesWorker {
|
|
|
34
34
|
* @returns {CodeGenConfig.templatePaths}
|
|
35
35
|
*/
|
|
36
36
|
getTemplatePaths = (config) => {
|
|
37
|
-
const baseTemplatesPath = resolve(__dirname,
|
|
38
|
-
const defaultTemplatesPath = resolve(__dirname,
|
|
39
|
-
const modularTemplatesPath = resolve(__dirname,
|
|
37
|
+
const baseTemplatesPath = resolve(__dirname, "../templates/base");
|
|
38
|
+
const defaultTemplatesPath = resolve(__dirname, "../templates/default");
|
|
39
|
+
const modularTemplatesPath = resolve(__dirname, "../templates/modular");
|
|
40
40
|
const originalTemplatesPath = config.modular
|
|
41
41
|
? modularTemplatesPath
|
|
42
42
|
: defaultTemplatesPath;
|
|
@@ -59,12 +59,12 @@ class TemplatesWorker {
|
|
|
59
59
|
|
|
60
60
|
cropExtension = (path) =>
|
|
61
61
|
this.config.templateExtensions.reduce(
|
|
62
|
-
(path, ext) => (_.endsWith(path, ext) ? path.replace(ext,
|
|
62
|
+
(path, ext) => (_.endsWith(path, ext) ? path.replace(ext, "") : path),
|
|
63
63
|
path,
|
|
64
64
|
);
|
|
65
65
|
|
|
66
66
|
getTemplateFullPath = (path, fileName) => {
|
|
67
|
-
const raw = resolve(path,
|
|
67
|
+
const raw = resolve(path, "./", this.cropExtension(fileName));
|
|
68
68
|
const pathVariants = this.config.templateExtensions.map(
|
|
69
69
|
(extension) => `${raw}${extension}`,
|
|
70
70
|
);
|
|
@@ -76,13 +76,16 @@ class TemplatesWorker {
|
|
|
76
76
|
|
|
77
77
|
requireFnFromTemplate = (packageOrPath) => {
|
|
78
78
|
const isPath =
|
|
79
|
-
_.startsWith(packageOrPath,
|
|
79
|
+
_.startsWith(packageOrPath, "./") || _.startsWith(packageOrPath, "../");
|
|
80
80
|
|
|
81
81
|
if (isPath) {
|
|
82
|
-
return require(
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
82
|
+
return require(
|
|
83
|
+
path.resolve(
|
|
84
|
+
this.config.templatePaths.custom ||
|
|
85
|
+
this.config.templatePaths.original,
|
|
86
|
+
packageOrPath,
|
|
87
|
+
),
|
|
88
|
+
);
|
|
86
89
|
}
|
|
87
90
|
|
|
88
91
|
return require(packageOrPath);
|
|
@@ -95,7 +98,7 @@ class TemplatesWorker {
|
|
|
95
98
|
return this.fileSystem.getFileContent(path);
|
|
96
99
|
}
|
|
97
100
|
|
|
98
|
-
if (!fileName) return
|
|
101
|
+
if (!fileName) return "";
|
|
99
102
|
|
|
100
103
|
const customFullPath =
|
|
101
104
|
templatePaths.custom &&
|
|
@@ -120,7 +123,7 @@ class TemplatesWorker {
|
|
|
120
123
|
`"${_.lowerCase(name)}" template not found in "${
|
|
121
124
|
templatePaths.custom
|
|
122
125
|
}"`,
|
|
123
|
-
|
|
126
|
+
"\nCode generator will use the default template",
|
|
124
127
|
);
|
|
125
128
|
} else {
|
|
126
129
|
this.logger.log(
|
|
@@ -202,7 +205,7 @@ class TemplatesWorker {
|
|
|
202
205
|
return this.fileSystem.getFileContent(originalPath);
|
|
203
206
|
}
|
|
204
207
|
|
|
205
|
-
return
|
|
208
|
+
return "";
|
|
206
209
|
};
|
|
207
210
|
|
|
208
211
|
/**
|
|
@@ -212,7 +215,7 @@ class TemplatesWorker {
|
|
|
212
215
|
* @returns {Promise<string|string|void>}
|
|
213
216
|
*/
|
|
214
217
|
renderTemplate = (template, configuration, options) => {
|
|
215
|
-
if (!template) return
|
|
218
|
+
if (!template) return "";
|
|
216
219
|
|
|
217
220
|
return Eta.render(
|
|
218
221
|
template,
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
const ts = require(
|
|
2
|
-
const { Translator } = require(
|
|
1
|
+
const ts = require("typescript");
|
|
2
|
+
const { Translator } = require("./translator");
|
|
3
3
|
|
|
4
4
|
class JavascriptTranslator extends Translator {
|
|
5
5
|
/**
|
|
@@ -51,17 +51,17 @@ class JavascriptTranslator extends Translator {
|
|
|
51
51
|
const dtsFileName = `${input.fileName}${ts.Extension.Dts}`;
|
|
52
52
|
const sourceContent = compiled[jsFileName];
|
|
53
53
|
const tsImportRows = input.fileContent
|
|
54
|
-
.split(
|
|
55
|
-
.filter((line) => line.startsWith(
|
|
54
|
+
.split("\n")
|
|
55
|
+
.filter((line) => line.startsWith("import "));
|
|
56
56
|
const declarationContent = compiled[dtsFileName]
|
|
57
|
-
.split(
|
|
57
|
+
.split("\n")
|
|
58
58
|
.map((line) => {
|
|
59
|
-
if (line.startsWith(
|
|
59
|
+
if (line.startsWith("import ")) {
|
|
60
60
|
return tsImportRows.shift();
|
|
61
61
|
}
|
|
62
62
|
return line;
|
|
63
63
|
})
|
|
64
|
-
.join(
|
|
64
|
+
.join("\n");
|
|
65
65
|
|
|
66
66
|
return [
|
|
67
67
|
{
|