Skip to content

Commit

Permalink
Merge pull request #33 from loicduong/28-featcore-add-response-to-fla…
Browse files Browse the repository at this point in the history
…trequest-when-success-in-saaxios

feat(core): add response to flatRequest when success in @sa/axios
  • Loading branch information
loicduong authored Sep 7, 2024
2 parents 85744e8 + 6f72e6c commit 11a7cc0
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
4 changes: 2 additions & 2 deletions packages/axios/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,13 @@ export function createFlatRequest<ResponseData = any, State = Record<string, unk
if (responseType === 'json') {
const data = opts.transformBackendResponse(response)

return { data, error: null }
return { data, error: null, response }
}

return { data: response.data as MappedType<R, T>, error: null }
}
catch (error) {
return { data: null, error }
return { data: null, error, response: (error as AxiosError<ResponseData>).response }
}
} as FlatRequestInstance<State, ResponseData>

Expand Down
7 changes: 4 additions & 3 deletions packages/axios/src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,19 @@ export interface RequestInstance<S = Record<string, unknown>> extends RequestIns
<T = any, R extends ResponseType = 'json'>(config: CustomAxiosRequestConfig<R>): Promise<MappedType<R, T>>
}

export interface FlatResponseSuccessData<T = any> {
export type FlatResponseSuccessData<T = any, ResponseData = any> = {
data: T
error: null
response: AxiosResponse<ResponseData>
}

export interface FlatResponseFailData<ResponseData = any> {
data: null
error: AxiosError<ResponseData>
response: AxiosResponse<ResponseData>
}

export type FlatResponseData<T = any, ResponseData = any> =
| FlatResponseSuccessData<T>
| FlatResponseSuccessData<T, ResponseData>
| FlatResponseFailData<ResponseData>

export interface FlatRequestInstance<S = Record<string, unknown>, ResponseData = any> extends RequestInstanceCommon<S> {
Expand Down

0 comments on commit 11a7cc0

Please sign in to comment.