Comparing version 1.0.2 to 1.1.0
const axios = require('../client'); | ||
const errors = require('../errors'); | ||
const uri = '/sms'; | ||
@@ -28,3 +28,3 @@ const ajv = require('../validator'); | ||
if (Object.keys(_data).length == 0) { | ||
callback(Error('sms data not provided')); | ||
callback(Error(errors.sms.missingData)); | ||
return promise; | ||
@@ -144,3 +144,3 @@ } | ||
if (_options.offset + (_options.limit || 20) > 10000) { | ||
callback('offset and limit (defualt 20) total can not exceed 10000'); | ||
callback(errors.searchOffsetLimit); | ||
return promise; | ||
@@ -147,0 +147,0 @@ } |
const axios = require('../client'); | ||
const ajv = require('../validator'); | ||
const errors = require('../errors'); | ||
const uri = '/sms-incoming'; | ||
@@ -80,3 +81,3 @@ | ||
if (_options.offset + (_options.limit || 20) > 10000) { | ||
callback('offset and limit (defualt 20) total can not exceed 10000'); | ||
callback(errors.searchOffsetLimit); | ||
return promise; | ||
@@ -83,0 +84,0 @@ } |
@@ -25,3 +25,4 @@ const axios = require('axios'); | ||
const nonce = Math.floor(Math.random() * 10000000); | ||
const auth = `${timestamp}\n${nonce}\n${config.method.toUpperCase()}\n${urlFragments.path}\n${urlFragments.host}\n${443}\n\n`; | ||
const port = urlFragments.protocol == 'https:' ? 443 : 80; | ||
const auth = `${timestamp}\n${nonce}\n${config.method.toUpperCase()}\n${urlFragments.path}\n${urlFragments.host}\n${port}\n\n`; | ||
const hash = crypto | ||
@@ -32,3 +33,2 @@ .createHmac('sha256', process.env.SMSGLOBAL_API_SECRET) | ||
const token = `MAC id="${process.env.SMSGLOBAL_API_KEY}", ts="${timestamp}", nonce="${nonce}", mac="${hash}"`; | ||
Object.assign( | ||
@@ -35,0 +35,0 @@ config.headers, |
@@ -9,2 +9,6 @@ const dateTimeFormat = 'yyyy-MM-dd HH:mm:ss'; | ||
{ | ||
$id: '#identityString', | ||
type: ['string'], | ||
}, | ||
{ | ||
$id: '#sms/send', | ||
@@ -109,2 +113,62 @@ type: 'object', | ||
}, | ||
{ | ||
$id: '#otp/verify', | ||
type: 'object', | ||
additionalProperties: false, | ||
required: ['id', 'code'], | ||
properties: { | ||
id: { type: 'string'}, | ||
code: { | ||
type: 'string', minLength: 4, maxKength: 10 }, | ||
}, | ||
errorMessage: { | ||
properties: { | ||
id: 'id should be a string', | ||
code: 'code should be an integer', | ||
}, | ||
}, | ||
}, | ||
{ | ||
$id: '#otp/getAll', | ||
type: 'object', | ||
additionalProperties: false, | ||
useDefaults: true, | ||
properties: { | ||
offset: { type: 'integer', minimum: 1 }, | ||
limit: { type: 'integer', minimum: 1, maximum: 1000 }, | ||
status: { type: 'string' }, | ||
startDate: { type: ['string', 'object'], dateTimeFormat: dateTimeFormat}, | ||
endDate: { type: ['string', 'object'], dateTimeFormat: dateTimeFormat}, | ||
}, | ||
}, | ||
{ | ||
$id: '#otp/send', | ||
type: 'object', | ||
additionalProperties: false, | ||
required: ['destination', 'message'], | ||
properties: { | ||
origin: { | ||
type: ['null', 'string'], | ||
minLength: 3, | ||
maxLength: 15, | ||
}, | ||
destination: { | ||
type: ['string'], | ||
minLength: 3, | ||
maxLength: 15, | ||
}, | ||
message: { type: 'string' }, | ||
messageExpiryDateTime: { | ||
type: ['string', 'object'], | ||
dateTimeFormat: dateTimeFormat, | ||
}, | ||
length: { | ||
type: 'integer', | ||
}, | ||
codeExpiry: { | ||
type: 'integer', | ||
}, | ||
}, | ||
}, | ||
]; |
@@ -0,1 +1,2 @@ | ||
const errors = require('./errors'); | ||
/** | ||
@@ -23,3 +24,3 @@ * @constructor Smsglobal | ||
if (this.key === undefined || this.secret === undefined) { | ||
throw new Error('api key and secret are both required parameters.'); | ||
throw new Error(errors.smsglobal); | ||
} | ||
@@ -29,2 +30,3 @@ }; | ||
Smsglobal.prototype.sms = require('./api/Sms'); | ||
Smsglobal.prototype.otp = require('./api/Otp'); | ||
module.exports = Smsglobal; |
@@ -19,3 +19,3 @@ const Ajv = require('ajv'); | ||
// validate data and convert into given as format | ||
return typeof data === 'string' && luxon.DateTime.fromString(data, schema).isValid; | ||
return typeof data === 'string' && luxon.DateTime.fromFormat(data, schema).isValid; | ||
@@ -22,0 +22,0 @@ }, |
{ | ||
"name": "smsglobal", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "SDK for SMSGlobal rest api", | ||
@@ -50,3 +50,3 @@ "homepage": "https://github.com/smsglobal/smsglobal-node#readme", | ||
"engines": { | ||
"node": ">=9 <=14" | ||
"node": ">=9" | ||
}, | ||
@@ -53,0 +53,0 @@ "directories": { |
162
README.md
@@ -6,4 +6,4 @@ # SMSGlobal node SDK | ||
[](https://codecov.io/gh/smsglobal/smsglobal-node) | ||
 | ||
 | ||
[](https://www.npmjs.com/package/smsglobal) | ||
[](https://www.npmjs.com/package/smsglobal) | ||
[](https://www.npmjs.com/package/smsglobal) | ||
@@ -43,4 +43,6 @@ | ||
> All method return promise if no callback is given | ||
### To send a sms | ||
``` | ||
```js | ||
var payload = { | ||
@@ -58,3 +60,3 @@ origin: 'from number', | ||
``` | ||
```js | ||
var promise = smsglobal.sms.getAll(); | ||
@@ -73,3 +75,3 @@ | ||
``` | ||
```js | ||
var id = 'outgoing-sms-id'; | ||
@@ -89,3 +91,3 @@ var promise = smsglobal.sms.get(id); | ||
``` | ||
```js | ||
var promise = smsglobal.sms.incoming.getAll(); | ||
@@ -104,3 +106,3 @@ | ||
``` | ||
```js | ||
var id = 'incoming-sms-id'; | ||
@@ -117,4 +119,147 @@ var promise = smsglobal.sms.incoming.get(id); | ||
``` | ||
> All method returns promise if no callback is given | ||
### To send an OTP | ||
```js | ||
var payload = { | ||
origin: 'from number', | ||
message: '{*code*} is your SMSGlobal verification code.', | ||
destination: 'destination' | ||
}; | ||
// {*code*} placeholder is mandatory and will be replaced by an auto generated numeric code. | ||
smsglobal.otp.send(payload, function(error, response) { | ||
if (response) { | ||
console.log(response); | ||
} | ||
if (error) { | ||
console.log(error); | ||
} | ||
}); | ||
``` | ||
*Success response object* | ||
```js | ||
{ | ||
statusCode: 200, | ||
status: 'OK', | ||
data: { | ||
requestId: '404372541683676561917558', | ||
validUnitlTimestamp: '2020-11-18 17:08:14', | ||
createdTimestamp: '2020-11-18 16:58:14', | ||
lastEventTimestamp: '2020-11-18 16:58:14', | ||
status: 'Sent' | ||
} | ||
} | ||
``` | ||
*Error response object in the case of validation error* | ||
```js | ||
{ | ||
statusCode: 400, | ||
status: 'Bad Request', | ||
data: { | ||
errors: { | ||
message: { | ||
errors: [ | ||
'Message template should contain a placeholder for code i.e. {*code*}.' | ||
] | ||
} | ||
} | ||
} | ||
} | ||
``` | ||
### To cancel an OTP request | ||
```js | ||
var id = 'otp-request-id'; // requestId received upon sending an OTP | ||
var promise = smsglobal.otp.cancel(id) | ||
promise.then((response) => { | ||
console.log(response) | ||
}).catch((err) => { | ||
console.log(error) | ||
}); | ||
``` | ||
### To verify an OTP code entered by your user | ||
```js | ||
var id = 'otp-request-id'; // requestId received upon sending an OTP | ||
var code = 'otp-code'; // input code entered by your user | ||
smsglobal.otp.verify(id, code, function(error, response) { | ||
if (response) { | ||
console.log(response); | ||
} | ||
if (error) { | ||
console.log(error); | ||
} | ||
}); | ||
``` | ||
### To fetch an OTP request | ||
```js | ||
var id = 'otp-request-id'; // requestId received upon sending an OTP | ||
var promise = smsglobal.otp.get(id); | ||
promise | ||
.then(function(response) { | ||
console.log(response) | ||
}) | ||
.catch(function(error){ | ||
console.log(error) | ||
}); | ||
``` | ||
### To fetch a list of OTP requests | ||
The `getAll` method accepts two arguments first one is filter options and second one is a callback method. Both arguments are optional. Following exampels illustrate different ways of using it. | ||
*Filter options argument* | ||
```js | ||
var options = { status: 'Verfied'}; | ||
var promise = smsglobal.otp.getAll(options); | ||
promise | ||
.then(function(response) { | ||
console.log(response) | ||
}) | ||
.catch(function(error){ | ||
console.log(error) | ||
}); | ||
``` | ||
#### *Callback method arguments* | ||
```js | ||
smsglobal.otp.getAll(function (error, response) { | ||
console.log(response); | ||
}); | ||
``` | ||
#### *Filter options and callback method arguments* | ||
``` | ||
var options = { status: 'Verfied'}; | ||
smsglobal.otp.getAll(options, function (error, response) { | ||
console.log(response); | ||
}); | ||
``` | ||
## Running tests | ||
@@ -137,2 +282,3 @@ | ||
* [sms-incoming](https://www.smsglobal.com/rest-api/?utm_source=dev&utm_medium=github&utm_campaign=node_sdk#api-endpoints) | ||
* [OTP](https://www.smsglobal.com/rest-api/?utm_source=dev&utm_medium=github&utm_campaign=node_sdk#api-endpoints) | ||
@@ -139,0 +285,0 @@ # Reference |
const nock = require('nock'); | ||
const config = require('../lib/config'); | ||
const { assert, expect } = require('chai'); | ||
const errors = require('../lib/errors'); | ||
describe('Outgoing', () => { | ||
@@ -334,3 +334,3 @@ const uri = '/sms'; | ||
(err) => { | ||
assert.equal(err, 'offset and limit (defualt 20) total can not exceed 10000'); | ||
assert.equal(err, errors.searchOffsetLimit); | ||
}, | ||
@@ -351,3 +351,3 @@ ); | ||
(err) => { | ||
assert.equal(err, 'offset and limit (defualt 20) total can not exceed 10000'); | ||
assert.equal(err, errors.searchOffsetLimit); | ||
}, | ||
@@ -354,0 +354,0 @@ ); |
const { expect } = require('chai'); | ||
const errors = require('../lib/errors'); | ||
@@ -12,3 +13,3 @@ describe('Smsglobal', () => { | ||
var Smsglobal = require('../lib'); | ||
expect(() => { new Smsglobal();}).to.throw('api key and secret are both required parameters.'); | ||
expect(() => { new Smsglobal();}).to.throw(errors.smsglobal); | ||
}); | ||
@@ -15,0 +16,0 @@ |
const nock = require('nock'); | ||
const config = require('../lib/config'); | ||
const { assert, expect } = require('chai'); | ||
const errors = require('../lib/errors'); | ||
@@ -135,3 +135,3 @@ describe('Incoming', () => { | ||
(err) => { | ||
assert.equal(err, 'offset and limit (defualt 20) total can not exceed 10000'); | ||
assert.equal(err, errors.searchOffsetLimit); | ||
}, | ||
@@ -138,0 +138,0 @@ ); |
68897
23
2017
281
14