@calculusky/meta-whatsapp-sdk
Version:
Meta WhatsApp SDK for the Cloud API
67 lines • 2.92 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const https_1 = require("https");
const errors_1 = require("./errors");
class HttpsClient {
constructor(clientOptions) {
this.clientOptions = clientOptions;
}
sendRequest(options) {
return new Promise((resolve, reject) => {
const req = (0, https_1.request)({
hostname: this.clientOptions.baseURL,
method: options.method,
path: options.path,
headers: this.clientOptions.headers,
}, (res) => {
const clientResponse = new HttpsClientResponse();
clientResponse.status = res.statusCode;
let resp = "";
res.on("error", (error) => {
var _a;
const err = new errors_1.WhatsAppError((_a = error.message) !== null && _a !== void 0 ? _a : "Something went wrong");
err.status = 500;
err.stack = error.stack;
reject(err);
});
res.on("data", (chunk) => {
resp += chunk.toString();
});
res.on("end", () => {
var _a, _b, _c, _d;
try {
const respObj = JSON.parse(resp);
if (clientResponse.status >= 400) {
const err = new errors_1.WhatsAppError((_b = (_a = respObj === null || respObj === void 0 ? void 0 : respObj.error) === null || _a === void 0 ? void 0 : _a.message) !== null && _b !== void 0 ? _b : "Something went wrong");
err.status = clientResponse.status;
err.code = (_c = respObj === null || respObj === void 0 ? void 0 : respObj.error) === null || _c === void 0 ? void 0 : _c.code;
reject(err);
}
else {
clientResponse.data = respObj;
resolve(clientResponse);
}
}
catch (error) {
const err = new errors_1.WhatsAppError((_d = error.message) !== null && _d !== void 0 ? _d : "Something went wrong");
err.status = 500;
err.stack = error.stack;
reject(err);
}
});
});
const postDataMethods = ["POST", "PUT"];
req.on("error", (error) => {
reject(error);
});
if (postDataMethods.includes(options.method)) {
req.write(options.requestData);
}
req.end();
});
}
}
exports.default = HttpsClient;
class HttpsClientResponse {
}
//# sourceMappingURL=httpsClient.js.map