UNPKG

@foxy.io/sdk

Version:

Universal SDK for a full server-side and a limited in-browser access to Foxy hAPI.

86 lines (85 loc) 4.1 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 }); exports.API = void 0; const cross_fetch_1 = require("cross-fetch"); const consola_1 = __importDefault(require("consola")); const v8n_js_1 = require("../v8n.js"); const AuthError_js_1 = require("./AuthError.js"); const fake_storage_1 = __importDefault(require("fake-storage")); const Node_js_1 = require("./Node.js"); /** * Base API class for all custom clients in this SDK. If you're building * your own client, consider extending this class for consistency. */ class API extends Node_js_1.Node { constructor(init) { var _a, _b, _c; API.v8n.classConstructor.check(init); super({ cache: (_a = init.cache) !== null && _a !== void 0 ? _a : new fake_storage_1.default(), console: consola_1.default.create({ level: init.level }).withTag('@foxy.io/sdk'), fetch: (_b = init.fetch) !== null && _b !== void 0 ? _b : cross_fetch_1.fetch, path: [init.base], }); this.console = this._console; this.storage = (_c = init.storage) !== null && _c !== void 0 ? _c : new fake_storage_1.default(); this.cache = this._cache; this.base = init.base; } /** * Makes a raw and, if possible, authenticated request to the API. * This method implements native [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API). * * @param args Fetch API arguments (url and request parameters). * @returns Fetch API response. */ fetch(...args) { return __awaiter(this, void 0, void 0, function* () { return this._fetch(...args); }); } } exports.API = API; /** * Polyfilled version of the built-in `Response` class. If you need to check the return value of `.fetch()`, * use this class instead of the built-in one to avoid [this issue](https://github.com/github/fetch/issues/860). */ API.WHATWGResponse = cross_fetch_1.Response; /** * Polyfilled version of the built-in `Request` class. If you need to pass an instance of `Request` to `.fetch()`, * use this class instead of the built-in one to avoid [this issue](https://github.com/github/fetch/issues/860). */ API.WHATWGRequest = cross_fetch_1.Request; /** * Polyfilled version of the built-in `Headers` class. If you need to pass an instance of `Headers` to `.fetch()`, * use this class instead of the built-in one to avoid [this issue](https://github.com/github/fetch/issues/860). */ API.WHATWGHeaders = cross_fetch_1.Headers; /** * Polyfilled version of the built-in `fetch` function. If you need to call fetch() with poyfilled request, response or headers, * use this function instead of the built-in one to avoid [this issue](https://github.com/github/fetch/issues/860). */ API.whatwgFetch = cross_fetch_1.fetch; API.AuthError = AuthError_js_1.AuthError; API.Node = Node_js_1.Node; API.v8n = { classConstructor: v8n_js_1.v8n().schema({ base: v8n_js_1.v8n().instanceOf(URL), cache: v8n_js_1.v8n().optional(v8n_js_1.storageV8N), fetch: v8n_js_1.v8n().optional(v8n_js_1.v8n().typeOf('function')), level: v8n_js_1.v8n().optional(v8n_js_1.v8n().integer()), storage: v8n_js_1.v8n().optional(v8n_js_1.storageV8N), }), };