@foxy.io/sdk
Version:
Universal SDK for a full server-side and a limited in-browser access to Foxy hAPI.
108 lines (107 loc) • 4.85 kB
JavaScript
;
/* eslint-disable @typescript-eslint/ban-types */
/* eslint-disable @typescript-eslint/no-unused-vars */
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 __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Response = void 0;
const consola_1 = __importDefault(require("consola"));
const v8n_js_1 = require("../v8n.js");
const cross_fetch_1 = require("cross-fetch");
const Node_js_1 = require("./Node.js");
/**
* Adds {@link Node} methods such as `.get()` or `.follow()` to each value in resource `_links`.
*
* @param params Response parameters and JSON.
* @returns Enriched JSON including followable links.
*/
function addFollowableLinks(params) {
const { json } = params, nodeInit = __rest(params, ["json"]);
if ('_links' in json) {
const links = json._links;
json._links = Object.entries(links).reduce((links, [curie, link]) => {
if (Array.isArray(link))
return Object.assign(Object.assign({}, links), { [curie]: link });
const node = new Node_js_1.Node(Object.assign(Object.assign({}, nodeInit), { path: [new URL(link.href)] }));
const methods = {
delete: node.delete.bind(node),
follow: node.follow.bind(node),
get: node.get.bind(node),
patch: node.patch.bind(node),
post: node.post.bind(node),
put: node.put.bind(node),
};
return Object.assign(Object.assign({}, links), { [curie]: Object.assign(Object.assign({}, link), methods) });
}, {});
}
if ('_embedded' in json) {
const embeds = json._embedded;
json._embedded = Object.entries(embeds).reduce((embeds, [embedCurie, embedJSON]) => Object.assign(embeds, {
[embedCurie]: Array.isArray(embedJSON)
? embedJSON.map(itemJSON => addFollowableLinks(Object.assign(Object.assign({}, nodeInit), { json: itemJSON })))
: addFollowableLinks(Object.assign(Object.assign({}, nodeInit), { json: embedJSON })),
}), {});
}
return json;
}
/**
* Base class representing any response returned by API. Extends the native Response
* object of Fetch API and has all the data of the original response in addition
* to a few custom methods. You shouldn't need to construct instances of this
* class directly unless you're building a custom API client with this SDK.
*/
class Response extends cross_fetch_1.Response {
constructor(init) {
Response.v8n.constructor.check(init);
super(init.body, init);
this._console = init.console;
this._fetch = init.fetch;
this._cache = init.cache;
}
/**
* Gets JSON data from the response body and generates
* a followable response with a number of shortcuts
* for the most common actions.
*
* @returns Followable API response.
*/
json() {
const _super = Object.create(null, {
json: { get: () => super.json }
});
return __awaiter(this, void 0, void 0, function* () {
const json = yield _super.json.call(this);
const config = { cache: this._cache, console: this._console, fetch: this._fetch };
return addFollowableLinks(Object.assign({ json }, config));
});
}
}
exports.Response = Response;
Response.v8n = {
constructor: v8n_js_1.v8n().schema({
cache: v8n_js_1.storageV8N,
console: v8n_js_1.v8n().instanceOf(consola_1.default.constructor),
fetch: v8n_js_1.v8n().typeOf('function'),
}),
};