UNPKG

@calculusky/meta-whatsapp-sdk

Version:
59 lines 1.96 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Requester = void 0; const httpsClient_1 = require("./httpsClient"); const errors_1 = require("./errors"); class Requester { constructor(options) { this.options = options; const config = this.buildConfig(); this.client = new httpsClient_1.default(config); } buildConfig() { const clientOptions = { baseURL: this.options.baseUrl, headers: { Authorization: `Bearer ${this.options.accessToken}`, "Content-Type": "application/json", }, }; return clientOptions; } updateAccessToken(token) { this.options.accessToken = token; const config = this.buildConfig(); this.client = new httpsClient_1.default(config); } updatePhoneNumberId(phoneNumberId) { this.options.phoneNumberId = phoneNumberId; } buildPath(endpoint) { return `/${this.options.apiVersion}/${this.options.phoneNumberId}/${endpoint}`; } async sendAPIRequest(options) { var _a; try { const resp = await this.client.sendRequest({ method: options.method, path: this.buildPath(options.endpoint), requestData: options.body, }); return resp.data; } catch (error) { switch (true) { case error instanceof errors_1.WhatsAppError: { throw error; } default: { const err = new errors_1.WhatsAppError((_a = error.message) !== null && _a !== void 0 ? _a : "Something went wrong"); err.stack = error.stack; err.status = 500; throw err; } } } } } exports.Requester = Requester; //# sourceMappingURL=requester.js.map