Skip to content

Commit

Permalink
feat(projects): @sa/axios: add response to flatRequest when success
Browse files Browse the repository at this point in the history
  • Loading branch information
mufeng889 committed Sep 12, 2024
1 parent 13b0cab commit 92e3cec
Show file tree
Hide file tree
Showing 2 changed files with 18 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
18 changes: 16 additions & 2 deletions packages/axios/src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,19 @@ export type CustomAxiosRequestConfig<R extends ResponseType = 'json'> = Omit<Axi
};

export interface RequestInstanceCommon<T> {
/**
* cancel the request by request id
*
* if the request provide abort controller sign from config, it will not collect in the abort controller map
*
* @param requestId
*/
cancelRequest: (requestId: string) => void;
/**
* cancel all request
*
* if the request provide abort controller sign from config, it will not collect in the abort controller map
*/
cancelAllRequest: () => void;
/** you can set custom state in the request instance */
state: T;
Expand All @@ -80,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 92e3cec

Please sign in to comment.