UNPKG

@omneedia/client-js

Version:

Isomorphic Javascript client for Omneedia

226 lines 8.35 kB
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()); }); }; import { isBrowser } from './util'; import fetch from 'cross-fetch'; export default class OmneediaRequest { constructor(ref) { this.url = ''; this.ref = ref; if (isBrowser()) { this.fetch = globalThis.fetch.bind(globalThis); } else { this.fetch = fetch; } } get(uri, o) { return __awaiter(this, void 0, void 0, function* () { if (uri.indexOf('://') > -1) var url = `${uri}`; else var url = `${this.ref.omneediaUrl}${uri}`; let headers = this.ref.headers(); if (isBrowser()) { if (localStorage.getItem('omneedia.auth.token')) { headers.Authorization = 'Bearer ' + localStorage.getItem('omneedia.auth.token'); } headers.redirect_to = window.location.origin + window.location.pathname; } else { // use cookie const token = this.ref.options.cookies.getAll(); if (token) { const bearer = token.find((el) => el.name === 'omneedia.auth.token'); if (bearer) headers.Authorization = 'Bearer ' + bearer.value; } } var res = yield this.fetch(url, { headers: headers, }); var body = yield res.json(); var response = { data: null, error: null, statusText: res.statusText, status: res.status, }; if (res.ok) response.data = body; else response.error = body; return response; }); } delete(uri, o) { return __awaiter(this, void 0, void 0, function* () { let data = []; if (uri.indexOf('://') > -1) var url = `${uri}`; else var url = `${this.ref.omneediaUrl}${uri}`; let headers = this.ref.headers(); if (isBrowser()) { if (localStorage.getItem('omneedia.auth.token')) { headers.Authorization = 'Bearer ' + localStorage.getItem('omneedia.auth.token'); } headers.redirect_to = window.location.origin + window.location.pathname; } else { // use cookie const token = this.ref.options.cookies.getAll(); if (token) { const bearer = token.find((el) => el.name === 'omneedia.auth.token'); if (bearer) headers.Authorization = 'Bearer ' + bearer.value; } } var res = yield this.fetch(url, { method: 'DELETE', headers: headers, }); try { data = yield res.json(); } catch (e) { data = []; } return { data: data, statusText: res.statusText, status: res.status, }; }); } patch(uri, o) { return __awaiter(this, void 0, void 0, function* () { let data = []; if (uri.indexOf('://') > -1) var url = `${uri}`; else var url = `${this.ref.omneediaUrl}${uri}`; let headers = this.ref.headers(); if (isBrowser()) { headers.redirect_to = window.location.origin + window.location.pathname; if (localStorage.getItem('omneedia.auth.token')) { headers.Authorization = 'Bearer ' + localStorage.getItem('omneedia.auth.token'); } } else { // use cookie const token = this.ref.options.cookies.getAll(); if (token) { const bearer = token.find((el) => el.name === 'omneedia.auth.token'); if (bearer) headers.Authorization = 'Bearer ' + bearer.value; } } var res = yield this.fetch(url, { method: 'PATCH', headers: headers, body: JSON.stringify(o), }); try { data = yield res.json(); } catch (e) { data = []; } return { data: yield data, statusText: res.statusText, status: res.status, }; }); } put(uri, o) { return __awaiter(this, void 0, void 0, function* () { let data = []; if (uri.indexOf('://') > -1) var url = `${uri}`; else var url = `${this.ref.omneediaUrl}${uri}`; let headers = this.ref.headers(); if (isBrowser()) { headers.redirect_to = window.location.origin + window.location.pathname; if (localStorage.getItem('omneedia.auth.token')) { headers.Authorization = 'Bearer ' + localStorage.getItem('omneedia.auth.token'); } } else { // use cookie const token = this.ref.options.cookies.getAll(); if (token) { const bearer = token.find((el) => el.name === 'omneedia.auth.token'); if (bearer) headers.Authorization = 'Bearer ' + bearer.value; } } var res = yield this.fetch(url, { method: 'PUT', headers: headers, body: JSON.stringify(o), }); try { data = yield res.json(); } catch (e) { data = []; } return { data: yield data, statusText: res.statusText, status: res.status, }; }); } post(uri, o) { return __awaiter(this, void 0, void 0, function* () { let data = []; if (uri.indexOf('://') > -1) var url = `${uri}`; else var url = `${this.ref.omneediaUrl}${uri}`; let headers = this.ref.headers(); if (isBrowser()) { headers.redirect_to = window.location.origin + window.location.pathname; if (localStorage.getItem('omneedia.auth.token')) { headers.Authorization = 'Bearer ' + localStorage.getItem('omneedia.auth.token'); } } else { // use cookie const token = this.ref.options.cookies.getAll(); if (token) { const bearer = token.find((el) => el.name === 'omneedia.auth.token'); if (bearer) headers.Authorization = 'Bearer ' + bearer.value; } } var res = yield this.fetch(url, { method: 'POST', headers: headers, body: JSON.stringify(o), }); try { data = yield res.json(); } catch (e) { data = []; } return { data: data, statusText: res.statusText, status: res.status, }; }); } } //# sourceMappingURL=request.js.map