I have a query related to RTK. I find myself repeating code in my action creators created with createAsyncThunk because I want to be able to cancel any request made.
I am thinking of creating a wrapper to streamline this process, but I am facing challenges with TypeScript implementation. Are the arguments used in createAsyncThunk accessible somewhere?
Upon examining the code, I noticed that the thunkAPI (which I am particularly interested in) is defined with GetThunkAPI<'3rd parameter'> using 3 TypeScript parameters.
An example of an action creator could resemble the following:
export const resendValidationKey = createAsyncThunk<
void,
IAdminResendValidationKey,
{ rejectValue: AxiosError }
>('admin/resendValidationKey', async (data, thunkAPI) => {
const { signal, rejectWithValue } = thunkAPI;
try {
const source = axios.CancelToken.source();
signal.addEventListener('abort', () => {
source.cancel();
});
await const response = axios.post(`admin/account/validate/resend`, data, {
cancelToken: source.token,
});
return response.data;
} catch (error) {
return rejectWithValue(error);
}
});
Ideally, I would like a wrapper where I can simply input the URL, method, data, and success callback (if applicable). Is such a solution feasible?
I hope all of this makes sense.