whatsapp-mdf
Version:
SDK for interfacing with WhatsApp Business Platform in Typescript or Node.js using the Cloud API, hosted by Meta.
96 lines (95 loc) • 4.06 kB
JavaScript
;
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the license found in the
* LICENSE file in the root directory of this source tree.
*/
var __createBinding = this && this.__createBinding || (Object.create ? function (o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = {
enumerable: true,
get: function () {
return m[k];
}
};
}
Object.defineProperty(o, k2, desc);
} : function (o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
});
var __setModuleDefault = this && this.__setModuleDefault || (Object.create ? function (o, v) {
Object.defineProperty(o, "default", {
enumerable: true,
value: v
});
} : function (o, v) {
o["default"] = v;
});
var __importStar = this && this.__importStar || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = this && this.__importDefault || function (mod) {
return mod && mod.__esModule ? mod : {
"default": mod
};
};
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.generateXHub256Sig = exports.importConfig = void 0;
const crypto = __importStar(require("crypto"));
const enums_1 = require("./types/enums");
const logger_1 = __importDefault(require("./logger"));
const LIB_NAME = 'UTILS';
const LOG_LOCAL = false;
const LOGGER = new logger_1.default(LIB_NAME, process.env.DEBUG === 'true' || LOG_LOCAL);
const DEFAULT_BASE_URL = 'graph.facebook.com';
const DEFAULT_LISTENER_PORT = 3000;
const DEFAULT_MAX_RETRIES_AFTER_WAIT = 30;
const DEFAULT_REQUEST_TIMEOUT = 20000;
const emptyConfigChecker = partialConfig => {
if (!partialConfig?.WA_PHONE_NUMBER_ID) {
LOGGER.log(`Environmental variable: WA_PHONE_NUMBER_ID and/or sender phone number id arguement is undefined.`);
throw new Error('Missing WhatsApp sender phone number Id.');
}
for (const value of Object.values(enums_1.WARequiredConfigEnum)) {
if (!partialConfig?.[value]) {
LOGGER.log(`Environmental variable: ${value} is undefined`);
throw new Error('Invalid configuration.');
}
}
};
const importConfig = partialConfig => {
emptyConfigChecker(partialConfig);
const config = {
[enums_1.WAConfigEnum.BaseURL]: partialConfig?.WA_BASE_URL || DEFAULT_BASE_URL,
[enums_1.WAConfigEnum.AppId]: partialConfig?.M4D_APP_ID || '',
[enums_1.WAConfigEnum.AppSecret]: partialConfig?.M4D_APP_SECRET || '',
[enums_1.WAConfigEnum.PhoneNumberId]: partialConfig?.WA_PHONE_NUMBER_ID || 0,
[enums_1.WAConfigEnum.BusinessAcctId]: partialConfig?.WA_BUSINESS_ACCOUNT_ID || '',
[enums_1.WAConfigEnum.APIVersion]: partialConfig?.CLOUD_API_VERSION || '',
[enums_1.WAConfigEnum.AccessToken]: partialConfig?.CLOUD_API_ACCESS_TOKEN || '',
[enums_1.WAConfigEnum.WebhookEndpoint]: partialConfig?.WEBHOOK_ENDPOINT || '',
[enums_1.WAConfigEnum.WebhookVerificationToken]: partialConfig?.WEBHOOK_VERIFICATION_TOKEN || '',
[enums_1.WAConfigEnum.ListenerPort]: partialConfig?.LISTENER_PORT || DEFAULT_LISTENER_PORT,
[enums_1.WAConfigEnum.MaxRetriesAfterWait]: partialConfig?.MAX_RETRIES_AFTER_WAIT || DEFAULT_MAX_RETRIES_AFTER_WAIT,
[enums_1.WAConfigEnum.RequestTimeout]: partialConfig?.REQUEST_TIMEOUT || DEFAULT_REQUEST_TIMEOUT,
[enums_1.WAConfigEnum.Debug]: partialConfig?.DEBUG || process.env.DEBUG === 'true'
};
LOGGER.log(`Configuration loaded for App Id ${config[enums_1.WAConfigEnum.AppId]}`);
return config;
};
exports.importConfig = importConfig;
const generateXHub256Sig = (body, appSecret) => {
return crypto.createHmac('sha256', appSecret).update(body, 'utf-8').digest('hex');
};
exports.generateXHub256Sig = generateXHub256Sig;