UNPKG

ippanel-node-sdk

Version:
143 lines (128 loc) 3.83 kB
/** * ippanel Node.js SDK * A simple client for interacting with the ippanel API for sending messages. */ const axios = require('axios'); const DEFAULT_BASE_URL = 'https://edge.ippanel.com/v1/api'; /** * Client class for interfacing with the ippanel API. */ class Client { /** * Create a new ippanel client. * @param {string} apiKey - Your ippanel API key * @param {string} [baseUrl] - Optional custom API base URL */ constructor(apiKey, baseUrl = DEFAULT_BASE_URL) { if (!apiKey) { throw new Error('API key is required'); } this.apiKey = apiKey; this.baseUrl = baseUrl; this.httpClient = axios.create({ timeout: 10000, // 10 seconds timeout headers: { 'Authorization': apiKey, 'Content-Type': 'application/json' } }); } /** * Send a web service message. * @param {string} message - The message to send * @param {string} sender - The sender number * @param {string[]} recipients - Array of recipient phone numbers * @returns {Promise<Object>} API response */ async sendWebservice(message, sender, recipients) { const payload = { from_number: sender, params: { recipients }, message, sending_type: 'webservice' }; return this._post('/send', payload); } /** * Send a message using a predefined pattern. * @param {string} patternCode - The pattern code * @param {string} sender - The sender number * @param {string} recipient - The recipient phone number * @param {Object} params - Pattern parameters * @returns {Promise<Object>} API response */ async sendPattern(patternCode, sender, recipient, params) { const payload = { from_number: sender, recipients: [recipient], code: patternCode, params, sending_type: 'pattern' }; return this._post('/send', payload); } /** * Send a verification OTP (VOTP) message. * @param {number} code - The OTP code * @param {string} recipient - The recipient phone number * @returns {Promise<Object>} API response */ async sendVOTP(code, recipient) { const payload = { message: code.toString(), sending_type: 'votp', params: { recipients: [recipient] } }; return this._post('/send', payload); } /** * Make a POST request to the API. * @private * @param {string} path - API endpoint path * @param {Object} payload - Request payload * @returns {Promise<Object>} API response */ async _post(path, payload) { try { const response = await this.httpClient.post(`${this.baseUrl}${path}`, payload); return response.data; } catch (error) { if (error.response) { // The request was made and the server responded with a status code // that falls out of the range of 2xx throw new Error(`API Error: ${error.response.status} - ${JSON.stringify(error.response.data)}`); } else if (error.request) { // The request was made but no response was received throw new Error('No response received from the API'); } else { // Something happened in setting up the request throw new Error(`Request failed: ${error.message}`); } } } /** * Set a custom HTTP client or override HTTP client settings. * @param {Object} httpClient - Custom HTTP client or settings */ setHttpClient(httpClient) { if (httpClient) { this.httpClient = httpClient; } } } module.exports = { Client, /** * Create a new ippanel client. * @param {string} apiKey - Your ippanel API key * @param {string} [baseUrl] - Optional custom API base URL * @returns {Client} A new Client instance */ createClient(apiKey, baseUrl) { return new Client(apiKey, baseUrl); } };