Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 拓展AxiosRequestConfig定义能力 #198

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
7 changes: 6 additions & 1 deletion example/swagger/codegen.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, SecurityTye>`
}
})
6 changes: 5 additions & 1 deletion src/baseInterfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
25 changes: 15 additions & 10 deletions src/templates/serviceHeader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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<string, 'md5' | 'sha1' | 'aes' | 'des'>;
/**
* indicates whether Authorization credentials are required for the request
* @default true
*/
withAuthorization?: boolean;
withAuthorization?: boolean;`
)}
}

export interface IRequestConfig {
Expand Down Expand Up @@ -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
*/
Expand All @@ -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<string, 'md5' | 'sha1' | 'aes' | 'des'>;
/**
* indicates whether Authorization credentials are required for the request
* @default true
*/
withAuthorization?: boolean;
withAuthorization?: boolean;`
)}
}

export interface IRequestPromise<T=any> extends Promise<IRequestResponse<T>> {}
Expand Down
Loading