Skip to content

Commit e56d604

Browse files
authored
Release 1.7.0 (#42)
* BREAKING_CHANGE: removed title and version public Api properties (moved it to Api class JSDOC) * BREAKING_CHANGE: move out all http client handlers/properties into HttpClient local class in module; chore: default value for SecurityDataType Co-authored-by: svolkov <[email protected]>
1 parent 6695576 commit e56d604

Some content is hidden

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

45 files changed

+544
-380
lines changed

CHANGELOG.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
# next release
22

3+
# 1.7.0
4+
5+
Breaking Changes:
6+
7+
- Remove `title` and `version` public Api class properties (moved it to Api class JSDOC)([fixes this issue](https://github.com/acacode/swagger-typescript-api/issues/41))
8+
![removed title and version properties](./assets/changelog_assets/removed-title-version-props.jpg)
9+
- Move out all http client handlers/properties into `HttpClient` local class in module
10+
![http-client-class1](./assets/changelog_assets/http-client-class1.jpg)
11+
![http-client-class2](./assets/changelog_assets/http-client-class2.jpg)
12+
13+
Chore:
14+
15+
- default value for `SecurityDataType` Api class generic type
16+
17+
318
# 1.6.3
419

520
Fixes:
69.3 KB
Loading
Loading
Loading

package-lock.json

Lines changed: 1 addition & 1 deletion
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": "1.6.3",
3+
"version": "1.7.0",
44
"description": "Create typescript api module from swagger schema",
55
"scripts": {
66
"cli": "node index.js -d -p ./swagger-test-cli.json -n swagger-test-cli.ts",

src/apiConfig.js

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,50 @@
1+
const _ = require("lodash");
12
const { formatDescription } = require("./common");
23

3-
const createApiConfig = ({ info, servers, }) => {
4-
const server = (servers && servers[0]) || { url: '' }
4+
const createApiConfig = ({ info, servers }) => {
5+
const server = (servers && servers[0]) || { url: "" };
56

6-
const generic = [
7-
'SecurityDataType'
8-
].filter(Boolean).join(', ')
7+
const generic = _.compact([
8+
{
9+
name: "SecurityDataType",
10+
defaultValue: "any",
11+
},
12+
]);
13+
14+
const description = _.compact([
15+
`@title ${info.title || "Api"}`,
16+
info.version && `@version ${info.version}`,
17+
server.url && `@baseUrl ${server.url}`,
18+
_.replace(formatDescription(info.description), /\n/g, "\n * "),
19+
]);
920

1021
return {
1122
props: [
1223
{
13-
name: 'baseUrl',
24+
name: "baseUrl",
1425
optional: true,
15-
type: 'string'
26+
type: "string",
1627
},
1728
{
18-
name: 'baseApiParams',
29+
name: "baseApiParams",
1930
optional: true,
20-
type: 'RequestParams'
31+
type: "RequestParams",
2132
},
2233
{
23-
name: 'securityWorker',
34+
name: "securityWorker",
2435
optional: true,
25-
type: '(securityData: SecurityDataType) => RequestParams'
26-
}
36+
type: "(securityData: SecurityDataType) => RequestParams",
37+
},
2738
].filter(Boolean),
28-
generic: generic && `<${generic}>`,
39+
generic,
2940
baseUrl: server.url,
3041
title: info.title,
3142
version: info.version,
32-
description: formatDescription(info.description),
33-
}
34-
}
43+
description,
44+
hasDescription: !!description.length,
45+
};
46+
};
3547

3648
module.exports = {
3749
createApiConfig,
38-
}
50+
};

src/templates/client.mustache

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export type RequestParams = Omit<RequestInit, "body" | "method"> & {
77
export type RequestQueryParamsType = Record<string, string | string[] | number | number[] | boolean | undefined>;
88
{{/hasQueryRoutes}}
99

10-
type ApiConfig{{apiConfig.generic}} = {
10+
type ApiConfig<{{#apiConfig.generic}}{{name}},{{/apiConfig.generic}}> = {
1111
{{#apiConfig.props}}
1212
{{name}}{{#optional}}?{{/optional}}: {{type}},
1313
{{/apiConfig.props}}
@@ -21,17 +21,11 @@ type TPromise<ResolveType, RejectType = any> = Omit<Promise<ResolveType>, "then"
2121
}
2222
{{/generateResponses}}
2323

24-
{{#apiConfig.description}}
25-
/** {{.}} */
26-
{{/apiConfig.description}}
27-
export class Api{{apiConfig.generic}} {
28-
29-
public baseUrl = "{{apiConfig.baseUrl}}";
30-
public title = "{{apiConfig.title}}";
31-
public version = "{{apiConfig.version}}";
3224

25+
class HttpClient<{{#apiConfig.generic}}{{name}},{{/apiConfig.generic}}> {
26+
public baseUrl: string = "{{apiConfig.baseUrl}}";
3327
private securityData: SecurityDataType = (null as any);
34-
private securityWorker: ApiConfig{{apiConfig.generic}}["securityWorker"] = (() => {}) as any
28+
private securityWorker: ApiConfig<{{#apiConfig.generic}}{{name}},{{/apiConfig.generic}}>["securityWorker"] = (() => {}) as any
3529

3630
private baseApiParams: RequestParams = {
3731
credentials: 'same-origin',
@@ -42,7 +36,7 @@ export class Api{{apiConfig.generic}} {
4236
referrerPolicy: 'no-referrer',
4337
}
4438

45-
constructor({ {{#apiConfig.props}}{{name}},{{/apiConfig.props}} }: ApiConfig{{apiConfig.generic}} = {}) {
39+
constructor({ {{#apiConfig.props}}{{name}},{{/apiConfig.props}} }: ApiConfig<{{#apiConfig.generic}}{{name}},{{/apiConfig.generic}}> = {}) {
4640
{{#apiConfig.props}}
4741
this.{{name}} = {{name}} || this.{{name}};
4842
{{/apiConfig.props}}
@@ -57,7 +51,7 @@ export class Api{{apiConfig.generic}} {
5751
return encodeURIComponent(key) + "=" + encodeURIComponent(Array.isArray(query[key]) ? (query[key] as any).join(",") : query[key])
5852
}
5953

60-
private addQueryParams(query?: RequestQueryParamsType): string {
54+
protected addQueryParams(query?: RequestQueryParamsType): string {
6155
const fixedQuery = query || {};
6256
const keys = Object.keys(fixedQuery).filter(key => "undefined" !== typeof fixedQuery[key]);
6357
return keys.length === 0 ? "" : `?${keys.map(key => this.addQueryParam(fixedQuery, key)).join("&")}`;
@@ -99,7 +93,16 @@ export class Api{{apiConfig.generic}} {
9993
if (!response.ok) throw data
10094
return data
10195
})
96+
}
10297

98+
{{#apiConfig.hasDescription}}
99+
/**
100+
{{#apiConfig.description}}
101+
* {{.}}
102+
{{/apiConfig.description}}
103+
*/
104+
{{/apiConfig.hasDescription}}
105+
export class Api<{{#apiConfig.generic}}{{name}}{{#defaultValue}} = {{.}}{{/defaultValue}},{{/apiConfig.generic}}> extends HttpClient<{{#apiConfig.generic}}{{name}},{{/apiConfig.generic}}>{
103106
{{#routes}}
104107

105108
{{#outOfModule}}

0 commit comments

Comments
 (0)