feima-shortcuts 1.0.11 → 1.1.0

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.11",
3
+ "version": "1.1.0",
4
4
  "description": "快捷指令",
5
5
  "main": "index.js",
6
6
  "directories": {
@@ -2,9 +2,8 @@ const { Command } = require("commander");
2
2
  const { spawn } = require("child_process");
3
3
  const fs = require("fs");
4
4
  const path = require("path");
5
- const { deleteFolder } = require("../utils/deleteFolder");
6
- const { makeDir } = require("../utils/makeDir");
7
- const requestTemplate = require("./requestTemplate");
5
+
6
+ const getTemplate = require("./orval.config.ts.template");
8
7
 
9
8
  exports.run = () => {
10
9
  const program = new Command();
@@ -14,59 +13,44 @@ exports.run = () => {
14
13
  .description("从 Swagger/OpenAPI 文档地址生成 API 元数据")
15
14
  .option(
16
15
  "-i, --input <url>",
17
- "Swagger/OpenAPI 文档地址,例如 http://192.168.0.74:9999/app/v3/api-docs"
18
- )
19
- .option(
20
- "-o, --output <dir>",
21
- "输出目录,默认 src/api",
22
- "src/api"
23
- )
24
- .option(
25
- "-bu, --base-url <dir>",
26
- "基础URL,默认 空",
27
- ""
16
+ "Swagger/OpenAPI 文档地址,例如 http://192.168.0.74:9999/admin/v3/api-docs"
28
17
  )
18
+ .option("-o, --output <dir>", "输出目录,默认 src/api")
19
+ .option("-bu, --base-url <dir>", "基础URL,默认 空")
29
20
  .allowUnknownOption(true) // 保持向后兼容,忽略未知参数
30
21
  .action(async (opts) => {
31
- const input = opts?.input || "http://192.168.0.74:9999/app/v3/api-docs";
32
- const output = opts?.output || "src/api";
22
+ const input = opts?.input || "";
23
+ const output = opts?.output || "";
33
24
  const baseUrl = opts?.baseUrl || "";
34
- console.log(`📥 generate-api 输入地址: ${input}`);
35
- console.log(`📦 输出目录: ${output}`);
36
-
37
-
38
- // 生成前清空并重建输出目录,避免遗留文件
39
- try {
40
- const fullOutputDir = path.resolve(process.cwd(), output);
41
- if (fs.existsSync(fullOutputDir)) {
42
- deleteFolder(fullOutputDir);
43
- }
44
- makeDir(fullOutputDir);
45
- console.log("🧹 已清空并初始化输出目录");
46
- } catch (e) {
47
- console.error("❌ 初始化输出目录失败:", e.message);
48
- process.exit(1);
25
+ if (!input) {
26
+ console.error("请输入 Swagger/OpenAPI 文档地址");
27
+ return;
28
+ }
29
+ if (!output) {
30
+ console.error("请输入输出目录");
31
+ return;
32
+ }
33
+ if (!baseUrl) {
34
+ console.error("请输入基础URL");
35
+ return;
49
36
  }
50
37
 
51
- // 确保 ./tmpRequest.ts 使用内置的 requestStr(放在项目根目录)
52
- const requestFile = path.resolve(process.cwd(), "tmpRequest.ts");
38
+ // 确保 ./orvalConfig.ts 使用内置的 requestStr(放在项目根目录)
39
+ const orvalConfig = path.resolve(process.cwd(), "orval.config.ts");
53
40
  try {
54
- fs.writeFileSync(requestFile, requestTemplate, "utf8");
55
- console.log("📝 已写入 ./tmpRequest.ts");
41
+ fs.writeFileSync(
42
+ orvalConfig,
43
+ getTemplate(input, output, baseUrl),
44
+ "utf8"
45
+ );
46
+ console.log("📝 已写入 ./orvalConfig.ts");
56
47
  } catch (e) {
57
- console.error("❌ 写入 ./tmpRequest.ts 失败:", e.message);
48
+ console.error("❌ 写入 ./orvalConfig.ts 失败:", e.message);
58
49
  process.exit(1);
59
50
  }
60
- // 在当前执行目录生成到指定 output,并指定自定义 request 实现
61
- const args = [
62
- "openapi-typescript-codegen",
63
- "--input",
64
- input,
65
- "--output",
66
- output,
67
- "--request",
68
- "./tmpRequest.ts",
69
- ];
51
+
52
+ // 在当前执行目录生成API代码
53
+ const args = ["orval"];
70
54
 
71
55
  const child = spawn("npx", ["--yes", ...args], {
72
56
  stdio: "inherit",
@@ -74,11 +58,11 @@ exports.run = () => {
74
58
  shell: false,
75
59
  });
76
60
 
77
- const cleanupTempRequest = () => {
61
+ const cleanupOrvalConfig = () => {
78
62
  try {
79
- if (fs.existsSync(requestFile)) {
80
- fs.unlinkSync(requestFile);
81
- console.log("🧹 已清理临时文件 ./tmpRequest.ts");
63
+ if (fs.existsSync(orvalConfig)) {
64
+ fs.unlinkSync(orvalConfig);
65
+ console.log("🧹 已清理临时文件 ./orval.config.ts");
82
66
  }
83
67
  } catch (e) {
84
68
  console.warn("⚠️ 清理临时文件失败:", e.message);
@@ -86,7 +70,7 @@ exports.run = () => {
86
70
  };
87
71
 
88
72
  child.on("close", (code) => {
89
- cleanupTempRequest();
73
+ cleanupOrvalConfig();
90
74
  if (code === 0) {
91
75
  console.log(`✅ OpenAPI 代码已生成到 ${output}`);
92
76
  process.exit(0);
@@ -96,5 +80,6 @@ exports.run = () => {
96
80
  }
97
81
  });
98
82
  });
83
+
99
84
  program.parse(process.argv);
100
85
  };
@@ -0,0 +1,26 @@
1
+ const getTemplate = (input, output, baseUrl) => {
2
+ return `export default {
3
+ admin: {
4
+ input: "${input}",
5
+ output: {
6
+ mode: "tags-split",
7
+ target: "${output}",
8
+ schemas: "${output}/model",
9
+ baseUrl: "${baseUrl}",
10
+ client: "axios-functions",
11
+ clean: true,
12
+ override: {
13
+ mutator: {
14
+ path: "src/utils/request.ts",
15
+ name: "request",
16
+ },
17
+ },
18
+ },
19
+ hooks: {
20
+ afterAllFilesWrite: "npx prettier --write",
21
+ },
22
+ },
23
+ };`
24
+ }
25
+
26
+ module.exports = getTemplate;
@@ -1,371 +0,0 @@
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;
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
- if (!result.ok) {
298
- const errorStatus = result.status ?? "unknown";
299
- const errorStatusText = result.statusText ?? "unknown";
300
- const errorBody = (() => {
301
- try {
302
- return JSON.stringify(result.body, null, 2);
303
- } catch (e) {
304
- return undefined;
305
- }
306
- })();
307
-
308
- throw new ApiError(
309
- options,
310
- result,
311
- \`Generic Error: status: \${errorStatus}; status text: \${errorStatusText}; body: \${errorBody}\`
312
- );
313
- }
314
- };
315
-
316
- /**
317
- * Request method
318
- * @param config The OpenAPI configuration object
319
- * @param options The request options from the service
320
- * @param axiosClient The axios client instance to use
321
- * @returns CancelablePromise<T>
322
- * @throws ApiError
323
- */
324
- export const request = <T>(
325
- config: OpenAPIConfig,
326
- options: ApiRequestOptions,
327
- axiosClient: AxiosInstance = service
328
- ): CancelablePromise<T> => {
329
- return new CancelablePromise(async (resolve, reject, onCancel) => {
330
- try {
331
- const url = getUrl(config, options);
332
- const formData = getFormData(options);
333
- const body = getRequestBody(options);
334
- const headers = await getHeaders(config, options, formData);
335
-
336
- if (!onCancel.isCancelled) {
337
- const response: any = await sendRequest<T>(
338
- config,
339
- options,
340
- url,
341
- body,
342
- formData,
343
- headers,
344
- onCancel,
345
- axiosClient
346
- );
347
- const responseBody = getResponseBody(response);
348
- const responseHeader = getResponseHeader(
349
- response,
350
- options.responseHeader
351
- );
352
- const result: ApiResult = {
353
- url,
354
- ok: response.ok,
355
- status: response.code,
356
- statusText: response.msg,
357
- body: responseHeader ?? responseBody,
358
- };
359
-
360
- catchErrorCodes(options, result);
361
-
362
- resolve(result.body);
363
- }
364
- } catch (error) {
365
- reject(error);
366
- }
367
- });
368
- };
369
- `
370
-
371
- module.exports = template;