From eab0c3148098b192943fe5743f6d4a26a59fda31 Mon Sep 17 00:00:00 2001 From: zhaocl97 Date: Mon, 28 Feb 2022 16:28:42 +0800 Subject: [PATCH] feat(axios): add transformStringBoolean option --- src/utils/axios/index.ts | 3 +++ src/utils/axios/src/types.ts | 6 ++++++ src/utils/axios/transform.ts | 10 +++++++++- 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/utils/axios/index.ts b/src/utils/axios/index.ts index 02927486..39be32a1 100644 --- a/src/utils/axios/index.ts +++ b/src/utils/axios/index.ts @@ -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), }, diff --git a/src/utils/axios/src/types.ts b/src/utils/axios/src/types.ts index 1f78a0fb..da1fdd64 100644 --- a/src/utils/axios/src/types.ts +++ b/src/utils/axios/src/types.ts @@ -69,6 +69,12 @@ export interface AxiosCustomConfig { * @default true */ filterNull?: boolean + + /** + * @description transform "true"/"false" to real true/false + * @default true + */ + transformStringBoolean?: boolean } /** diff --git a/src/utils/axios/transform.ts b/src/utils/axios/transform.ts index c0b4d9e1..389a29c1 100644 --- a/src/utils/axios/transform.ts +++ b/src/utils/axios/transform.ts @@ -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' @@ -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 },