UNPKG

@omneedia/client-js

Version:

Isomorphic Javascript client for Omneedia

206 lines 7.4 kB
"use strict"; 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()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const util_1 = require("./util"); const cross_fetch_1 = __importDefault(require("cross-fetch")); class OmneediaRequest { constructor(ref) { this.url = ''; this.ref = ref; if ((0, util_1.isBrowser)()) { console.log('using browser fetch'); this.fetch = globalThis.fetch.bind(globalThis); } else { console.log('using node fetch'); this.fetch = cross_fetch_1.default; } } 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 ((0, util_1.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 } 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 ((0, util_1.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 } 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 ((0, util_1.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 } 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 ((0, util_1.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 } 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(); headers.redirect_to = window.location.origin + window.location.pathname; if ((0, util_1.isBrowser)()) { headers.redirect_to = window.location.origin + window.location.pathname; if (localStorage.getItem('omneedia.auth.token')) { headers.Authorization = 'Bearer ' + localStorage.getItem('omneedia.auth.token'); } console.log('->', headers.Authorization); } else { // use cookie } 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, }; }); } } exports.default = OmneediaRequest; //# sourceMappingURL=request.js.map