UNPKG

@prezly/theme-kit-core

Version:

Data layer and utility library for developing Prezly themes with JavaScript

66 lines (65 loc) 3.94 kB
"use strict"; 'use client'; /* eslint-disable @typescript-eslint/no-use-before-define */ Object.defineProperty(exports, "__esModule", { value: true }); exports.HttpError = void 0; exports.create = create; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); } function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; } function create() { var _options$fetch, _options$headers; var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var fetchImpl = (_options$fetch = options.fetch) !== null && _options$fetch !== void 0 ? _options$fetch : fetch; var headers = (_options$headers = options.headers) !== null && _options$headers !== void 0 ? _options$headers : {}; return { get(path) { var _arguments = arguments; return _asyncToGenerator(function* () { var query = _arguments.length > 1 && _arguments[1] !== undefined ? _arguments[1] : {}; var searchParams = Object.entries(query).filter(_ref => { var [, value] = _ref; return value !== null && value !== undefined; }).reduce((result, _ref2) => { var [name, value] = _ref2; return [...result, "".concat(name, "=").concat(encodeURIComponent(value !== null && value !== void 0 ? value : ''))]; }, []).join('&'); var url = searchParams ? "".concat(path, "?").concat(searchParams) : path; var response = yield fetchImpl(url, { headers }); if (!response.ok) { var status = { code: response.status, text: response.statusText }; throw new HttpError(status, response.headers, yield response.text()); } return response.json(); })(); }, withHeaders(extraHeaders) { return create(_objectSpread(_objectSpread({}, options), {}, { headers: _objectSpread(_objectSpread({}, headers), extraHeaders) })); } }; } class HttpError extends Error { constructor(status, headers, body) { super(status.text); _defineProperty(this, "status", void 0); _defineProperty(this, "body", void 0); _defineProperty(this, "headers", void 0); this.status = status; this.headers = headers; this.body = body; } } exports.HttpError = HttpError;