@coveo/platform-client
Version:
The main goal of this package is to provide an easy to configure and straightforward way of querying Coveo Cloud APIs using JavaScript.
133 lines • 5.55 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const exponential_backoff_1 = require("exponential-backoff");
const Endpoints_js_1 = tslib_1.__importStar(require("./Endpoints.js"));
const ResponseHandlers_js_1 = tslib_1.__importDefault(require("./handlers/response/ResponseHandlers.js"));
const createFetcher_js_1 = require("./utils/createFetcher.js");
const Retriever_js_1 = tslib_1.__importDefault(require("./utils/Retriever.js"));
const RequestHandlers_js_1 = require("./handlers/request/RequestHandlers.js");
const HEADERS_JSON_CONTENT_TYPE = Object.freeze({ 'Content-Type': 'application/json' });
/**
* Check whether the response status is `429 Too Many Requests`.
* @param response
*/
const isTooManyRequests = (response) => response.status === 429;
/**
* Check whether the method is 'GET' (case insensitive).
* @param value
*/
const isGet = (value) => (value ? /^GET$/i.test(value) : false);
/**
* "Logical OR" two optional abort signals.
* @param signal1
* @param signal2
*/
const abortOnEither = (signal1, signal2) => {
if (signal1 && signal2) {
const joined = new AbortController();
/**
*
*/
function forwardAbort() {
joined.abort(this.reason);
}
signal1.addEventListener('abort', forwardAbort);
signal2.addEventListener('abort', forwardAbort);
return joined.signal;
}
return signal1 ?? signal2;
};
const withBody = (body, userArgs) => userArgs?.body ? undefined : { headers: HEADERS_JSON_CONTENT_TYPE, body: JSON.stringify(body) };
class API {
config;
isServerlessHost;
static orgPlaceholder = '{organizationName}';
getRequestsController;
tokenInfo;
constructor(config, isServerlessHost) {
this.config = config;
this.isServerlessHost = isServerlessHost;
this.getRequestsController = new AbortController();
}
get organizationId() {
if (this.config.organizationId === undefined) {
throw new Error('No organization ID found in the config.');
}
return (0, Retriever_js_1.default)(this.config.organizationId);
}
async get(url, args) {
return await this.request(url, this.buildRequestInit('GET', args), args?.responseBodyFormat);
}
async post(url, body = {}, args) {
return await this.request(url, this.buildRequestInit('POST', args, withBody(body, args)), args?.responseBodyFormat);
}
async postForm(url, body, args) {
return await this.request(url, this.buildRequestInit('POST', args, { body }), args?.responseBodyFormat);
}
async put(url, body = {}, args) {
return await this.request(url, this.buildRequestInit('PUT', args, withBody(body, args)), args?.responseBodyFormat);
}
async patch(url, body = {}, args) {
return await this.request(url, this.buildRequestInit('PATCH', args, withBody(body, args)), args?.responseBodyFormat);
}
async delete(url, args) {
return await this.request(url, this.buildRequestInit('DELETE', args), args?.responseBodyFormat);
}
abortGetRequests() {
this.getRequestsController.abort();
this.getRequestsController = new AbortController();
}
async checkToken() {
const formData = new FormData();
formData.append('token', this.accessToken);
this.tokenInfo = await this.postForm('/oauth/check_token', formData);
}
get currentUser() {
return this.tokenInfo?.authentication;
}
get environment() {
return this.config.environment || Endpoints_js_1.Environment.prod;
}
get region() {
return this.config.region || Endpoints_js_1.Region.US;
}
get endpoint() {
return ((0, Retriever_js_1.default)(this.isServerlessHost ? this.config.serverlessHost : this.config.host) ||
(0, Endpoints_js_1.default)(this.environment, this.region, this.isServerlessHost));
}
get accessToken() {
return (0, Retriever_js_1.default)(this.config.accessToken);
}
getUrlFromRoute(route) {
return `${this.endpoint}${route}`.replace(API.orgPlaceholder, this.organizationId);
}
buildRequestInit(method, args, prefilled) {
const { responseBodyFormat: _, ...requestArgs } = args ?? {};
const globalRequestSettings = this.config.globalRequestSettings;
const init = {
...prefilled,
...globalRequestSettings,
...requestArgs,
method,
headers: {
Authorization: `Bearer ${this.accessToken}`,
...prefilled?.headers,
...globalRequestSettings?.headers,
...requestArgs?.headers,
},
};
if (isGet(init.method)) {
init.signal = abortOnEither(this.getRequestsController.signal, init.signal);
}
return init;
}
async request(route, init, responseBodyFormat) {
const { url: enrichedRoute, ...enrichedRequestInit } = (0, RequestHandlers_js_1.handleRequest)(route, init, this.config.requestHandlers);
const fetcher = (0, createFetcher_js_1.createFetcher)(this.getUrlFromRoute(enrichedRoute), enrichedRequestInit, isTooManyRequests);
const response = await (0, exponential_backoff_1.backOff)(fetcher, { retry: isTooManyRequests });
return (0, ResponseHandlers_js_1.default)(response, this.config.responseHandlers, responseBodyFormat);
}
}
exports.default = API;
//# sourceMappingURL=APICore.js.map