UNPKG

@foxy.io/sdk

Version:

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

79 lines (78 loc) 3.59 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 { Headers, Request, Response, fetch } from 'cross-fetch'; import consola from 'consola'; import { storageV8N, v8n } from '../v8n.js'; import { AuthError } from './AuthError.js'; import MemoryStorage from 'fake-storage'; import { Node } from './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. */ export class API extends Node { constructor(init) { var _a, _b, _c; API.v8n.classConstructor.check(init); super({ cache: (_a = init.cache) !== null && _a !== void 0 ? _a : new MemoryStorage(), console: consola.create({ level: init.level }).withTag('@foxy.io/sdk'), fetch: (_b = init.fetch) !== null && _b !== void 0 ? _b : fetch, path: [init.base], }); this.console = this._console; this.storage = (_c = init.storage) !== null && _c !== void 0 ? _c : new MemoryStorage(); 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); }); } } /** * 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 = 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 = 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 = 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 = fetch; API.AuthError = AuthError; API.Node = Node; API.v8n = { classConstructor: v8n().schema({ base: v8n().instanceOf(URL), cache: v8n().optional(storageV8N), fetch: v8n().optional(v8n().typeOf('function')), level: v8n().optional(v8n().integer()), storage: v8n().optional(storageV8N), }), };