import { AxiosResponse } from 'axios'; export type RequestState = Readonly<{ data: R | null; loading: boolean; isCalled: boolean; prevHeaders?: Record; prevVariables?: Record; prevParams?: Record }> export type RequestAction = | { type: 'call', headers: Record, variables: Record, params?: Record } | { type: 'success', response: AxiosResponse } | { type: 'failure', response: AxiosResponse } export function requestReducer(state: RequestState, action: RequestAction) { 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 }; } } }