intercom-client
Version:
Official Node bindings to the Intercom API
149 lines (148 loc) • 10.3 kB
JavaScript
// This file was auto-generated by Fern from our API Definition.
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 { normalizeClientOptionsWithAuth } from "../../../../BaseClient.mjs";
import { mergeHeaders, mergeOnlyDefinedHeaders } from "../../../../core/headers.mjs";
import * as core from "../../../../core/index.mjs";
import * as environments from "../../../../environments.mjs";
import { handleNonStatusCodeError } from "../../../../errors/handleNonStatusCodeError.mjs";
import * as errors from "../../../../errors/index.mjs";
import * as Intercom from "../../../index.mjs";
import { CollectionsClient } from "../resources/collections/client/Client.mjs";
export class HelpCentersClient {
constructor(options = {}) {
this._options = normalizeClientOptionsWithAuth(options);
}
get collections() {
var _a;
return ((_a = this._collections) !== null && _a !== void 0 ? _a : (this._collections = new CollectionsClient(this._options)));
}
/**
* You can fetch the details of a single Help Center by making a GET request to `https://api.intercom.io/help_center/help_center/<id>`.
*
* @param {Intercom.FindHelpCenterRequest} request
* @param {HelpCentersClient.RequestOptions} requestOptions - Request-specific configuration.
*
* @throws {@link Intercom.UnauthorizedError}
* @throws {@link Intercom.NotFoundError}
*
* @example
* await client.helpCenters.find({
* help_center_id: 1
* })
*/
find(request, requestOptions) {
return core.HttpResponsePromise.fromPromise(this.__find(request, requestOptions));
}
__find(request, requestOptions) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
const { help_center_id: helpCenterId } = request;
const _authRequest = yield this._options.authProvider.getAuthRequest();
const _headers = mergeHeaders(_authRequest.headers, (_a = this._options) === null || _a === void 0 ? void 0 : _a.headers, mergeOnlyDefinedHeaders({ "Intercom-Version": requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.version }), requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.headers);
const _response = yield ((_b = this._options.fetcher) !== null && _b !== void 0 ? _b : core.fetcher)({
url: core.url.join((_d = (_c = (yield core.Supplier.get(this._options.baseUrl))) !== null && _c !== void 0 ? _c : (yield core.Supplier.get(this._options.environment))) !== null && _d !== void 0 ? _d : environments.IntercomEnvironment.UsProduction, `help_center/help_centers/${core.url.encodePathParam(helpCenterId)}`),
method: "GET",
headers: _headers,
queryParameters: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.queryParams,
timeoutMs: ((_g = (_e = requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.timeoutInSeconds) !== null && _e !== void 0 ? _e : (_f = this._options) === null || _f === void 0 ? void 0 : _f.timeoutInSeconds) !== null && _g !== void 0 ? _g : 20) * 1000,
maxRetries: (_h = requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.maxRetries) !== null && _h !== void 0 ? _h : (_j = this._options) === null || _j === void 0 ? void 0 : _j.maxRetries,
abortSignal: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.abortSignal,
fetchFn: (_k = this._options) === null || _k === void 0 ? void 0 : _k.fetch,
logging: this._options.logging,
});
if (_response.ok) {
return { data: _response.body, rawResponse: _response.rawResponse };
}
if (_response.error.reason === "status-code") {
switch (_response.error.statusCode) {
case 401:
throw new Intercom.UnauthorizedError(_response.error.body, _response.rawResponse);
case 404:
throw new Intercom.NotFoundError(_response.error.body, _response.rawResponse);
default:
throw new errors.IntercomError({
statusCode: _response.error.statusCode,
body: _response.error.body,
rawResponse: _response.rawResponse,
});
}
}
return handleNonStatusCodeError(_response.error, _response.rawResponse, "GET", "/help_center/help_centers/{help_center_id}");
});
}
/**
* You can list all Help Centers by making a GET request to `https://api.intercom.io/help_center/help_centers`.
*
* @param {Intercom.ListHelpCentersRequest} request
* @param {HelpCentersClient.RequestOptions} requestOptions - Request-specific configuration.
*
* @throws {@link Intercom.UnauthorizedError}
*
* @example
* await client.helpCenters.list()
*/
list() {
return __awaiter(this, arguments, void 0, function* (request = {}, requestOptions) {
const list = core.HttpResponsePromise.interceptFunction((request) => __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
const { page, per_page: perPage } = request;
const _queryParams = {};
if (page != null) {
_queryParams.page = page.toString();
}
if (perPage != null) {
_queryParams.per_page = perPage.toString();
}
const _authRequest = yield this._options.authProvider.getAuthRequest();
const _headers = mergeHeaders(_authRequest.headers, (_a = this._options) === null || _a === void 0 ? void 0 : _a.headers, mergeOnlyDefinedHeaders({ "Intercom-Version": requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.version }), requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.headers);
const _response = yield ((_b = this._options.fetcher) !== null && _b !== void 0 ? _b : core.fetcher)({
url: core.url.join((_d = (_c = (yield core.Supplier.get(this._options.baseUrl))) !== null && _c !== void 0 ? _c : (yield core.Supplier.get(this._options.environment))) !== null && _d !== void 0 ? _d : environments.IntercomEnvironment.UsProduction, "help_center/help_centers"),
method: "GET",
headers: _headers,
queryParameters: Object.assign(Object.assign({}, _queryParams), requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.queryParams),
timeoutMs: ((_g = (_e = requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.timeoutInSeconds) !== null && _e !== void 0 ? _e : (_f = this._options) === null || _f === void 0 ? void 0 : _f.timeoutInSeconds) !== null && _g !== void 0 ? _g : 20) * 1000,
maxRetries: (_h = requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.maxRetries) !== null && _h !== void 0 ? _h : (_j = this._options) === null || _j === void 0 ? void 0 : _j.maxRetries,
abortSignal: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.abortSignal,
fetchFn: (_k = this._options) === null || _k === void 0 ? void 0 : _k.fetch,
logging: this._options.logging,
});
if (_response.ok) {
return { data: _response.body, rawResponse: _response.rawResponse };
}
if (_response.error.reason === "status-code") {
switch (_response.error.statusCode) {
case 401:
throw new Intercom.UnauthorizedError(_response.error.body, _response.rawResponse);
default:
throw new errors.IntercomError({
statusCode: _response.error.statusCode,
body: _response.error.body,
rawResponse: _response.rawResponse,
});
}
}
return handleNonStatusCodeError(_response.error, _response.rawResponse, "GET", "/help_center/help_centers");
}));
let _offset = (request === null || request === void 0 ? void 0 : request.page) != null ? request === null || request === void 0 ? void 0 : request.page : 1;
const dataWithRawResponse = yield list(request).withRawResponse();
return new core.Page({
response: dataWithRawResponse.data,
rawResponse: dataWithRawResponse.rawResponse,
hasNextPage: (response) => { var _a; return ((_a = response === null || response === void 0 ? void 0 : response.data) !== null && _a !== void 0 ? _a : []).length > 0; },
getItems: (response) => { var _a; return (_a = response === null || response === void 0 ? void 0 : response.data) !== null && _a !== void 0 ? _a : []; },
loadPage: (_response) => {
_offset += 1;
return list(core.setObjectProperty(request, "page", _offset));
},
});
});
}
}