Skip to content

Commit

Permalink
Release 3.0.0 (#91)
Browse files Browse the repository at this point in the history
* 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 <benjamin.dobell+github@glassechidna.com.au>
  • Loading branch information
js2me and Benjamin-Dobell authored Nov 10, 2020
1 parent 9329559 commit 1f6e21b
Show file tree
Hide file tree
Showing 62 changed files with 1,821 additions and 166 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# next release


# 3.0.0

BREAKING_CHANGES:
- Renamed mustache templates:
- `api.mustache` -> `data-contracts.mustache`
- `client.mustache` -> `http.client.mustache` + `api.mustache`
- Split the `client.mustache` template into two parts: `http-client.mustache` and `api.mustache`

Fixes:
- Fixed unsafe clone() of Response causing json() hang. (Thanks @Benjamin-Dobell)

# 2.0.0

Features:
Expand Down
1,619 changes: 1,617 additions & 2 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "swagger-typescript-api",
"version": "2.0.0",
"version": "3.0.0",
"description": "Create typescript api module from swagger schema",
"scripts": {
"cli:json": "node index.js -r -d -p ./swagger-test-cli.json -n swagger-test-cli.ts",
Expand Down
2 changes: 1 addition & 1 deletion src/config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const config = {
/** CLI flag */
templates: "./templates",
templates: "./templates/defaults",
/** CLI flag */
generateResponses: false,
/** CLI flag */
Expand Down
14 changes: 14 additions & 0 deletions src/filePrefix.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = {
filePrefix: `/* tslint:disable */
/* eslint-disable */
/*
* ---------------------------------------------------------------
* ## THIS FILE WAS GENERATED VIA SWAGGER-TYPESCRIPT-API ##
* ## ##
* ## AUTHOR: acacode ##
* ## SOURCE: https://github.com/acacode/swagger-typescript-api ##
* ---------------------------------------------------------------
*/
`,
};
3 changes: 2 additions & 1 deletion src/files.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
const _ = require("lodash");
const fs = require("fs");
const { resolve } = require("path");
const { filePrefix } = require("./filePrefix");

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

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

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

module.exports = {
createFile,
Expand Down
12 changes: 9 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,12 @@ module.exports = {
});
(spec ? convertSwaggerObject(spec) : getSwaggerObject(input, url))
.then(({ usageSchema, originalSchema }) => {
const { apiTemplate, clientTemplate, routeTypesTemplate } = getTemplates();
const {
dataContractsTemplate,
httpClientTemplate,
apiTemplate,
routeTypesTemplate,
} = getTemplates();

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

Expand Down Expand Up @@ -90,9 +95,10 @@ module.exports = {
};

let sourceFileContent = [
mustache.render(apiTemplate, configuration),
mustache.render(dataContractsTemplate, configuration),
generateRouteTypes ? mustache.render(routeTypesTemplate, configuration) : "",
generateClient ? mustache.render(clientTemplate, configuration) : "",
generateClient ? mustache.render(httpClientTemplate, configuration) : "",
generateClient ? mustache.render(apiTemplate, configuration) : "",
].join("");

if (toJS) {
Expand Down
11 changes: 6 additions & 5 deletions src/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@ const { getFileContent } = require("./files");
const { config } = require("./config");
const { resolve } = require("path");

const getTemplate = (templateName) =>
getFileContent(resolve(config.templates, `./${templateName}.mustache`));

const getTemplates = () => {
console.log(`✨ try to read templates from directory "${config.templates}"`);

return {
apiTemplate: getTemplate("api"),
clientTemplate: config.generateClient ? getTemplate("client") : null,
dataContractsTemplate: getTemplate("data-contracts"),
routeTypesTemplate: config.generateRouteTypes ? getTemplate("route-types") : null,
httpClientTemplate: config.generateClient ? getTemplate("http-client") : null,
apiTemplate: config.generateClient ? getTemplate("api") : null,
};
};

const getTemplate = (templateName) =>
getFileContent(resolve(config.templates, `./${templateName}.mustache`));

module.exports = {
getTemplates,
};
20 changes: 0 additions & 20 deletions src/templates/api.mustache

This file was deleted.

41 changes: 41 additions & 0 deletions src/templates/defaults/api.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

{{#apiConfig.hasDescription}}
/**
{{#apiConfig.description}}
* {{.}}
{{/apiConfig.description}}
*/
{{/apiConfig.hasDescription}}
export class Api<{{#apiConfig.generic}}{{name}}{{#defaultValue}} = {{.}}{{/defaultValue}},{{/apiConfig.generic}}> extends HttpClient<{{#apiConfig.generic}}{{name}},{{/apiConfig.generic}}>{
{{#routes}}

{{#outOfModule}}


/**
{{#comments}}
* {{.}}
{{/comments}}
*/
{{name}} = ({{#routeArgs}}{{name}}{{#optional}}?{{/optional}}: {{type}}, {{/routeArgs}}) =>
this.request<{{returnType}}, {{errorReturnType}}>({{requestMethodContent}})
{{/outOfModule}}

{{#combined}}
{{moduleName}} = {
{{#routes}}


/**
{{#comments}}
* {{.}}
{{/comments}}
*/
{{name}}: ({{#routeArgs}}{{name}}{{#optional}}?{{/optional}}: {{type}}, {{/routeArgs}}) =>
this.request<{{returnType}}, {{errorReturnType}}>({{requestMethodContent}}),
{{/routes}}
}
{{/combined}}
{{/routes}}

}
9 changes: 9 additions & 0 deletions src/templates/defaults/data-contracts.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{{#modelTypes}}

{{#description}}
/**
* {{.}}
*/
{{/description}}
export {{typeIdentifier}} {{name}} {{content}}
{{/modelTypes}}
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class HttpClient<{{#apiConfig.generic}}{{name}},{{/apiConfig.generic}}> {
}

private safeParseResponse = <T = any, E = any>(response: Response): Promise<HttpResponse<T, E>> => {
const r = response.clone() as HttpResponse<T, E>;
const r = response as HttpResponse<T, E>;
r.data = null;
r.error = null;
Expand Down Expand Up @@ -139,44 +139,3 @@ class HttpClient<{{#apiConfig.generic}}{{name}},{{/apiConfig.generic}}> {
})
}
}

{{#apiConfig.hasDescription}}
/**
{{#apiConfig.description}}
* {{.}}
{{/apiConfig.description}}
*/
{{/apiConfig.hasDescription}}
export class Api<{{#apiConfig.generic}}{{name}}{{#defaultValue}} = {{.}}{{/defaultValue}},{{/apiConfig.generic}}> extends HttpClient<{{#apiConfig.generic}}{{name}},{{/apiConfig.generic}}>{
{{#routes}}

{{#outOfModule}}


/**
{{#comments}}
* {{.}}
{{/comments}}
*/
{{name}} = ({{#routeArgs}}{{name}}{{#optional}}?{{/optional}}: {{type}}, {{/routeArgs}}) =>
this.request<{{returnType}}, {{errorReturnType}}>({{requestMethodContent}})
{{/outOfModule}}

{{#combined}}
{{moduleName}} = {
{{#routes}}


/**
{{#comments}}
* {{.}}
{{/comments}}
*/
{{name}}: ({{#routeArgs}}{{name}}{{#optional}}?{{/optional}}: {{type}}, {{/routeArgs}}) =>
this.request<{{returnType}}, {{errorReturnType}}>({{requestMethodContent}}),
{{/routes}}
}
{{/combined}}
{{/routes}}

}
File renamed without changes.
2 changes: 1 addition & 1 deletion tests/generated/v2.0/adafruit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ class HttpClient<SecurityDataType> {
}

private safeParseResponse = <T = any, E = any>(response: Response): Promise<HttpResponse<T, E>> => {
const r = response.clone() as HttpResponse<T, E>;
const r = response as HttpResponse<T, E>;
r.data = null;
r.error = null;

Expand Down
2 changes: 1 addition & 1 deletion tests/generated/v2.0/another-example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ class HttpClient<SecurityDataType> {
}

private safeParseResponse = <T = any, E = any>(response: Response): Promise<HttpResponse<T, E>> => {
const r = response.clone() as HttpResponse<T, E>;
const r = response as HttpResponse<T, E>;
r.data = null;
r.error = null;

Expand Down
2 changes: 1 addition & 1 deletion tests/generated/v2.0/api-with-examples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class HttpClient<SecurityDataType> {
}

private safeParseResponse = <T = any, E = any>(response: Response): Promise<HttpResponse<T, E>> => {
const r = response.clone() as HttpResponse<T, E>;
const r = response as HttpResponse<T, E>;
r.data = null;
r.error = null;

Expand Down
2 changes: 1 addition & 1 deletion tests/generated/v2.0/authentiq.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class HttpClient<SecurityDataType> {
}

private safeParseResponse = <T = any, E = any>(response: Response): Promise<HttpResponse<T, E>> => {
const r = response.clone() as HttpResponse<T, E>;
const r = response as HttpResponse<T, E>;
r.data = null;
r.error = null;

Expand Down
2 changes: 1 addition & 1 deletion tests/generated/v2.0/example1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class HttpClient<SecurityDataType> {
}

private safeParseResponse = <T = any, E = any>(response: Response): Promise<HttpResponse<T, E>> => {
const r = response.clone() as HttpResponse<T, E>;
const r = response as HttpResponse<T, E>;
r.data = null;
r.error = null;

Expand Down
2 changes: 1 addition & 1 deletion tests/generated/v2.0/file-formdata-example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class HttpClient<SecurityDataType> {
}

private safeParseResponse = <T = any, E = any>(response: Response): Promise<HttpResponse<T, E>> => {
const r = response.clone() as HttpResponse<T, E>;
const r = response as HttpResponse<T, E>;
r.data = null;
r.error = null;

Expand Down
2 changes: 1 addition & 1 deletion tests/generated/v2.0/furkot-example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class HttpClient<SecurityDataType> {
}

private safeParseResponse = <T = any, E = any>(response: Response): Promise<HttpResponse<T, E>> => {
const r = response.clone() as HttpResponse<T, E>;
const r = response as HttpResponse<T, E>;
r.data = null;
r.error = null;

Expand Down
2 changes: 1 addition & 1 deletion tests/generated/v2.0/giphy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ class HttpClient<SecurityDataType> {
}

private safeParseResponse = <T = any, E = any>(response: Response): Promise<HttpResponse<T, E>> => {
const r = response.clone() as HttpResponse<T, E>;
const r = response as HttpResponse<T, E>;
r.data = null;
r.error = null;

Expand Down
2 changes: 1 addition & 1 deletion tests/generated/v2.0/github-swagger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1486,7 +1486,7 @@ class HttpClient<SecurityDataType> {
}

private safeParseResponse = <T = any, E = any>(response: Response): Promise<HttpResponse<T, E>> => {
const r = response.clone() as HttpResponse<T, E>;
const r = response as HttpResponse<T, E>;
r.data = null;
r.error = null;

Expand Down
2 changes: 1 addition & 1 deletion tests/generated/v2.0/path-args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class HttpClient<SecurityDataType> {
}

private safeParseResponse = <T = any, E = any>(response: Response): Promise<HttpResponse<T, E>> => {
const r = response.clone() as HttpResponse<T, E>;
const r = response as HttpResponse<T, E>;
r.data = null;
r.error = null;

Expand Down
2 changes: 1 addition & 1 deletion tests/generated/v2.0/petstore-expanded.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class HttpClient<SecurityDataType> {
}

private safeParseResponse = <T = any, E = any>(response: Response): Promise<HttpResponse<T, E>> => {
const r = response.clone() as HttpResponse<T, E>;
const r = response as HttpResponse<T, E>;
r.data = null;
r.error = null;

Expand Down
2 changes: 1 addition & 1 deletion tests/generated/v2.0/petstore-minimal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class HttpClient<SecurityDataType> {
}

private safeParseResponse = <T = any, E = any>(response: Response): Promise<HttpResponse<T, E>> => {
const r = response.clone() as HttpResponse<T, E>;
const r = response as HttpResponse<T, E>;
r.data = null;
r.error = null;

Expand Down
2 changes: 1 addition & 1 deletion tests/generated/v2.0/petstore-simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class HttpClient<SecurityDataType> {
}

private safeParseResponse = <T = any, E = any>(response: Response): Promise<HttpResponse<T, E>> => {
const r = response.clone() as HttpResponse<T, E>;
const r = response as HttpResponse<T, E>;
r.data = null;
r.error = null;

Expand Down
2 changes: 1 addition & 1 deletion tests/generated/v2.0/petstore-swagger-io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class HttpClient<SecurityDataType> {
}

private safeParseResponse = <T = any, E = any>(response: Response): Promise<HttpResponse<T, E>> => {
const r = response.clone() as HttpResponse<T, E>;
const r = response as HttpResponse<T, E>;
r.data = null;
r.error = null;

Expand Down
2 changes: 1 addition & 1 deletion tests/generated/v2.0/petstore-with-external-docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class HttpClient<SecurityDataType> {
}

private safeParseResponse = <T = any, E = any>(response: Response): Promise<HttpResponse<T, E>> => {
const r = response.clone() as HttpResponse<T, E>;
const r = response as HttpResponse<T, E>;
r.data = null;
r.error = null;

Expand Down
2 changes: 1 addition & 1 deletion tests/generated/v2.0/petstore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class HttpClient<SecurityDataType> {
}

private safeParseResponse = <T = any, E = any>(response: Response): Promise<HttpResponse<T, E>> => {
const r = response.clone() as HttpResponse<T, E>;
const r = response as HttpResponse<T, E>;
r.data = null;
r.error = null;

Expand Down
2 changes: 1 addition & 1 deletion tests/generated/v2.0/query-path-param.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class HttpClient<SecurityDataType> {
}

private safeParseResponse = <T = any, E = any>(response: Response): Promise<HttpResponse<T, E>> => {
const r = response.clone() as HttpResponse<T, E>;
const r = response as HttpResponse<T, E>;
r.data = null;
r.error = null;

Expand Down
Loading

0 comments on commit 1f6e21b

Please sign in to comment.