From ca2ed209c8d03fbc18caed53d3ade194ed0ae964 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=98=8E=E5=8D=8E?= <735161977@qq.com> Date: Fri, 16 Aug 2024 16:37:47 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=8B=93=E5=B1=95AxiosRequestConfig?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=E8=83=BD=E5=8A=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 6 ++++++ example/swagger/codegen.js | 7 ++++++- src/baseInterfaces.ts | 6 +++++- src/templates/serviceHeader.ts | 25 +++++++++++++++---------- 4 files changed, 32 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index f6ac96c..b697ac8 100644 --- a/README.md +++ b/README.md @@ -76,6 +76,12 @@ export interface ISwaggerOptions { sharedServiceOptions?: boolean | undefined /** use parameters in header or not*/ useHeaderParameters?: boolean + /** wrapper response type */ + responseTypeWrapper?: (responseType: string)=> string + /** wrapper IRequestOptions */ + requestOptionsWrapper?: (IRequestOptionsStr: string)=> string + /** add an additional declaration before the IRequestOptions */ + customDefinition?: string } const defaultOptions: ISwaggerOptions = { diff --git a/example/swagger/codegen.js b/example/swagger/codegen.js index c8699ee..38e731c 100644 --- a/example/swagger/codegen.js +++ b/example/swagger/codegen.js @@ -12,5 +12,10 @@ codegen({ extendDefinitionFile: './swagger/customerDefinition.ts', extendGenericType: ['JsonResult'], sharedServiceOptions: true, - classNameMode: 'parentPath' + classNameMode: 'parentPath', + customDefinition: `type SecurityTye = 'md5' | 'sha1' | 'aes' | 'des'`, + requestOptionsWrapper: (str) => { + return str + ` retryCount?: number + security?: Record` + } }) diff --git a/src/baseInterfaces.ts b/src/baseInterfaces.ts index 5102a8e..714957b 100644 --- a/src/baseInterfaces.ts +++ b/src/baseInterfaces.ts @@ -40,7 +40,11 @@ export interface ISwaggerOptions { /** use parameters in header or not*/ useHeaderParameters?: boolean /** wrapper response type */ - responseTypeWrapper ?: (responseType: string)=> string + responseTypeWrapper?: (responseType: string)=> string + /** wrapper IRequestOptions */ + requestOptionsWrapper?: (IRequestOptionsStr: string)=> string + /** add an additional declaration before the IRequestOptions */ + customDefinition?: string } export interface IPropDef { diff --git a/src/templates/serviceHeader.ts b/src/templates/serviceHeader.ts index d6fa784..1bf53f4 100644 --- a/src/templates/serviceHeader.ts +++ b/src/templates/serviceHeader.ts @@ -4,18 +4,24 @@ import { ISwaggerOptions } from '../baseInterfaces' import { abpGenericTypeDefinition, universalGenericTypeDefinition } from './genericTypeDefinitionTemplate' export function serviceHeader(options: ISwaggerOptions) { + const customDefinition = options.customDefinition || '' + const requestOptionsWrapper = options.requestOptionsWrapper || ((str)=> str) const classTransformerImport = options.useClassTransformer ? `import { Expose, Transform, Type, plainToClass } from 'class-transformer'; ` : '' + return `/** Generate by swagger-axios-codegen */ /* eslint-disable */ // @ts-nocheck import axiosStatic, { type AxiosInstance, type AxiosRequestConfig } from 'axios'; ${classTransformerImport} + ${customDefinition} export interface IRequestOptions extends AxiosRequestConfig { + ${ +requestOptionsWrapper(` /** * show loading status */ @@ -24,15 +30,12 @@ export function serviceHeader(options: ISwaggerOptions) { * display error message */ showError?: boolean; - /** - * data security, extended fields are encrypted using the specified algorithm - */ - security?: Record; /** * indicates whether Authorization credentials are required for the request * @default true */ - withAuthorization?: boolean; + withAuthorization?: boolean;` + )} } export interface IRequestConfig { @@ -64,12 +67,17 @@ export function disableLint() { } export function customerServiceHeader(options: ISwaggerOptions) { + const customDefinition = options.customDefinition || '' + const requestOptionsWrapper = options.requestOptionsWrapper || ((str)=> str) return `/** Generate by swagger-axios-codegen */ // @ts-nocheck /* eslint-disable */ import axiosStatic, { type AxiosInstance, type AxiosRequestConfig } from 'axios'; + ${customDefinition} export interface IRequestOptions extends AxiosRequestConfig { + ${ +requestOptionsWrapper(` /** * show loading status */ @@ -78,15 +86,12 @@ export function customerServiceHeader(options: ISwaggerOptions) { * display error message */ showError?: boolean; - /** - * data security, extended fields are encrypted using the specified algorithm - */ - security?: Record; /** * indicates whether Authorization credentials are required for the request * @default true */ - withAuthorization?: boolean; + withAuthorization?: boolean;` + )} } export interface IRequestPromise extends Promise> {}