Skip to content

Commit

Permalink
feat(packages): @sa/axios: add response to flatRequest when success
Browse files Browse the repository at this point in the history
  • Loading branch information
honghuangdc committed Sep 8, 2024
1 parent 60366c2 commit aace819
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 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,12 +162,12 @@ 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
6 changes: 4 additions & 2 deletions packages/axios/src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,20 @@ export interface RequestInstance<S = Record<string, unknown>> extends RequestIns
<T = any, R extends ResponseType = 'json'>(config: CustomAxiosRequestConfig<R>): Promise<MappedType<R, T>>;
}

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

export type 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 aace819

Please sign in to comment.