UNPKG

@sonatel-os/juf

Version:

The community SDK for Orange Money, SMS, Email & Sonatel APIs on the Orange Developer Platform.

1 lines 10.9 kB
{"version":3,"sources":["/Users/JOHNSON028877/Documents/new-juf.js/dist/chunk-WRKKDVFU.cjs","../src/communication/communication.structure.js","../src/communication/communicationService.js"],"names":["logger"],"mappings":"AAAA;AACE;AACF,wDAA6B;AAC7B;AACE;AACA;AACA;AACA;AACF,wDAA6B;AAC7B;AACE;AACA;AACA;AACA;AACA;AACF,wDAA6B;AAC7B;AACA;ACjBA,0CAA0D;AAEnD,IAAM,eAAA,EAAiB,iCAAA;AAAO,EACnC,OAAA,EAAS,iCAAA,CAAO;AAAA,EAChB,EAAA,EAAI,iCAAA,CAAO;AAAA,EACX,IAAA,EAAM,iCAAA,CAAO;AAAA,EACb,IAAA,EAAM,iCAAA,CAAO;AAAA,EACb,IAAA,EAAM,mCAAA,kCAAS,CAAS;AAC1B,CAAC,CAAA;AAED,IAAM,eAAA,EAAiB,iCAAA,gBAAO,EAAkB,CAAC,KAAA,EAAA,GAAU;AACzD,EAAA,OAAO,OAAO,MAAA,IAAU,SAAA,GAAY,CAAC,KAAA,CAAM,IAAA,CAAK,KAAA,CAAM,KAAK,CAAC,CAAA;AAC9D,CAAC,CAAA;AAGM,IAAM,aAAA,EAAe,iCAAA;AAAO,EACjC,IAAA,EAAM,iCAAA,CAAO;AAAA,EACb,EAAA,EAAI,iCAAA,CAAO;AAAA,EACX,YAAA,EAAc,mCAAA,kCAAS,CAAS,CAAA;AAAA,EAChC,YAAA,EAAc,mCAAA,cAAuB,CAAA;AAAA,EACrC,UAAA,EAAY,iCAAA;AACd,CAAC,CAAA;ADeD;AACA;AENA,IAAM,cAAA,EAAN,MAAM,eAAc;AAAA;AAAA,EAElB,CAAA,WAAA;AAAA;AAAA,EAGA,CAAA,MAAA;AAAA;AAAA,EAGA,CAAA,MAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUA,WAAA,CA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,IAAA,IAAI;AACF,MAAA,MAAM,EAAE,KAAK,EAAA,EAAI,MAAM,IAAA,CAAK,CAAA,MAAA,CAAQ,IAAA,CAAK,GAAA,EAAK,OAAA,EAAS,EAAE,QAAQ,CAAC,CAAA;AAClE,MAAA,OAAO,IAAA;AAAA,IACT,EAAA,MAAA,CAAS,KAAA,EAAO;AACd,MAAA,IAAA,CAAK,CAAA,MAAA,CAAQ,KAAA,CAAM,8BAAA,EAAgC,EAAE,GAAA,EAAK,MAAA,kBAAQ,KAAA,mBAAM,QAAA,6BAAU,SAAO,CAAC,CAAA;AAC1F,MAAA,MAAM,8CAAA,KAAe,EAAO,wDAAwD,CAAA;AAAA,IACtF;AAAA,EACF;AACF,CAAA;AAEA,IAAO,6BAAA,EAAQ,aAAA;AFPf;AACA;AACE;AACA;AACA;AACF,kJAAC","file":"/Users/JOHNSON028877/Documents/new-juf.js/dist/chunk-WRKKDVFU.cjs","sourcesContent":[null,"import { object, string, boolean, optional, define } from 'superstruct';\n\nexport const EmailStructure = object({\n subject: string(),\n to: string(),\n from: string(),\n body: string(),\n html: optional(boolean()),\n});\n\nconst DateTimeString = define('DateTimeString', (value) => {\n return typeof value === 'string' && !isNaN(Date.parse(value));\n});\n\n// Define the structure for SMS parameters\nexport const SmsStructure = object({\n body: string(),\n to: string(),\n confidential: optional(boolean()),\n scheduledFor: optional(DateTimeString),\n senderName: string(),\n});\n","/**\n * Communication\n * @namespace Service\\Communication\n */\n\nimport Authentication from '../auth/authenticationService.js';\nimport { EMAIL_URI, SMS_URI } from '../core/urls.js';\nimport { getApiUrl } from '../core/apiUrl.js';\nimport Requester from '../core/requester.js';\nimport { EmailStructure, SmsStructure } from './communication.structure.js';\nimport { logger as rootLogger } from '../core/logger.js';\nimport { validate } from '../core/validation.js';\nimport { fromAxiosError } from '../core/errors.js';\nimport { SMS_CHANNEL } from '../core/constants.js';\nimport { buildAuthHeader } from '../core/authHeader.js';\n\n/**\n * @class Communication\n * @classdesc Provides methods for sending emails and SMS through the Apigee API.\n *\n * Supports dependency injection for testability — pass dependencies via constructor,\n * or use the {@link Communication.init} factory for default behavior.\n *\n * @example\n * // Default usage (backward compatible)\n * const commService = Communication.init();\n *\n * @example\n * // With dependency injection (for testing)\n * const commService = new Communication({ authService: mockAuth, client: mockAxios, logger: mockLogger });\n */\nclass Communication {\n /** @private @type {Authentication} */\n #authService;\n\n /** @private @type {import('axios').AxiosInstance} */\n #client;\n\n /** @private */\n #logger;\n\n /**\n * Creates a Communication instance with injectable dependencies.\n *\n * @param {object} deps - Dependencies for the communication service.\n * @param {Authentication} deps.authService - Authentication service instance.\n * @param {import('axios').AxiosInstance} deps.client - HTTP client instance.\n * @param {object} deps.logger - Logger instance with error/warn/info/debug methods.\n */\n constructor({ authService, client, logger }) {\n this.#authService = authService;\n this.#client = client;\n this.#logger = logger;\n }\n\n /**\n * Factory method to initialize Communication with default dependencies.\n * @method init\n * @memberof Service\\Communication\n * @param {object} [deps] - Optional shared dependencies.\n * @param {Authentication} [deps.authService] - Shared auth instance (avoids duplicate token fetches).\n * @returns {Communication} An initialized instance of Communication.\n */\n static init({ authService } = {}) {\n return new Communication({\n authService: authService || Authentication.init(),\n client: Requester.bootstrap({ baseURL: getApiUrl() }),\n logger: rootLogger.child('communication'),\n });\n }\n\n /**\n * Sends an email through the Apigee API.\n *\n * @async\n * @method sendEmail\n * @memberof Service\\Communication\n * @param {object} emailParams - Parameters for the email to be sent.\n * @param {string} emailParams.subject - The subject of the email.\n * @param {string} emailParams.to - The email address of the recipient.\n * @param {string} emailParams.from - The email address of the sender.\n * @param {string} emailParams.body - The plaintext body of the email.\n * @param {boolean} [emailParams.html] - Whether the body is HTML (optional).\n * @returns {Promise<{ id: string, status: string }>} The response from the Apigee API.\n * @throws {import('../core/errors.js').ValidationError} When input validation fails.\n * @throws {import('../core/errors.js').ExternalServiceError} When the API request fails.\n *\n * @example\n * communication.sendEmail({\n * subject: 'Hello!',\n * to: 'recipient@example.com',\n * from: 'sender@example.com',\n * body: '<p>This is a test email.</p>',\n * html: true\n * });\n */\n async sendEmail({ subject, to, from, body, html }) {\n validate({ subject, to, from, body, html }, EmailStructure, 'sendEmail');\n\n const headers = await this.#getAuthHeaders();\n\n const payload = {\n subject,\n from,\n to,\n body,\n ...(html && { html }),\n };\n\n return this.#sendRequest(EMAIL_URI, payload, headers);\n }\n\n /**\n * Sends an SMS through the Apigee API.\n *\n * @async\n * @method sendSMS\n * @memberof Service\\Communication\n * @param {object} smsParams - Parameters for the SMS to be sent.\n * @param {string} smsParams.body - The message body of the SMS.\n * @param {boolean} [smsParams.confidential=true] - Whether the message is confidential.\n * @param {string} [smsParams.scheduledFor] - Scheduled time (ISO 8601 format).\n * @param {string} smsParams.senderName - The name of the sender.\n * @param {string} smsParams.to - The phone number of the recipient.\n * @returns {Promise<{ id: string, status: string }>} The response from the Apigee API.\n * @throws {import('../core/errors.js').ValidationError} When input validation fails.\n * @throws {import('../core/errors.js').ExternalServiceError} When the API request fails.\n *\n * @example\n * communication.sendSMS({\n * body: 'This is a test SMS.',\n * to: '+1234567890',\n * senderName: 'MyApp'\n * });\n */\n async sendSMS({ body, confidential = true, scheduledFor, senderName, to }) {\n validate({ body, confidential, scheduledFor, senderName, to }, SmsStructure, 'sendSMS');\n\n const headers = await this.#getAuthHeaders();\n\n const payload = {\n body,\n channel: SMS_CHANNEL,\n confidential,\n ...(scheduledFor && { scheduledFor }),\n ...(senderName && { senderName }),\n to,\n };\n\n return this.#sendRequest(SMS_URI, payload, headers);\n }\n\n /**\n * @private\n * @returns {Promise<{ Authorization: string }>} Authorization headers.\n */\n async #getAuthHeaders() {\n const { access_token, token_type } = await this.#authService.debug();\n return buildAuthHeader(access_token, token_type);\n }\n\n /**\n * @private\n * @param {string} uri - The API endpoint path.\n * @param {object} payload - The request payload.\n * @param {object} headers - The request headers.\n * @returns {Promise<object>} The response data.\n * @throws {import('../core/errors.js').ExternalServiceError} On request failure.\n */\n async #sendRequest(uri, payload, headers) {\n try {\n const { data } = await this.#client.post(uri, payload, { headers });\n return data;\n } catch (error) {\n this.#logger.error('Communication request failed', { uri, status: error.response?.status });\n throw fromAxiosError(error, 'Request failed. Please check your input and try again.');\n }\n }\n}\n\nexport default Communication;\n"]}