swagger-typescript-api 10.0.1 → 10.0.2
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/LICENSE +21 -21
- package/README.md +284 -262
- package/index.d.ts +7 -1
- package/index.js +115 -113
- package/package.json +116 -114
- package/src/apiConfig.js +30 -30
- package/src/common.js +28 -28
- package/src/components.js +3 -5
- package/src/config.js +4 -0
- package/src/constants.js +7 -0
- package/src/filePrefix.js +14 -14
- package/src/files.js +6 -6
- package/src/formatFileContent.js +13 -6
- package/src/index.js +271 -270
- package/src/logger.js +59 -59
- package/src/modelNames.js +78 -78
- package/src/modelTypes.js +31 -30
- package/src/output.js +165 -166
- package/src/prettierOptions.js +23 -23
- package/src/render/utils/fmtToJSDocLine.js +10 -10
- package/src/render/utils/index.js +31 -23
- package/src/render/utils/templateRequire.js +17 -17
- package/src/routeNames.js +46 -46
- package/src/routes.js +4 -1
- package/src/schema.js +87 -64
- package/src/swagger.js +4 -1
- package/src/templates.js +155 -132
- package/src/translators/JavaScript.js +60 -60
- package/src/typeFormatters.js +121 -75
- package/src/utils/id.js +9 -9
- package/src/utils/random.js +14 -14
- package/src/utils/resolveName.js +97 -97
- package/templates/README.md +17 -13
- package/templates/base/README.md +7 -7
- package/templates/base/data-contract-jsdoc.ejs +32 -0
- package/templates/base/data-contracts.ejs +28 -0
- package/templates/base/enum-data-contract.ejs +15 -0
- package/templates/base/{http-client.eta → http-client.ejs} +2 -2
- package/templates/base/http-clients/{axios-http-client.eta → axios-http-client.ejs} +133 -145
- package/templates/base/http-clients/{fetch-http-client.eta → fetch-http-client.ejs} +222 -222
- package/templates/base/interface-data-contract.ejs +10 -0
- package/templates/base/object-field-jsdoc.ejs +28 -0
- package/templates/base/{route-docs.eta → route-docs.ejs} +31 -31
- package/templates/base/{route-name.eta → route-name.ejs} +42 -42
- package/templates/base/{route-type.eta → route-type.ejs} +21 -21
- package/templates/base/type-data-contract.ejs +15 -0
- package/templates/default/README.md +6 -6
- package/templates/default/{api.eta → api.ejs} +65 -65
- package/templates/default/{procedure-call.eta → procedure-call.ejs} +98 -98
- package/templates/default/{route-types.eta → route-types.ejs} +28 -28
- package/templates/modular/README.md +6 -6
- package/templates/modular/{api.eta → api.ejs} +28 -28
- package/templates/modular/{procedure-call.eta → procedure-call.ejs} +98 -98
- package/templates/modular/{route-types.eta → route-types.ejs} +18 -18
- package/CHANGELOG.md +0 -872
- package/templates/base/data-contracts.eta +0 -45
package/src/utils/resolveName.js
CHANGED
|
@@ -1,97 +1,97 @@
|
|
|
1
|
-
const _ = require("lodash");
|
|
2
|
-
const { getRandomInt } = require("./random");
|
|
3
|
-
|
|
4
|
-
class NameResolver {
|
|
5
|
-
reservedNames = [];
|
|
6
|
-
getDefaultName = null;
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
*
|
|
10
|
-
* @param {string[]} reservedNames
|
|
11
|
-
*/
|
|
12
|
-
constructor(reservedNames, getDefaultName) {
|
|
13
|
-
this.getDefaultName = getDefaultName;
|
|
14
|
-
this.reserve(reservedNames);
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
*
|
|
19
|
-
* @param {string[]} names
|
|
20
|
-
*/
|
|
21
|
-
reserve(names) {
|
|
22
|
-
this.reservedNames.push(..._.uniq(_.compact(names)));
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
unreserve(names) {
|
|
26
|
-
this.reservedNames.filter((reservedName) => !names.some((name) => name === reservedName));
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
isReserved(name) {
|
|
30
|
-
return _.some(this.reservedNames, (reservedName) => reservedName === name);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
*
|
|
35
|
-
* @param {string[]} variants
|
|
36
|
-
* @param {((variant: string) => string) | undefined} onSelectMutation
|
|
37
|
-
* @returns {string | null}
|
|
38
|
-
*/
|
|
39
|
-
resolve(variants, onSelectMutation) {
|
|
40
|
-
let usageName = null;
|
|
41
|
-
const uniqVariants = _.uniq(_.compact(variants));
|
|
42
|
-
|
|
43
|
-
_.forEach(uniqVariants, (variant) => {
|
|
44
|
-
const mutatedVariant = onSelectMutation ? onSelectMutation(variant) : variant;
|
|
45
|
-
if (!usageName && !this.isReserved(mutatedVariant)) {
|
|
46
|
-
usageName = mutatedVariant;
|
|
47
|
-
}
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
if (usageName) {
|
|
51
|
-
this.reserve([usageName]);
|
|
52
|
-
return usageName;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
const defaultName = this.getDefaultName && this.getDefaultName(variants);
|
|
56
|
-
|
|
57
|
-
if (defaultName) {
|
|
58
|
-
this.reserve([onSelectMutation ? onSelectMutation(defaultName) : defaultName]);
|
|
59
|
-
return defaultName;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
return null;
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
class SpecificArgNameResolver extends NameResolver {
|
|
67
|
-
/**
|
|
68
|
-
*
|
|
69
|
-
* @param {string[]} reservedNames
|
|
70
|
-
*/
|
|
71
|
-
constructor(reservedNames) {
|
|
72
|
-
super(reservedNames, (variants) => {
|
|
73
|
-
return (variants[0] && `${variants[0]}${getRandomInt(1, 10)}`) || `arg${getRandomInt(1, 10)}`;
|
|
74
|
-
});
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
class ComponentTypeNameResolver extends NameResolver {
|
|
79
|
-
/**
|
|
80
|
-
*
|
|
81
|
-
* @param {string[]} reservedNames
|
|
82
|
-
*/
|
|
83
|
-
constructor(reservedNames) {
|
|
84
|
-
super(reservedNames, (variants) => {
|
|
85
|
-
return (
|
|
86
|
-
(variants[0] && `${variants[0]}${getRandomInt(1, 10)}`) ||
|
|
87
|
-
`ComponentType${getRandomInt(1, 10)}`
|
|
88
|
-
);
|
|
89
|
-
});
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
module.exports = {
|
|
94
|
-
NameResolver,
|
|
95
|
-
SpecificArgNameResolver,
|
|
96
|
-
ComponentTypeNameResolver,
|
|
97
|
-
};
|
|
1
|
+
const _ = require("lodash");
|
|
2
|
+
const { getRandomInt } = require("./random");
|
|
3
|
+
|
|
4
|
+
class NameResolver {
|
|
5
|
+
reservedNames = [];
|
|
6
|
+
getDefaultName = null;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
*
|
|
10
|
+
* @param {string[]} reservedNames
|
|
11
|
+
*/
|
|
12
|
+
constructor(reservedNames, getDefaultName) {
|
|
13
|
+
this.getDefaultName = getDefaultName;
|
|
14
|
+
this.reserve(reservedNames);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
*
|
|
19
|
+
* @param {string[]} names
|
|
20
|
+
*/
|
|
21
|
+
reserve(names) {
|
|
22
|
+
this.reservedNames.push(..._.uniq(_.compact(names)));
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
unreserve(names) {
|
|
26
|
+
this.reservedNames.filter((reservedName) => !names.some((name) => name === reservedName));
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
isReserved(name) {
|
|
30
|
+
return _.some(this.reservedNames, (reservedName) => reservedName === name);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
*
|
|
35
|
+
* @param {string[]} variants
|
|
36
|
+
* @param {((variant: string) => string) | undefined} onSelectMutation
|
|
37
|
+
* @returns {string | null}
|
|
38
|
+
*/
|
|
39
|
+
resolve(variants, onSelectMutation) {
|
|
40
|
+
let usageName = null;
|
|
41
|
+
const uniqVariants = _.uniq(_.compact(variants));
|
|
42
|
+
|
|
43
|
+
_.forEach(uniqVariants, (variant) => {
|
|
44
|
+
const mutatedVariant = onSelectMutation ? onSelectMutation(variant) : variant;
|
|
45
|
+
if (!usageName && !this.isReserved(mutatedVariant)) {
|
|
46
|
+
usageName = mutatedVariant;
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
if (usageName) {
|
|
51
|
+
this.reserve([usageName]);
|
|
52
|
+
return usageName;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const defaultName = this.getDefaultName && this.getDefaultName(variants);
|
|
56
|
+
|
|
57
|
+
if (defaultName) {
|
|
58
|
+
this.reserve([onSelectMutation ? onSelectMutation(defaultName) : defaultName]);
|
|
59
|
+
return defaultName;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
class SpecificArgNameResolver extends NameResolver {
|
|
67
|
+
/**
|
|
68
|
+
*
|
|
69
|
+
* @param {string[]} reservedNames
|
|
70
|
+
*/
|
|
71
|
+
constructor(reservedNames) {
|
|
72
|
+
super(reservedNames, (variants) => {
|
|
73
|
+
return (variants[0] && `${variants[0]}${getRandomInt(1, 10)}`) || `arg${getRandomInt(1, 10)}`;
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
class ComponentTypeNameResolver extends NameResolver {
|
|
79
|
+
/**
|
|
80
|
+
*
|
|
81
|
+
* @param {string[]} reservedNames
|
|
82
|
+
*/
|
|
83
|
+
constructor(reservedNames) {
|
|
84
|
+
super(reservedNames, (variants) => {
|
|
85
|
+
return (
|
|
86
|
+
(variants[0] && `${variants[0]}${getRandomInt(1, 10)}`) ||
|
|
87
|
+
`ComponentType${getRandomInt(1, 10)}`
|
|
88
|
+
);
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
module.exports = {
|
|
94
|
+
NameResolver,
|
|
95
|
+
SpecificArgNameResolver,
|
|
96
|
+
ComponentTypeNameResolver,
|
|
97
|
+
};
|
package/templates/README.md
CHANGED
|
@@ -1,13 +1,17 @@
|
|
|
1
|
-
# swagger-typescript-api
|
|
2
|
-
|
|
3
|
-
# templates
|
|
4
|
-
|
|
5
|
-
Templates:
|
|
6
|
-
- `api.
|
|
7
|
-
- `data-contracts.
|
|
8
|
-
- `http-client.
|
|
9
|
-
- `procedure-call.
|
|
10
|
-
- `route-docs.
|
|
11
|
-
- `route-name.
|
|
12
|
-
- `route-type.
|
|
13
|
-
- `route-types.
|
|
1
|
+
# swagger-typescript-api
|
|
2
|
+
|
|
3
|
+
# templates
|
|
4
|
+
|
|
5
|
+
Templates:
|
|
6
|
+
- `api.ejs` - *(generates file)* Api class module (locations: [default](https://github.com/acacode/swagger-typescript-api/tree/next/templates/default/api.ejs), [modular](https://github.com/acacode/swagger-typescript-api/tree/next/templates/modular/api.ejs))
|
|
7
|
+
- `data-contracts.ejs` - *(generates file)* all types (data contracts) from swagger schema (locations: [base](https://github.com/acacode/swagger-typescript-api/tree/next/templates/base/data-contracts.ejs))
|
|
8
|
+
- `http-client.ejs` - *(generates file)* HttpClient class module (locations: [base](https://github.com/acacode/swagger-typescript-api/tree/next/templates/base/http-client.ejs))
|
|
9
|
+
- `procedure-call.ejs` - *(subtemplate)* route in Api class (locations: [default](https://github.com/acacode/swagger-typescript-api/tree/next/templates/default/procedure-call.ejs), [modular](https://github.com/acacode/swagger-typescript-api/tree/next/templates/modular/procedure-call.ejs))
|
|
10
|
+
- `route-docs.ejs` - *(generates file)* documentation for route in Api class (locations: [base](https://github.com/acacode/swagger-typescript-api/tree/next/templates/base/route-docs.ejs))
|
|
11
|
+
- `route-name.ejs` - *(subtemplate)* route name for route in Api class (locations: [base](https://github.com/acacode/swagger-typescript-api/tree/next/templates/base/route-name.ejs))
|
|
12
|
+
- `route-type.ejs` - *(`--route-types` option)* *(subtemplate)* (locations: [base](https://github.com/acacode/swagger-typescript-api/tree/next/templates/base/route-type.ejs))
|
|
13
|
+
- `route-types.ejs` - *(`--route-types` option)* *(subtemplate)* (locations: [base](https://github.com/acacode/swagger-typescript-api/tree/next/templates/base/route-types.ejs)) - `data-contract-jsdoc.ejs` - *(subtemplate)* generates JSDOC for data contract (locations: [base](https://github.com/acacode/swagger-typescript-api/tree/next/templates/base/data-contract-jsdoc.ejs))
|
|
14
|
+
|
|
15
|
+
[//]: # (- `enum-data-contract.ejs` - *(subtemplate)* generates `enum` data contract (locations: [base](https://github.com/acacode/swagger-typescript-api/tree/next/templates/base/enum-data-contract.ejs)))
|
|
16
|
+
[//]: # (- `interface-data-contract.ejs` - *(subtemplate)* generates `interface` data contract (locations: [base](https://github.com/acacode/swagger-typescript-api/tree/next/templates/base/interface-data-contract.ejs)))
|
|
17
|
+
[//]: # (- `type-data-contract.ejs` - *(subtemplate)* generates `type` data contract (locations: [base](https://github.com/acacode/swagger-typescript-api/tree/next/templates/base/type-data-contract.ejs)))
|
package/templates/base/README.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
# swagger-typescript-api
|
|
2
|
-
|
|
3
|
-
# templates/base
|
|
4
|
-
|
|
5
|
-
This templates use both for multiple api files and single api file
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
# swagger-typescript-api
|
|
2
|
+
|
|
3
|
+
# templates/base
|
|
4
|
+
|
|
5
|
+
This templates use both for multiple api files and single api file
|
|
6
|
+
|
|
7
|
+
|
|
8
8
|
path prefix `@base`
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
<%
|
|
2
|
+
const { contract, utils } = it;
|
|
3
|
+
const { formatDescription, require, _ } = utils;
|
|
4
|
+
|
|
5
|
+
let jsDocLines = [];
|
|
6
|
+
|
|
7
|
+
jsDocLines.push(
|
|
8
|
+
contract.title,
|
|
9
|
+
contract.description && formatDescription(contract.description),
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
if (contract.typeData) {
|
|
13
|
+
jsDocLines.push(
|
|
14
|
+
contract.typeData.deprecated === true && '@deprecated',
|
|
15
|
+
!_.isUndefined(contract.typeData.format) && `@format ${contract.typeData.format}`,
|
|
16
|
+
!_.isUndefined(contract.typeData.minimum) && `@min ${contract.typeData.minimum}`,
|
|
17
|
+
!_.isUndefined(contract.typeData.maximum) && `@max ${contract.typeData.maximum}`,
|
|
18
|
+
!_.isUndefined(contract.typeData.pattern) && `@pattern ${contract.typeData.pattern}`,
|
|
19
|
+
!_.isUndefined(contract.typeData.example) && `@example ${
|
|
20
|
+
_.isObject(contract.typeData.example) ? JSON.stringify(contract.typeData.example) : contract.typeData.example
|
|
21
|
+
}`
|
|
22
|
+
)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
jsDocLines = _.compact(jsDocLines);
|
|
26
|
+
%>
|
|
27
|
+
<% if (jsDocLines.length) { %>
|
|
28
|
+
/**
|
|
29
|
+
<%~ jsDocLines.map(part => `* ${part}`).join("\n") %>
|
|
30
|
+
|
|
31
|
+
*/
|
|
32
|
+
<% } %>
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
<%
|
|
2
|
+
const { modelTypes, utils, config } = it;
|
|
3
|
+
const { formatDescription, require, _ } = utils;
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
const dataContractTemplates = {
|
|
7
|
+
enum: (contract) => {
|
|
8
|
+
return `enum ${contract.name} {\r\n${contract.content} \r\n }`;
|
|
9
|
+
},
|
|
10
|
+
interface: (contract) => {
|
|
11
|
+
return `interface ${contract.name} {\r\n${contract.content}}`;
|
|
12
|
+
},
|
|
13
|
+
type: (contract) => {
|
|
14
|
+
return `type ${contract.name} = ${contract.content}`;
|
|
15
|
+
},
|
|
16
|
+
}
|
|
17
|
+
%>
|
|
18
|
+
|
|
19
|
+
<% if (config.internalTemplateOptions.addUtilRequiredKeysType) { %>
|
|
20
|
+
type UtilRequiredKeys<T, K extends keyof T> = Omit<T, K> & Required<Pick<T, K>>
|
|
21
|
+
<% } %>
|
|
22
|
+
|
|
23
|
+
<% modelTypes.forEach((contract) => { %>
|
|
24
|
+
<%~ includeFile('@base/data-contract-jsdoc.ejs', { ...it, contract }) %>
|
|
25
|
+
export <%~ (dataContractTemplates[contract.typeIdentifier] || dataContractTemplates.type)(contract) %>
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
<% }) %>
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
<%
|
|
2
|
+
const { contract, utils, config } = it;
|
|
3
|
+
const { formatDescription, require, _ } = utils;
|
|
4
|
+
const { name, $content } = contract;
|
|
5
|
+
|
|
6
|
+
const isNumberEnum = _.some($content, (content) => typeof content.key === "number");
|
|
7
|
+
const formatAsUnionType = !!(isNumberEnum || config.generateUnionEnums);
|
|
8
|
+
%>
|
|
9
|
+
<% if (formatAsUnionType) { %>
|
|
10
|
+
export type <%~ name %> = <%~ _.map($content, ({ value }) => value).join(" | ") %>
|
|
11
|
+
<% } else { %>
|
|
12
|
+
export enum <%~ name %> {
|
|
13
|
+
<%~ _.map($content, ({ key, value }) => `${key} = ${value}`).join(",\n") %>
|
|
14
|
+
}
|
|
15
|
+
<% } %>
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
<% const { config } = it; %>
|
|
2
|
-
<% /* https://github.com/acacode/swagger-typescript-api/tree/next/templates/base/http-clients/ */ %>
|
|
1
|
+
<% const { config } = it; %>
|
|
2
|
+
<% /* https://github.com/acacode/swagger-typescript-api/tree/next/templates/base/http-clients/ */ %>
|
|
3
3
|
<%~ includeFile(`@base/http-clients/${config.httpClientType}-http-client`, it) %>
|
|
@@ -1,145 +1,133 @@
|
|
|
1
|
-
<%
|
|
2
|
-
const { apiConfig, generateResponses, config } = it;
|
|
3
|
-
%>
|
|
4
|
-
|
|
5
|
-
import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse, ResponseType, HeadersDefaults } from "axios";
|
|
6
|
-
|
|
7
|
-
export type QueryParamsType = Record<string | number, any>;
|
|
8
|
-
|
|
9
|
-
export interface FullRequestParams extends Omit<AxiosRequestConfig, "data" | "params" | "url" | "responseType"> {
|
|
10
|
-
/** set parameter to `true` for call `securityWorker` for this request */
|
|
11
|
-
secure?: boolean;
|
|
12
|
-
/** request path */
|
|
13
|
-
path: string;
|
|
14
|
-
/** content type of request body */
|
|
15
|
-
type?: ContentType;
|
|
16
|
-
/** query params */
|
|
17
|
-
query?: QueryParamsType;
|
|
18
|
-
/** format of response (i.e. response.json() -> format: "json") */
|
|
19
|
-
format?: ResponseType;
|
|
20
|
-
/** request body */
|
|
21
|
-
body?: unknown;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export type RequestParams = Omit<FullRequestParams, "body" | "method" | "query" | "path">;
|
|
25
|
-
|
|
26
|
-
export interface ApiConfig<SecurityDataType = unknown> extends Omit<AxiosRequestConfig, "data" | "cancelToken"> {
|
|
27
|
-
securityWorker?: (securityData: SecurityDataType | null) => Promise<AxiosRequestConfig | void> | AxiosRequestConfig | void;
|
|
28
|
-
secure?: boolean;
|
|
29
|
-
format?: ResponseType;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export enum ContentType {
|
|
33
|
-
Json = "application/json",
|
|
34
|
-
FormData = "multipart/form-data",
|
|
35
|
-
UrlEncoded = "application/x-www-form-urlencoded",
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export class HttpClient<SecurityDataType = unknown> {
|
|
39
|
-
public instance: AxiosInstance;
|
|
40
|
-
private securityData: SecurityDataType | null = null;
|
|
41
|
-
private securityWorker?: ApiConfig<SecurityDataType>["securityWorker"];
|
|
42
|
-
private secure?: boolean;
|
|
43
|
-
private format?: ResponseType;
|
|
44
|
-
|
|
45
|
-
constructor({ securityWorker, secure, format, ...axiosConfig }: ApiConfig<SecurityDataType> = {}) {
|
|
46
|
-
this.instance = axios.create({ ...axiosConfig, baseURL: axiosConfig.baseURL || "<%~ apiConfig.baseUrl %>" })
|
|
47
|
-
this.secure = secure;
|
|
48
|
-
this.format = format;
|
|
49
|
-
this.securityWorker = securityWorker;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
public setSecurityData = (data: SecurityDataType | null) => {
|
|
53
|
-
this.securityData = data
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
protected mergeRequestParams(params1: AxiosRequestConfig, params2?: AxiosRequestConfig): AxiosRequestConfig {
|
|
57
|
-
const method = params1.method || (params2 && params2.method)
|
|
58
|
-
|
|
59
|
-
return {
|
|
60
|
-
...this.instance.defaults,
|
|
61
|
-
...params1,
|
|
62
|
-
...(params2 || {}),
|
|
63
|
-
headers: {
|
|
64
|
-
...((method && this.instance.defaults.headers[method.toLowerCase() as keyof HeadersDefaults]) || {}),
|
|
65
|
-
...(params1.headers || {}),
|
|
66
|
-
...((params2 && params2.headers) || {}),
|
|
67
|
-
},
|
|
68
|
-
};
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
protected stringifyFormItem(formItem: unknown) {
|
|
72
|
-
if (typeof formItem === "object" && formItem !== null) {
|
|
73
|
-
return JSON.stringify(formItem);
|
|
74
|
-
} else {
|
|
75
|
-
return `${formItem}`;
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
protected createFormData(input: Record<string, unknown>): FormData {
|
|
80
|
-
return Object.keys(input || {}).reduce((formData, key) => {
|
|
81
|
-
const property = input[key];
|
|
82
|
-
const propertyContent: Iterable<any> = (property instanceof Array) ? property : [property]
|
|
83
|
-
|
|
84
|
-
for (const formItem of propertyContent) {
|
|
85
|
-
const isFileType = formItem instanceof Blob || formItem instanceof File;
|
|
86
|
-
formData.append(
|
|
87
|
-
key,
|
|
88
|
-
isFileType ? formItem : this.stringifyFormItem(formItem)
|
|
89
|
-
);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
return formData;
|
|
93
|
-
}, new FormData());
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
secure
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
},
|
|
135
|
-
params: query,
|
|
136
|
-
responseType: responseFormat,
|
|
137
|
-
data: body,
|
|
138
|
-
url: path,
|
|
139
|
-
<% if (config.unwrapResponseData) { %>
|
|
140
|
-
}).then(response => response.data);
|
|
141
|
-
<% } else { %>
|
|
142
|
-
});
|
|
143
|
-
<% } %>
|
|
144
|
-
};
|
|
145
|
-
}
|
|
1
|
+
<%
|
|
2
|
+
const { apiConfig, generateResponses, config } = it;
|
|
3
|
+
%>
|
|
4
|
+
|
|
5
|
+
import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse, ResponseType, HeadersDefaults } from "axios";
|
|
6
|
+
|
|
7
|
+
export type QueryParamsType = Record<string | number, any>;
|
|
8
|
+
|
|
9
|
+
export interface FullRequestParams extends Omit<AxiosRequestConfig, "data" | "params" | "url" | "responseType"> {
|
|
10
|
+
/** set parameter to `true` for call `securityWorker` for this request */
|
|
11
|
+
secure?: boolean;
|
|
12
|
+
/** request path */
|
|
13
|
+
path: string;
|
|
14
|
+
/** content type of request body */
|
|
15
|
+
type?: ContentType;
|
|
16
|
+
/** query params */
|
|
17
|
+
query?: QueryParamsType;
|
|
18
|
+
/** format of response (i.e. response.json() -> format: "json") */
|
|
19
|
+
format?: ResponseType;
|
|
20
|
+
/** request body */
|
|
21
|
+
body?: unknown;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export type RequestParams = Omit<FullRequestParams, "body" | "method" | "query" | "path">;
|
|
25
|
+
|
|
26
|
+
export interface ApiConfig<SecurityDataType = unknown> extends Omit<AxiosRequestConfig, "data" | "cancelToken"> {
|
|
27
|
+
securityWorker?: (securityData: SecurityDataType | null) => Promise<AxiosRequestConfig | void> | AxiosRequestConfig | void;
|
|
28
|
+
secure?: boolean;
|
|
29
|
+
format?: ResponseType;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export enum ContentType {
|
|
33
|
+
Json = "application/json",
|
|
34
|
+
FormData = "multipart/form-data",
|
|
35
|
+
UrlEncoded = "application/x-www-form-urlencoded",
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export class HttpClient<SecurityDataType = unknown> {
|
|
39
|
+
public instance: AxiosInstance;
|
|
40
|
+
private securityData: SecurityDataType | null = null;
|
|
41
|
+
private securityWorker?: ApiConfig<SecurityDataType>["securityWorker"];
|
|
42
|
+
private secure?: boolean;
|
|
43
|
+
private format?: ResponseType;
|
|
44
|
+
|
|
45
|
+
constructor({ securityWorker, secure, format, ...axiosConfig }: ApiConfig<SecurityDataType> = {}) {
|
|
46
|
+
this.instance = axios.create({ ...axiosConfig, baseURL: axiosConfig.baseURL || "<%~ apiConfig.baseUrl %>" })
|
|
47
|
+
this.secure = secure;
|
|
48
|
+
this.format = format;
|
|
49
|
+
this.securityWorker = securityWorker;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
public setSecurityData = (data: SecurityDataType | null) => {
|
|
53
|
+
this.securityData = data
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
protected mergeRequestParams(params1: AxiosRequestConfig, params2?: AxiosRequestConfig): AxiosRequestConfig {
|
|
57
|
+
const method = params1.method || (params2 && params2.method)
|
|
58
|
+
|
|
59
|
+
return {
|
|
60
|
+
...this.instance.defaults,
|
|
61
|
+
...params1,
|
|
62
|
+
...(params2 || {}),
|
|
63
|
+
headers: {
|
|
64
|
+
...((method && this.instance.defaults.headers[method.toLowerCase() as keyof HeadersDefaults]) || {}),
|
|
65
|
+
...(params1.headers || {}),
|
|
66
|
+
...((params2 && params2.headers) || {}),
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
protected stringifyFormItem(formItem: unknown) {
|
|
72
|
+
if (typeof formItem === "object" && formItem !== null) {
|
|
73
|
+
return JSON.stringify(formItem);
|
|
74
|
+
} else {
|
|
75
|
+
return `${formItem}`;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
protected createFormData(input: Record<string, unknown>): FormData {
|
|
80
|
+
return Object.keys(input || {}).reduce((formData, key) => {
|
|
81
|
+
const property = input[key];
|
|
82
|
+
const propertyContent: Iterable<any> = (property instanceof Array) ? property : [property]
|
|
83
|
+
|
|
84
|
+
for (const formItem of propertyContent) {
|
|
85
|
+
const isFileType = formItem instanceof Blob || formItem instanceof File;
|
|
86
|
+
formData.append(
|
|
87
|
+
key,
|
|
88
|
+
isFileType ? formItem : this.stringifyFormItem(formItem)
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return formData;
|
|
93
|
+
}, new FormData());
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
public request = async <T = any, _E = any>({
|
|
97
|
+
secure,
|
|
98
|
+
path,
|
|
99
|
+
type,
|
|
100
|
+
query,
|
|
101
|
+
format,
|
|
102
|
+
body,
|
|
103
|
+
...params
|
|
104
|
+
<% if (config.unwrapResponseData) { %>
|
|
105
|
+
}: FullRequestParams): Promise<T> => {
|
|
106
|
+
<% } else { %>
|
|
107
|
+
}: FullRequestParams): Promise<AxiosResponse<T>> => {
|
|
108
|
+
<% } %>
|
|
109
|
+
const secureParams = ((typeof secure === 'boolean' ? secure : this.secure) && this.securityWorker && (await this.securityWorker(this.securityData))) || {};
|
|
110
|
+
const requestParams = this.mergeRequestParams(params, secureParams);
|
|
111
|
+
const responseFormat = (format || this.format) || undefined;
|
|
112
|
+
|
|
113
|
+
if (type === ContentType.FormData && body && body !== null && typeof body === "object") {
|
|
114
|
+
body = this.createFormData(body as Record<string, unknown>);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return this.instance.request({
|
|
118
|
+
...requestParams,
|
|
119
|
+
headers: {
|
|
120
|
+
...(requestParams.headers || {}),
|
|
121
|
+
...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}),
|
|
122
|
+
},
|
|
123
|
+
params: query,
|
|
124
|
+
responseType: responseFormat,
|
|
125
|
+
data: body,
|
|
126
|
+
url: path,
|
|
127
|
+
<% if (config.unwrapResponseData) { %>
|
|
128
|
+
}).then(response => response.data);
|
|
129
|
+
<% } else { %>
|
|
130
|
+
});
|
|
131
|
+
<% } %>
|
|
132
|
+
};
|
|
133
|
+
}
|