Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

africastalking

Package Overview
Dependencies
Maintainers
4
Versions
53
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

africastalking - npm Package Compare versions

Comparing version 0.4.2 to 0.4.3

lib/customAxios/airtime.js

169

lib/airtime.js
'use strict';
const unirest = require('unirest');
const validate = require('validate.js');
const _ = require('lodash');
const Joi = require('@hapi/joi');
const initializeAxios = require('./customAxios');
const Common = require('./common');
class Airtime {
constructor(config) {
this.config = config;
}
const DEFAULT_CURRENCY = 'KES';
send(options) {
return new Promise((resolve, reject) => {
const { error, value } = this.validateOptions(options);
function Airtime(options) {
this.options = options;
}
if (error) {
const combinedMessages = error.details.map(d => d.message).join(';');
reject(new Error(combinedMessages));
return;
}
Airtime.prototype.send = function (params) {
const { recipients: rawRecipients } = value;
const recipients = rawRecipients.map(r => ({
phoneNumber: r.phoneNumber,
amount: `${r.currencyCode} ${r.amount}`,
}));
const _self = this;
const options = _.cloneDeep(params);
const _recipients = [];
let validationError;
// Validate params
const _validateParams = function () {
const constraints = {
recipients: function (value) {
if (validate.isEmpty(value)) {
return {
presence: {
message: 'is required'
}
};
const customAxios = initializeAxios(this.config, false);
customAxios.airtime.sendAirtimeRequest({
recipients: JSON.stringify(recipients),
})
.then(function (response) {
if (response.status === 201) {
resolve(response.data);
} else {
reject(response.data || response.error);
}
})
.catch(function (err) {
reject(err);
});
});
}
if (!validate.isArray(value)) {
console.log("is an array :", validate.isArray(value));
return {
format: {
message: 'must be an array'
}
};
}
validateOptions(options) {
const schema = Joi.object({
recipients: Joi.array().items(
Joi.object({
phoneNumber: Joi.string().required().pattern(/^\+\d{1,3}\d{3,}$/),
currencyCode: Joi.string().required(),
amount: Joi.number().required(),
}),
).min(1).required(),
}).length(1).required();
if (!value.length) {
return {
format: {
message: 'must be an array of at least one recipient'
}
};
}
for(let i in value) {
let recipient = value[i];
let phoneNumber = recipient.phoneNumber;
let currencyCode = recipient.currencyCode;
let amount = recipient.amount;
if (validate.isEmpty(phoneNumber) ||
validate.isEmpty(currencyCode) ||
validate.isEmpty(amount)) {
return {
format: {
message: 'must specify phoneNumber, currencyCode and amount for all recipients'
}
}
}
if (!/^\+\d{1,3}\d{3,}$/.test(phoneNumber)) {
return {
format: {
message: 'must not contain invalid phone numbers'
}
}
}
if (!validate.isNumber(amount)) {
return {
format: {
message: 'must not contain invalid amount. Must be a number.'
}
}
}
_recipients.push( { phoneNumber, "amount" : `${currencyCode} ${amount}` });
};
return null;
}
};
const error = validate(options, constraints);
if (error) {
var msg = "";
for (var k in error) {
msg += error[k] + "; ";
}
validationError = new Error(msg);
}
return schema.validate(options);
}
}
_validateParams();
return new Promise(function (resolve, reject) {
if (validationError) {
return reject(validationError);
}
let body = {
username: _self.options.username,
recipients: JSON.stringify(_recipients)
};
let rq = unirest.post(Common.AIRTIME_URL);
rq.headers({
apikey: _self.options.apiKey,
Accept: _self.options.format
});
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);
}
});
});
};
module.exports = Airtime;
'use strict';
const unirest = require('unirest');
const validate = require('validate.js');
const _ = require('lodash');
const initializeAxios = require('./customAxios');
const Common = require('./common');
class Application {
constructor(config) {
this.config = config;
}
function Application(options) {
this.options = options;
}
fetchApplicationData() {
return new Promise((resolve, reject) => {
const customAxios = initializeAxios(this.config, false);
customAxios.application.getApplicationData()
.then(function (response) {
if (response.status === 200) {
resolve(response.data);
} else {
reject(response.data || response.error);
}
})
.catch(function (err) {
reject(err);
});
Application.prototype.fetchApplicationData = function () {
const _self = this;
return new Promise(function (resolve, reject) {
const rq = unirest.get(Common.USER_URL);
rq.headers({
apiKey: _self.options.apiKey,
Accept: _self.options.format
});
rq.query({'username': _self.options.username});
rq.end(function (resp) {
if (resp.status === 200) {
resolve(resp.body);
} else {
reject(resp.body || resp.error);
}
});
});
}
};
/* backward compatibility */
fetchAccount() {
return this.fetchApplicationData();
}
/* end backward compatibility */
}
/* For backward compatibility */
Application.prototype.fetchAccount = Application.prototype.fetchApplicationData;
/* End */
module.exports = Application;
'use strict';
const BASE_DOMAIN = "africastalking.com";
const BASE_DOMAIN = "africastalking.com";
const BASE_SANDBOX_DOMAIN = "sandbox." + BASE_DOMAIN;

@@ -5,0 +5,0 @@

'use strict';
const unirest = require('unirest');
const validate = require('validate.js');

@@ -4,0 +3,0 @@ const _ = require('lodash');

{
"name": "africastalking",
"version": "0.4.2",
"version": "0.4.3",
"description": "Official AfricasTalking node.js API wrapper",
"main": "index.js",
"scripts": {
"test": "istanbul cover _mocha -- --recursive --exit ./test/",
"test-windows": "istanbul cover node_modules/mocha/bin/_mocha -- --recursive --exit ./test/"
"test": "nyc mocha ./test",
"test-windows": "nyc node_modules/mocha/bin/_mocha ./test"
},

@@ -32,16 +32,18 @@ "repository": {

"dependencies": {
"body-parser": "^1.18.3",
"grpc": "^1.15.1",
"install": "^0.10.4",
"lodash": "^4.17.11",
"unirest": "^0.5.1",
"validate.js": "^0.12.0"
"@hapi/joi": "^16.1.7",
"axios": "^0.19.0",
"body-parser": "^1.19.0",
"grpc": "^1.24.2",
"lodash": "^4.17.15",
"querystring": "^0.2.0",
"unirest": "^0.6.0",
"validate.js": "^0.13.1"
},
"devDependencies": {
"express": "^4.16.4",
"istanbul": "^0.4.5",
"mocha": "^4.1.0",
"express": "^4.17.1",
"mocha": "^6.2.2",
"nyc": "^14.1.1",
"should": "^13.2.3",
"supertest": "^3.3.0"
"supertest": "^4.0.2"
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc