Skip to content

Commit 1f6e21b

Browse files
Release 3.0.0 (#91)
* fix: handling x-omitempty property for definition properties (#68) * docs: update CHANGELOG * BREAKING_CHANGE: renamed mustache templates, split client mustache template * Fixed unsafe clone() of Response causing json() hang. (#89) Fixed unsafe clone() of Response causing json() hang * bump: up project version to 3.0.0; chore: refresh generated api modules; docs; update CHANGELOG Co-authored-by: Benjamin Dobell <[email protected]>
1 parent 9329559 commit 1f6e21b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+1821
-166
lines changed

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
# next release
2+
3+
4+
# 3.0.0
5+
6+
BREAKING_CHANGES:
7+
- Renamed mustache templates:
8+
- `api.mustache` -> `data-contracts.mustache`
9+
- `client.mustache` -> `http.client.mustache` + `api.mustache`
10+
- Split the `client.mustache` template into two parts: `http-client.mustache` and `api.mustache`
11+
12+
Fixes:
13+
- Fixed unsafe clone() of Response causing json() hang. (Thanks @Benjamin-Dobell)
14+
115
# 2.0.0
216

317
Features:

package-lock.json

Lines changed: 1617 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "swagger-typescript-api",
3-
"version": "2.0.0",
3+
"version": "3.0.0",
44
"description": "Create typescript api module from swagger schema",
55
"scripts": {
66
"cli:json": "node index.js -r -d -p ./swagger-test-cli.json -n swagger-test-cli.ts",

src/config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const config = {
22
/** CLI flag */
3-
templates: "./templates",
3+
templates: "./templates/defaults",
44
/** CLI flag */
55
generateResponses: false,
66
/** CLI flag */

src/filePrefix.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module.exports = {
2+
filePrefix: `/* tslint:disable */
3+
/* eslint-disable */
4+
/*
5+
* ---------------------------------------------------------------
6+
* ## THIS FILE WAS GENERATED VIA SWAGGER-TYPESCRIPT-API ##
7+
* ## ##
8+
* ## AUTHOR: acacode ##
9+
* ## SOURCE: https://github.com/acacode/swagger-typescript-api ##
10+
* ---------------------------------------------------------------
11+
*/
12+
13+
`,
14+
};

src/files.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
const _ = require("lodash");
22
const fs = require("fs");
33
const { resolve } = require("path");
4+
const { filePrefix } = require("./filePrefix");
45

56
const getFileContent = (path) => fs.readFileSync(path, { encoding: "UTF-8" });
67

78
const pathIsExist = (path) => path && fs.existsSync(path);
89

910
const createFile = (pathTo, fileName, content) =>
10-
fs.writeFileSync(resolve(__dirname, pathTo, `./${fileName}`), content, _.noop);
11+
fs.writeFileSync(resolve(__dirname, pathTo, `./${fileName}`), `${filePrefix}${content}`, _.noop);
1112

1213
module.exports = {
1314
createFile,

src/index.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,12 @@ module.exports = {
5656
});
5757
(spec ? convertSwaggerObject(spec) : getSwaggerObject(input, url))
5858
.then(({ usageSchema, originalSchema }) => {
59-
const { apiTemplate, clientTemplate, routeTypesTemplate } = getTemplates();
59+
const {
60+
dataContractsTemplate,
61+
httpClientTemplate,
62+
apiTemplate,
63+
routeTypesTemplate,
64+
} = getTemplates();
6065

6166
console.log("☄️ start generating your typescript api");
6267

@@ -90,9 +95,10 @@ module.exports = {
9095
};
9196

9297
let sourceFileContent = [
93-
mustache.render(apiTemplate, configuration),
98+
mustache.render(dataContractsTemplate, configuration),
9499
generateRouteTypes ? mustache.render(routeTypesTemplate, configuration) : "",
95-
generateClient ? mustache.render(clientTemplate, configuration) : "",
100+
generateClient ? mustache.render(httpClientTemplate, configuration) : "",
101+
generateClient ? mustache.render(apiTemplate, configuration) : "",
96102
].join("");
97103

98104
if (toJS) {

src/templates.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,20 @@ const { getFileContent } = require("./files");
22
const { config } = require("./config");
33
const { resolve } = require("path");
44

5+
const getTemplate = (templateName) =>
6+
getFileContent(resolve(config.templates, `./${templateName}.mustache`));
7+
58
const getTemplates = () => {
69
console.log(`✨ try to read templates from directory "${config.templates}"`);
710

811
return {
9-
apiTemplate: getTemplate("api"),
10-
clientTemplate: config.generateClient ? getTemplate("client") : null,
12+
dataContractsTemplate: getTemplate("data-contracts"),
1113
routeTypesTemplate: config.generateRouteTypes ? getTemplate("route-types") : null,
14+
httpClientTemplate: config.generateClient ? getTemplate("http-client") : null,
15+
apiTemplate: config.generateClient ? getTemplate("api") : null,
1216
};
1317
};
1418

15-
const getTemplate = (templateName) =>
16-
getFileContent(resolve(config.templates, `./${templateName}.mustache`));
17-
1819
module.exports = {
1920
getTemplates,
2021
};

src/templates/api.mustache

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/templates/defaults/api.mustache

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
{{#apiConfig.hasDescription}}
3+
/**
4+
{{#apiConfig.description}}
5+
* {{.}}
6+
{{/apiConfig.description}}
7+
*/
8+
{{/apiConfig.hasDescription}}
9+
export class Api<{{#apiConfig.generic}}{{name}}{{#defaultValue}} = {{.}}{{/defaultValue}},{{/apiConfig.generic}}> extends HttpClient<{{#apiConfig.generic}}{{name}},{{/apiConfig.generic}}>{
10+
{{#routes}}
11+
12+
{{#outOfModule}}
13+
14+
15+
/**
16+
{{#comments}}
17+
* {{.}}
18+
{{/comments}}
19+
*/
20+
{{name}} = ({{#routeArgs}}{{name}}{{#optional}}?{{/optional}}: {{type}}, {{/routeArgs}}) =>
21+
this.request<{{returnType}}, {{errorReturnType}}>({{requestMethodContent}})
22+
{{/outOfModule}}
23+
24+
{{#combined}}
25+
{{moduleName}} = {
26+
{{#routes}}
27+
28+
29+
/**
30+
{{#comments}}
31+
* {{.}}
32+
{{/comments}}
33+
*/
34+
{{name}}: ({{#routeArgs}}{{name}}{{#optional}}?{{/optional}}: {{type}}, {{/routeArgs}}) =>
35+
this.request<{{returnType}}, {{errorReturnType}}>({{requestMethodContent}}),
36+
{{/routes}}
37+
}
38+
{{/combined}}
39+
{{/routes}}
40+
41+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{{#modelTypes}}
2+
3+
{{#description}}
4+
/**
5+
* {{.}}
6+
*/
7+
{{/description}}
8+
export {{typeIdentifier}} {{name}} {{content}}
9+
{{/modelTypes}}

src/templates/client.mustache renamed to src/templates/defaults/http-client.mustache

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ class HttpClient<{{#apiConfig.generic}}{{name}},{{/apiConfig.generic}}> {
9696
}
9797

9898
private safeParseResponse = <T = any, E = any>(response: Response): Promise<HttpResponse<T, E>> => {
99-
const r = response.clone() as HttpResponse<T, E>;
99+
const r = response as HttpResponse<T, E>;
100100
r.data = null;
101101
r.error = null;
102102
@@ -139,44 +139,3 @@ class HttpClient<{{#apiConfig.generic}}{{name}},{{/apiConfig.generic}}> {
139139
})
140140
}
141141
}
142-
143-
{{#apiConfig.hasDescription}}
144-
/**
145-
{{#apiConfig.description}}
146-
* {{.}}
147-
{{/apiConfig.description}}
148-
*/
149-
{{/apiConfig.hasDescription}}
150-
export class Api<{{#apiConfig.generic}}{{name}}{{#defaultValue}} = {{.}}{{/defaultValue}},{{/apiConfig.generic}}> extends HttpClient<{{#apiConfig.generic}}{{name}},{{/apiConfig.generic}}>{
151-
{{#routes}}
152-
153-
{{#outOfModule}}
154-
155-
156-
/**
157-
{{#comments}}
158-
* {{.}}
159-
{{/comments}}
160-
*/
161-
{{name}} = ({{#routeArgs}}{{name}}{{#optional}}?{{/optional}}: {{type}}, {{/routeArgs}}) =>
162-
this.request<{{returnType}}, {{errorReturnType}}>({{requestMethodContent}})
163-
{{/outOfModule}}
164-
165-
{{#combined}}
166-
{{moduleName}} = {
167-
{{#routes}}
168-
169-
170-
/**
171-
{{#comments}}
172-
* {{.}}
173-
{{/comments}}
174-
*/
175-
{{name}}: ({{#routeArgs}}{{name}}{{#optional}}?{{/optional}}: {{type}}, {{/routeArgs}}) =>
176-
this.request<{{returnType}}, {{errorReturnType}}>({{requestMethodContent}}),
177-
{{/routes}}
178-
}
179-
{{/combined}}
180-
{{/routes}}
181-
182-
}

tests/generated/v2.0/adafruit.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ class HttpClient<SecurityDataType> {
237237
}
238238

239239
private safeParseResponse = <T = any, E = any>(response: Response): Promise<HttpResponse<T, E>> => {
240-
const r = response.clone() as HttpResponse<T, E>;
240+
const r = response as HttpResponse<T, E>;
241241
r.data = null;
242242
r.error = null;
243243

tests/generated/v2.0/another-example.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ class HttpClient<SecurityDataType> {
190190
}
191191

192192
private safeParseResponse = <T = any, E = any>(response: Response): Promise<HttpResponse<T, E>> => {
193-
const r = response.clone() as HttpResponse<T, E>;
193+
const r = response as HttpResponse<T, E>;
194194
r.data = null;
195195
r.error = null;
196196

tests/generated/v2.0/api-with-examples.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class HttpClient<SecurityDataType> {
6868
}
6969

7070
private safeParseResponse = <T = any, E = any>(response: Response): Promise<HttpResponse<T, E>> => {
71-
const r = response.clone() as HttpResponse<T, E>;
71+
const r = response as HttpResponse<T, E>;
7272
r.data = null;
7373
r.error = null;
7474

tests/generated/v2.0/authentiq.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ class HttpClient<SecurityDataType> {
142142
}
143143

144144
private safeParseResponse = <T = any, E = any>(response: Response): Promise<HttpResponse<T, E>> => {
145-
const r = response.clone() as HttpResponse<T, E>;
145+
const r = response as HttpResponse<T, E>;
146146
r.data = null;
147147
r.error = null;
148148

tests/generated/v2.0/example1.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class HttpClient<SecurityDataType> {
109109
}
110110

111111
private safeParseResponse = <T = any, E = any>(response: Response): Promise<HttpResponse<T, E>> => {
112-
const r = response.clone() as HttpResponse<T, E>;
112+
const r = response as HttpResponse<T, E>;
113113
r.data = null;
114114
r.error = null;
115115

tests/generated/v2.0/file-formdata-example.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class HttpClient<SecurityDataType> {
7474
}
7575

7676
private safeParseResponse = <T = any, E = any>(response: Response): Promise<HttpResponse<T, E>> => {
77-
const r = response.clone() as HttpResponse<T, E>;
77+
const r = response as HttpResponse<T, E>;
7878
r.data = null;
7979
r.error = null;
8080

tests/generated/v2.0/furkot-example.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ class HttpClient<SecurityDataType> {
116116
}
117117

118118
private safeParseResponse = <T = any, E = any>(response: Response): Promise<HttpResponse<T, E>> => {
119-
const r = response.clone() as HttpResponse<T, E>;
119+
const r = response as HttpResponse<T, E>;
120120
r.data = null;
121121
r.error = null;
122122

tests/generated/v2.0/giphy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ class HttpClient<SecurityDataType> {
257257
}
258258

259259
private safeParseResponse = <T = any, E = any>(response: Response): Promise<HttpResponse<T, E>> => {
260-
const r = response.clone() as HttpResponse<T, E>;
260+
const r = response as HttpResponse<T, E>;
261261
r.data = null;
262262
r.error = null;
263263

tests/generated/v2.0/github-swagger.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1486,7 +1486,7 @@ class HttpClient<SecurityDataType> {
14861486
}
14871487

14881488
private safeParseResponse = <T = any, E = any>(response: Response): Promise<HttpResponse<T, E>> => {
1489-
const r = response.clone() as HttpResponse<T, E>;
1489+
const r = response as HttpResponse<T, E>;
14901490
r.data = null;
14911491
r.error = null;
14921492

tests/generated/v2.0/path-args.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ class HttpClient<SecurityDataType> {
9090
}
9191

9292
private safeParseResponse = <T = any, E = any>(response: Response): Promise<HttpResponse<T, E>> => {
93-
const r = response.clone() as HttpResponse<T, E>;
93+
const r = response as HttpResponse<T, E>;
9494
r.data = null;
9595
r.error = null;
9696

tests/generated/v2.0/petstore-expanded.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ class HttpClient<SecurityDataType> {
116116
}
117117

118118
private safeParseResponse = <T = any, E = any>(response: Response): Promise<HttpResponse<T, E>> => {
119-
const r = response.clone() as HttpResponse<T, E>;
119+
const r = response as HttpResponse<T, E>;
120120
r.data = null;
121121
r.error = null;
122122

tests/generated/v2.0/petstore-minimal.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class HttpClient<SecurityDataType> {
7575
}
7676

7777
private safeParseResponse = <T = any, E = any>(response: Response): Promise<HttpResponse<T, E>> => {
78-
const r = response.clone() as HttpResponse<T, E>;
78+
const r = response as HttpResponse<T, E>;
7979
r.data = null;
8080
r.error = null;
8181

tests/generated/v2.0/petstore-simple.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ class HttpClient<SecurityDataType> {
112112
}
113113

114114
private safeParseResponse = <T = any, E = any>(response: Response): Promise<HttpResponse<T, E>> => {
115-
const r = response.clone() as HttpResponse<T, E>;
115+
const r = response as HttpResponse<T, E>;
116116
r.data = null;
117117
r.error = null;
118118

tests/generated/v2.0/petstore-swagger-io.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ class HttpClient<SecurityDataType> {
147147
}
148148

149149
private safeParseResponse = <T = any, E = any>(response: Response): Promise<HttpResponse<T, E>> => {
150-
const r = response.clone() as HttpResponse<T, E>;
150+
const r = response as HttpResponse<T, E>;
151151
r.data = null;
152152
r.error = null;
153153

tests/generated/v2.0/petstore-with-external-docs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ class HttpClient<SecurityDataType> {
102102
}
103103

104104
private safeParseResponse = <T = any, E = any>(response: Response): Promise<HttpResponse<T, E>> => {
105-
const r = response.clone() as HttpResponse<T, E>;
105+
const r = response as HttpResponse<T, E>;
106106
r.data = null;
107107
r.error = null;
108108

tests/generated/v2.0/petstore.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ class HttpClient<SecurityDataType> {
103103
}
104104

105105
private safeParseResponse = <T = any, E = any>(response: Response): Promise<HttpResponse<T, E>> => {
106-
const r = response.clone() as HttpResponse<T, E>;
106+
const r = response as HttpResponse<T, E>;
107107
r.data = null;
108108
r.error = null;
109109

tests/generated/v2.0/query-path-param.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ class HttpClient<SecurityDataType> {
9090
}
9191

9292
private safeParseResponse = <T = any, E = any>(response: Response): Promise<HttpResponse<T, E>> => {
93-
const r = response.clone() as HttpResponse<T, E>;
93+
const r = response as HttpResponse<T, E>;
9494
r.data = null;
9595
r.error = null;
9696

0 commit comments

Comments
 (0)