feima-shortcuts 1.0.8 → 1.0.9

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "feima-shortcuts",
3
- "version": "1.0.8",
3
+ "version": "1.0.9",
4
4
  "description": "快捷指令",
5
5
  "main": "index.js",
6
6
  "directories": {
@@ -4,169 +4,7 @@ const fs = require("fs");
4
4
  const path = require("path");
5
5
  const { deleteFolder } = require("../utils/deleteFolder");
6
6
  const { makeDir } = require("../utils/makeDir");
7
-
8
-
9
- const requestStrFun = () => {
10
- return `/**
11
- * 通过 feima generate-api 命令生成
12
- * 请勿手动修改此文件
13
- */
14
- import axios from "axios";
15
- import type { AxiosInstance, AxiosResponse, InternalAxiosRequestConfig } from "axios";
16
- import { ElMessage } from "element-plus";
17
- import qs from "qs";
18
- import { useGlobalStore } from "@/store"; // 路径根据实际情况调整
19
- import { wrapEncryption, encryptRequestParams, decrypt } from "@/utils/crypto";
20
- import other from "@/utils/other";
21
-
22
- import type { ApiRequestOptions } from "./ApiRequestOptions";
23
- import { CancelablePromise } from "./CancelablePromise";
24
- import type { OpenAPIConfig } from "./OpenAPI";
25
-
26
- // 常用header
27
- export enum CommonHeaderEnum {
28
- "TENANT_ID" = "TENANT-ID",
29
- "ENC_FLAG" = "Enc-Flag",
30
- "AUTHORIZATION" = "Authorization",
31
- "VERSION" = "VERSION",
32
- }
33
-
34
- const service: AxiosInstance = axios.create({
35
- baseURL: \`\${import.meta.env.VITE_API_URL}\`,
36
- timeout: 50000, // 全局超时时间
37
- paramsSerializer: {
38
- serialize: (params: any) => {
39
- return qs.stringify(params, { arrayFormat: "repeat" });
40
- },
41
- },
42
- });
43
-
44
- /**
45
- * Axios请求拦截器,对请求进行处理
46
- * 1. 序列化get请求参数
47
- * 2. 统一增加Authorization和TENANT-ID请求头
48
- * 3. 自动适配单体、微服务架构不同的URL
49
- * @param config AxiosRequestConfig对象,包含请求配置信息
50
- */
51
- service.interceptors.request.use(
52
- (config: InternalAxiosRequestConfig) => {
53
- // 统一增加Authorization请求头, skipToken 跳过增加token
54
- const token = localStorage.getItem("token");
55
- if (token && !config.headers?.skipToken) {
56
- config.headers![CommonHeaderEnum.AUTHORIZATION] = \`Bearer \${token}\`;
57
- }
58
-
59
- // 统一增加TENANT-ID请求头, skipTenant 跳过增加租户ID
60
- const tenantId = localStorage.getItem("tenantId");
61
- if (tenantId && !config.headers?.skipTenant) {
62
- config.headers![CommonHeaderEnum.TENANT_ID] = tenantId;
63
- }
64
-
65
- // 增加 gray_version 请求头
66
- const version = import.meta.env.VITE_GRAY_VERSION;
67
- if (version) {
68
- config.headers![CommonHeaderEnum.VERSION] = version;
69
- }
70
-
71
- // 请求报文加密 ,如果请求头中不包含 ENC_FLAG : false 则加密
72
- if (config.data && !config.headers![CommonHeaderEnum.ENC_FLAG]) {
73
- config.data = wrapEncryption(config.data);
74
- }
75
-
76
- // 如果是 GET ,加密 config.param 的每一个参数,并URLencode
77
- if (config.method === "get" && config.params) {
78
- config.params = encryptRequestParams(config.params);
79
- }
80
-
81
- // 自动适配单体和微服务架构不同的URL
82
- config.url = other.adaptationUrl(config.url);
83
-
84
- // 处理完毕,返回config对象
85
- return config;
86
- },
87
- (error: any) => {
88
- // 对请求错误进行处理
89
- return Promise.reject(error);
90
- }
91
- );
92
-
93
- /**
94
- * 响应拦截器处理函数
95
- * @param response 响应结果
96
- * @returns 如果响应成功,则返回响应的data属性;否则,抛出错误或者执行其他操作
97
- */
98
- const handleResponse = (response: AxiosResponse<any>) => {
99
- if (response.data.code === 1) {
100
- throw response.data;
101
- }
102
-
103
- // 针对密文返回解密
104
- if (response.data.encryption) {
105
- response.data = decrypt(response.data.encryption);
106
- return response.data;
107
- }
108
-
109
- return response.data;
110
- };
111
-
112
- /**
113
- * 添加 Axios 的响应拦截器,用于全局响应结果处理
114
- */
115
- service.interceptors.response.use(handleResponse, (error: any) => {
116
- const status = Number(error.response.status) || 200;
117
-
118
- if (status === 424) {
119
- const { logout } = useGlobalStore();
120
- logout();
121
- ElMessage.error("令牌状态已过期,请点击重新登录");
122
- window.location.replace(\`\${import.meta.env.BASE_URL}login\`);
123
- }
124
-
125
- if (status === 426) {
126
- const { logout } = useGlobalStore();
127
- logout();
128
- ElMessage.error("租户状态已过期,请联系管理员");
129
- window.location.replace(\`\${import.meta.env.BASE_URL}login\`);
130
- }
131
-
132
- // 针对密文返回解密
133
- if (error.response?.data?.encryption) {
134
- error.response.data = decrypt(error.response?.data.encryption);
135
- }
136
-
137
- return Promise.reject(error.response.data);
138
- });
139
-
140
- export const request = <T>(
141
- config: OpenAPIConfig,
142
- options: ApiRequestOptions
143
- ): CancelablePromise<T> => {
144
- const { query, ...rest } = options;
145
-
146
- return new CancelablePromise((resolve, reject, onCancel) => {
147
- const url = \`\${config.BASE}\${rest.url}\`;
148
-
149
- const source = axios.CancelToken.source();
150
-
151
- onCancel(() => source.cancel("The user aborted a request."));
152
-
153
- service
154
- .request({
155
- ...rest,
156
- params: query,
157
- url,
158
- data: rest?.body,
159
- cancelToken: source.token,
160
- })
161
- .then((data: any) => {
162
- resolve(data);
163
- })
164
- .catch((error: any) => {
165
- reject(error);
166
- });
167
- });
168
- };`;
169
- };
7
+ const requestTemplate = require("./requestTemplate");
170
8
 
171
9
 
172
10
  exports.run = () => {
@@ -213,8 +51,9 @@ exports.run = () => {
213
51
 
214
52
  // 确保 ./tmpRequest.ts 使用内置的 requestStr(放在项目根目录)
215
53
  const requestFile = path.resolve(process.cwd(), "tmpRequest.ts");
54
+
216
55
  try {
217
- fs.writeFileSync(requestFile, requestStrFun(), "utf8");
56
+ fs.writeFileSync(requestFile, requestTemplate, "utf8");
218
57
  console.log("📝 已写入 ./tmpRequest.ts");
219
58
  } catch (e) {
220
59
  console.error("❌ 写入 ./tmpRequest.ts 失败:", e.message);
@@ -0,0 +1,370 @@
1
+ const template = `/* generated using openapi-typescript-codegen -- do not edit */
2
+ /* istanbul ignore file */
3
+ /* tslint:disable */
4
+ /* eslint-disable */
5
+ import axios from "axios";
6
+ import service from "@/utils/request";
7
+ import type {
8
+ AxiosError,
9
+ AxiosRequestConfig,
10
+ AxiosResponse,
11
+ AxiosInstance,
12
+ } from "axios";
13
+ import FormData from "form-data";
14
+
15
+ import { ApiError } from "./ApiError";
16
+ import type { ApiRequestOptions } from "./ApiRequestOptions";
17
+ import type { ApiResult } from "./ApiResult";
18
+ import { CancelablePromise } from "./CancelablePromise";
19
+ import type { OnCancel } from "./CancelablePromise";
20
+ import type { OpenAPIConfig } from "./OpenAPI";
21
+
22
+ export const isDefined = <T>(
23
+ value: T | null | undefined
24
+ ): value is Exclude<T, null | undefined> => {
25
+ return value !== undefined && value !== null;
26
+ };
27
+
28
+ export const isString = (value: any): value is string => {
29
+ return typeof value === "string";
30
+ };
31
+
32
+ export const isStringWithValue = (value: any): value is string => {
33
+ return isString(value) && value !== "";
34
+ };
35
+
36
+ export const isBlob = (value: any): value is Blob => {
37
+ return (
38
+ typeof value === "object" &&
39
+ typeof value.type === "string" &&
40
+ typeof value.stream === "function" &&
41
+ typeof value.arrayBuffer === "function" &&
42
+ typeof value.constructor === "function" &&
43
+ typeof value.constructor.name === "string" &&
44
+ /^(Blob|File)$/.test(value.constructor.name) &&
45
+ /^(Blob|File)$/.test(value[Symbol.toStringTag])
46
+ );
47
+ };
48
+
49
+ export const isFormData = (value: any): value is FormData => {
50
+ return value instanceof FormData;
51
+ };
52
+
53
+ export const isSuccess = (status: number): boolean => {
54
+ return status >= 200 && status < 300;
55
+ };
56
+
57
+ export const base64 = (str: string): string => {
58
+ try {
59
+ return btoa(str);
60
+ } catch (err) {
61
+ // @ts-ignore
62
+ return Buffer.from(str).toString("base64");
63
+ }
64
+ };
65
+
66
+ export const getQueryString = (params: Record<string, any>): string => {
67
+ const qs: string[] = [];
68
+
69
+ const append = (key: string, value: any) => {
70
+ qs.push(\`\${encodeURIComponent(key)}=\${encodeURIComponent(String(value))}\`);
71
+ };
72
+
73
+ const process = (key: string, value: any) => {
74
+ if (isDefined(value)) {
75
+ if (Array.isArray(value)) {
76
+ value.forEach((v) => {
77
+ process(key, v);
78
+ });
79
+ } else if (typeof value === "object") {
80
+ Object.entries(value).forEach(([k, v]) => {
81
+ process(\`\${key}[\${k}]\`, v);
82
+ });
83
+ } else {
84
+ append(key, value);
85
+ }
86
+ }
87
+ };
88
+
89
+ Object.entries(params).forEach(([key, value]) => {
90
+ process(key, value);
91
+ });
92
+
93
+ if (qs.length > 0) {
94
+ return \`?\${qs.join("&")}\`;
95
+ }
96
+
97
+ return "";
98
+ };
99
+
100
+ const getUrl = (config: OpenAPIConfig, options: ApiRequestOptions): string => {
101
+ const encoder = config.ENCODE_PATH || encodeURI;
102
+
103
+ const path = options.url
104
+ .replace("{api-version}", config.VERSION)
105
+ .replace(/{(.*?)}/g, (substring: string, group: string) => {
106
+ if (options.path?.hasOwnProperty(group)) {
107
+ return encoder(String(options.path[group]));
108
+ }
109
+ return substring;
110
+ });
111
+
112
+ const url = \`\${config.BASE}\${path}\`;
113
+ if (options.query) {
114
+ return \`\${url}\${getQueryString(options.query)}\`;
115
+ }
116
+ return url;
117
+ };
118
+
119
+ export const getFormData = (
120
+ options: ApiRequestOptions
121
+ ): FormData | undefined => {
122
+ if (options.formData) {
123
+ const formData = new FormData();
124
+
125
+ const process = (key: string, value: any) => {
126
+ if (isString(value) || isBlob(value)) {
127
+ formData.append(key, value);
128
+ } else {
129
+ formData.append(key, JSON.stringify(value));
130
+ }
131
+ };
132
+
133
+ Object.entries(options.formData)
134
+ .filter(([_, value]) => isDefined(value))
135
+ .forEach(([key, value]) => {
136
+ if (Array.isArray(value)) {
137
+ value.forEach((v) => process(key, v));
138
+ } else {
139
+ process(key, value);
140
+ }
141
+ });
142
+
143
+ return formData;
144
+ }
145
+ return undefined;
146
+ };
147
+
148
+ type Resolver<T> = (options: ApiRequestOptions) => Promise<T>;
149
+
150
+ export const resolve = async <T>(
151
+ options: ApiRequestOptions,
152
+ resolver?: T | Resolver<T>
153
+ ): Promise<T | undefined> => {
154
+ if (typeof resolver === "function") {
155
+ return (resolver as Resolver<T>)(options);
156
+ }
157
+ return resolver;
158
+ };
159
+
160
+ export const getHeaders = async (
161
+ config: OpenAPIConfig,
162
+ options: ApiRequestOptions,
163
+ formData?: FormData
164
+ ): Promise<Record<string, string>> => {
165
+ const [token, username, password, additionalHeaders] = await Promise.all([
166
+ resolve(options, config.TOKEN),
167
+ resolve(options, config.USERNAME),
168
+ resolve(options, config.PASSWORD),
169
+ resolve(options, config.HEADERS),
170
+ ]);
171
+
172
+ const formHeaders =
173
+ (typeof formData?.getHeaders === "function" && formData?.getHeaders()) ||
174
+ {};
175
+
176
+ const headers = Object.entries({
177
+ Accept: "application/json",
178
+ ...additionalHeaders,
179
+ ...options.headers,
180
+ ...formHeaders,
181
+ })
182
+ .filter(([_, value]) => isDefined(value))
183
+ .reduce(
184
+ (headers, [key, value]) => ({
185
+ ...headers,
186
+ [key]: String(value),
187
+ }),
188
+ {} as Record<string, string>
189
+ );
190
+
191
+ if (isStringWithValue(token)) {
192
+ headers["Authorization"] = \`Bearer \${token}\`;
193
+ }
194
+
195
+ if (isStringWithValue(username) && isStringWithValue(password)) {
196
+ const credentials = base64(\`\${username}:\${password}\`);
197
+ headers["Authorization"] = \`Basic \${credentials}\`;
198
+ }
199
+
200
+ if (options.body !== undefined) {
201
+ if (options.mediaType) {
202
+ headers["Content-Type"] = options.mediaType;
203
+ } else if (isBlob(options.body)) {
204
+ headers["Content-Type"] = options.body.type || "application/octet-stream";
205
+ } else if (isString(options.body)) {
206
+ headers["Content-Type"] = "text/plain";
207
+ } else if (!isFormData(options.body)) {
208
+ headers["Content-Type"] = "application/json";
209
+ }
210
+ }
211
+
212
+ return headers;
213
+ };
214
+
215
+ export const getRequestBody = (options: ApiRequestOptions): any => {
216
+ if (options.body) {
217
+ return options.body;
218
+ }
219
+ return undefined;
220
+ };
221
+
222
+ export const sendRequest = async <T>(
223
+ config: OpenAPIConfig,
224
+ options: ApiRequestOptions,
225
+ url: string,
226
+ body: any,
227
+ formData: FormData | undefined,
228
+ headers: Record<string, string>,
229
+ onCancel: OnCancel,
230
+ axiosClient: AxiosInstance
231
+ ): Promise<AxiosResponse<T>> => {
232
+ const source = axios.CancelToken.source();
233
+
234
+ const requestConfig: AxiosRequestConfig = {
235
+ url,
236
+ headers,
237
+ data: body ?? formData,
238
+ method: options.method,
239
+ withCredentials: config.WITH_CREDENTIALS,
240
+ withXSRFToken:
241
+ config.CREDENTIALS === "include" ? config.WITH_CREDENTIALS : false,
242
+ cancelToken: source.token,
243
+ };
244
+
245
+ onCancel(() => source.cancel("The user aborted a request."));
246
+
247
+ try {
248
+ return await axiosClient.request(requestConfig);
249
+ } catch (error) {
250
+ const axiosError = error as AxiosError<T>;
251
+ if (axiosError.response) {
252
+ return axiosError.response;
253
+ }
254
+ throw error;
255
+ }
256
+ };
257
+
258
+ export const getResponseHeader = (
259
+ response: AxiosResponse<any>,
260
+ responseHeader?: string
261
+ ): string | undefined => {
262
+ if (responseHeader) {
263
+ const content = response.headers[responseHeader];
264
+ if (isString(content)) {
265
+ return content;
266
+ }
267
+ }
268
+ return undefined;
269
+ };
270
+
271
+ export const getResponseBody = (response: AxiosResponse<any>): any => {
272
+ if (response.status !== 204) {
273
+ return response.data;
274
+ }
275
+ return undefined;
276
+ };
277
+
278
+ export const catchErrorCodes = (
279
+ options: ApiRequestOptions,
280
+ result: ApiResult
281
+ ): void => {
282
+ const errors: Record<number, string> = {
283
+ 400: "Bad Request",
284
+ 401: "Unauthorized",
285
+ 403: "Forbidden",
286
+ 404: "Not Found",
287
+ 500: "Internal Server Error",
288
+ 502: "Bad Gateway",
289
+ 503: "Service Unavailable",
290
+ ...options.errors,
291
+ };
292
+
293
+ const error = errors[result.status];
294
+ if (error) {
295
+ throw new ApiError(options, result, error);
296
+ }
297
+
298
+ if (!result.ok) {
299
+ const errorStatus = result.status ?? "unknown";
300
+ const errorStatusText = result.statusText ?? "unknown";
301
+ const errorBody = (() => {
302
+ try {
303
+ return JSON.stringify(result.body, null, 2);
304
+ } catch (e) {
305
+ return undefined;
306
+ }
307
+ })();
308
+
309
+ throw new ApiError(
310
+ options,
311
+ result,
312
+ \`Generic Error: status: \${errorStatus}; status text: \${errorStatusText}; body: \${errorBody}\`
313
+ );
314
+ }
315
+ };
316
+
317
+ /**
318
+ * Request method
319
+ * @param config The OpenAPI configuration object
320
+ * @param options The request options from the service
321
+ * @param axiosClient The axios client instance to use
322
+ * @returns CancelablePromise<T>
323
+ * @throws ApiError
324
+ */
325
+ export const request = <T>(
326
+ config: OpenAPIConfig,
327
+ options: ApiRequestOptions,
328
+ axiosClient: AxiosInstance = service
329
+ ): CancelablePromise<T> => {
330
+ return new CancelablePromise(async (resolve, reject, onCancel) => {
331
+ try {
332
+ const url = getUrl(config, options);
333
+ const formData = getFormData(options);
334
+ const body = getRequestBody(options);
335
+ const headers = await getHeaders(config, options, formData);
336
+
337
+ if (!onCancel.isCancelled) {
338
+ const response = await sendRequest<T>(
339
+ config,
340
+ options,
341
+ url,
342
+ body,
343
+ formData,
344
+ headers,
345
+ onCancel,
346
+ axiosClient
347
+ );
348
+ const responseBody = getResponseBody(response);
349
+ const responseHeader = getResponseHeader(
350
+ response,
351
+ options.responseHeader
352
+ );
353
+
354
+ const result: ApiResult = {
355
+ url,
356
+ ok: isSuccess(response.status),
357
+ status: response.status,
358
+ statusText: response.statusText,
359
+ body: responseHeader ?? responseBody,
360
+ };
361
+
362
+ catchErrorCodes(options, result);
363
+
364
+ resolve(result.body);
365
+ }
366
+ } catch (error) {
367
+ reject(error);
368
+ }
369
+ });
370
+ };`