|
| 1 | +import { Middleware } from 'redux'; |
| 2 | + |
| 3 | +export const RSAA = '@@chainerui/RSAA'; |
| 4 | + |
| 5 | +export interface RSAACall<State = any, Payload = any, Meta = any> { |
| 6 | + endpoint: string; |
| 7 | + method: 'GET' | 'HEAD' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS'; |
| 8 | + types: [RSAARequestType, RSAASuccessType, RSAAFailureType]; |
| 9 | + body?: BodyInit | null; |
| 10 | + headers?: HeadersInit; |
| 11 | + options?: RequestInit; |
| 12 | + credentials?: RequestCredentials; |
| 13 | +} |
| 14 | + |
| 15 | +export interface RSAAAction<State = any, Payload = any, Meta = any> { |
| 16 | + [RSAA]: RSAACall<State, Payload, Meta>; |
| 17 | +} |
| 18 | + |
| 19 | +export interface RSAARequestTypeDescriptor { |
| 20 | + type: string; |
| 21 | +} |
| 22 | +export interface RSAASuccessTypeDescriptor { |
| 23 | + type: string; |
| 24 | +} |
| 25 | +export interface RSAAFailureTypeDescriptor { |
| 26 | + type: string; |
| 27 | +} |
| 28 | + |
| 29 | +export type RSAARequestType = string; |
| 30 | +export type RSAASuccessType = string; |
| 31 | +export type RSAAFailureType = string; |
| 32 | + |
| 33 | +const isPlainObject = (obj: any): boolean => { |
| 34 | + return obj && typeof obj == 'object' && Object.getPrototypeOf(obj) === Object.prototype; |
| 35 | +}; |
| 36 | + |
| 37 | +const isRSAA = <State = any, Payload = any, Meta = any>( |
| 38 | + action: any |
| 39 | +): action is RSAAAction<State, Payload, Meta> => { |
| 40 | + return isPlainObject(action) && action.hasOwnProperty(RSAA); |
| 41 | +}; |
| 42 | + |
| 43 | +const normalizeTypeDescriptors = ( |
| 44 | + types: [RSAARequestType, RSAASuccessType, RSAAFailureType] |
| 45 | +): [RSAARequestTypeDescriptor, RSAASuccessTypeDescriptor, RSAAFailureTypeDescriptor] => { |
| 46 | + const [requestType, successType, failureType] = types; |
| 47 | + return [{ type: requestType }, { type: successType }, { type: failureType }]; |
| 48 | +}; |
| 49 | + |
| 50 | +export const apiMiddleware: Middleware = () => (next) => (action) => { |
| 51 | + if (!isRSAA(action)) { |
| 52 | + return next(action); |
| 53 | + } |
| 54 | + |
| 55 | + return (async () => { |
| 56 | + const callAPI = action[RSAA]; |
| 57 | + const { endpoint, method, types, body, headers, options = {}, credentials } = callAPI; |
| 58 | + const [requestType, successType, failureType] = normalizeTypeDescriptors(types); |
| 59 | + |
| 60 | + next(requestType); |
| 61 | + |
| 62 | + let res; |
| 63 | + try { |
| 64 | + res = await fetch(endpoint, { |
| 65 | + ...options, |
| 66 | + method, |
| 67 | + body: body || undefined, |
| 68 | + credentials, |
| 69 | + headers: headers || {}, |
| 70 | + }); |
| 71 | + } catch (e) { |
| 72 | + return next({ |
| 73 | + ...failureType, |
| 74 | + payload: new RequestError(e.message), |
| 75 | + error: true, |
| 76 | + }); |
| 77 | + } |
| 78 | + |
| 79 | + const isOk = res.ok; |
| 80 | + if (isOk) { |
| 81 | + return next(successType); |
| 82 | + } else { |
| 83 | + return next({ |
| 84 | + ...failureType, |
| 85 | + error: true, |
| 86 | + }); |
| 87 | + } |
| 88 | + })(); |
| 89 | +}; |
0 commit comments