feat: custom get response data

This commit is contained in:
Innei
2024-03-22 23:10:33 +08:00
parent 3f0a330112
commit 1d1e3c580f
2 changed files with 9 additions and 4 deletions

View File

@@ -33,8 +33,8 @@ class HTTPClient<
.replace(/\/*$/, '')
.replace('localhost', '127.0.0.1')
this._proxy = this.buildRoute(this)()
options.transformResponse =
options.transformResponse || ((data) => camelcaseKeys(data))
options.transformResponse ||= (data) => camelcaseKeys(data)
options.getDataFromResponse ||= (res: any) => res.data
this.initGetClient()
@@ -45,7 +45,7 @@ class HTTPClient<
for (const name of allControllerNames) {
Object.defineProperty(this, name, {
get() {
const client = Reflect.get(this, `${methodPrefix}${name}`)
const client: any = Reflect.get(this, `${methodPrefix}${name}`)
if (!client) {
throw new ReferenceError(
`${
@@ -176,7 +176,7 @@ class HTTPClient<
: new RequestError(message, code, url, e)
}
const data = res.data
const data = that.options.getDataFromResponse!(res)
if (!data) {
return null
}

View File

@@ -11,5 +11,10 @@ interface IClientOptions {
}
customThrowResponseError: <T extends Error = Error>(err: any) => T
transformResponse: <T = any>(data: any) => T
/**
*
* @default (res) => res.data
*/
getDataFromResponse: <T = any>(response: unknown) => T
}
export type ClientOptions = Partial<IClientOptions>