notifications-node-client 8.0.0 → 8.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
+ ## 8.2.0 - 2024-05-17
2
+
3
+ * Add support for providing a custom underlying Axios client via `setClient`.
4
+
5
+ ## 8.1.0 - 2024-05-09
6
+
7
+ * The `sendEmail` function can now be passed `oneClickUnsubscribeURL` as an optional argument.
8
+
1
9
  ## 8.0.0 - 2023-12-27
2
10
 
3
11
  * Remove the default `is_csv` boolean parameter from `prepareUpload`. This method now accepts a file and an options map with the following options. For more specific information please read the documentation.
@@ -1,4 +1,4 @@
1
- var restClient = require('axios').default,
1
+ var defaultRestClient = require('axios').default,
2
2
  createGovukNotifyToken = require('../client/authentication.js'),
3
3
  notifyProductionAPI = 'https://api.notifications.service.gov.uk',
4
4
  version = require('../package.json').version;
@@ -13,6 +13,7 @@ var restClient = require('axios').default,
13
13
  function ApiClient() {
14
14
 
15
15
  this.proxy = null;
16
+ this.restClient = defaultRestClient;
16
17
 
17
18
  if (arguments.length === 1) {
18
19
  this.urlBase = notifyProductionAPI;
@@ -75,7 +76,7 @@ Object.assign(ApiClient.prototype, {
75
76
  Object.assign(options, additionalOptions)
76
77
  if(this.proxy !== null) options.proxy = this.proxy;
77
78
 
78
- return restClient(options);
79
+ return this.restClient(options);
79
80
  },
80
81
 
81
82
  /**
@@ -98,7 +99,7 @@ Object.assign(ApiClient.prototype, {
98
99
 
99
100
  if(this.proxy !== null) options.proxy = this.proxy;
100
101
 
101
- return restClient(options);
102
+ return this.restClient(options);
102
103
  },
103
104
 
104
105
  /**
@@ -107,6 +108,14 @@ Object.assign(ApiClient.prototype, {
107
108
  */
108
109
  setProxy: function(proxyConfig){
109
110
  this.proxy = proxyConfig
111
+ },
112
+
113
+ /**
114
+ *
115
+ * @param {object} an axios instance
116
+ */
117
+ setClient: function(restClient){
118
+ this.restClient = restClient;
110
119
  }
111
120
  });
112
121
 
@@ -23,10 +23,11 @@ function NotifyClient() {
23
23
  * @param {Object} personalisation
24
24
  * @param {String} reference
25
25
  * @param {String} replyToId
26
+ * @param {String} oneClickUnsubscribeURL
26
27
  *
27
28
  * @returns {Object}
28
29
  */
29
- function createNotificationPayload(type, templateId, to, personalisation, reference, replyToId) {
30
+ function createNotificationPayload(type, templateId, to, personalisation, reference, replyToId, oneClickUnsubscribeURL) {
30
31
 
31
32
  var payload = {
32
33
  template_id: templateId
@@ -53,6 +54,10 @@ function createNotificationPayload(type, templateId, to, personalisation, refere
53
54
  payload.sms_sender_id = replyToId;
54
55
  }
55
56
 
57
+ if (oneClickUnsubscribeURL && type == 'email') {
58
+ payload.one_click_unsubscribe_url = oneClickUnsubscribeURL;
59
+ }
60
+
56
61
  return payload;
57
62
  }
58
63
 
@@ -144,16 +149,17 @@ Object.assign(NotifyClient.prototype, {
144
149
  */
145
150
  sendEmail: function (templateId, emailAddress, options) {
146
151
  options = options || {};
147
- var err = checkOptionsKeys(['personalisation', 'reference', 'emailReplyToId'], options)
152
+ var err = checkOptionsKeys(['personalisation', 'reference', 'emailReplyToId', 'oneClickUnsubscribeURL'], options)
148
153
  if (err) {
149
154
  return Promise.reject(err);
150
155
  }
151
156
  var personalisation = options.personalisation || undefined,
152
157
  reference = options.reference || undefined,
153
- emailReplyToId = options.emailReplyToId || undefined;
158
+ emailReplyToId = options.emailReplyToId || undefined,
159
+ oneClickUnsubscribeURL = options.oneClickUnsubscribeURL || undefined;
154
160
 
155
161
  return this.apiClient.post('/v2/notifications/email',
156
- createNotificationPayload('email', templateId, emailAddress, personalisation, reference, emailReplyToId));
162
+ createNotificationPayload('email', templateId, emailAddress, personalisation, reference, emailReplyToId, oneClickUnsubscribeURL));
157
163
  },
158
164
 
159
165
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "notifications-node-client",
3
- "version": "8.0.0",
3
+ "version": "8.2.0",
4
4
  "homepage": "https://docs.notifications.service.gov.uk/node.html",
5
5
  "repository": {
6
6
  "type": "git",
@@ -20,20 +20,20 @@
20
20
  "author": "GDS developers",
21
21
  "license": "MIT",
22
22
  "dependencies": {
23
- "jsonwebtoken": "^9.0.0",
24
- "axios": "^1.6.1"
23
+ "axios": "^1.6.1",
24
+ "jsonwebtoken": "^9.0.0"
25
25
  },
26
26
  "devDependencies": {
27
27
  "chai": "4.1.2",
28
28
  "chai-as-promised": "7.1.1",
29
- "chai-json-schema": "1.5.0",
30
29
  "chai-bytes": "0.1.2",
30
+ "chai-json-schema": "1.5.0",
31
31
  "jsonschema": "1.2.4",
32
32
  "mocha": "10.1.0",
33
33
  "mockdate": "2.0.2",
34
34
  "nock": "9.2.6",
35
35
  "optimist": "0.6.1",
36
+ "sinon": "^18.0.0",
36
37
  "standard-markdown": "5.0.1"
37
38
  }
38
39
  }
39
-
@@ -3,8 +3,9 @@ var expect = require('chai').expect,
3
3
  ApiClient = require('../client/api_client.js'),
4
4
  nock = require('nock'),
5
5
  createGovukNotifyToken = require('../client/authentication.js'),
6
- version = require('../package.json').version;
7
-
6
+ version = require('../package.json').version,
7
+ axios = require('axios'),
8
+ sinon = require('sinon');
8
9
 
9
10
  describe('api client', function () {
10
11
 
@@ -117,4 +118,39 @@ describe('api client', function () {
117
118
  done();
118
119
  });
119
120
  });
121
+
122
+ it('should use the custom Axios client when set', function (done) {
123
+ var urlBase = 'https://api.notifications.service.gov.uk',
124
+ path = '/email',
125
+ body = {
126
+ 'body': 'body text'
127
+ },
128
+ serviceId = 'c745a8d8-b48a-4b0d-96e5-dbea0165ebd1',
129
+ apiKeyId = '8b3aa916-ec82-434e-b0c5-d5d9b371d6a3';
130
+
131
+ var customClientStub = sinon.stub().resolves({ data: body });
132
+
133
+ var apiClient = new ApiClient(serviceId, apiKeyId);
134
+ apiClient.setClient(customClientStub);
135
+
136
+ nock(urlBase, {
137
+ reqheaders: {
138
+ 'Authorization': 'Bearer ' + createGovukNotifyToken('GET', path, apiKeyId, serviceId),
139
+ 'User-Agent': 'NOTIFY-API-NODE-CLIENT/' + version
140
+ }
141
+ })
142
+ .get(path)
143
+ .reply(200, body);
144
+
145
+ apiClient.get(path)
146
+ .then(function (response) {
147
+ expect(response.data).to.deep.equal(body);
148
+ expect(customClientStub.calledOnce).to.be.true;
149
+ expect(customClientStub.args[0][0].url).to.equal(urlBase + path);
150
+ expect(customClientStub.args[0][0].headers['Authorization']).to.equal('Bearer ' + createGovukNotifyToken('GET', path, apiKeyId, serviceId));
151
+ expect(customClientStub.args[0][0].headers['User-Agent']).to.equal('NOTIFY-API-NODE-CLIENT/' + version);
152
+ done();
153
+ })
154
+ .catch(done);
155
+ });
120
156
  });
@@ -25,7 +25,8 @@
25
25
  "properties": {
26
26
  "body": {"type": "string"},
27
27
  "from_email": {"type": "string", "format": "email_address"},
28
- "subject": {"type": "string"}
28
+ "subject": {"type": "string"},
29
+ "one_click_unsubscribe_url": {"type": ["string", "null"], "format": "uri"}
29
30
  },
30
31
  "required": ["body", "from_email", "subject"]
31
32
  },
@@ -36,6 +36,7 @@ describer('notification api with a live service', function () {
36
36
  let letterNotificationId;
37
37
  const personalisation = { name: 'Foo' };
38
38
  const clientRef = 'client-ref';
39
+ const oneClickUnsubscribeURL = 'https://www.example.com';
39
40
  const email = process.env.FUNCTIONAL_TEST_EMAIL;
40
41
  const phoneNumber = process.env.FUNCTIONAL_TEST_NUMBER;
41
42
  const letterContact = {
@@ -88,6 +89,22 @@ describer('notification api with a live service', function () {
88
89
  expect(response.data).to.be.jsonSchema(postEmailNotificationResponseJson);
89
90
  response.data.content.body.should.equal('Hello Foo\r\n\r\nFunctional test help make our world a better place');
90
91
  response.data.content.subject.should.equal('Functional Tests are good');
92
+ should.equal(response.data.content.one_click_unsubscribe_url, null);
93
+ response.data.reference.should.equal(clientRef);
94
+ emailNotificationId = response.data.id;
95
+ })
96
+ });
97
+
98
+ it('send email notification with unsubscribe link', () => {
99
+ var postEmailNotificationResponseJson = require('./schemas/v2/POST_notification_email_response.json'),
100
+ options = {personalisation: personalisation, reference: clientRef, oneClickUnsubscribeURL: oneClickUnsubscribeURL};
101
+
102
+ return notifyClient.sendEmail(emailTemplateId, email, options).then((response) => {
103
+ response.status.should.equal(201);
104
+ expect(response.data).to.be.jsonSchema(postEmailNotificationResponseJson);
105
+ response.data.content.body.should.equal('Hello Foo\r\n\r\nFunctional test help make our world a better place');
106
+ response.data.content.subject.should.equal('Functional Tests are good');
107
+ response.data.content.one_click_unsubscribe_url.should.equal(oneClickUnsubscribeURL);
91
108
  response.data.reference.should.equal(clientRef);
92
109
  emailNotificationId = response.data.id;
93
110
  })
@@ -92,6 +92,31 @@ describe('notification api', () => {
92
92
  });
93
93
  });
94
94
 
95
+ it('should send an email with a one click unsubscribe URL', () => {
96
+
97
+ let email = 'me@example.com',
98
+ templateId = '123',
99
+ options = {
100
+ personalisation: {foo: 'bar'},
101
+ oneClickUnsubscribeURL: '456',
102
+ },
103
+ data = {
104
+ template_id: templateId,
105
+ email_address: email,
106
+ personalisation: options.personalisation,
107
+ one_click_unsubscribe_url: options.oneClickUnsubscribeURL
108
+ };
109
+
110
+ notifyAuthNock
111
+ .post('/v2/notifications/email', data)
112
+ .reply(200, {hooray: 'bkbbk'});
113
+
114
+ return notifyClient.sendEmail(templateId, email, options)
115
+ .then((response) => {
116
+ expect(response.status).to.equal(200);
117
+ });
118
+ });
119
+
95
120
  it('should send an email with document upload', () => {
96
121
  let email = 'dom@example.com',
97
122
  templateId = '123',