Dmitriy Pleshevskiy
e9c18c9fea
feat(context): add context for request feat(client): add hook for get memo rest client feat(endpoint): add endpoint object for configure rest methods feat(request): add request hook refac(request): return cached data for same query refac(request): remove void variables for endpoints feat(request): add headers to request cache
45 lines
No EOL
1.3 KiB
TypeScript
45 lines
No EOL
1.3 KiB
TypeScript
import { AxiosResponse } from 'axios';
|
|
|
|
export type RequestState<R> = Readonly<{
|
|
data: R | null;
|
|
loading: boolean;
|
|
isCalled: boolean;
|
|
prevHeaders?: Record<string, string>;
|
|
prevVariables?: Record<string, any>;
|
|
prevParams?: Record<string, any>
|
|
}>
|
|
|
|
export type RequestAction<R> =
|
|
| { type: 'call', headers: Record<string, string>, variables: Record<string, any>, params?: Record<string, any> }
|
|
| { type: 'success', response: AxiosResponse<R> }
|
|
| { type: 'failure', response: AxiosResponse<R> }
|
|
|
|
export function requestReducer<R>(state: RequestState<R>, action: RequestAction<R>) {
|
|
switch (action.type) {
|
|
case 'call': {
|
|
return {
|
|
...state,
|
|
loading: true,
|
|
isCalled: true,
|
|
prevHeaders: action.headers,
|
|
prevVariables: action.variables,
|
|
prevParams: action.params,
|
|
};
|
|
}
|
|
case 'success': {
|
|
return {
|
|
...state,
|
|
loading: false,
|
|
data: action.response.data,
|
|
};
|
|
}
|
|
case 'failure': {
|
|
return {
|
|
...state,
|
|
loading: false,
|
|
data: null,
|
|
// TODO: need to append errors
|
|
};
|
|
}
|
|
}
|
|
} |