dotcms
Version:
This library allows you to interact with DotCMS API's easily from the browser, nodejs and React Native. [Full Documentation](https://dotcms.github.io/core-web/dotcms/)
48 lines • 1.63 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.request = void 0;
const tslib_1 = require("tslib");
const cross_fetch_1 = require("cross-fetch");
function getUrl({ params, url }, { host, hostId }) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
if (!host) {
throw new Error('Please pass the DotCMS instance in the initDotCMS initialization');
}
const newUrl = new URL(`${host}${url}`);
const paramsKeys = params ? Object.keys(params) : [];
if (paramsKeys.length) {
paramsKeys.map((key) => {
newUrl.searchParams.append(key, params[key]);
});
}
if (hostId) {
newUrl.searchParams.append('host_id', hostId);
}
return `${newUrl.toString()}`;
});
}
function shouldAppendBody(params) {
return params.method === 'POST' && !!params.body;
}
function getOpts(params, config) {
let opts = {
method: params.method || 'GET',
headers: {
Authorization: `Bearer ${config.token}`,
'Content-type': 'application/json'
}
};
if (shouldAppendBody(params)) {
opts = Object.assign(Object.assign({}, opts), { body: params.body });
}
return opts;
}
function request(options, config) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const url = yield getUrl(options, config);
const opts = getOpts(options, config);
return (0, cross_fetch_1.default)(url, opts);
});
}
exports.request = request;
//# sourceMappingURL=request.js.map
;