africastalking
Advanced tools
Comparing version 0.5.7 to 0.5.8
@@ -29,5 +29,6 @@ 'use strict'; | ||
const customAxios = value.idempotencyKey ? initializeAxios(this.config, value.idempotencyKey) : initializeAxios(this.config); | ||
customAxios.airtime.sendAirtimeRequest({ | ||
recipients: JSON.stringify(recipients), | ||
maxNumRetry: options.maxNumRetry? Number(options.maxNumRetry) : "None" | ||
}) | ||
@@ -59,2 +60,3 @@ .then(function (response) { | ||
).min(1).required(), | ||
maxNumRetry: Joi.number().optional(), | ||
}).required(); | ||
@@ -61,0 +63,0 @@ |
167
lib/sms.js
'use strict'; | ||
const unirest = require('unirest'); | ||
const axios = require('axios'); | ||
const validate = require('validate.js'); | ||
@@ -131,16 +131,27 @@ const _ = require('lodash'); | ||
const url = isBulk ? Common.SMS_URL : Common.CONTENT_URL + '/messaging'; | ||
const rq = unirest.post(url); | ||
rq.headers({ | ||
apikey: _self.options.apiKey, | ||
Accept: _self.options.format, | ||
const headers = { | ||
apikey: _self.options.apiKey, | ||
Accept: _self.options.format, | ||
}; | ||
axios({ | ||
method:'POST', | ||
url, | ||
headers, | ||
data: new URLSearchParams(body) | ||
}) | ||
.then(function (response) { | ||
if (response.status === 201) { | ||
// API returns CREATED on success!? | ||
resolve(response.data); | ||
} else { | ||
reject(response.data); | ||
} | ||
}) | ||
.catch(function (error) { | ||
reject(error) | ||
}); | ||
rq.send(body); | ||
rq.end(function (resp) { | ||
if (resp.status === 201) { | ||
// API returns CREATED on success!? | ||
resolve(resp.body); | ||
} else { | ||
reject(resp.body || resp.error); | ||
} | ||
}); | ||
}); | ||
@@ -192,17 +203,31 @@ } | ||
return new Promise(function (resolve, reject) { | ||
const rq = unirest.get(Common.SMS_URL); | ||
rq.headers({ | ||
const headers = { | ||
apikey: _self.options.apiKey, | ||
Accept: _self.options.format, | ||
}); | ||
rq.query({ | ||
Accept: _self.options.format | ||
}; | ||
const query = new URLSearchParams({ | ||
username: _self.options.username, | ||
lastReceivedId: opts.lastReceivedId, | ||
}); | ||
rq.end(function (resp) { | ||
if (resp.status === 200) { | ||
resolve(resp.body); | ||
keyword: opts.keyword, | ||
shortCode: opts.shortCode, | ||
}).toString(); | ||
const url = `${Common.SMS_URL}?${query}`; | ||
axios({ | ||
method:'GET', | ||
url, | ||
headers | ||
}) | ||
.then(function (response) { | ||
if (response.status === 200) { | ||
resolve(response.data); | ||
} else { | ||
reject(resp.body || resp.error); | ||
reject(response.data); | ||
} | ||
}) | ||
.catch(function (error) { | ||
reject(error) | ||
}); | ||
@@ -240,16 +265,25 @@ }); | ||
} | ||
const rq = unirest.post(Common.CONTENT_URL + '/subscription/create'); | ||
rq.headers({ | ||
const url = `${Common.CONTENT_URL}/subscription/create`; | ||
const headers = { | ||
apikey: _self.options.apiKey, | ||
Accept: _self.options.format, | ||
}); | ||
rq.send(body); | ||
rq.end(function (resp) { | ||
if (resp.status === 201) { | ||
Accept: _self.options.format | ||
}; | ||
axios({ | ||
method:'POST', | ||
url, | ||
headers, | ||
data: new URLSearchParams(body) | ||
}) | ||
.then(function (response) { | ||
if (response.status === 201) { | ||
// API returns CREATED on success!? | ||
resolve(resp.body); | ||
resolve(response.data); | ||
} else { | ||
reject(resp.body || resp.error); | ||
reject(response.data); | ||
} | ||
}) | ||
.catch(function (error) { | ||
reject(error) | ||
}); | ||
@@ -282,9 +316,10 @@ }); | ||
return reject(validationError); | ||
} | ||
const rq = unirest.get(Common.CONTENT_URL + '/subscription'); | ||
rq.headers({ | ||
apikey: _self.options.apiKey, | ||
Accept: _self.options.format, | ||
}); | ||
rq.query({ | ||
}; | ||
const headers ={ | ||
apikey: _self.options.apiKey, | ||
Accept: _self.options.format, | ||
}; | ||
const query = new URLSearchParams({ | ||
username: _self.options.username, | ||
@@ -294,10 +329,22 @@ lastReceivedId: opts.lastReceivedId, | ||
shortCode: opts.shortCode, | ||
}); | ||
rq.end(function (resp) { | ||
if (resp.status === 200) { | ||
resolve(resp.body); | ||
}).toString(); | ||
const url = `${Common.CONTENT_URL}/subscription?${query}`; | ||
axios({ | ||
method:'GET', | ||
url, | ||
headers | ||
}) | ||
.then(function (response) { | ||
if (response.status === 200) { | ||
resolve(response.data); | ||
} else { | ||
reject(resp.body || resp.error); | ||
reject(response.data); | ||
} | ||
}) | ||
.catch(function (error) { | ||
reject(error) | ||
}); | ||
}); | ||
@@ -351,18 +398,26 @@ }; | ||
}; | ||
const rq = unirest.post(Common.CONTENT_URL + '/subscription/delete'); | ||
rq.headers({ | ||
const url = `${Common.CONTENT_URL}/subscription/delete`; | ||
const headers = { | ||
apiKey: apiKey, | ||
Accept: format, | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
}); | ||
rq.send(body); | ||
rq.end(function (resp) { | ||
if (resp.status === 200) { | ||
}; | ||
axios({ | ||
method:'POST', | ||
url, | ||
headers, | ||
data: new URLSearchParams(body) | ||
}) | ||
.then(function (response) { | ||
if (response.status === 200) { | ||
// API deleted successfully | ||
resolve(resp.body); | ||
resolve(response.data); | ||
} else { | ||
reject(resp.body || resp.error); | ||
reject(response.data); | ||
} | ||
}) | ||
.catch(function (error) { | ||
reject(error) | ||
}); | ||
@@ -369,0 +424,0 @@ }); |
'use strict'; | ||
const unirest = require('unirest'); | ||
const validate = require('validate.js'); | ||
const _ = require('lodash'); | ||
const axios = require('axios'); | ||
const Common = require('./common'); | ||
class Token{ | ||
constructor(options){ | ||
this.options = options; | ||
}; | ||
class Token { | ||
constructor(options) { | ||
this.options = options; | ||
} | ||
generateAuthToken() { | ||
var _self = this; | ||
return new Promise(function (resolve, reject) { | ||
var body = { | ||
username: _self.options.username, | ||
const _self = this; | ||
return new Promise((resolve,reject) => { | ||
const config = { | ||
method: 'post', | ||
url: Common.AUTH_TOKEN_URL, | ||
headers: { | ||
apiKey: _self.options.apiKey, | ||
Accept: _self.options.format, | ||
'Content-Type': 'application/json' | ||
}, | ||
data : JSON.stringify({ | ||
username: _self.options.username, | ||
}) | ||
}; | ||
var rq = unirest.post(Common.AUTH_TOKEN_URL); | ||
rq.headers({ | ||
apiKey: _self.options.apiKey, | ||
Accept: _self.options.format, | ||
'Content-Type': 'application/json' | ||
axios(config) | ||
.then(function (resp) { | ||
const results = resp.data; | ||
if (!results.token || results.token === 'None') { | ||
return reject(results.description); | ||
}; | ||
return resolve(results) | ||
}) | ||
.catch(function (error) { | ||
return reject(error); | ||
}); | ||
rq.send(body); | ||
rq.end(function (resp) { | ||
if (resp.error) | ||
return reject(resp.error); | ||
if (resp.body.token === undefined || resp.body.token === 'None') { | ||
return reject(resp.body.description); | ||
} | ||
return resolve(resp.body); | ||
}); | ||
}); | ||
} | ||
} | ||
}; | ||
module.exports = Token; |
120
lib/voice.js
'use strict'; | ||
const unirest = require('unirest'); | ||
const validate = require('validate.js'); | ||
@@ -76,24 +74,31 @@ const _ = require('lodash'); | ||
}else{ | ||
const body = { | ||
username: this.options.username, | ||
to: callTo.join(','), | ||
from: callFrom, | ||
clientRequestId: clientRequestId | ||
const config = { | ||
method: 'post', | ||
url: `${Common.VOICE_URL}/call`, | ||
headers: { | ||
apikey: this.options.apiKey, | ||
Accept: this.options.format | ||
}, | ||
data : JSON.stringify({ | ||
username: this.options.username, | ||
to: callTo.join(','), | ||
from: callFrom, | ||
clientRequestId: clientRequestId | ||
}) | ||
}; | ||
axios(config) | ||
.then(function (resp) { | ||
const httpStatus = resp.status; | ||
const rq = unirest.post(Common.VOICE_URL + '/call'); | ||
rq.headers({ | ||
apikey: this.options.apiKey, | ||
Accept: this.options.format | ||
}); | ||
rq.send(body); | ||
rq.end(function (resp) { | ||
if (resp.status === 200 || resp.status === 201) { | ||
if (httpStatus === 200 || httpStatus === 201) { | ||
// API returns CREATED on success | ||
resolve(resp.body); | ||
resolve(resp.data); | ||
} else { | ||
reject(resp.body || resp.error); | ||
reject(resp.data); | ||
}; | ||
}) | ||
.catch(function (error) { | ||
return reject(error); | ||
}); | ||
@@ -139,23 +144,28 @@ }; | ||
return new Promise(function (resolve, reject) { | ||
// list of phoneNumbers, comma separated | ||
let body = { | ||
username: _self.options.username, | ||
phoneNumbers: options.phoneNumbers | ||
const config = { | ||
method: 'post', | ||
url: `${Common.VOICE_URL}/queueStatus`, | ||
headers: { | ||
apikey: _self.options.apiKey, | ||
Accept: _self.options.format | ||
}, | ||
data : JSON.stringify({ | ||
username: _self.options.username, | ||
phoneNumbers: options.phoneNumbers | ||
}) | ||
}; | ||
axios(config) | ||
.then(function (resp) { | ||
const httpStatus = resp.status; | ||
let rq = unirest.post(Common.VOICE_URL + '/queueStatus'); | ||
rq.headers({ | ||
apikey: _self.options.apiKey, | ||
Accept: _self.options.format | ||
}); | ||
rq.send(body); | ||
rq.end(function (resp) { | ||
if (resp.status === 200 || resp.status === 201) { | ||
if (httpStatus === 200 || httpStatus === 201) { | ||
// API returns CREATED on success | ||
resolve(resp.body); | ||
resolve(resp.data); | ||
} else { | ||
reject(resp.body || resp.error); | ||
} | ||
reject(resp.data); | ||
}; | ||
}) | ||
.catch(function (error) { | ||
return reject(error); | ||
}); | ||
@@ -216,23 +226,31 @@ }); | ||
return new Promise(function (resolve, reject) { | ||
let body = { | ||
username: _self.options.username, | ||
url: options.url, | ||
phoneNumber: options.phoneNumber | ||
}; | ||
let rq = unirest.post(Common.VOICE_URL + '/mediaUpload'); | ||
rq.headers({ | ||
const config = { | ||
method: 'post', | ||
url: `${Common.VOICE_URL}/mediaUpload`, | ||
headers: { | ||
apikey: _self.options.apiKey, | ||
Accept: _self.options.format | ||
}); | ||
}, | ||
data : JSON.stringify({ | ||
username: _self.options.username, | ||
url: options.url, | ||
phoneNumber: options.phoneNumber | ||
}) | ||
}; | ||
rq.send(body); | ||
axios(config) | ||
.then(function (resp) { | ||
const httpStatus = resp.status; | ||
rq.end(function (resp) { | ||
if (resp.status === 201) { | ||
if (httpStatus === 200 || httpStatus === 201) { | ||
// API returns CREATED on success | ||
resolve(resp.body); | ||
resolve(resp.data); | ||
} else { | ||
reject(resp.body || resp.error); | ||
} | ||
reject(resp.data); | ||
}; | ||
}) | ||
.catch(function (error) { | ||
return reject(error); | ||
}); | ||
@@ -239,0 +257,0 @@ }); |
{ | ||
"name": "africastalking", | ||
"version": "0.5.7", | ||
"version": "0.5.8", | ||
"description": "Official AfricasTalking node.js API wrapper", | ||
@@ -20,3 +20,4 @@ "main": "index.js", | ||
"m-pesa", | ||
"payments" | ||
"payments", | ||
"airtime" | ||
], | ||
@@ -34,3 +35,3 @@ "author": "Africa's Talking", | ||
"@hapi/joi": "^16.1.7", | ||
"axios": "^0.25.0", | ||
"axios": "^1.2.6", | ||
"body-parser": "^1.19.1", | ||
@@ -40,3 +41,2 @@ "lodash": "^4.17.21", | ||
"querystring": "^0.2.0", | ||
"unirest": "^0.6.0", | ||
"validate.js": "^0.13.1" | ||
@@ -43,0 +43,0 @@ }, |
@@ -90,11 +90,32 @@ # Africa's Talking Node.js SDK | ||
- `airtime.send({ recipients })`: Send airtime to a bunch of phone numbers. `recipients`: An array of objects containing the following keys: | ||
- `phoneNumber`: Recipient of airtime. `REQUIRED` | ||
- `currencyCode`: 3-digit ISO format currency code. `REQUIRED` | ||
- `amount`: Amount to charge. `REQUIRED` | ||
- `airtime.send({ recipients })`: Send airtime to a bunch of phone numbers. `recipients`: An array of objects containing the following keys: | ||
- `phoneNumber`: Recipient of airtime. `REQUIRED`. | ||
- `currencyCode`: 3-digit ISO format currency code. `REQUIRED`. | ||
- `amount`: Amount to charge. `REQUIRED`. | ||
- `maxNumRetry`: This is the number of times a request will be retried. It is equivalent to **minutes**. If you set a maxNumRetry of 4 that means your transaction will be retried 4 times consecutively in the next 4 minutes because retries are done after every minute. If you don't set maxNumRetry, the request will **NOT** be retried. `OPTIONAL`. | ||
For more information, please read [http://docs.africastalking.com/airtime/sending](http://docs.africastalking.com/airtime/sending) | ||
- Example: | ||
```javascript | ||
airtime.send({ | ||
recipients: [ | ||
{ | ||
phoneNumber: '+xxxxxxxxxxxx', | ||
currencyCode: 'KES', | ||
amount: 90 | ||
}, | ||
{ | ||
phoneNumber: '+xxxxxxxxxxxx', | ||
currencyCode: 'KES', | ||
amount: 897 | ||
} | ||
], | ||
maxNumRetry: 4 // will be retried 4 times consecutively in the next 4 minutes | ||
}); | ||
``` | ||
For more information, please read [http://docs.africastalking.com/airtime/sending](http://docs.africastalking.com/airtime/sending) | ||
### `SmsService` | ||
@@ -101,0 +122,0 @@ |
Sorry, the diff of this file is too big to display
386990
7
20
3343
392
+ Addedaxios@1.7.9(transitive)
+ Addedes-set-tostringtag@2.1.0(transitive)
+ Addedform-data@4.0.2(transitive)
+ Addedhas-tostringtag@1.0.2(transitive)
+ Addedproxy-from-env@1.1.0(transitive)
- Removedunirest@^0.6.0
- Removedajv@6.12.6(transitive)
- Removedasn1@0.2.6(transitive)
- Removedassert-plus@1.0.0(transitive)
- Removedasync@0.9.2(transitive)
- Removedaws-sign2@0.7.0(transitive)
- Removedaws4@1.13.2(transitive)
- Removedaxios@0.25.0(transitive)
- Removedbcrypt-pbkdf@1.0.2(transitive)
- Removedcaseless@0.12.0(transitive)
- Removedcombined-stream@0.0.7(transitive)
- Removedcore-util-is@1.0.2(transitive)
- Removeddashdash@1.14.1(transitive)
- Removeddelayed-stream@0.0.5(transitive)
- Removedecc-jsbn@0.1.2(transitive)
- Removedextend@3.0.2(transitive)
- Removedextsprintf@1.3.0(transitive)
- Removedfast-deep-equal@3.1.3(transitive)
- Removedfast-json-stable-stringify@2.1.0(transitive)
- Removedforever-agent@0.6.1(transitive)
- Removedform-data@0.2.02.3.3(transitive)
- Removedgetpass@0.1.7(transitive)
- Removedhar-schema@2.0.0(transitive)
- Removedhar-validator@5.1.5(transitive)
- Removedhttp-signature@1.2.0(transitive)
- Removedis-typedarray@1.0.0(transitive)
- Removedisstream@0.1.2(transitive)
- Removedjsbn@0.1.1(transitive)
- Removedjson-schema@0.4.0(transitive)
- Removedjson-schema-traverse@0.4.1(transitive)
- Removedjson-stringify-safe@5.0.1(transitive)
- Removedjsprim@1.4.2(transitive)
- Removedmime@2.6.0(transitive)
- Removedmime-db@1.12.0(transitive)
- Removedmime-types@2.0.14(transitive)
- Removedoauth-sign@0.9.0(transitive)
- Removedperformance-now@2.1.0(transitive)
- Removedpsl@1.15.0(transitive)
- Removedpunycode@2.3.1(transitive)
- Removedqs@6.5.3(transitive)
- Removedrequest@2.88.2(transitive)
- Removedsafe-buffer@5.2.1(transitive)
- Removedsshpk@1.18.0(transitive)
- Removedtough-cookie@2.5.0(transitive)
- Removedtunnel-agent@0.6.0(transitive)
- Removedtweetnacl@0.14.5(transitive)
- Removedunirest@0.6.0(transitive)
- Removeduri-js@4.4.1(transitive)
- Removeduuid@3.4.0(transitive)
- Removedverror@1.10.0(transitive)
Updatedaxios@^1.2.6