oneframe-react
Version:
Oneframe React ## Components, Hooks, Helper Functions & State Management
126 lines (125 loc) • 4.97 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const lodash_1 = require("lodash");
require('es6-promise').polyfill();
require('isomorphic-fetch');
const getHeaders = (response) => {
let x = {};
response.forEach(function (value, name) {
x = Object.assign(Object.assign({}, x), { [name]: value });
});
return x;
};
const abortTime = Number(process.env.REACT_APP_ABORT_TIME) || 10000;
const kraxFetch = (options) => __awaiter(void 0, void 0, void 0, function* () {
try {
const controller = new AbortController();
const signal = controller.signal;
setTimeout(() => {
controller.abort();
}, abortTime);
const res = yield fetch(options.url, Object.assign(Object.assign({}, lodash_1.omit(options, 'url')), { signal: signal }));
let tempData = null;
let tempError = null;
if (res.ok) {
if (res.statusText !== 'No Content') {
tempData = yield res.json();
}
}
else {
if (res.status !== 404) {
tempError = yield res.clone().json();
}
}
return new Promise(resolve => {
const kraxResponse = {
data: tempData,
statusCode: res.status,
headers: getHeaders(res.headers),
error: tempError,
};
resolve(kraxResponse);
});
}
catch (e) {
return new Promise(resolve => {
const kraxResponse = {
data: null,
statusCode: 100,
headers: {},
error: null,
};
resolve(kraxResponse);
});
}
});
exports.kraxFetch = kraxFetch;
const kraxFetchOptions = (fetchParams) => {
const { url, method, mode, cache, credentials, headers, redirect, referrer, body, isFile, isForm, isJson, } = fetchParams;
const METHOD = method ? lodash_1.toUpper(method) : 'GET';
const MODE = mode ? { mode } : {};
const CACHE = cache ? { cache } : {};
const CREDENTIAL = credentials ? { credentials } : {};
const REDIRECT = redirect ? { redirect } : {};
const REFERRER = referrer ? { referrer } : {};
let BODY = {};
let HEADERS = {};
const choosenWay = (isJson ? 1 : 0) + (isFile ? 1 : 0) + (isForm ? 1 : 0);
if (choosenWay > 1) {
throw new Error('KraxFetch: You can only choose one option between isJson, isFormWithFile and isFormWithoutFile!');
}
if (isJson) {
BODY = body ? { body: JSON.stringify(body) } : {};
HEADERS = {
headers: Object.assign({ Accept: 'application/json', 'Content-Type': 'application/json' }, (headers || {})),
};
}
if (isFile) {
const formData = new FormData();
if (body) {
Object.keys(lodash_1.omit(body, 'files')).forEach((key) => {
formData.append(key, body[key]);
});
if (body.files) {
for (const file of body.files) {
formData.append('files', file, file.name);
}
}
}
BODY = body ? { body: formData } : {};
HEADERS = {
headers: Object.assign({}, (headers || {})),
};
}
if (isForm) {
const formData = new URLSearchParams();
if (body) {
Object.keys(body).forEach((key) => {
formData.append(key, body[key]);
});
BODY = { body: formData };
}
HEADERS = {
headers: Object.assign({ 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' }, (headers || {})),
};
}
if (!isForm && !isFile && !isJson) {
BODY = body ? body : {};
HEADERS = headers ? headers : {};
}
let fetchBody = Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({ method: METHOD }, BODY), HEADERS), MODE), CACHE), CREDENTIAL), REDIRECT), REFERRER);
if (METHOD && (lodash_1.toUpper(METHOD) === 'DELETE' || lodash_1.toUpper(METHOD) === 'GET')) {
fetchBody = lodash_1.omit(fetchBody, 'body');
}
return Object.assign({ url }, fetchBody);
};
exports.kraxFetchOptions = kraxFetchOptions;
;