Skip to content

Commit

Permalink
feat: axios 导入名称配置优化
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudcome committed Jan 2, 2025
1 parent c5e661a commit a3ba8d1
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 15 deletions.
12 changes: 5 additions & 7 deletions src/printer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ export class Printer {
throw new Error(`当前仅支持 openapi ${OpenAPIVersion.V3_1},当前版本为 ${openapi}`);

this.registerComponents();
this.named.internalVarName(options?.axiosImportName || AXIOS_IMPORT_NAME);
}

schemas: Record<string /** refId */, string /** refType */> = {};
Expand Down Expand Up @@ -188,7 +187,6 @@ export class Printer {
private _printImports() {
const {
axiosImportName = AXIOS_IMPORT_NAME,
axiosNamedImport,
axiosImportFile = AXIOS_IMPORT_FILE,
axiosRequestConfigTypeName = AXIOS_QUEST_CONFIG_TYPE_NAME,
axiosResponseTypeName = AXIOS_PROMISE_TYPE_NAME,
Expand All @@ -197,11 +195,11 @@ export class Printer {
const importPath = toRelative(axiosImportFile, file);

return [
axiosNamedImport
// 具名导入
? `import {${axiosImportName}} from "${importPath}";`
axiosImportName === ''
// 默认导入
: `import ${axiosImportName} from "${importPath}";`,
? `import ${AXIOS_IMPORT_NAME} from "${importPath}";`
// 具名导入
: `import {${axiosImportName} as ${AXIOS_IMPORT_NAME}} from "${importPath}";`,
`import type {${axiosRequestConfigTypeName}, ${axiosResponseTypeName}} from "${importPath}";`,
'',
].join('\n');
Expand Down Expand Up @@ -361,7 +359,7 @@ export class Printer {

return `${jsDoc.print()}
export async function ${funcName}(${requestArgs.toArgs()}): AxiosPromise<${respType}> {
return axios({
return ${AXIOS_IMPORT_NAME}({
method: ${JSON.stringify(method)},
${requestArgs.toValues()}
});
Expand Down
10 changes: 2 additions & 8 deletions src/printer/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,15 @@ export type OperationIdNormalize = (context: OperationContext) => string;

export interface PrinterOptions {
/**
* 导入名称
* 导入名称,为空字符串时默认导入
* @default axios
*/
axiosImportName?: string;

/**
* axios 是否具名导入
* @default false
* @example
* // 具名导入
* import { axios } from 'axios';
* // 默认导入(非具名导入)
* import axios from 'axios';
*/
axiosNamedImport?: boolean;
axiosImportName?: string;

/**
* 指定导入文件
Expand Down
108 changes: 108 additions & 0 deletions test/printer/config.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import { Printer } from '../../src/printer';

it('axios 模块导入名称默认', () => {
const printer = new Printer({
openapi: '3.1.0',
info: {
title: 'test',
version: '1.0.0',
},
paths: {
'/': {
get: {},
},
},
}, {
});
expect(printer.print({
hideInfo: true,
hideHelpers: true,
})).toMatchInlineSnapshot(`
"import {axios as axios} from "axios";
import type {AxiosRequestConfig, AxiosPromise} from "axios";
/**
* @param [config] request config
*/
export async function get_2(config?:AxiosRequestConfig): AxiosPromise<unknown> {
return axios({
method: "get",
url: \`/\`,
...config
});
}"
`);
});

it('axios 模块导入名称指定', () => {
const printer = new Printer({
openapi: '3.1.0',
info: {
title: 'test',
version: '1.0.0',
},
paths: {
'/': {
get: {},
},
},
}, {
axiosImportName: 'axios2',
});
expect(printer.print({
hideInfo: true,
hideHelpers: true,
})).toMatchInlineSnapshot(`
"import {axios2 as axios} from "axios";
import type {AxiosRequestConfig, AxiosPromise} from "axios";
/**
* @param [config] request config
*/
export async function get_2(config?:AxiosRequestConfig): AxiosPromise<unknown> {
return axios({
method: "get",
url: \`/\`,
...config
});
}"
`);
});

it('axios 默认导入名称为空', () => {
const printer = new Printer({
openapi: '3.1.0',
info: {
title: 'test',
version: '1.0.0',
},
paths: {
'/': {
get: {},
},
},
}, {
axiosImportName: '',
});
expect(printer.print({
hideInfo: true,
hideHelpers: true,
})).toMatchInlineSnapshot(`
"import axios from "axios";
import type {AxiosRequestConfig, AxiosPromise} from "axios";
/**
* @param [config] request config
*/
export async function get_2(config?:AxiosRequestConfig): AxiosPromise<unknown> {
return axios({
method: "get",
url: \`/\`,
...config
});
}"
`);
});

0 comments on commit a3ba8d1

Please sign in to comment.