africastalking
Advanced tools
Comparing version 0.4.2 to 0.4.3
'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" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
21
122670
8
3301
1
+ Added@hapi/joi@^16.1.7
+ Addedaxios@^0.19.0
+ Addedquerystring@^0.2.0
+ Added@hapi/address@2.1.4(transitive)
+ Added@hapi/formula@1.2.0(transitive)
+ Added@hapi/hoek@8.5.1(transitive)
+ Added@hapi/joi@16.1.8(transitive)
+ Added@hapi/pinpoint@1.0.2(transitive)
+ Added@hapi/topo@3.1.6(transitive)
+ Added@types/node@22.9.1(transitive)
+ Addedajv@6.12.6(transitive)
+ Addedasynckit@0.4.0(transitive)
+ Addedaws-sign2@0.7.0(transitive)
+ Addedaxios@0.19.2(transitive)
+ Addedcaseless@0.12.0(transitive)
+ Addedfast-deep-equal@3.1.3(transitive)
+ Addedfast-json-stable-stringify@2.1.0(transitive)
+ Addedfollow-redirects@1.5.10(transitive)
+ Addedform-data@2.3.3(transitive)
+ Addedhar-schema@2.0.0(transitive)
+ Addedhar-validator@5.1.5(transitive)
+ Addedhttp-signature@1.2.0(transitive)
+ Addedjson-schema-traverse@0.4.1(transitive)
+ Addedmime@2.6.0(transitive)
+ Addedoauth-sign@0.9.0(transitive)
+ Addedperformance-now@2.1.0(transitive)
+ Addedpsl@1.13.0(transitive)
+ Addedpunycode@2.3.1(transitive)
+ Addedqs@6.5.3(transitive)
+ Addedquerystring@0.2.1(transitive)
+ Addedrequest@2.88.2(transitive)
+ Addedtough-cookie@2.5.0(transitive)
+ Addedtunnel-agent@0.6.0(transitive)
+ Addedunirest@0.6.0(transitive)
+ Addeduri-js@4.4.1(transitive)
+ Addeduuid@3.4.0(transitive)
+ Addedvalidate.js@0.13.1(transitive)
- Removedinstall@^0.10.4
- Removed@types/node@22.9.3(transitive)
- Removedansi-styles@2.2.1(transitive)
- Removedassert-plus@0.2.0(transitive)
- Removedasync@2.6.4(transitive)
- Removedaws-sign2@0.6.0(transitive)
- Removedbl@1.1.2(transitive)
- Removedboom@2.10.1(transitive)
- Removedcaseless@0.11.0(transitive)
- Removedchalk@1.1.3(transitive)
- Removedcommander@2.20.3(transitive)
- Removedcore-util-is@1.0.3(transitive)
- Removedcryptiles@2.0.5(transitive)
- Removedescape-string-regexp@1.0.5(transitive)
- Removedform-data@1.0.1(transitive)
- Removedgenerate-function@2.3.1(transitive)
- Removedgenerate-object-property@1.2.0(transitive)
- Removedhar-validator@2.0.6(transitive)
- Removedhas-ansi@2.0.0(transitive)
- Removedhawk@3.1.3(transitive)
- Removedhoek@2.16.3(transitive)
- Removedhttp-signature@1.1.1(transitive)
- Removedinstall@0.10.4(transitive)
- Removedis-my-ip-valid@1.0.1(transitive)
- Removedis-my-json-valid@2.20.6(transitive)
- Removedis-property@1.0.2(transitive)
- Removedisarray@1.0.0(transitive)
- Removedjsonpointer@5.0.1(transitive)
- Removedmime@1.3.6(transitive)
- Removednode-uuid@1.4.8(transitive)
- Removedoauth-sign@0.8.2(transitive)
- Removedpinkie@2.0.4(transitive)
- Removedpinkie-promise@2.0.1(transitive)
- Removedprocess-nextick-args@1.0.7(transitive)
- Removedpunycode@1.4.1(transitive)
- Removedqs@6.2.4(transitive)
- Removedreadable-stream@2.0.6(transitive)
- Removedrequest@2.74.0(transitive)
- Removedsntp@1.0.9(transitive)
- Removedstring_decoder@0.10.31(transitive)
- Removedstringstream@0.0.6(transitive)
- Removedsupports-color@2.0.0(transitive)
- Removedtough-cookie@2.3.4(transitive)
- Removedtunnel-agent@0.4.3(transitive)
- Removedunirest@0.5.1(transitive)
- Removedvalidate.js@0.12.0(transitive)
- Removedxtend@4.0.2(transitive)
Updatedbody-parser@^1.19.0
Updatedgrpc@^1.24.2
Updatedlodash@^4.17.15
Updatedunirest@^0.6.0
Updatedvalidate.js@^0.13.1