UNPKG

termii-api-client

Version:

A TypeScript client for interacting with the Termii API.

452 lines (443 loc) 15.2 kB
import axios from 'axios'; var __defProp = Object.defineProperty; var __getOwnPropSymbols = Object.getOwnPropertySymbols; var __hasOwnProp = Object.prototype.hasOwnProperty; var __propIsEnum = Object.prototype.propertyIsEnumerable; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __spreadValues = (a, b) => { for (var prop in b || (b = {})) if (__hasOwnProp.call(b, prop)) __defNormalProp(a, prop, b[prop]); if (__getOwnPropSymbols) for (var prop of __getOwnPropSymbols(b)) { if (__propIsEnum.call(b, prop)) __defNormalProp(a, prop, b[prop]); } return a; }; var __async = (__this, __arguments, generator) => { return new Promise((resolve, reject) => { var fulfilled = (value) => { try { step(generator.next(value)); } catch (e) { reject(e); } }; var rejected = (value) => { try { step(generator.throw(value)); } catch (e) { reject(e); } }; var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected); step((generator = generator.apply(__this, __arguments)).next()); }); }; var axiosInstance = axios.create({ validateStatus: () => true }); // src/campaign/index.ts var Campaign = class { constructor(config) { this.config = config; } /** * Returns available campaigns on the integration. * @returns */ fetch() { return __async(this, null, function* () { const data = (yield axiosInstance.get( this.config.baseUrl + "/api/sms/campaigns?" /* fetchCampaigns */ + new URLSearchParams({ api_key: this.config.apiKey }) )).data; return data; }); } }; // src/messaging/index.ts var Messaging = class { constructor(config) { this.config = config; } /** * Send a custom message. {@link https://developer.termii.com/messaging-api Read Documentation.} * * @example * await client.Messaging.send({ * to: "2347880234567", * from: "talert", * sms: "Hi there, testing Termii", * type: "plain", * channel: "generic", * }); */ send(payload) { return __async(this, null, function* () { const data = (yield axiosInstance.post(this.config.baseUrl + "/api/sms/send" /* sendMessage */, __spreadValues({ api_key: this.config.apiKey }, payload))).data; return data; }); } /** * Send a custom bulk messages. {@link https://developer.termii.com/messaging-api Read Documentation.} * * @example * await client.Messaging.sendBulk({ * to: ["23490555546", "23423490126999","23490555546"], * from: "talert", * sms: "Hi there, testing Termii", * type: "plain", * channel: "generic", * }); */ sendBulk(payload) { return __async(this, null, function* () { const data = (yield axiosInstance.post( this.config.baseUrl + "/api/sms/send/bulk" /* sendBulkMessage */, __spreadValues({ api_key: this.config.apiKey }, payload) )).data; return data; }); } }; // src/phonebooks/index.ts var PhoneBooks = class { constructor(config) { this.config = config; } /** * Fetches the phonebooks available on the integration. {@link https://developer.termii.com/phonebook#fetch-phonebooks Read Documentation.} * * @example * await client.PhoneBooks.fetch(); */ fetch() { return __async(this, null, function* () { const data = (yield axiosInstance.get( this.config.baseUrl + "/api/phonebooks?" /* fetchPhoneBooks */ + new URLSearchParams({ api_key: this.config.apiKey }) )).data; return data; }); } /** * Creates a phonebook. {@link https://developer.termii.com/phonebook#create--a-phonebook Read Documentation.} * @example * await client.PhoneBooks.create({ * phonebook_name:"Phone test", * description:"Phonebook for test" * }); */ create(payload) { return __async(this, null, function* () { const data = (yield axiosInstance.post(this.config.baseUrl + "/api/phonebooks" /* phoneBooksBase */, __spreadValues({ api_key: this.config.apiKey }, payload))).data; return data; }); } /** * Fetches contacts in a phonebook. {@link https://developer.termii.com/contacts#fetch-contacts-by-phonebook-id Read Documentation.} * @param {string} phoneBookID - The ID of the phonebook. * * @example * await client.PhoneBooks.contacts("04c3ebcc-3a7e-485a-88c1-68e731386f77"); */ contacts(phoneBookID) { return __async(this, null, function* () { const data = (yield axiosInstance.get( this.config.baseUrl + "/api/phonebooks" /* phoneBooksBase */ + `/${phoneBookID}/contacts?` + new URLSearchParams({ api_key: this.config.apiKey }) )).data; return data; }); } /** * * @param {string} phoneBookID - The ID of the phonebook. * @param {ContactCreateInput} payload - data of the contact to be added. * @returns */ addContact(phoneBookID, payload) { return __async(this, null, function* () { const data = (yield axiosInstance.post( this.config.baseUrl + "/api/phonebooks" /* phoneBooksBase */ + `/${phoneBookID}/contacts?`, __spreadValues({ api_key: this.config.apiKey }, payload) )).data; return data; }); } }; // src/senderID/index.ts var SenderID = class { constructor(config) { this.config = config; } /** * Fetches the sender IDs available on an integration. {@link https://developer.termii.com/sender-id#fetch-sender-id Read Documentation.} * * @example * await client.SenderID.fetch(); * */ fetch() { return __async(this, null, function* () { const data = (yield axiosInstance.get( this.config.baseUrl + "/api/sender-id?" /* fetchSenderIDs */ + new URLSearchParams({ api_key: this.config.apiKey }) )).data; return data; }); } /** * Request for a sender ID. {@link https://developer.termii.com/sender-id#request-sender-id Read Documentation.} * * @example * await client.SenderID.request({ * sender_id: "Acme", * usecase: "Your OTP code is zxsds", * company: "Acme Corp" * }); * */ request(payload) { return __async(this, null, function* () { const data = (yield axiosInstance.post( this.config.baseUrl + "/api/sender-id/request" /* requestSenderID */, __spreadValues({ api_key: this.config.apiKey }, payload) )).data; return data; }); } }; // src/token/index.ts var Token = class { constructor(config) { this.config = config; } /** * The send token API allows businesses trigger one-time-passwords (OTP) across any available messaging channel on Termii. One-time-passwords created are generated randomly and there's an option to set an expiry time. {@link https://developer.termii.com/send-token Read Documentation.} * @example * await client.Token.send({ * message_type : "NUMERIC", * to : "eg. 2348109077743", * from : "Approved Sender ID or Configuration ID", * channel : "dnd", * pin_attempts : 10, * pin_time_to_live : 5, * pin_length : 6, * pin_placeholder : "< 1234 >", * message_text : "Your pin is < 1234 >", * pin_type : "NUMERIC" * }); */ send(payload) { return __async(this, null, function* () { const data = (yield axiosInstance.post(this.config.baseUrl + "/api/sms/otp/send" /* sendToken */, __spreadValues({ api_key: this.config.apiKey }, payload))).data; return data; }); } /** * The voice token API enables you to generate and trigger one-time passwords (OTP) through the voice channel to a phone number. OTPs are generated and sent to the phone number and can only be verified using our Verify Token API. {@link https://developer.termii.com/voice-token Read Documentation.} * * @example * await client.Token.voice({ * phone_number : "23409800000000", * pin_attempts : 10, * pin_time_to_live : 5, * pin_length : 6 * }); */ voice(payload) { return __async(this, null, function* () { const data = (yield axiosInstance.post(this.config.baseUrl + "/api/sms/otp/send/voice" /* voiceToken */, __spreadValues({ api_key: this.config.apiKey }, payload))).data; return data; }); } /** * The voice call API enables you to send messages from your application through our voice channel to a phone number. Only one-time-passwords (OTP) are allowed for now and these OTPs can not be verified using our Verify Token API. {@link https://developer.termii.com/voice-call Read Documentation.} * * @example * await client.Token.voiceCall({ * phone_number : "2349800000000", * code : 55675, * }); * */ voiceCall(payload) { return __async(this, null, function* () { const data = (yield axiosInstance.post(this.config.baseUrl + "/api/sms/otp/call" /* voiceCall */, __spreadValues({ api_key: this.config.apiKey }, payload))).data; return data; }); } /** * The email token API enables you to send one-time-passwords from your application through our email channel to an email address. Only one-time-passwords (OTP) are allowed for now and these OTPs can not be verified using our Verify Token API. {@link https://developer.termii.com/email-token Read Documentation.} * * @example * await client.Token.email({ * email_address: "shola.olu@term.ii", * code: "092471", * email_configuration_id: "0a53c416-uocj-95af-ab3c306aellc" * }); * */ email(payload) { return __async(this, null, function* () { const data = (yield axiosInstance.post(this.config.baseUrl + "/api/email/otp/send" /* emailToken */, __spreadValues({ api_key: this.config.apiKey }, payload))).data; return data; }); } /** * * This API returns OTP codes in JSON format which can be used within any web or mobile app. Tokens are numeric or alpha-numeric codes generated to authenticate login requests and verify customer transactions. {@link https://developer.termii.com/in-app-token Read Documentation.} * * @example * await client.Token.inApp({ * pin_type: "NUMERIC", * phone_number: "2348109477743", * pin_attempts: 3, * pin_time_to_live: 0, * pin_length: 4 * }); */ inApp(payload) { return __async(this, null, function* () { const data = (yield axiosInstance.post(this.config.baseUrl + "/api/sms/otp/generate" /* inAppToken */, __spreadValues({ api_key: this.config.apiKey }, payload))).data; return data; }); } /** * Verify token API, checks tokens sent to customers and returns a response confirming the status of the token. A token can either be confirmed as verified or expired based on the timer set for the token. {@link https://developer.termii.com/verify-token Read Documentation.} * * @param {string} pin_id ID of the PIN sent * @param {string} pin The PIN code * * @example * await client.Token.verify("c8dcd048-5e7f-4347-8c89-4470c3af0b", "195558"); */ verify(pin_id, pin) { return __async(this, null, function* () { const data = (yield axiosInstance.post(this.config.baseUrl + "/api/sms/otp/verify" /* verifyToken */, { api_key: this.config.apiKey, pin_id, pin })).data; return data; }); } }; // src/client.ts var TermiiClient = class { constructor(config) { this.config = config; this.Messaging = new Messaging(config); this.SenderID = new SenderID(config); this.PhoneBooks = new PhoneBooks(config); this.Token = new Token(config); this.Campaign = new Campaign(config); } /** * The Balance API returns your total balance and balance information from your wallet, such as currency. {@link https://developer.termii.com/balance Read Documentation.} * * @example * await client.getBalance(); */ getBalance() { return __async(this, null, function* () { const data = (yield axiosInstance.get( this.config.baseUrl + "/api/get-balance?" /* getBalance */ + new URLSearchParams({ api_key: this.config.apiKey }) )).data; return data; }); } /** * The status API allows businesses to detect if a number is fake or has ported to a new network. {@link https://developer.termii.com/status Read Documentation.} * @param {string} phoneNumber - Represents the phone number to be verified. Phone number must be in the international format * @param {string} countryCode - Represents short alphabetic codes developed to represent countries. * * @example * await client.checkNumberStatus("2348753243651", "NG"); */ checkNumberStatus(phoneNumber, countryCode) { return __async(this, null, function* () { const data = (yield axiosInstance.get( this.config.baseUrl + "/api/insight/number/query?" /* checkNumberStatus */ + new URLSearchParams({ api_key: this.config.apiKey, phone_number: phoneNumber, country_code: countryCode }) )).data; return data; }); } /** * The search API allows businesses verify phone numbers and automatically detect their status as well as current network. It also tells if the number has activated the do-not-disturb settings. * @param {string} phoneNumber - Represents the phone number to be verified. Phone number must be in the international format. {@link https://developer.termii.com/search Read Documentation.} * * @example * await client.searchNumber("2348753243651"); */ searchNumber(phoneNumber) { return __async(this, null, function* () { const data = (yield axiosInstance.get( this.config.baseUrl + "/api/check/dnd?" /* searchNumber */ + new URLSearchParams({ api_key: this.config.apiKey, phone_number: phoneNumber }) )).data; return data; }); } /** * This Inbox API returns reports for messages sent across the sms, voice & whatsapp channels. Reports can either display all messages on termii or a single message. Optionally accepts a `message_id` to query the report of a single message. {@link https://developer.termii.com/history Read Documentation.} * * @example * // All messages * await client.messageReports(); * * // Single message * await client.messageReports("04c3ebcc-3a7e-485a-88c1-68e731386f77"); * */ messageReports(message_id) { return __async(this, null, function* () { const data = (yield axiosInstance.get( this.config.baseUrl + "/api/sms/inbox?" /* getMessageReports */ + new URLSearchParams(__spreadValues({ api_key: this.config.apiKey }, message_id ? { message_id } : {})) )).data; return data; }); } }; // src/index.ts var index_default = TermiiClient; export { index_default as default }; //# sourceMappingURL=index.mjs.map //# sourceMappingURL=index.mjs.map