@fxri/swagger2api 1.1.7 → 1.1.8
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/README.md +31 -4
- package/dist/templates/api.ejs +69 -69
- package/dist/templates/http-client.ejs +151 -155
- package/dist/templates/route-types.ejs +32 -32
- package/dist/templates/type-data-contract.ejs +15 -15
- package/package.json +4 -3
package/README.md
CHANGED
|
@@ -107,10 +107,37 @@ npx @fxri/swagger2api
|
|
|
107
107
|
直接调用生成的API类方法:
|
|
108
108
|
|
|
109
109
|
```typescript
|
|
110
|
-
import { Api } from "./
|
|
110
|
+
import { Api } from "./api"
|
|
111
|
+
/** 请求器 */
|
|
112
|
+
export const request = new Api()
|
|
113
|
+
request.getUsers().then((res) => console.log(res))
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
### 4. 提取响应(如没有使用提取响应参数,则忽略该项)
|
|
117
|
+
|
|
118
|
+
声明自定义的根部字段(以下字段为示例,请根据实际情况自行调整):
|
|
119
|
+
|
|
120
|
+
```typescript
|
|
121
|
+
declare module "." {
|
|
122
|
+
interface AxiosResponseRaw<T = any> {
|
|
123
|
+
/** 状态代码 */
|
|
124
|
+
code: number
|
|
125
|
+
/** 提示信息 */
|
|
126
|
+
message: string
|
|
127
|
+
/** 响应数据 */
|
|
128
|
+
data: T["data"]
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
```
|
|
111
132
|
|
|
112
|
-
|
|
113
|
-
|
|
133
|
+
根据已声明字段,配置对应的返回值:
|
|
134
|
+
|
|
135
|
+
```typescript
|
|
136
|
+
// 响应拦截
|
|
137
|
+
client.instance.interceptors.response.use(
|
|
138
|
+
(response) => ({ code: 0, data: null, message: "", raw: response }),
|
|
139
|
+
(error) => ({ code: -1, data: null, message: error.message, raw: error })
|
|
140
|
+
)
|
|
114
141
|
```
|
|
115
142
|
|
|
116
143
|
每个方法会返回 Promise,可通过 async/await 或 .then() 处理响应。
|
|
@@ -131,7 +158,7 @@ api.getUsers().then((res) => console.log(res))
|
|
|
131
158
|
| `--remove-prefix-index,-rpi` | number | `-1` | 移除前缀索引 |
|
|
132
159
|
| `--remove-dts,-rd` | boolean | `false` | 移除使用--js参数时生成的d.ts文件 |
|
|
133
160
|
| `--extract-request-query,-erq` | string | - | 提取参数,将query参数中的指定对象字段提取为根部字段,支持多选,用【,】分隔,每个接口只提取第一次命中的字段 |
|
|
134
|
-
| `--extract-response-raw,-err` | boolean | `false` | 提取响应,将AxiosResponse返回值转移到raw
|
|
161
|
+
| `--extract-response-raw,-err` | boolean | `false` | 提取响应,将AxiosResponse返回值转移到raw字段,根部字段自行定义,使用该参数需在响应拦截中重新设计实际的返回值 |
|
|
135
162
|
|
|
136
163
|
扩展:[更多 swagger-typescript-api 配置选项](https://fig.io/manual/swagger-typescript-api)
|
|
137
164
|
|
package/dist/templates/api.ejs
CHANGED
|
@@ -1,69 +1,69 @@
|
|
|
1
|
-
<%
|
|
2
|
-
const { apiConfig, routes, utils, config } = it;
|
|
3
|
-
const { info, servers, externalDocs } = apiConfig;
|
|
4
|
-
const { _, require, formatDescription } = utils;
|
|
5
|
-
|
|
6
|
-
const server = (servers && servers[0]) || { url: "" };
|
|
7
|
-
|
|
8
|
-
const descriptionLines = _.compact([
|
|
9
|
-
`@title ${info.title || "No title"}`,
|
|
10
|
-
info.version && `@version ${info.version}`,
|
|
11
|
-
info.license && `@license ${_.compact([
|
|
12
|
-
info.license.name,
|
|
13
|
-
info.license.url && `(${info.license.url})`,
|
|
14
|
-
]).join(" ")}`,
|
|
15
|
-
info.termsOfService && `@termsOfService ${info.termsOfService}`,
|
|
16
|
-
server.url && `@baseUrl ${server.url}`,
|
|
17
|
-
externalDocs.url && `@externalDocs ${externalDocs.url}`,
|
|
18
|
-
info.contact && `@contact ${_.compact([
|
|
19
|
-
info.contact.name,
|
|
20
|
-
info.contact.email && `<${info.contact.email}>`,
|
|
21
|
-
info.contact.url && `(${info.contact.url})`,
|
|
22
|
-
]).join(" ")}`,
|
|
23
|
-
info.description && " ",
|
|
24
|
-
info.description && _.replace(formatDescription(info.description), /\n/g, "\n * "),
|
|
25
|
-
]);
|
|
26
|
-
|
|
27
|
-
%>
|
|
28
|
-
|
|
29
|
-
<% if (config.httpClientType === config.constants.HTTP_CLIENT.AXIOS) { %> import type { AxiosRequestConfig, AxiosResponse } from "axios"; <% } %>
|
|
30
|
-
|
|
31
|
-
<% if (descriptionLines.length) { %>
|
|
32
|
-
/**
|
|
33
|
-
<% descriptionLines.forEach((descriptionLine) => { %>
|
|
34
|
-
* <%~ descriptionLine %>
|
|
35
|
-
|
|
36
|
-
<% }) %>
|
|
37
|
-
*/
|
|
38
|
-
<% } %>
|
|
39
|
-
export class <%~ config.apiClassName %><SecurityDataType extends unknown><% if (!config.singleHttpClient) { %> extends HttpClient<SecurityDataType> <% } %> {
|
|
40
|
-
|
|
41
|
-
<% if(config.singleHttpClient) { %>
|
|
42
|
-
http: HttpClient<SecurityDataType>;
|
|
43
|
-
|
|
44
|
-
constructor (http: HttpClient<SecurityDataType>) {
|
|
45
|
-
this.http = http;
|
|
46
|
-
}
|
|
47
|
-
<% } %>
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
<% if (routes.outOfModule) { %>
|
|
51
|
-
<% for (const route of routes.outOfModule) { %>
|
|
52
|
-
|
|
53
|
-
<%~ includeFile('./procedure-call.ejs', { ...it, route }) %>
|
|
54
|
-
|
|
55
|
-
<% } %>
|
|
56
|
-
<% } %>
|
|
57
|
-
|
|
58
|
-
<% if (routes.combined) { %>
|
|
59
|
-
<% for (const { routes: combinedRoutes = [], moduleName } of routes.combined) { %>
|
|
60
|
-
<%~ moduleName %> = {
|
|
61
|
-
<% for (const route of combinedRoutes) { %>
|
|
62
|
-
|
|
63
|
-
<%~ includeFile('./procedure-call.ejs', { ...it, route }) %>
|
|
64
|
-
|
|
65
|
-
<% } %>
|
|
66
|
-
}
|
|
67
|
-
<% } %>
|
|
68
|
-
<% } %>
|
|
69
|
-
}
|
|
1
|
+
<%
|
|
2
|
+
const { apiConfig, routes, utils, config } = it;
|
|
3
|
+
const { info, servers, externalDocs } = apiConfig;
|
|
4
|
+
const { _, require, formatDescription } = utils;
|
|
5
|
+
|
|
6
|
+
const server = (servers && servers[0]) || { url: "" };
|
|
7
|
+
|
|
8
|
+
const descriptionLines = _.compact([
|
|
9
|
+
`@title ${info.title || "No title"}`,
|
|
10
|
+
info.version && `@version ${info.version}`,
|
|
11
|
+
info.license && `@license ${_.compact([
|
|
12
|
+
info.license.name,
|
|
13
|
+
info.license.url && `(${info.license.url})`,
|
|
14
|
+
]).join(" ")}`,
|
|
15
|
+
info.termsOfService && `@termsOfService ${info.termsOfService}`,
|
|
16
|
+
server.url && `@baseUrl ${server.url}`,
|
|
17
|
+
externalDocs.url && `@externalDocs ${externalDocs.url}`,
|
|
18
|
+
info.contact && `@contact ${_.compact([
|
|
19
|
+
info.contact.name,
|
|
20
|
+
info.contact.email && `<${info.contact.email}>`,
|
|
21
|
+
info.contact.url && `(${info.contact.url})`,
|
|
22
|
+
]).join(" ")}`,
|
|
23
|
+
info.description && " ",
|
|
24
|
+
info.description && _.replace(formatDescription(info.description), /\n/g, "\n * "),
|
|
25
|
+
]);
|
|
26
|
+
|
|
27
|
+
%>
|
|
28
|
+
|
|
29
|
+
<% if (config.httpClientType === config.constants.HTTP_CLIENT.AXIOS && !config.param?.extractResponseRaw) { %> import type { AxiosRequestConfig, AxiosResponse } from "axios"; <% } %>
|
|
30
|
+
|
|
31
|
+
<% if (descriptionLines.length) { %>
|
|
32
|
+
/**
|
|
33
|
+
<% descriptionLines.forEach((descriptionLine) => { %>
|
|
34
|
+
* <%~ descriptionLine %>
|
|
35
|
+
|
|
36
|
+
<% }) %>
|
|
37
|
+
*/
|
|
38
|
+
<% } %>
|
|
39
|
+
export class <%~ config.apiClassName %><SecurityDataType extends unknown><% if (!config.singleHttpClient) { %> extends HttpClient<SecurityDataType> <% } %> {
|
|
40
|
+
|
|
41
|
+
<% if(config.singleHttpClient) { %>
|
|
42
|
+
http: HttpClient<SecurityDataType>;
|
|
43
|
+
|
|
44
|
+
constructor (http: HttpClient<SecurityDataType>) {
|
|
45
|
+
this.http = http;
|
|
46
|
+
}
|
|
47
|
+
<% } %>
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
<% if (routes.outOfModule) { %>
|
|
51
|
+
<% for (const route of routes.outOfModule) { %>
|
|
52
|
+
|
|
53
|
+
<%~ includeFile('./procedure-call.ejs', { ...it, route }) %>
|
|
54
|
+
|
|
55
|
+
<% } %>
|
|
56
|
+
<% } %>
|
|
57
|
+
|
|
58
|
+
<% if (routes.combined) { %>
|
|
59
|
+
<% for (const { routes: combinedRoutes = [], moduleName } of routes.combined) { %>
|
|
60
|
+
<%~ moduleName %> = {
|
|
61
|
+
<% for (const route of combinedRoutes) { %>
|
|
62
|
+
|
|
63
|
+
<%~ includeFile('./procedure-call.ejs', { ...it, route }) %>
|
|
64
|
+
|
|
65
|
+
<% } %>
|
|
66
|
+
}
|
|
67
|
+
<% } %>
|
|
68
|
+
<% } %>
|
|
69
|
+
}
|
|
@@ -1,155 +1,151 @@
|
|
|
1
|
-
<%
|
|
2
|
-
const { apiConfig, generateResponses, config } = it;
|
|
3
|
-
%>
|
|
4
|
-
|
|
5
|
-
import type { AxiosInstance, AxiosRequestConfig, HeadersDefaults, ResponseType, AxiosResponse } from "axios";
|
|
6
|
-
import axios from "axios";
|
|
7
|
-
|
|
8
|
-
export type QueryParamsType = Record<string | number, any>;
|
|
9
|
-
|
|
10
|
-
<% if (config.param?.extractResponseRaw) { %>
|
|
11
|
-
/** 提取响应 */
|
|
12
|
-
export interface
|
|
13
|
-
/** 原始数据 */
|
|
14
|
-
raw:
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
<% } %>
|
|
18
|
-
export interface FullRequestParams extends Omit<AxiosRequestConfig, "data" | "params" | "url" | "responseType"> {
|
|
19
|
-
/** set parameter to `true` for call `securityWorker` for this request */
|
|
20
|
-
secure?: boolean;
|
|
21
|
-
/** request path */
|
|
22
|
-
path: string;
|
|
23
|
-
/** content type of request body */
|
|
24
|
-
type?: ContentType;
|
|
25
|
-
/** query params */
|
|
26
|
-
query?: QueryParamsType;
|
|
27
|
-
/** format of response (i.e. response.json() -> format: "json") */
|
|
28
|
-
format?: ResponseType;
|
|
29
|
-
/** request body */
|
|
30
|
-
body?: unknown;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export type RequestParams = Omit<FullRequestParams, "body" | "method" | "query" | "path">;
|
|
34
|
-
|
|
35
|
-
export interface ApiConfig<SecurityDataType = unknown> extends Omit<AxiosRequestConfig, "data" | "cancelToken"> {
|
|
36
|
-
securityWorker?: (securityData: SecurityDataType | null) => Promise<AxiosRequestConfig | void> | AxiosRequestConfig | void;
|
|
37
|
-
secure?: boolean;
|
|
38
|
-
format?: ResponseType;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export enum ContentType {
|
|
42
|
-
Json = "application/json",
|
|
43
|
-
JsonApi = "application/vnd.api+json",
|
|
44
|
-
FormData = "multipart/form-data",
|
|
45
|
-
UrlEncoded = "application/x-www-form-urlencoded",
|
|
46
|
-
Text = "text/plain",
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
export class HttpClient<SecurityDataType = unknown> {
|
|
50
|
-
public instance: AxiosInstance;
|
|
51
|
-
private securityData: SecurityDataType | null = null;
|
|
52
|
-
private securityWorker?: ApiConfig<SecurityDataType>["securityWorker"];
|
|
53
|
-
private secure?: boolean;
|
|
54
|
-
private format?: ResponseType;
|
|
55
|
-
|
|
56
|
-
constructor({ securityWorker, secure, format, ...axiosConfig }: ApiConfig<SecurityDataType> = {}) {
|
|
57
|
-
this.instance = axios.create({ ...axiosConfig, baseURL: axiosConfig.baseURL || "<%~ apiConfig.baseUrl %>" })
|
|
58
|
-
this.secure = secure;
|
|
59
|
-
this.format = format;
|
|
60
|
-
this.securityWorker = securityWorker;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
public setSecurityData = (data: SecurityDataType | null) => {
|
|
64
|
-
this.securityData = data
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
protected mergeRequestParams(params1: AxiosRequestConfig, params2?: AxiosRequestConfig): AxiosRequestConfig {
|
|
68
|
-
const method = params1.method || (params2 && params2.method)
|
|
69
|
-
|
|
70
|
-
return {
|
|
71
|
-
...this.instance.defaults,
|
|
72
|
-
...params1,
|
|
73
|
-
...(params2 || {}),
|
|
74
|
-
headers: {
|
|
75
|
-
...((method && this.instance.defaults.headers[method.toLowerCase() as keyof HeadersDefaults]) || {}),
|
|
76
|
-
...(params1.headers || {}),
|
|
77
|
-
...((params2 && params2.headers) || {}),
|
|
78
|
-
},
|
|
79
|
-
};
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
protected stringifyFormItem(formItem: unknown) {
|
|
83
|
-
if (typeof formItem === "object" && formItem !== null) {
|
|
84
|
-
return JSON.stringify(formItem);
|
|
85
|
-
} else {
|
|
86
|
-
return `${formItem}`;
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
protected createFormData(input: Record<string, unknown>): FormData {
|
|
91
|
-
if (input instanceof FormData) {
|
|
92
|
-
return input;
|
|
93
|
-
}
|
|
94
|
-
return Object.keys(input || {}).reduce((formData, key) => {
|
|
95
|
-
const property = input[key];
|
|
96
|
-
const propertyContent: any[] = (property instanceof Array) ? property : [property]
|
|
97
|
-
|
|
98
|
-
for (const formItem of propertyContent) {
|
|
99
|
-
const isFileType = formItem instanceof Blob || formItem instanceof File;
|
|
100
|
-
formData.append(
|
|
101
|
-
key,
|
|
102
|
-
isFileType ? formItem : this.stringifyFormItem(formItem)
|
|
103
|
-
);
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
return formData;
|
|
107
|
-
}, new FormData());
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
public request = async <T = any, _E = any>({
|
|
111
|
-
secure,
|
|
112
|
-
path,
|
|
113
|
-
type,
|
|
114
|
-
query,
|
|
115
|
-
format,
|
|
116
|
-
body,
|
|
117
|
-
...params
|
|
118
|
-
<% if (config.unwrapResponseData) { %>
|
|
119
|
-
}: FullRequestParams): Promise<T> => {
|
|
120
|
-
<% } else
|
|
121
|
-
}: FullRequestParams): Promise<
|
|
122
|
-
<% }
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
const
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
<%
|
|
148
|
-
})
|
|
149
|
-
<% }
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
});
|
|
153
|
-
<% } %>
|
|
154
|
-
};
|
|
155
|
-
}
|
|
1
|
+
<%
|
|
2
|
+
const { apiConfig, generateResponses, config } = it;
|
|
3
|
+
%>
|
|
4
|
+
|
|
5
|
+
import type { AxiosInstance, AxiosRequestConfig, HeadersDefaults, ResponseType, AxiosResponse as AxiosResponseRaw } from "axios";
|
|
6
|
+
import axios from "axios";
|
|
7
|
+
|
|
8
|
+
export type QueryParamsType = Record<string | number, any>;
|
|
9
|
+
|
|
10
|
+
<% if (config.param?.extractResponseRaw) { %>
|
|
11
|
+
/** 提取响应 */
|
|
12
|
+
export interface AxiosResponse<T = any> {
|
|
13
|
+
/** 原始数据 */
|
|
14
|
+
raw: AxiosResponseRaw<T>;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
<% } %>
|
|
18
|
+
export interface FullRequestParams extends Omit<AxiosRequestConfig, "data" | "params" | "url" | "responseType"> {
|
|
19
|
+
/** set parameter to `true` for call `securityWorker` for this request */
|
|
20
|
+
secure?: boolean;
|
|
21
|
+
/** request path */
|
|
22
|
+
path: string;
|
|
23
|
+
/** content type of request body */
|
|
24
|
+
type?: ContentType;
|
|
25
|
+
/** query params */
|
|
26
|
+
query?: QueryParamsType;
|
|
27
|
+
/** format of response (i.e. response.json() -> format: "json") */
|
|
28
|
+
format?: ResponseType;
|
|
29
|
+
/** request body */
|
|
30
|
+
body?: unknown;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export type RequestParams = Omit<FullRequestParams, "body" | "method" | "query" | "path">;
|
|
34
|
+
|
|
35
|
+
export interface ApiConfig<SecurityDataType = unknown> extends Omit<AxiosRequestConfig, "data" | "cancelToken"> {
|
|
36
|
+
securityWorker?: (securityData: SecurityDataType | null) => Promise<AxiosRequestConfig | void> | AxiosRequestConfig | void;
|
|
37
|
+
secure?: boolean;
|
|
38
|
+
format?: ResponseType;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export enum ContentType {
|
|
42
|
+
Json = "application/json",
|
|
43
|
+
JsonApi = "application/vnd.api+json",
|
|
44
|
+
FormData = "multipart/form-data",
|
|
45
|
+
UrlEncoded = "application/x-www-form-urlencoded",
|
|
46
|
+
Text = "text/plain",
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export class HttpClient<SecurityDataType = unknown> {
|
|
50
|
+
public instance: AxiosInstance;
|
|
51
|
+
private securityData: SecurityDataType | null = null;
|
|
52
|
+
private securityWorker?: ApiConfig<SecurityDataType>["securityWorker"];
|
|
53
|
+
private secure?: boolean;
|
|
54
|
+
private format?: ResponseType;
|
|
55
|
+
|
|
56
|
+
constructor({ securityWorker, secure, format, ...axiosConfig }: ApiConfig<SecurityDataType> = {}) {
|
|
57
|
+
this.instance = axios.create({ ...axiosConfig, baseURL: axiosConfig.baseURL || "<%~ apiConfig.baseUrl %>" })
|
|
58
|
+
this.secure = secure;
|
|
59
|
+
this.format = format;
|
|
60
|
+
this.securityWorker = securityWorker;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
public setSecurityData = (data: SecurityDataType | null) => {
|
|
64
|
+
this.securityData = data
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
protected mergeRequestParams(params1: AxiosRequestConfig, params2?: AxiosRequestConfig): AxiosRequestConfig {
|
|
68
|
+
const method = params1.method || (params2 && params2.method)
|
|
69
|
+
|
|
70
|
+
return {
|
|
71
|
+
...this.instance.defaults,
|
|
72
|
+
...params1,
|
|
73
|
+
...(params2 || {}),
|
|
74
|
+
headers: {
|
|
75
|
+
...((method && this.instance.defaults.headers[method.toLowerCase() as keyof HeadersDefaults]) || {}),
|
|
76
|
+
...(params1.headers || {}),
|
|
77
|
+
...((params2 && params2.headers) || {}),
|
|
78
|
+
},
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
protected stringifyFormItem(formItem: unknown) {
|
|
83
|
+
if (typeof formItem === "object" && formItem !== null) {
|
|
84
|
+
return JSON.stringify(formItem);
|
|
85
|
+
} else {
|
|
86
|
+
return `${formItem}`;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
protected createFormData(input: Record<string, unknown>): FormData {
|
|
91
|
+
if (input instanceof FormData) {
|
|
92
|
+
return input;
|
|
93
|
+
}
|
|
94
|
+
return Object.keys(input || {}).reduce((formData, key) => {
|
|
95
|
+
const property = input[key];
|
|
96
|
+
const propertyContent: any[] = (property instanceof Array) ? property : [property]
|
|
97
|
+
|
|
98
|
+
for (const formItem of propertyContent) {
|
|
99
|
+
const isFileType = formItem instanceof Blob || formItem instanceof File;
|
|
100
|
+
formData.append(
|
|
101
|
+
key,
|
|
102
|
+
isFileType ? formItem : this.stringifyFormItem(formItem)
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return formData;
|
|
107
|
+
}, new FormData());
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
public request = async <T = any, _E = any>({
|
|
111
|
+
secure,
|
|
112
|
+
path,
|
|
113
|
+
type,
|
|
114
|
+
query,
|
|
115
|
+
format,
|
|
116
|
+
body,
|
|
117
|
+
...params
|
|
118
|
+
<% if (config.unwrapResponseData) { %>
|
|
119
|
+
}: FullRequestParams): Promise<T> => {
|
|
120
|
+
<% } else { %>
|
|
121
|
+
}: FullRequestParams): Promise<AxiosResponse<T>> => {
|
|
122
|
+
<% } %>
|
|
123
|
+
const secureParams = ((typeof secure === 'boolean' ? secure : this.secure) && this.securityWorker && (await this.securityWorker(this.securityData))) || {};
|
|
124
|
+
const requestParams = this.mergeRequestParams(params, secureParams);
|
|
125
|
+
const responseFormat = (format || this.format) || undefined;
|
|
126
|
+
|
|
127
|
+
if (type === ContentType.FormData && body && body !== null && typeof body === "object") {
|
|
128
|
+
body = this.createFormData(body as Record<string, unknown>);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (type === ContentType.Text && body && body !== null && typeof body !== "string") {
|
|
132
|
+
body = JSON.stringify(body);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
return this.instance.request({
|
|
136
|
+
...requestParams,
|
|
137
|
+
headers: {
|
|
138
|
+
...(requestParams.headers || {}),
|
|
139
|
+
...(type ? { "Content-Type": type } : {}),
|
|
140
|
+
},
|
|
141
|
+
params: query,
|
|
142
|
+
responseType: responseFormat,
|
|
143
|
+
data: body,
|
|
144
|
+
url: path,
|
|
145
|
+
<% if (config.unwrapResponseData) { %>
|
|
146
|
+
}).then(response => response.data);
|
|
147
|
+
<% } else { %>
|
|
148
|
+
});
|
|
149
|
+
<% } %>
|
|
150
|
+
};
|
|
151
|
+
}
|
|
@@ -1,32 +1,32 @@
|
|
|
1
|
-
<%
|
|
2
|
-
const { utils, config, routes, modelTypes } = it;
|
|
3
|
-
const { _, pascalCase } = utils;
|
|
4
|
-
const dataContracts = config.modular ? _.map(modelTypes, "name") : [];
|
|
5
|
-
%>
|
|
6
|
-
|
|
7
|
-
<% if (dataContracts.length) { %>
|
|
8
|
-
import { <%~ dataContracts.join(", ") %> } from "./<%~ config.fileNames.dataContracts %>"
|
|
9
|
-
<% } %>
|
|
10
|
-
|
|
11
|
-
<%
|
|
12
|
-
/* TODO: outOfModule, combined should be attributes of route, which will allow to avoid duplication of code */
|
|
13
|
-
%>
|
|
14
|
-
|
|
15
|
-
<% if (routes.outOfModule) { %>
|
|
16
|
-
<% for (const { routes: outOfModuleRoutes = [] } of routes.outOfModule) { %>
|
|
17
|
-
<% for (const route of outOfModuleRoutes) { %>
|
|
18
|
-
<%~ includeFile('./route-type.ejs', { ...it, route }) %>
|
|
19
|
-
<% } %>
|
|
20
|
-
<% } %>
|
|
21
|
-
<% } %>
|
|
22
|
-
|
|
23
|
-
<% if (routes.combined) { %>
|
|
24
|
-
<% for (const { routes: combinedRoutes = [], moduleName } of routes.combined) { %>
|
|
25
|
-
export namespace <%~ pascalCase(moduleName) %> {
|
|
26
|
-
<% for (const route of combinedRoutes) { %>
|
|
27
|
-
<%~ includeFile('./route-type.ejs', { ...it, route }) %>
|
|
28
|
-
<% } %>
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
<% } %>
|
|
32
|
-
<% } %>
|
|
1
|
+
<%
|
|
2
|
+
const { utils, config, routes, modelTypes } = it;
|
|
3
|
+
const { _, pascalCase } = utils;
|
|
4
|
+
const dataContracts = config.modular ? _.map(modelTypes, "name") : [];
|
|
5
|
+
%>
|
|
6
|
+
|
|
7
|
+
<% if (dataContracts.length) { %>
|
|
8
|
+
import { <%~ dataContracts.join(", ") %> } from "./<%~ config.fileNames.dataContracts %>"
|
|
9
|
+
<% } %>
|
|
10
|
+
|
|
11
|
+
<%
|
|
12
|
+
/* TODO: outOfModule, combined should be attributes of route, which will allow to avoid duplication of code */
|
|
13
|
+
%>
|
|
14
|
+
|
|
15
|
+
<% if (routes.outOfModule) { %>
|
|
16
|
+
<% for (const { routes: outOfModuleRoutes = [] } of routes.outOfModule) { %>
|
|
17
|
+
<% for (const route of outOfModuleRoutes) { %>
|
|
18
|
+
<%~ includeFile('./route-type.ejs', { ...it, route }) %>
|
|
19
|
+
<% } %>
|
|
20
|
+
<% } %>
|
|
21
|
+
<% } %>
|
|
22
|
+
|
|
23
|
+
<% if (routes.combined) { %>
|
|
24
|
+
<% for (const { routes: combinedRoutes = [], moduleName } of routes.combined) { %>
|
|
25
|
+
export namespace <%~ pascalCase(moduleName) %> {
|
|
26
|
+
<% for (const route of combinedRoutes) { %>
|
|
27
|
+
<%~ includeFile('./route-type.ejs', { ...it, route }) %>
|
|
28
|
+
<% } %>
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
<% } %>
|
|
32
|
+
<% } %>
|
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
<%
|
|
2
|
-
const { contract, utils } = it;
|
|
3
|
-
const { formatDescription, require, _ } = utils;
|
|
4
|
-
|
|
5
|
-
%>
|
|
6
|
-
<% if (contract.$content.length) { %>
|
|
7
|
-
export type <%~ contract.name %> = {
|
|
8
|
-
<% for (const field of contract.$content) { %>
|
|
9
|
-
<%~ includeFile('./object-field-jsdoc.ejs', { ...it, field }) %>
|
|
10
|
-
<%~ field.field %>;
|
|
11
|
-
<% } %>
|
|
12
|
-
}<%~ utils.isNeedToAddNull(contract) ? ' | null' : ''%>
|
|
13
|
-
<% } else { %>
|
|
14
|
-
export type <%~ contract.name %> = Record<string, any>;
|
|
15
|
-
<% } %>
|
|
1
|
+
<%
|
|
2
|
+
const { contract, utils } = it;
|
|
3
|
+
const { formatDescription, require, _ } = utils;
|
|
4
|
+
|
|
5
|
+
%>
|
|
6
|
+
<% if (contract.$content.length) { %>
|
|
7
|
+
export type <%~ contract.name %> = {
|
|
8
|
+
<% for (const field of contract.$content) { %>
|
|
9
|
+
<%~ includeFile('./object-field-jsdoc.ejs', { ...it, field }) %>
|
|
10
|
+
<%~ field.field %>;
|
|
11
|
+
<% } %>
|
|
12
|
+
}<%~ utils.isNeedToAddNull(contract) ? ' | null' : ''%>
|
|
13
|
+
<% } else { %>
|
|
14
|
+
export type <%~ contract.name %> = Record<string, any>;
|
|
15
|
+
<% } %>
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fxri/swagger2api",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.8",
|
|
4
4
|
"description": "基于 swagger-typescript-api,将 Swagger 文档自动转为 TypeScript 格式的 API 文件。",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": {
|
|
@@ -32,6 +32,7 @@
|
|
|
32
32
|
"@types/prompts": "^2.4.9",
|
|
33
33
|
"@typescript-eslint/eslint-plugin": "^6.21.0",
|
|
34
34
|
"@typescript-eslint/parser": "^6.21.0",
|
|
35
|
+
"cross-env": "^10.1.0",
|
|
35
36
|
"esbuild-plugin-copy": "^2.1.1",
|
|
36
37
|
"eslint": "^8.57.1",
|
|
37
38
|
"eslint-config-prettier": "^9.1.2",
|
|
@@ -55,8 +56,8 @@
|
|
|
55
56
|
},
|
|
56
57
|
"scripts": {
|
|
57
58
|
"dev": "pnpm build:debug && node ./dist/cli.js",
|
|
58
|
-
"build": "NODE_ENV=production tsup",
|
|
59
|
-
"build:debug": "NODE_ENV=development tsup --config tsup.dev.config.ts",
|
|
59
|
+
"build": "cross-env NODE_ENV=production tsup",
|
|
60
|
+
"build:debug": "cross-env NODE_ENV=development tsup --config tsup.dev.config.ts",
|
|
60
61
|
"release": "pnpm build && pnpm publish",
|
|
61
62
|
"api": "pnpm dev --axios --responses --remove-prefix-index 0 --quick --extract-request-query param --extract-response-raw"
|
|
62
63
|
}
|