Skip to content

Commit 710e968

Browse files
committed
feat: axiosImport 修改为 axiosImportPath
1 parent 157eaef commit 710e968

File tree

9 files changed

+47
-41
lines changed

9 files changed

+47
-41
lines changed

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,16 @@
2525
"require": "./dist/index.cjs"
2626
},
2727
"./package.json": "./package.json",
28-
"./helpers": {
29-
"import": "./dist/helpers.mjs",
30-
"require": "./dist/helpers.cjs"
28+
"./client": {
29+
"import": "./dist/client.mjs",
30+
"require": "./dist/client.cjs"
3131
}
3232
},
3333
"types": "./dist/index.d.ts",
3434
"typesVersions": {
3535
"*": {
36-
"helpers": [
37-
"./dist/helpers.d.ts"
36+
"client": [
37+
"./dist/client.d.ts"
3838
]
3939
}
4040
},
File renamed without changes.

src/parsers/const.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ export const HTTP_METHODS: OpenAPIV3.HttpMethods[] = [
1414
export const JSON_MIME = 'application/json';
1515
export const BLOB_MIME = 'application/octet-stream';
1616

17+
export const AXIOS_QUEST_CONFIG_TYPE_NAME = 'AxiosRequestConfig';
18+
export const AXIOS_PROMISE_TYPE_NAME = 'AxiosPromise';
19+
1720
// 内部名称,文档里如果重复了会生成新的唯一值
1821
export const INTERNAL_NAMES = [
1922
// native
@@ -22,7 +25,6 @@ export const INTERNAL_NAMES = [
2225
'OneOf',
2326
// @ref PathsWriter
2427
'axios',
25-
'request',
2628
'DELETE',
2729
'GET',
2830
'HEAD',
@@ -33,8 +35,8 @@ export const INTERNAL_NAMES = [
3335
'TRACE',
3436
'resolveURL',
3537
'BASE_URL',
36-
'AxiosPromise',
37-
'AxiosRequestConfig',
38+
AXIOS_QUEST_CONFIG_TYPE_NAME,
39+
AXIOS_PROMISE_TYPE_NAME,
3840
];
3941

4042
// @ref https://www.w3schools.com/js/js_reserved.asp

src/printers/BasePrinter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import type { StrictPrinterOptions, PrinterOptions } from './types';
44

55
export class BasePrinter {
66
static defaults: StrictPrinterOptions = {
7-
axiosImport: `import axios from 'axios';`,
7+
axiosImportPath: 'axios',
88
requestPathArgName: 'path',
99
requestQueryArgName: 'params',
1010
requestBodyArgName: 'data',
11-
responseTypeName: 'AxiosPromise',
11+
requestConfigArgName: 'config',
1212
};
1313

1414
options: StrictPrinterOptions;

src/printers/ComponentsPrinter.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { pkgName } from '../const';
12
import type { TypeItem, TypeOrigin } from '../parsers/types';
23
import { joinSlices, toTypePath } from '../utils/string';
34
import { BasePrinter } from './BasePrinter';
@@ -6,7 +7,7 @@ import { CommentsPrinter } from './CommentsPrinter';
67
export class ComponentsPrinter extends CommentsPrinter {
78
protected init() {
89
super.init();
9-
this.imports.push('import type { OneOf } from "openapi-axios/helpers"');
10+
this.imports.push(`import type { OneOf } from "${pkgName}/client"`);
1011
}
1112

1213
printComponents() {

src/printers/PathsPrinter.ts

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import type { AxiosRequestConfig } from 'axios';
22
import { groupBy } from 'lodash';
3+
import { pkgName } from '../const';
4+
import { AXIOS_PROMISE_TYPE_NAME, AXIOS_QUEST_CONFIG_TYPE_NAME } from '../parsers/const';
35
import type { TypeItem, TypeList, TypeOperation, TypeOperations, TypeOrigin } from '../parsers/types';
46
import { joinSlices, nextUniqueName, varString } from '../utils/string';
57
import { isBoolean, isString } from '../utils/type-is';
@@ -10,10 +12,9 @@ const { stringify } = JSON;
1012
export class PathsPrinter extends ComponentsPrinter {
1113
protected init() {
1214
super.init();
13-
this.imports.push('import type { AxiosPromise, AxiosRequestConfig } from "axios";');
14-
this.imports.push('import { DELETE, GET, HEAD, OPTIONS, PATCH, POST, PUT, resolveURL } from "openapi-axios/helpers";');
15-
this.imports.push(this.options.axiosImport);
16-
this.helpers.push(`const request = axios.request;`);
15+
this.imports.push(`import { DELETE, GET, HEAD, OPTIONS, PATCH, POST, PUT, resolveURL } from "${pkgName}/client";`);
16+
this.imports.push(`import type {AxiosRequestConfig, AxiosPromise} from "${this.options.axiosImportPath}";`);
17+
this.imports.push(`import axios from "${this.options.axiosImportPath}";`);
1718
if (this.document.info.baseURL) this.helpers.push(`const BASE_URL = ${stringify(this.document.info.baseURL)};`);
1819
}
1920

@@ -40,32 +41,30 @@ export class PathsPrinter extends ComponentsPrinter {
4041
request: { path, query, body: reqBody },
4142
response: { body: resBody },
4243
} = type;
43-
const { responseTypeName } = this.options;
4444
const argNameCountMap = new Map<string, number>();
4545
const requestPathArgName = nextUniqueName(this.options.requestPathArgName, argNameCountMap);
4646
const requestQueryArgName = nextUniqueName(this.options.requestQueryArgName, argNameCountMap);
4747
const requestBodyArgName = nextUniqueName(this.options.requestBodyArgName, argNameCountMap);
48-
const configArgName = nextUniqueName('config', argNameCountMap);
48+
const requestConfigArgName = nextUniqueName(this.options.requestConfigArgName, argNameCountMap);
4949
const comments = this.printComments(type, true);
50-
const argsGroup = groupBy(
51-
[
52-
this.printArg(requestPathArgName, path),
53-
this.printArg(requestQueryArgName, query),
54-
this.printArg(requestBodyArgName, reqBody),
55-
this.printArg(configArgName, 'AxiosRequestConfig', false),
56-
],
57-
(item) => item?.required,
58-
);
59-
const args_ = joinSlices(
50+
const args = [
51+
//
52+
this.printArg(requestPathArgName, path),
53+
this.printArg(requestQueryArgName, query),
54+
this.printArg(requestBodyArgName, reqBody),
55+
this.printArg(requestConfigArgName, AXIOS_QUEST_CONFIG_TYPE_NAME, false),
56+
];
57+
const argsGroup = groupBy(args, (item) => item?.required);
58+
const argStr = joinSlices(
6059
[
6160
// 可能没有任何必填参数
6261
...(argsGroup['true'] || []),
63-
// 至少有一个 config 可选参数
64-
...argsGroup['false'],
62+
// 可能没有任何可选参数
63+
...(argsGroup['false'] || []),
6564
].map((desc) => desc?.text),
6665
', ',
6766
);
68-
const return_ = `${responseTypeName}<${resBody?.name || 'never'}>`;
67+
const return_ = `${AXIOS_PROMISE_TYPE_NAME}<${resBody?.name || 'never'}>`;
6968
const url_ = this.printAxiosProp('url', this.toURL(type, requestPathArgName));
7069
const method_ = this.printAxiosProp('method', type.method.toUpperCase());
7170
const params_ = this.printAxiosProp('params', query ? requestQueryArgName : '');
@@ -76,11 +75,11 @@ export class PathsPrinter extends ComponentsPrinter {
7675
method_,
7776
params_,
7877
data_,
79-
`...${configArgName}`,
78+
`...${requestConfigArgName}`,
8079
]);
8180

82-
return `${comments}export async function ${name}(${args_}): ${return_} {
83-
return request({
81+
return `${comments}export async function ${name}(${argStr}): ${return_} {
82+
return axios({
8483
${props}
8584
});
8685
}`;

src/printers/types.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
export interface PrinterOptions {
22
/**
3-
* axios 导入,必须有名称 axios,且 axios.request 是一个请求方法
4-
* @default import axios from 'axios';
3+
* axios 导入,必须有 axios 默认方法和 AxiosRequestConfig、AxiosPromise 两个类型
4+
* 可以修改为当前工程的 axios 实例,或其他类 axios 方法,类型导出不要忘记了
5+
* @example
6+
* import type {AxiosRequestConfig, AxiosPromise} from 'axios';
7+
* import axios from 'axios';
8+
* @default axios
59
*/
6-
axiosImport?: string;
10+
axiosImportPath?: string;
711

812
/**
913
* 请求路径的参数名称
@@ -24,9 +28,9 @@ export interface PrinterOptions {
2428
requestBodyArgName?: string;
2529

2630
/**
27-
* 自定义响应包装类型
28-
* @default AxiosPromise
31+
* 请求配置的参数名称
32+
* @default config
2933
*/
30-
responseTypeName?: 'Promise' | 'AxiosPromise' | string;
34+
requestConfigArgName?: string;
3135
}
3236
export type StrictPrinterOptions = Required<PrinterOptions>;

test/helpers.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { resolveURL } from '../src/helpers';
1+
import { resolveURL } from '../src/client';
22

33
test('resolveBaseURL', () => {
44
expect(resolveURL('/', '/a/b')).toEqual('/a/b');

vite.config.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export default defineConfig({
2828
lib: {
2929
entry: {
3030
index: 'src/index.ts',
31-
helpers: 'src/helpers.ts',
31+
client: 'src/client.ts',
3232
},
3333
},
3434
rollupOptions: {

0 commit comments

Comments
 (0)