Skip to content

Commit

Permalink
feat: 去除不必要的配置 baseURL
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudcome committed Jul 30, 2024
1 parent fcabf25 commit df0c7d2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
16 changes: 12 additions & 4 deletions src/printer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export class Printer {

print(configs?: PrinterConfigs) {
Object.assign(this.configs, configs);
const { hideHeaders, hideFooters, hideInfo, hideComponents, hideImports, hidePaths } = this.configs;
const { hideHeaders, hideHelpers, hideFooters, hideInfo, hideComponents, hideImports, hidePaths } = this.configs;
const defaultHeaders = [
//
'/* eslint-disable @typescript-eslint/ban-ts-comment */',
Expand All @@ -95,6 +95,7 @@ export class Printer {
!hideHeaders && headers,
!hideInfo && this._printInfo(),
!hideImports && this._printImports(),
!hideHelpers && Printer.helpersCode,
!hideComponents && this._printComponents(),
!hidePaths && this._printPaths(),
!hideFooters && footers,
Expand Down Expand Up @@ -129,11 +130,9 @@ export class Printer {
axiosImportFile = AXIOS_IMPORT_FILE,
axiosRequestConfigTypeName = AXIOS_QUEST_CONFIG_TYPE_NAME,
axiosResponseTypeName = AXIOS_PROMISE_TYPE_NAME,
baseURL,
} = this.options || {};
const firstServer = this.document.servers?.[0];
const defaultBaseURL = firstServer?.url || '/';
const BASE_URL = isString(baseURL) ? baseURL : baseURL?.(this.document) || defaultBaseURL;
const { file } = this.configs;
const importPath = toRelative(axiosImportFile, file);

Expand All @@ -148,7 +147,6 @@ export class Printer {
`import type {OneOf, AllOf, AnyOf, AnyObject, AnyArray} from "${pkgName}/client";`,
`import {resolveURL} from "${pkgName}/client";`,
'',
`const BASE_URL=${JSON.stringify(BASE_URL)};`,
].join('\n');
}

Expand Down Expand Up @@ -393,4 +391,14 @@ export async function ${funcName}(${requestArgs.toArgs(axiosRequestConfigTypeNam

this._parseContents(arg, content, response, (contentType, content) => contentMatch(contentType, content, response));
}

static helpersCode = `
// helpers --- start
export type OneOf<T extends unknown[]> = T extends [infer A, ...infer B] ? A | OneOf<B> : never;
export type AllOf<T extends unknown[]> = T extends [infer A, ...infer B] ? A & AllOf<B> : unknown;
export type AnyOf<T extends unknown[]> = T extends [infer A, ...infer B] ? A | AnyOf<B> | (A & AnyOf<B>) : never;
export type AnyObject = Record<string, any>;
export type AnyArray = any[];
// helpers --- end
`;
}
7 changes: 1 addition & 6 deletions src/printer/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,6 @@ export type PrinterOptions = {
*/
axiosResponseTypeName?: string;

/**
* 基础路径
* @default /
*/
baseURL?: string | ((document: OpenAPILatest.Document) => string);

/**
* 请求内容类型判断
*/
Expand Down Expand Up @@ -130,6 +124,7 @@ export type PrinterConfigs = {
file?: string;

hideHeaders?: boolean;
hideHelpers?: boolean;
hideFooters?: boolean;
hideInfo?: boolean;
hideImports?: boolean;
Expand Down

0 comments on commit df0c7d2

Please sign in to comment.