swagger-typescript-api 11.1.2 → 12.0.0
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 +23 -5
- package/index.d.ts +67 -3
- package/index.js +6 -1
- package/package.json +6 -2
- package/src/code-gen-process.js +12 -14
- package/src/configuration.js +27 -4
- package/src/schema-parser/schema-formatters.js +26 -19
- package/src/schema-parser/schema-parser.js +131 -204
- package/src/schema-parser/schema-routes.js +113 -53
- package/src/schema-parser/schema-utils.js +165 -0
- package/src/templates.js +2 -2
- package/src/type-name.js +49 -30
- package/src/util/logger.js +19 -1
- package/src/util/name-resolver.js +50 -31
- package/templates/base/enum-data-contract.ejs +1 -4
- package/templates/base/http-clients/axios-http-client.ejs +5 -0
- package/templates/base/http-clients/fetch-http-client.ejs +2 -0
- package/templates/default/procedure-call.ejs +1 -0
- package/templates/modular/procedure-call.ejs +1 -0
|
@@ -3,19 +3,24 @@ const { getRandomInt } = require("./random.js");
|
|
|
3
3
|
|
|
4
4
|
class NameResolver {
|
|
5
5
|
reservedNames = [];
|
|
6
|
-
|
|
6
|
+
getFallbackName = null;
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
|
-
*
|
|
9
|
+
* @type {Logger}
|
|
10
|
+
*/
|
|
11
|
+
logger;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @param {Logger} logger;
|
|
10
15
|
* @param {string[]} reservedNames
|
|
11
16
|
*/
|
|
12
|
-
constructor(reservedNames,
|
|
13
|
-
this.
|
|
17
|
+
constructor(logger, reservedNames, getFallbackName) {
|
|
18
|
+
this.logger = logger;
|
|
19
|
+
this.getFallbackName = getFallbackName;
|
|
14
20
|
this.reserve(reservedNames);
|
|
15
21
|
}
|
|
16
22
|
|
|
17
23
|
/**
|
|
18
|
-
*
|
|
19
24
|
* @param {string[]} names
|
|
20
25
|
*/
|
|
21
26
|
reserve(names) {
|
|
@@ -32,44 +37,59 @@ class NameResolver {
|
|
|
32
37
|
|
|
33
38
|
/**
|
|
34
39
|
*
|
|
35
|
-
* @param {string[]}
|
|
36
|
-
* @param {((variant: string) => string) | undefined} onSelectMutation
|
|
40
|
+
* @param {(string[]) | ((reserved: string[]) => string)} variantsOrResolver
|
|
37
41
|
* @returns {string | null}
|
|
38
42
|
*/
|
|
39
|
-
resolve(
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
const
|
|
45
|
-
|
|
46
|
-
|
|
43
|
+
resolve(variantsOrResolver) {
|
|
44
|
+
this.logger.debug("resolving name with using", variantsOrResolver);
|
|
45
|
+
if (Array.isArray(variantsOrResolver)) {
|
|
46
|
+
const variants = variantsOrResolver;
|
|
47
|
+
let usageName = null;
|
|
48
|
+
const uniqVariants = _.uniq(_.compact(variants));
|
|
49
|
+
|
|
50
|
+
_.forEach(uniqVariants, (variant) => {
|
|
51
|
+
if (!usageName && !this.isReserved(variant)) {
|
|
52
|
+
usageName = variant;
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
if (usageName) {
|
|
57
|
+
this.reserve([usageName]);
|
|
58
|
+
return usageName;
|
|
47
59
|
}
|
|
48
|
-
});
|
|
49
60
|
|
|
50
|
-
|
|
51
|
-
this.
|
|
52
|
-
|
|
53
|
-
|
|
61
|
+
this.logger.debug("trying to resolve name with using fallback name generator");
|
|
62
|
+
return this.resolve(this.getFallbackName);
|
|
63
|
+
} else if (typeof variantsOrResolver === "function") {
|
|
64
|
+
let usageName = null;
|
|
65
|
+
while (usageName === null) {
|
|
66
|
+
const variant = variantsOrResolver(this.reservedNames);
|
|
54
67
|
|
|
55
|
-
|
|
68
|
+
if (variant === undefined) {
|
|
69
|
+
this.logger.warn("unable to resolve name. current reserved names: ", this.reservedNames);
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
if (!this.isReserved(variant)) {
|
|
73
|
+
usageName = variant;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
56
76
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
return defaultName;
|
|
77
|
+
this.reserve([usageName]);
|
|
78
|
+
return usageName;
|
|
60
79
|
}
|
|
61
80
|
|
|
81
|
+
this.logger.debug("problem with reserving names. current reserved names: ", this.reservedNames);
|
|
62
82
|
return null;
|
|
63
83
|
}
|
|
64
84
|
}
|
|
65
85
|
|
|
66
86
|
class SpecificArgNameResolver extends NameResolver {
|
|
67
87
|
/**
|
|
68
|
-
*
|
|
88
|
+
* @param {Logger} logger;
|
|
69
89
|
* @param {string[]} reservedNames
|
|
70
90
|
*/
|
|
71
|
-
constructor(reservedNames) {
|
|
72
|
-
super(reservedNames, (variants) => {
|
|
91
|
+
constructor(logger, reservedNames) {
|
|
92
|
+
super(logger, reservedNames, (variants) => {
|
|
73
93
|
return (variants[0] && `${variants[0]}${getRandomInt(1, 10)}`) || `arg${getRandomInt(1, 10)}`;
|
|
74
94
|
});
|
|
75
95
|
}
|
|
@@ -77,18 +97,17 @@ class SpecificArgNameResolver extends NameResolver {
|
|
|
77
97
|
|
|
78
98
|
class ComponentTypeNameResolver extends NameResolver {
|
|
79
99
|
/**
|
|
80
|
-
*
|
|
100
|
+
* @param {Logger} logger;
|
|
81
101
|
* @param {string[]} reservedNames
|
|
82
102
|
*/
|
|
83
|
-
constructor(reservedNames) {
|
|
84
|
-
super(reservedNames, (variants) => {
|
|
103
|
+
constructor(logger, reservedNames) {
|
|
104
|
+
super(logger, reservedNames, (variants) => {
|
|
85
105
|
return (variants[0] && `${variants[0]}${getRandomInt(1, 10)}`) || `ComponentType${getRandomInt(1, 10)}`;
|
|
86
106
|
});
|
|
87
107
|
}
|
|
88
108
|
}
|
|
89
109
|
|
|
90
110
|
module.exports = {
|
|
91
|
-
NameResolver,
|
|
92
111
|
SpecificArgNameResolver,
|
|
93
112
|
ComponentTypeNameResolver,
|
|
94
113
|
};
|
|
@@ -2,11 +2,8 @@
|
|
|
2
2
|
const { contract, utils, config } = it;
|
|
3
3
|
const { formatDescription, require, _ } = utils;
|
|
4
4
|
const { name, $content } = contract;
|
|
5
|
-
|
|
6
|
-
const isNumberEnum = _.some($content, (content) => typeof content.key === "number");
|
|
7
|
-
const formatAsUnionType = !!(isNumberEnum || config.generateUnionEnums);
|
|
8
5
|
%>
|
|
9
|
-
<% if (
|
|
6
|
+
<% if (config.generateUnionEnums) { %>
|
|
10
7
|
export type <%~ name %> = <%~ _.map($content, ({ value }) => value).join(" | ") %>
|
|
11
8
|
<% } else { %>
|
|
12
9
|
export enum <%~ name %> {
|
|
@@ -33,6 +33,7 @@ export enum ContentType {
|
|
|
33
33
|
Json = "application/json",
|
|
34
34
|
FormData = "multipart/form-data",
|
|
35
35
|
UrlEncoded = "application/x-www-form-urlencoded",
|
|
36
|
+
Text = "text/plain",
|
|
36
37
|
}
|
|
37
38
|
|
|
38
39
|
export class HttpClient<SecurityDataType = unknown> {
|
|
@@ -114,6 +115,10 @@ export class HttpClient<SecurityDataType = unknown> {
|
|
|
114
115
|
body = this.createFormData(body as Record<string, unknown>);
|
|
115
116
|
}
|
|
116
117
|
|
|
118
|
+
if (type === ContentType.Text && body && body !== null && typeof body !== "string") {
|
|
119
|
+
body = JSON.stringify(body);
|
|
120
|
+
}
|
|
121
|
+
|
|
117
122
|
return this.instance.request({
|
|
118
123
|
...requestParams,
|
|
119
124
|
headers: {
|
|
@@ -45,6 +45,7 @@ export enum ContentType {
|
|
|
45
45
|
Json = "application/json",
|
|
46
46
|
FormData = "multipart/form-data",
|
|
47
47
|
UrlEncoded = "application/x-www-form-urlencoded",
|
|
48
|
+
Text = "text/plain",
|
|
48
49
|
}
|
|
49
50
|
|
|
50
51
|
export class HttpClient<SecurityDataType = unknown> {
|
|
@@ -102,6 +103,7 @@ export class HttpClient<SecurityDataType = unknown> {
|
|
|
102
103
|
|
|
103
104
|
private contentFormatters: Record<ContentType, (input: any) => any> = {
|
|
104
105
|
[ContentType.Json]: (input:any) => input !== null && (typeof input === "object" || typeof input === "string") ? JSON.stringify(input) : input,
|
|
106
|
+
[ContentType.Text]: (input:any) => input !== null && typeof input !== "string" ? JSON.stringify(input) : input,
|
|
105
107
|
[ContentType.FormData]: (input: any) =>
|
|
106
108
|
Object.keys(input || {}).reduce((formData, key) => {
|
|
107
109
|
const property = input[key];
|