ironbar-persea/frontend/app/api.js

70 lines
1.4 KiB
JavaScript
Raw Normal View History

2019-03-05 23:20:26 -06:00
import {RSAA} from 'redux-api-middleware';
const PERSEA_FRONTHOST = '10.0.0.93';
export function createActionTypes(type, prefix) {
return ['REQUEST', 'SUCCESS', 'FAILURE'].reduce((actions, action) => {
return {
...actions,
[`${type}_${action}`]: `${prefix}/${type}_${action}`,
};
}, {});
}
export function createCall(uri, types, type) {
const prefix = `http://${PERSEA_FRONTHOST}/api`;
return {
[RSAA]: {
endpoint: `${prefix}${uri}`,
method: 'GET',
headers: {
'Content-Type': 'application/json'
},
types: ['REQUEST', 'SUCCESS', 'FAILURE'].map((action) => {
return types[`${type}_${action}`]
}),
fetch: managedFetch,
},
};
}
export class ApiRequestHandler {
doesFetchEndpoint(input) {
return false;
}
async fetch(input, init) {
return new Response(null);
}
static normalizeRequestUrl(input) {
return ('string' === typeof input) ? input : input.url;
}
}
const handlers = [];
export function registerApiRequestHandler(handler) {
handlers.push(handler);
}
function handlerForInput(input) {
for (const handler of handlers) {
if (handler.doesFetchEndpoint(input)) {
return handler;
}
}
}
export function managedFetch(input, init) {
const handler = handlerForInput(input);
if (handler) {
return handler.fetch(input, init);
}
else {
return fetch(input, init);
}
}