UNPKG

node-calendly

Version:
97 lines 4.28 kB
"use strict"; 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 __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); /****************************************** * Author : Dr. Sebastian Herden * Created On : Fri Sep 16 2022 * File : Api.ts *******************************************/ const stopcock_1 = __importDefault(require("stopcock")); const Logger_1 = require("../Logger"); const logger = Logger_1.LogHandler.newInstance(); const LOG_NAMESPACE = 'API CALL'; class CalendlyApiEndpoint { constructor(ACCESS_TOKEN, params) { this.options = { timeout: (params === null || params === void 0 ? void 0 : params.timeout) || 60000, limit: (params === null || params === void 0 ? void 0 : params.limit) || 2, interval: (params === null || params === void 0 ? void 0 : params.interval) || 1000, bucketSize: (params === null || params === void 0 ? void 0 : params.bucketSize) || 35 }; this.ACCESS_TOKEN = ACCESS_TOKEN; // this.fetchGet = stopcock(this.fetchGet, this.options); // this.fetchPost = stopcock(this.fetchPost, this.options); this.request = (0, stopcock_1.default)(this.request, this.options); } fetchGet(url) { return __awaiter(this, void 0, void 0, function* () { return this.request(url, { method: 'GET', headers: { Authorization: `Bearer ${this.ACCESS_TOKEN}`, 'Content-Type': 'application/json' } }); }); } fetchPost(url, body) { return __awaiter(this, void 0, void 0, function* () { return this.request(url, { method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${this.ACCESS_TOKEN}` }, body }); }); } request(uri, options) { return __awaiter(this, void 0, void 0, function* () { return new Promise((resolve, reject) => { fetch(uri, options) .then((response) => { // console.log(response.headers); if (response.status === 429) { reject('Too many requests'); } else if (!response.ok) { logger.error(`Error fetching data. Response status ${response.status}`, LOG_NAMESPACE, 'request'); console.error(uri, response.status, response.statusText); if (Logger_1.DEBUG_LEVEL === 'debug') console.error(options); } if (response.status === 202) { return null; } return response.json().catch((error) => { logger.error(`Error parsing response: ${error}`, LOG_NAMESPACE, 'request'); return null; }); }) .then((data) => { if (data === null) { resolve(null); } resolve(data); }) .catch((error) => { reject(error); }); }); }); } } exports.default = CalendlyApiEndpoint; //# sourceMappingURL=CalendlyApiEndpoint.js.map