Skip to content

Commit

Permalink
feat(axios): add transformStringBoolean option
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhaocl1997 committed Feb 28, 2022
1 parent 80066c0 commit eab0c31
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/utils/axios/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ const axiosConfig: AxiosRequestConfigExtend = {
// filter null value
filterNull: true,

// transform "true"/"false"
transformStringBoolean: true,

// custom each api cached time
cachedMiliseconds: Number(import.meta.env.VITE_AXIOS_CACHE_MAXAGE),
},
Expand Down
6 changes: 6 additions & 0 deletions src/utils/axios/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ export interface AxiosCustomConfig {
* @default true
*/
filterNull?: boolean

/**
* @description transform "true"/"false" to real true/false
* @default true
*/
transformStringBoolean?: boolean
}

/**
Expand Down
10 changes: 9 additions & 1 deletion src/utils/axios/transform.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import type { AxiosTransform } from './src/types'

import { easyFilterEmptyValue } from 'easy-fns-ts'
import {
easyFilterEmptyValue,
easyTransformObjectStringBoolean,
} from 'easy-fns-ts'

import { checkReponseErrorStatus } from './checkStatus'

Expand Down Expand Up @@ -28,6 +31,11 @@ export const transform: AxiosTransform = {
config.data = easyFilterEmptyValue(config.data)
}

// transform "true"/"false" to true/false
if (mergedCustomOptions.transformStringBoolean && config.data) {
config.data = easyTransformObjectStringBoolean(config.data)
}

return config
},

Expand Down

0 comments on commit eab0c31

Please sign in to comment.