notifications-node-client 8.1.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,7 @@
1
+ ## 8.2.0 - 2024-05-17
2
+
3
+ * Add support for providing a custom underlying Axios client via `setClient`.
4
+
1
5
  ## 8.1.0 - 2024-05-09
2
6
 
3
7
  * The `sendEmail` function can now be passed `oneClickUnsubscribeURL` as an optional argument.
@@ -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
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "notifications-node-client",
3
- "version": "8.1.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,19 +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
  }
@@ -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
  });