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+
}

0 commit comments

Comments
 (0)