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(core): add response to flatRequest when success in @sa/axios #33

Merged
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
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
Loading