notifications-node-client
Advanced tools
Comparing version 2.1.0 to 2.3.1
var restClient = require('request-promise'), | ||
_ = require('underscore'), | ||
createGovukNotifyToken = require('../client/authentication.js'); | ||
createGovukNotifyToken = require('../client/authentication.js'), | ||
notifyProductionAPI = 'https://api.notifications.service.gov.uk'; | ||
/** | ||
* @param urlBase | ||
* @param clientId | ||
* @param secret | ||
* @param serviceId | ||
* @param apiKeyId | ||
* | ||
* @constructor | ||
*/ | ||
function ApiClient(urlBase, clientId, secret) { | ||
this.clientId = clientId; | ||
this.secret = secret; | ||
this.urlBase = urlBase; | ||
function ApiClient() { | ||
if (arguments.length === 1) { | ||
this.urlBase = notifyProductionAPI; | ||
this.apiKeyId = arguments[0].substring(arguments[0].length - 36, arguments[0].length); | ||
this.serviceId = arguments[0].substring(arguments[0].length - 73, arguments[0].length - 37); | ||
} | ||
if (arguments.length === 2) { | ||
if (arguments[0].startsWith('http')) { | ||
this.urlBase = arguments[0]; | ||
this.apiKeyId = arguments[1].substring(arguments[1].length - 36, arguments[1].length); | ||
this.serviceId = arguments[1].substring(arguments[1].length - 73, arguments[1].length - 37); | ||
} else { | ||
this.urlBase = notifyProductionAPI; | ||
this.serviceId = arguments[0]; | ||
this.apiKeyId = arguments[1].substring(arguments[1].length - 36, arguments[1].length); | ||
} | ||
} | ||
if (arguments.length === 3) { | ||
this.urlBase = arguments[0]; | ||
this.serviceId = arguments[1]; | ||
this.apiKeyId = arguments[2].substring(arguments[2].length - 36, arguments[2].length); | ||
} | ||
} | ||
@@ -22,9 +47,9 @@ | ||
* @param {string} requestPath | ||
* @param {string} secret | ||
* @param {string}clientId | ||
* @param {string} apiKeyId | ||
* @param {string} serviceId | ||
* | ||
* @returns {string} | ||
*/ | ||
function createToken(requestMethod, requestPath, secret, clientId) { | ||
return createGovukNotifyToken(requestMethod, requestPath, secret, clientId) | ||
function createToken(requestMethod, requestPath, apiKeyId, serviceId) { | ||
return createGovukNotifyToken(requestMethod, requestPath, apiKeyId, serviceId); | ||
} | ||
@@ -47,3 +72,3 @@ | ||
headers: { | ||
'Authorization': 'Bearer ' + createToken('GET', path, this.secret, this.clientId) | ||
'Authorization': 'Bearer ' + createToken('GET', path, this.apiKeyId, this.serviceId) | ||
} | ||
@@ -70,3 +95,3 @@ }; | ||
headers: { | ||
'Authorization': 'Bearer ' + createToken('GET', path, this.secret, this.clientId) | ||
'Authorization': 'Bearer ' + createToken('GET', path, this.apiKeyId, this.serviceId) | ||
} | ||
@@ -73,0 +98,0 @@ }; |
@@ -5,3 +5,3 @@ var jwt = require('jsonwebtoken'); | ||
function createGovukNotifyToken(request_method, request_path, secret, client_id, request_body) { | ||
function createGovukNotifyToken(request_method, request_path, secret, client_id) { | ||
@@ -8,0 +8,0 @@ return jwt.sign( |
@@ -7,9 +7,12 @@ var ApiClient = require('./api_client'), | ||
* @param baseUrl | ||
* @param clientId | ||
* @param secret | ||
* @param serviceId | ||
* @param apiKeyId | ||
* | ||
* @constructor | ||
*/ | ||
function NotifyClient(baseUrl, clientId, secret) { | ||
this.apiClient = new ApiClient(baseUrl, clientId, secret); | ||
function NotifyClient() { | ||
this.apiClient = new (Function.prototype.bind.apply( | ||
ApiClient, | ||
[null].concat(Array.prototype.slice.call(arguments)) | ||
)); | ||
} | ||
@@ -42,3 +45,3 @@ | ||
* | ||
* notifyClient = new NotifyClient(urlBase, clientId, secret); | ||
* notifyClient = new NotifyClient(urlBase, serviceId, apiKeyId); | ||
* | ||
@@ -86,3 +89,9 @@ * notifyClient.sendEmail(templateId, email, personalisation) | ||
return this.apiClient.get('/notifications/' + notificationId); | ||
} | ||
}, | ||
/** | ||
* | ||
* @returns {Promise} | ||
*/ | ||
getNotifications: function(){return this.apiClient.get('/notifications')} | ||
}); | ||
@@ -92,2 +101,2 @@ | ||
NotifyClient: NotifyClient | ||
}; | ||
}; |
{ | ||
"name": "notifications-node-client", | ||
"version": "2.1.0", | ||
"version": "2.3.1", | ||
"description": "GOV.UK Notify Node.js client ", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "mocha spec/*.js" | ||
"test": "mocha \"spec/**/*.js\"", | ||
"integration": "./node_modules/.bin/mocha spec/integration/test.js" | ||
}, | ||
@@ -21,2 +22,5 @@ "author": "GDS developers", | ||
"chai": "3.4.1", | ||
"chai-as-promised": "^5.3.0", | ||
"chai-json-schema": "^1.3.0", | ||
"jsonschema": "^1.1.0", | ||
"mocha": "2.3.4", | ||
@@ -23,0 +27,0 @@ "mockdate": "1.0.3", |
@@ -1,48 +0,49 @@ | ||
[![Build Status](https://travis-ci.org/alphagov/notifications-node-client.svg)](https://travis-ci.org/alphagov/notifications-node-client) | ||
[![Dependency Status](https://david-dm.org/alphagov/notifications-node-client.svg)](https://david-dm.org/alphagov/notifications-node-client) | ||
Work in progress GOV.UK Notify Node.js client. Usage: | ||
# GOV.UK Notify Node.js client | ||
## Installation | ||
```shell | ||
npm install notifications-node-client | ||
``` | ||
## Getting started | ||
```javascript | ||
var request = require('request'); | ||
var createGOVUKNotifyToken = require('notifications-node-client'); | ||
var yourServiceID = 123; | ||
var yourAPIKey = 'SECRET--DO NOT CHECK IN!'; | ||
var NotifyClient = require('notifications-node-client').NotifyClient, | ||
// GET request | ||
var token = createGOVUKNotifyToken(yourAPIKey, yourServiceID); | ||
notifyClient = new NotifyClient(apiKey); | ||
``` | ||
request( | ||
{ | ||
method: 'GET', | ||
url: 'https://api.notify.works/notifications/sms', | ||
headers: { | ||
'Content-type': "application/json", | ||
"Authorization": "Bearer " + token | ||
} | ||
}, | ||
function(error, response, body) { | ||
console.log(error, response, body); | ||
} | ||
); | ||
Generate an API key by logging in to | ||
[GOV.UK Notify](https://www.notifications.service.gov.uk) and going to | ||
the _API integration_ page. | ||
## Send a message | ||
// POST request | ||
var token = createGOVUKNotifyToken(yourAPIKey, yourServiceID); | ||
```javascript | ||
notifyClient.sendEmail(templateId, emailAddress, personalisation); | ||
``` | ||
request( | ||
{ | ||
method: 'POST', | ||
url: 'https://api.notify.works/notifications/sms', | ||
headers: { | ||
'Content-type': "application/json", | ||
"Authorization": "Bearer " + token | ||
}, | ||
body: "{content:'Hello world'}" | ||
}, | ||
function(error, response, body) { | ||
console.log(error, response, body); | ||
} | ||
); | ||
```javascript | ||
notifyClient.sendSms(templateId, phoneNumber, personalisation); | ||
``` | ||
Find template_id by clicking API info for the template you want to send. | ||
### With personalisation | ||
```javascript | ||
notifyClient.sendSms(templateId, phoneNumber, personalisation={ | ||
'name': 'Amala', | ||
'reference_number': '300241', | ||
}); | ||
``` | ||
## Get the status of one message | ||
```javascript | ||
notifyClient.getNotificationById(notificationId) | ||
``` | ||
## Get the status of all messages | ||
```javascript | ||
notifyClient.getNotifications() | ||
``` |
@@ -11,3 +11,3 @@ var expect = require('chai').expect, | ||
var urlBase = 'http://base', | ||
var urlBase = 'https://api.notifications.service.gov.uk', | ||
path = '/email', | ||
@@ -17,20 +17,28 @@ body = { | ||
}, | ||
clientId = 123, | ||
secret = 'SECRET', | ||
apiClient = new ApiClient(urlBase, clientId, secret); | ||
serviceId = 'c745a8d8-b48a-4b0d-96e5-dbea0165ebd1', | ||
apiKeyId = '8b3aa916-ec82-434e-b0c5-d5d9b371d6a3'; | ||
nock(urlBase, { | ||
reqheaders: { | ||
'Authorization': 'Bearer ' + createGovukNotifyToken('GET', path, secret, clientId) | ||
} | ||
}) | ||
.get(path) | ||
.reply(200, body); | ||
[ | ||
new ApiClient(serviceId, apiKeyId), | ||
new ApiClient(urlBase, serviceId, apiKeyId), | ||
new ApiClient(urlBase, 'key_name' + '-' + serviceId + '-' + apiKeyId), | ||
new ApiClient('key_name' + ':' + serviceId + ':' + apiKeyId), | ||
].forEach(function(client, index, clients) { | ||
apiClient = new ApiClient(urlBase, clientId, secret); | ||
apiClient.get(path) | ||
.then(function (response) { | ||
expect(response.body).to.deep.equal(body); | ||
done(); | ||
nock(urlBase, { | ||
reqheaders: { | ||
'Authorization': 'Bearer ' + createGovukNotifyToken('GET', path, apiKeyId, serviceId) | ||
} | ||
}) | ||
.get(path) | ||
.reply(200, body); | ||
client.get(path) | ||
.then(function (response) { | ||
expect(response.body).to.deep.equal(body); | ||
if (index == clients.length - 1) done(); | ||
}); | ||
}); | ||
}); | ||
@@ -45,5 +53,5 @@ | ||
}, | ||
clientId = 123, | ||
secret = 'SECRET', | ||
apiClient = new ApiClient(urlBase, clientId, secret); | ||
serviceId = 123, | ||
apiKeyId = 'SECRET', | ||
apiClient = new ApiClient(urlBase, serviceId, apiKeyId); | ||
@@ -53,3 +61,3 @@ | ||
reqheaders: { | ||
'Authorization': 'Bearer ' + createGovukNotifyToken('POST', path, secret, clientId) | ||
'Authorization': 'Bearer ' + createGovukNotifyToken('POST', path, apiKeyId, serviceId) | ||
} | ||
@@ -60,3 +68,3 @@ }) | ||
apiClient = new ApiClient(urlBase, clientId, secret); | ||
apiClient = new ApiClient(urlBase, serviceId, apiKeyId); | ||
apiClient.post(path, data) | ||
@@ -63,0 +71,0 @@ .then(function (response) { |
@@ -20,8 +20,8 @@ var expect = require('chai').expect, NotifyClient = require('../client/notification.js').NotifyClient, | ||
notifyClient, | ||
clientId = 123, | ||
secret = 'SECRET'; | ||
serviceId = 'c745a8d8-b48a-4b0d-96e5-dbea0165ebd1', | ||
apiKeyId = '8b3aa916-ec82-434e-b0c5-d5d9b371d6a3'; | ||
nock(urlBase, { | ||
reqheaders: { | ||
'Authorization': 'Bearer ' + createGovukNotifyToken('POST', '/notifications/email', secret, clientId) | ||
'Authorization': 'Bearer ' + createGovukNotifyToken('POST', '/notifications/email', apiKeyId, serviceId) | ||
}}) | ||
@@ -31,3 +31,3 @@ .post('/notifications/email', data) | ||
notifyClient = new NotifyClient(urlBase, clientId, secret); | ||
notifyClient = new NotifyClient(urlBase, serviceId, apiKeyId); | ||
notifyClient.sendEmail(templateId, email, personalisation) | ||
@@ -52,8 +52,8 @@ .then(function (response) { | ||
notifyClient, | ||
clientId = 123, | ||
secret = 'SECRET'; | ||
serviceId = 'c745a8d8-b48a-4b0d-96e5-dbea0165ebd1', | ||
apiKeyId = '8b3aa916-ec82-434e-b0c5-d5d9b371d6a3'; | ||
nock(urlBase, { | ||
reqheaders: { | ||
'Authorization': 'Bearer ' + createGovukNotifyToken('POST', '/notifications/email', secret, clientId) | ||
'Authorization': 'Bearer ' + createGovukNotifyToken('POST', '/notifications/email', apiKeyId, serviceId) | ||
}}) | ||
@@ -63,3 +63,3 @@ .post('/notifications/sms', data) | ||
notifyClient = new NotifyClient(urlBase, clientId, secret); | ||
notifyClient = new NotifyClient(urlBase, serviceId, apiKeyId); | ||
notifyClient.sendSms(templateId, phoneNo, personalisation) | ||
@@ -77,8 +77,8 @@ .then(function (response) { | ||
notifyClient, | ||
clientId = 123, | ||
secret = 'SECRET'; | ||
serviceId = 'c745a8d8-b48a-4b0d-96e5-dbea0165ebd1', | ||
apiKeyId = '8b3aa916-ec82-434e-b0c5-d5d9b371d6a3'; | ||
nock(urlBase, { | ||
reqheaders: { | ||
'Authorization': 'Bearer ' + createGovukNotifyToken('POST', '/notifications/email', secret, clientId) | ||
'Authorization': 'Bearer ' + createGovukNotifyToken('POST', '/notifications/email', apiKeyId, serviceId) | ||
}}) | ||
@@ -88,3 +88,3 @@ .get('/notifications/' + notificationId) | ||
notifyClient = new NotifyClient(urlBase, clientId, secret); | ||
notifyClient = new NotifyClient(urlBase, serviceId, apiKeyId); | ||
notifyClient.getNotificationById(notificationId) | ||
@@ -91,0 +91,0 @@ .then(function (response) { |
Sorry, the diff of this file is not supported yet
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
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 8 instances in 1 package
29578
23
786
50
8
9
1