@depup/africastalking 0.7.9-depup.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017 Africa's Talking
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,32 @@
1
+ # @depup/africastalking
2
+
3
+ > Dependency-bumped version of [africastalking](https://www.npmjs.com/package/africastalking)
4
+
5
+ Generated by [DepUp](https://github.com/depup/npm) -- all production
6
+ dependencies bumped to latest versions.
7
+
8
+ ## Installation
9
+
10
+ ```bash
11
+ npm install @depup/africastalking
12
+ ```
13
+
14
+ | Field | Value |
15
+ |-------|-------|
16
+ | Original | [africastalking](https://www.npmjs.com/package/africastalking) @ 0.7.9 |
17
+ | Processed | 2026-03-19 |
18
+ | Smoke test | passed |
19
+ | Deps updated | 2 |
20
+
21
+ ## Dependency Changes
22
+
23
+ | Dependency | From | To |
24
+ |------------|------|-----|
25
+ | axios | 1.13.5 | ^1.13.6 |
26
+ | dotenv | 17.2.4 | ^17.3.1 |
27
+
28
+ ---
29
+
30
+ Source: https://github.com/depup/npm | Original: https://www.npmjs.com/package/africastalking
31
+
32
+ License inherited from the original package.
package/changes.json ADDED
@@ -0,0 +1,14 @@
1
+ {
2
+ "bumped": {
3
+ "axios": {
4
+ "from": "1.13.5",
5
+ "to": "^1.13.6"
6
+ },
7
+ "dotenv": {
8
+ "from": "17.2.4",
9
+ "to": "^17.3.1"
10
+ }
11
+ },
12
+ "timestamp": "2026-03-19T03:32:18.199Z",
13
+ "totalUpdated": 2
14
+ }
package/index.js ADDED
@@ -0,0 +1,3 @@
1
+ 'use strict'
2
+
3
+ module.exports = require('./lib')
@@ -0,0 +1,329 @@
1
+ const Joi = require('joi')
2
+ const _ = require('lodash')
3
+
4
+ const playSchema = Joi.object().keys({
5
+ url: Joi.string().uri({ scheme: 'https' }).required()
6
+ })
7
+
8
+ const saySchema = Joi.object().keys({
9
+ text: Joi.string().required(),
10
+ voice: Joi.string().optional(),
11
+ playBeep: Joi.boolean().optional()
12
+ })
13
+
14
+ class ActionBuilder {
15
+ constructor () {
16
+ this.finalized = false
17
+ this.xml = '<?xml version="1.0" encoding="UTF-8"?><Response>'
18
+
19
+ this.buildAction = function (action) {
20
+ const {
21
+ tag,
22
+ text,
23
+ children,
24
+ attributes
25
+ } = action
26
+
27
+ this.xml = this.xml.concat('<' + tag)
28
+
29
+ if (attributes && Object.keys(attributes).length > 0) {
30
+ Object.entries(attributes)
31
+ .forEach(([key, value]) => {
32
+ this.xml = this.xml.concat(` ${key}="${value}"`)
33
+ })
34
+ }
35
+
36
+ if (children && Object.keys(children).length > 0) {
37
+ this.xml = this.xml.concat('>')
38
+ Object.entries(children)
39
+ .forEach(([child, options]) => {
40
+ switch (child) {
41
+ case 'say':
42
+ this.say(options.text, options.attributes)
43
+ break
44
+ case 'play':
45
+ this.play(options.url)
46
+ break
47
+ case 'getDigits':
48
+ this.getDigits(options.children, options.attributes)
49
+ break
50
+ case 'dial':
51
+ this.dial(options.phoneNumbers, options.attributes)
52
+ break
53
+ case 'record':
54
+ this.record(options.children, options.attributes)
55
+ break
56
+ case 'enqueue':
57
+ this.enqueue(options.attributes)
58
+ break
59
+ case 'dequeue':
60
+ this.dequeue(options.phoneNumber, options.attributes)
61
+ break
62
+ case 'redirect':
63
+ this.redirect(options.text)
64
+ break
65
+ case 'conference':
66
+ this.conference()
67
+ break
68
+ case 'reject':
69
+ this.redirect()
70
+ break
71
+ default:
72
+ throw new Error('Invalid child')
73
+ }
74
+ })
75
+ this.xml = this.xml.concat(`</${tag}>`)
76
+ } else {
77
+ if (text) {
78
+ this.xml = this.xml.concat(`>${text}</${tag}>`)
79
+ } else {
80
+ this.xml = this.xml.concat('/>')
81
+ }
82
+ }
83
+ }
84
+ }
85
+
86
+ build () {
87
+ if (this.finalized) throw new Error('This builder has been finalized by a call to build()')
88
+
89
+ this.finalized = true
90
+ this.xml = this.xml.concat('</Response>')
91
+ return this.xml
92
+ }
93
+
94
+ say (text, attributes = {}) {
95
+ const { error } = saySchema.required().validate({ ...attributes, text })
96
+
97
+ if (error) {
98
+ const msg = error.details.map(detail => detail.message).join(';')
99
+ throw new Error(msg)
100
+ }
101
+
102
+ const action = {
103
+ tag: 'Say',
104
+ text,
105
+ attributes
106
+ }
107
+
108
+ this.buildAction(action)
109
+ return this
110
+ }
111
+
112
+ play (url) {
113
+ const attributes = { url }
114
+
115
+ const { error } = playSchema.required().validate(attributes)
116
+
117
+ if (error) {
118
+ const msg = error.details.map(detail => detail.message).join(';')
119
+ throw new Error(msg)
120
+ }
121
+
122
+ const action = {
123
+ tag: 'Play',
124
+ attributes
125
+ }
126
+
127
+ this.buildAction(action)
128
+ return this
129
+ }
130
+
131
+ getDigits (children, attributes = {}) {
132
+ const params = {
133
+ children: _.cloneDeep(children),
134
+ attributes: _.cloneDeep(attributes)
135
+ }
136
+
137
+ const schema = Joi.object().keys({
138
+ attributes: Joi.object().keys({
139
+ finishOnKey: Joi.string().max(1).default('#'),
140
+ numDigits: Joi.number().integer().positive(),
141
+ timeout: Joi.number().integer().positive(),
142
+ callbackUrl: Joi.string().uri({ scheme: 'https' })
143
+ }).required(),
144
+
145
+ children: Joi.object().keys({
146
+ say: saySchema,
147
+ play: playSchema
148
+ })
149
+ .nand('say', 'play')
150
+ .required()
151
+ }).required()
152
+
153
+ const { error } = schema.validate(params)
154
+
155
+ if (error) {
156
+ const msg = error.details.map(detail => detail.message).join(';')
157
+ throw new Error(msg)
158
+ }
159
+
160
+ const action = {
161
+ tag: 'GetDigits',
162
+ children,
163
+ attributes
164
+ }
165
+
166
+ this.buildAction(action)
167
+ return this
168
+ }
169
+
170
+ dial (phoneNumbers, attributes = {}) {
171
+ const params = _.cloneDeep(attributes)
172
+ params.phoneNumbers = phoneNumbers
173
+
174
+ const schema = Joi.object().keys({
175
+ callerId: Joi.string().optional(),
176
+ phoneNumbers: Joi.string().required(),
177
+ record: Joi.boolean().optional(),
178
+ sequential: Joi.boolean().optional(),
179
+ maxDuration: Joi.number().integer().positive(),
180
+ ringBackTone: Joi.string().uri({ scheme: 'https' })
181
+ }).required()
182
+
183
+ const { error } = schema.validate(params)
184
+
185
+ if (error) {
186
+ const msg = error.details.map(detail => detail.message).join(';')
187
+ throw new Error(msg)
188
+ }
189
+
190
+ const action = {
191
+ tag: 'Dial',
192
+ attributes: params
193
+ }
194
+
195
+ this.buildAction(action)
196
+ return this
197
+ }
198
+
199
+ record (children, attributes = {}) {
200
+ const params = {
201
+ children: _.cloneDeep(children),
202
+ attributes: _.cloneDeep(attributes)
203
+ }
204
+
205
+ const schema = Joi.object().keys({
206
+ attributes: Joi.object().keys({
207
+ finishOnKey: Joi.string().max(1).default('#'),
208
+ maxLength: Joi.number().integer().positive(),
209
+ timeout: Joi.number().integer().positive(),
210
+ playBeep: Joi.boolean().optional(),
211
+ trimSilence: Joi.boolean().optional(),
212
+ callbackUrl: Joi.string().uri({ scheme: 'https' })
213
+ }),
214
+
215
+ children: Joi.object().keys({
216
+ say: saySchema,
217
+ play: playSchema
218
+ })
219
+ .nand('say', 'play')
220
+ .required()
221
+ }).required()
222
+
223
+ const { error } = schema.validate(params)
224
+
225
+ if (error) {
226
+ const msg = error.details.map(detail => detail.message).join(';')
227
+ throw new Error(msg)
228
+ }
229
+
230
+ const action = {
231
+ tag: 'Record',
232
+ children,
233
+ attributes
234
+ }
235
+
236
+ this.buildAction(action)
237
+ return this
238
+ }
239
+
240
+ enqueue (attributes) {
241
+ const params = _.cloneDeep(attributes)
242
+
243
+ const schema = Joi.object().keys({
244
+ holdMusic: Joi.string().uri({ scheme: 'https' }),
245
+ name: Joi.string()
246
+ }).optional()
247
+
248
+ const { error } = schema.validate(params)
249
+
250
+ if (error) {
251
+ const msg = error.details.map(detail => detail.message).join(';')
252
+ throw new Error(msg)
253
+ }
254
+
255
+ const action = {
256
+ tag: 'Enqueue',
257
+ attributes
258
+ }
259
+
260
+ this.buildAction(action)
261
+ return this
262
+ }
263
+
264
+ dequeue (phoneNumber, attributes = {}) {
265
+ const params = _.cloneDeep(attributes)
266
+ params.phoneNumber = phoneNumber
267
+
268
+ const schema = Joi.object().keys({
269
+ phoneNumber: Joi.string().required(),
270
+ name: Joi.string().optional()
271
+ }).required()
272
+
273
+ const { error } = schema.validate(params)
274
+
275
+ if (error) {
276
+ const msg = error.details.map(detail => detail.message).join(';')
277
+ throw new Error(msg)
278
+ }
279
+
280
+ const action = {
281
+ tag: 'Dequeue',
282
+ attributes: params
283
+ }
284
+
285
+ this.buildAction(action)
286
+ return this
287
+ }
288
+
289
+ redirect (url) {
290
+ const schema = Joi.object().keys({
291
+ url: Joi.string().uri({ scheme: 'https' }).required()
292
+ }).required()
293
+
294
+ const { error } = schema.validate({ url })
295
+
296
+ if (error) {
297
+ const msg = error.details.map(detail => detail.message).join(';')
298
+ throw new Error(msg)
299
+ }
300
+
301
+ const action = {
302
+ tag: 'Redirect',
303
+ text: url
304
+ }
305
+
306
+ this.buildAction(action)
307
+ return this
308
+ }
309
+
310
+ conference () {
311
+ const action = {
312
+ tag: 'Conference'
313
+ }
314
+
315
+ this.buildAction(action)
316
+ return this
317
+ }
318
+
319
+ reject () {
320
+ const action = {
321
+ tag: 'Reject'
322
+ }
323
+
324
+ this.buildAction(action)
325
+ return this
326
+ }
327
+ }
328
+
329
+ module.exports = ActionBuilder
package/lib/airtime.js ADDED
@@ -0,0 +1,93 @@
1
+ 'use strict'
2
+
3
+ const Joi = require('joi')
4
+ const Common = require('./common')
5
+
6
+ const initializeAxios = require('./customAxios')
7
+
8
+ class Airtime {
9
+ constructor (config) {
10
+ this.config = config
11
+ }
12
+
13
+ send (options) {
14
+ return new Promise((resolve, reject) => {
15
+ const { error, value } = this.validateOptions(options)
16
+
17
+ if (error) {
18
+ const combinedMessages = error.details.map(detail => detail.message).join(';')
19
+ reject(new Error(combinedMessages))
20
+ return
21
+ }
22
+
23
+ const rawRecipients = value.recipients
24
+ const recipients = rawRecipients.map(r => ({
25
+ phoneNumber: r.phoneNumber,
26
+ amount: `${r.currencyCode} ${r.amount}`
27
+ }))
28
+ const customAxios = value.idempotencyKey ? initializeAxios(this.config, value.idempotencyKey) : initializeAxios(this.config)
29
+
30
+ const requestBody = {
31
+ recipients: JSON.stringify(recipients)
32
+ }
33
+
34
+ if (options.maxNumRetry && (options.maxNumRetry > 0)) {
35
+ requestBody.maxNumRetry = Number(options.maxNumRetry)
36
+ };
37
+
38
+ customAxios.airtime.sendAirtimeRequest(requestBody)
39
+ .then(function (response) {
40
+ if (response.status === 201) {
41
+ resolve(response.data)
42
+ } else {
43
+ reject(response.data || response.error)
44
+ }
45
+ })
46
+ .catch(function (err) {
47
+ reject(err)
48
+ })
49
+ })
50
+ }
51
+
52
+ findTransactionStatus (transactionId) {
53
+ return new Promise((resolve, reject) => {
54
+ if (!transactionId) {
55
+ throw new Error('transactionId should be provided')
56
+ }
57
+
58
+ const { airtime } = initializeAxios(this.config)
59
+
60
+ airtime.findTransactionStatus(transactionId)
61
+ .then(function (resp) {
62
+ const httpStatus = resp.status
63
+ if (httpStatus === 200) {
64
+ resolve(resp.data)
65
+ } else {
66
+ reject(resp.data)
67
+ };
68
+ }).catch(function (error) {
69
+ return reject(error)
70
+ })
71
+ })
72
+ }
73
+
74
+ validateOptions (options) {
75
+ const schema = Joi.object().keys({
76
+ idempotencyKey: Joi.string().optional(),
77
+ recipients: Joi.array().items(
78
+ Joi.object({
79
+ phoneNumber: Joi.string().required().custom((value) => {
80
+ return Common.phoneUtil.isValidNumber(value) ? value : new Error(`the phone number ${value} is invalid`)
81
+ }),
82
+ currencyCode: Joi.string().required(),
83
+ amount: Joi.number().required()
84
+ })
85
+ ).min(1).required(),
86
+ maxNumRetry: Joi.number().optional()
87
+ }).required()
88
+
89
+ return schema.validate(options)
90
+ }
91
+ }
92
+
93
+ module.exports = Airtime
@@ -0,0 +1,35 @@
1
+ 'use strict'
2
+
3
+ const initializeAxios = require('./customAxios')
4
+
5
+ class Application {
6
+ constructor (config) {
7
+ this.config = config
8
+ }
9
+
10
+ fetchApplicationData () {
11
+ return new Promise((resolve, reject) => {
12
+ const customAxios = initializeAxios(this.config)
13
+
14
+ customAxios.application.getApplicationData()
15
+ .then(function (response) {
16
+ if (response.status === 200) {
17
+ resolve(response.data)
18
+ } else {
19
+ reject(response.data || response.error)
20
+ }
21
+ })
22
+ .catch(function (err) {
23
+ reject(err)
24
+ })
25
+ })
26
+ }
27
+
28
+ /* backward compatibility */
29
+ fetchAccount () {
30
+ return this.fetchApplicationData()
31
+ }
32
+ /* end backward compatibility */
33
+ }
34
+
35
+ module.exports = Application
package/lib/common.js ADDED
@@ -0,0 +1,50 @@
1
+ 'use strict'
2
+ const phoneNumberUtil = require('google-libphonenumber').PhoneNumberUtil.getInstance()
3
+
4
+ const BASE_DOMAIN = 'africastalking.com'
5
+ const BASE_SANDBOX_DOMAIN = 'sandbox.' + BASE_DOMAIN
6
+
7
+ const initUrls = function (sandbox) {
8
+ const baseDomain = sandbox ? BASE_SANDBOX_DOMAIN : BASE_DOMAIN
9
+ const baseUrl = 'https://api.' + baseDomain + '/version1'
10
+
11
+ exports.BASE_URL = baseUrl
12
+
13
+ exports.CHECKOUT_TOKEN_URL = 'https://api.' + baseDomain + '/checkout/token/create'
14
+
15
+ exports.AUTH_TOKEN_URL = 'https://api.' + baseDomain + '/auth-token/generate'
16
+
17
+ exports.USER_URL = baseUrl + '/user'
18
+
19
+ exports.SMS_URL = baseUrl + '/messaging'
20
+
21
+ exports.AIRTIME_URL = baseUrl + '/airtime/send'
22
+
23
+ exports.VOICE_URL = 'https://voice.' + baseDomain
24
+
25
+ exports.MOBILE_DATA_URL = 'https://bundles.' + baseDomain
26
+
27
+ exports.CONTENT_URL = sandbox
28
+ ? baseUrl
29
+ : 'https://content.' + baseDomain + '/version1'
30
+
31
+ exports.INSIGHTS_URL = 'https://insights.' + baseDomain
32
+ }
33
+
34
+ // no sandbox by default
35
+ initUrls(false)
36
+
37
+ exports.enableSandbox = function () {
38
+ initUrls(true)
39
+ }
40
+
41
+ exports.phoneUtil = {
42
+ isValidNumber: (value) => {
43
+ const phoneNumber = phoneNumberUtil.parse(value)
44
+ let isValid = phoneNumberUtil.isValidNumber(phoneNumber)
45
+ if (!isValid) {
46
+ isValid = /(\+|00)(297|93|244|1264|358|355|376|971|54|374|1684|1268|61|43|994|257|32|229|226|880|359|973|1242|387|590|375|501|1441|591|55|1246|673|975|267|236|1|61|41|56|86|225|237|243|242|682|57|269|238|506|53|5999|61|1345|357|420|49|253|1767|45|1809|1829|1849|213|593|20|291|212|34|372|251|358|679|500|33|298|691|241|44|995|44|233|350|224|590|220|245|240|30|1473|299|502|594|1671|592|852|504|385|509|36|62|44|91|246|353|98|964|354|972|39|1876|44|962|81|76|77|254|996|855|686|1869|82|383|965|856|961|231|218|1758|423|94|266|370|352|371|853|590|212|377|373|261|960|52|692|389|223|356|95|382|976|1670|258|222|1664|596|230|265|60|262|264|687|227|672|234|505|683|31|47|977|674|64|968|92|507|64|51|63|680|675|48|1787|1939|850|351|595|970|689|974|262|40|7|250|966|249|221|65|500|4779|677|232|503|378|252|508|381|211|239|597|421|386|46|268|1721|248|963|1649|235|228|66|992|690|993|670|676|1868|216|90|688|886|255|256|380|598|1|998|3906698|379|1784|58|1284|1340|84|678|681|685|967|27|260|263)(9[976]\d|8[987530]\d|6[987]\d|5[90]\d|42\d|3[875]\d|2[98654321]\d|9[8543210]|8[6421]|6[6543210]|5[87654321]|4[987654310]|3[9643210]|2[70]|7|1)\d{4,10}$/.test(value)
47
+ }
48
+ return isValid
49
+ }
50
+ }
@@ -0,0 +1,20 @@
1
+ function airtime ({ createAxiosInstance, endpoints, username }) {
2
+ return {
3
+ sendAirtimeRequest: data => {
4
+ return createAxiosInstance().post(endpoints.SEND_AIRTIME, new URLSearchParams({
5
+ ...data,
6
+ username
7
+ }).toString())
8
+ },
9
+ findTransactionStatus: transactionId => {
10
+ return createAxiosInstance('application/json').get(endpoints.FIND_AIRTIME_TRANSACTION, {
11
+ params: {
12
+ transactionId,
13
+ username
14
+ }
15
+ })
16
+ }
17
+ }
18
+ };
19
+
20
+ module.exports = airtime
@@ -0,0 +1,11 @@
1
+ function application ({ createAxiosInstance, endpoints, username }) {
2
+ return {
3
+ getApplicationData: () => {
4
+ return createAxiosInstance().get(endpoints.GET_APPLICATION_DATA, {
5
+ params: { username }
6
+ })
7
+ }
8
+ }
9
+ };
10
+
11
+ module.exports = application
@@ -0,0 +1,43 @@
1
+ const axios = require('axios')
2
+ const airtime = require('./airtime')
3
+ const application = require('./application')
4
+
5
+ function initializeAxios (config, idempotencyKey = null) {
6
+ const { username, apiKey, format } = config
7
+
8
+ const baseURL = username === 'sandbox'
9
+ ? 'https://api.sandbox.africastalking.com'
10
+ : 'https://api.africastalking.com'
11
+
12
+ const createAxiosInstance = (contentType) => {
13
+ const headers = {
14
+ apiKey,
15
+ Accept: format,
16
+ ...(contentType && { 'Content-Type': contentType }),
17
+ ...(idempotencyKey && { 'Idempotency-Key': idempotencyKey })
18
+ }
19
+ return axios.create({
20
+ baseURL,
21
+ headers
22
+ })
23
+ }
24
+
25
+ const endpoints = {
26
+ SEND_AIRTIME: '/version1/airtime/send',
27
+ FIND_AIRTIME_TRANSACTION: '/query/transaction/find',
28
+ GET_APPLICATION_DATA: '/version1/user'
29
+ }
30
+
31
+ const opts = {
32
+ createAxiosInstance,
33
+ endpoints,
34
+ username
35
+ }
36
+
37
+ return {
38
+ airtime: airtime(opts),
39
+ application: application(opts)
40
+ }
41
+ };
42
+
43
+ module.exports = initializeAxios