Skip to content
This repository has been archived by the owner on Feb 6, 2023. It is now read-only.

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
gky360 committed Dec 16, 2019
1 parent b5cc5f2 commit 5598064
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
2 changes: 2 additions & 0 deletions frontend/src/middleware/api.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export * from './apiMiddleware';

const API_ROOT = '/api/v1/';

const callApi = (endpoint, method = 'GET', body) => {
Expand Down
89 changes: 89 additions & 0 deletions frontend/src/middleware/apiMiddleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { Middleware } from 'redux';

export const RSAA = '@@chainerui/RSAA';

export interface RSAACall<State = any, Payload = any, Meta = any> {
endpoint: string;
method: 'GET' | 'HEAD' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS';
types: [RSAARequestType, RSAASuccessType, RSAAFailureType];
body?: BodyInit | null;
headers?: HeadersInit;
options?: RequestInit;
credentials?: RequestCredentials;
}

export interface RSAAAction<State = any, Payload = any, Meta = any> {
[RSAA]: RSAACall<State, Payload, Meta>;
}

export interface RSAARequestTypeDescriptor {
type: string;
}
export interface RSAASuccessTypeDescriptor {
type: string;
}
export interface RSAAFailureTypeDescriptor {
type: string;
}

export type RSAARequestType = string;
export type RSAASuccessType = string;
export type RSAAFailureType = string;

const isPlainObject = (obj: any): boolean => {
return obj && typeof obj == 'object' && Object.getPrototypeOf(obj) === Object.prototype;
};

const isRSAA = <State = any, Payload = any, Meta = any>(
action: any
): action is RSAAAction<State, Payload, Meta> => {
return isPlainObject(action) && action.hasOwnProperty(RSAA);
};

const normalizeTypeDescriptors = (
types: [RSAARequestType, RSAASuccessType, RSAAFailureType]
): [RSAARequestTypeDescriptor, RSAASuccessTypeDescriptor, RSAAFailureTypeDescriptor] => {
const [requestType, successType, failureType] = types;
return [{ type: requestType }, { type: successType }, { type: failureType }];
};

export const apiMiddleware: Middleware = () => (next) => (action) => {
if (!isRSAA(action)) {
return next(action);
}

return (async () => {
const callAPI = action[RSAA];
const { endpoint, method, types, body, headers, options = {}, credentials } = callAPI;
const [requestType, successType, failureType] = normalizeTypeDescriptors(types);

next(requestType);

let res;
try {
res = await fetch(endpoint, {
...options,
method,
body: body || undefined,
credentials,
headers: headers || {},
});
} catch (e) {
return next({
...failureType,
payload: new RequestError(e.message),
error: true,
});
}

const isOk = res.ok;
if (isOk) {
return next(successType);
} else {
return next({
...failureType,
error: true,
});
}
})();
};

0 comments on commit 5598064

Please sign in to comment.