Skip to content

Commit

Permalink
feat(projects): @sa/axios: support instance state, fix flatRequest er…
Browse files Browse the repository at this point in the history
…ror type
  • Loading branch information
honghuangdc committed Apr 13, 2024
1 parent b642032 commit 3f41811
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 29 deletions.
18 changes: 10 additions & 8 deletions packages/axios/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,13 @@ function createCommonRequest<ResponseData = any>(
* @param axiosConfig axios config
* @param options request options
*/
export function createRequest<ResponseData = any>(
export function createRequest<ResponseData = any, State = Record<string, unknown>>(
axiosConfig?: CreateAxiosDefaults,
options?: Partial<RequestOption<ResponseData>>
) {
const { instance, opts, cancelRequest, cancelAllRequest } = createCommonRequest<ResponseData>(axiosConfig, options);

const request: RequestInstance = async function request<T = any, R extends ResponseType = 'json'>(
const request: RequestInstance<State> = async function request<T = any, R extends ResponseType = 'json'>(
config: CustomAxiosRequestConfig
) {
const response: AxiosResponse<ResponseData> = await instance(config);
Expand All @@ -123,7 +123,7 @@ export function createRequest<ResponseData = any>(
}

return response.data as MappedType<R, T>;
} as RequestInstance;
} as RequestInstance<State>;

request.cancelRequest = cancelRequest;
request.cancelAllRequest = cancelAllRequest;
Expand All @@ -139,15 +139,16 @@ export function createRequest<ResponseData = any>(
* @param axiosConfig axios config
* @param options request options
*/
export function createFlatRequest<ResponseData = any>(
export function createFlatRequest<ResponseData = any, State = Record<string, unknown>>(
axiosConfig?: CreateAxiosDefaults,
options?: Partial<RequestOption<ResponseData>>
) {
const { instance, opts, cancelRequest, cancelAllRequest } = createCommonRequest<ResponseData>(axiosConfig, options);

const flatRequest: FlatRequestInstance = async function flatRequest<T = any, R extends ResponseType = 'json'>(
config: CustomAxiosRequestConfig
) {
const flatRequest: FlatRequestInstance<State, ResponseData> = async function flatRequest<
T = any,
R extends ResponseType = 'json'
>(config: CustomAxiosRequestConfig) {
try {
const response: AxiosResponse<ResponseData> = await instance(config);

Expand All @@ -163,10 +164,11 @@ export function createFlatRequest<ResponseData = any>(
} catch (error) {
return { data: null, error };
}
} as FlatRequestInstance;
} as FlatRequestInstance<State, ResponseData>;

flatRequest.cancelRequest = cancelRequest;
flatRequest.cancelAllRequest = cancelAllRequest;
flatRequest.state = {} as State;

return flatRequest;
}
Expand Down
27 changes: 16 additions & 11 deletions packages/axios/src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export interface RequestOption<ResponseData = any> {
onBackendFail: (
response: AxiosResponse<ResponseData>,
instance: AxiosInstance
) => Promise<AxiosResponse> | Promise<void>;
) => Promise<AxiosResponse | null> | Promise<void>;
/**
* transform backend response when the responseType is json
*
Expand Down Expand Up @@ -68,29 +68,34 @@ export type CustomAxiosRequestConfig<R extends ResponseType = 'json'> = Omit<Axi
responseType?: R;
};

/** The request instance */
export interface RequestInstance {
<T = any, R extends ResponseType = 'json'>(config: CustomAxiosRequestConfig<R>): Promise<MappedType<R, T>>;
export interface RequestInstanceCommon<T> {
cancelRequest: (requestId: string) => void;
cancelAllRequest: () => void;
/** you can set custom state in the request instance */
state: T;
}

/** The request instance */
export interface RequestInstance<S = Record<string, unknown>> extends RequestInstanceCommon<S> {
<T = any, R extends ResponseType = 'json'>(config: CustomAxiosRequestConfig<R>): Promise<MappedType<R, T>>;
}

export type FlatResponseSuccessData<T = any> = {
data: T;
error: null;
};

export type FlatResponseFailData<T = any> = {
export type FlatResponseFailData<ResponseData = any> = {
data: null;
error: AxiosError<T>;
error: AxiosError<ResponseData>;
};

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

export interface FlatRequestInstance {
export interface FlatRequestInstance<S = Record<string, unknown>, ResponseData = any> extends RequestInstanceCommon<S> {
<T = any, R extends ResponseType = 'json'>(
config: CustomAxiosRequestConfig<R>
): Promise<FlatResponseData<MappedType<R, T>>>;
cancelRequest: (requestId: string) => void;
cancelAllRequest: () => void;
): Promise<FlatResponseData<MappedType<R, T>, ResponseData>>;
}
20 changes: 10 additions & 10 deletions packages/hooks/src/use-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ export type HookRequestInstanceResponseSuccessData<T = any> = {
error: Ref<null>;
};

export type HookRequestInstanceResponseFailData<T = any> = {
export type HookRequestInstanceResponseFailData<ResponseData = any> = {
data: Ref<null>;
error: Ref<AxiosError<T>>;
error: Ref<AxiosError<ResponseData>>;
};

export type HookRequestInstanceResponseData<T = any> = {
export type HookRequestInstanceResponseData<T = any, ResponseData = any> = {
loading: Ref<boolean>;
} & (HookRequestInstanceResponseSuccessData<T> | HookRequestInstanceResponseFailData<T>);
} & (HookRequestInstanceResponseSuccessData<T> | HookRequestInstanceResponseFailData<ResponseData>);

export interface HookRequestInstance {
export interface HookRequestInstance<ResponseData = any> {
<T = any, R extends ResponseType = 'json'>(
config: CustomAxiosRequestConfig
): HookRequestInstanceResponseData<MappedType<R, T>>;
): HookRequestInstanceResponseData<MappedType<R, T>, ResponseData>;
cancelRequest: (requestId: string) => void;
cancelAllRequest: () => void;
}
Expand All @@ -45,13 +45,13 @@ export default function createHookRequest<ResponseData = any>(
) {
const request = createFlatRequest<ResponseData>(axiosConfig, options);

const hookRequest: HookRequestInstance = function hookRequest<T = any, R extends ResponseType = 'json'>(
const hookRequest: HookRequestInstance<ResponseData> = function hookRequest<T = any, R extends ResponseType = 'json'>(
config: CustomAxiosRequestConfig
) {
const { loading, startLoading, endLoading } = useLoading();

const data = ref<MappedType<R, T> | null>(null);
const error = ref<AxiosError<MappedType<R, T>> | null>(null);
const data = ref<MappedType<R, T> | null>(null) as Ref<MappedType<R, T>>;
const error = ref<AxiosError<ResponseData> | null>(null) as Ref<AxiosError<ResponseData> | null>;

startLoading();

Expand All @@ -70,7 +70,7 @@ export default function createHookRequest<ResponseData = any>(
data,
error
};
} as HookRequestInstance;
} as HookRequestInstance<ResponseData>;

hookRequest.cancelRequest = request.cancelRequest;
hookRequest.cancelAllRequest = request.cancelAllRequest;
Expand Down

0 comments on commit 3f41811

Please sign in to comment.