mailgun.js 4.0.1 → 4.1.3

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.
@@ -3,7 +3,12 @@ import url from 'url';
3
3
  import urljoin from 'url-join';
4
4
 
5
5
  import Request from './request';
6
- import { BounceData, ComplaintData, UnsubscribeData } from './interfaces/Supressions';
6
+ import {
7
+ BounceData,
8
+ ComplaintData,
9
+ UnsubscribeData,
10
+ WhiteListData
11
+ } from './interfaces/Supressions';
7
12
 
8
13
  const createOptions = {
9
14
  headers: { 'Content-Type': 'application/json' }
@@ -51,7 +56,21 @@ class Unsubscribe {
51
56
  }
52
57
  }
53
58
 
54
- type TModel = typeof Bounce | typeof Complaint | typeof Unsubscribe;
59
+ class WhiteList {
60
+ type: string;
61
+ value: string;
62
+ reason: string;
63
+ createdAt: Date;
64
+
65
+ constructor(data: WhiteListData) {
66
+ this.type = 'whitelists';
67
+ this.value = data.value;
68
+ this.reason = data.reason;
69
+ this.createdAt = new Date(data.createdAt);
70
+ }
71
+ }
72
+
73
+ type TModel = typeof Bounce | typeof Complaint | typeof Unsubscribe | typeof WhiteList;
55
74
 
56
75
  export default class SuppressionClient {
57
76
  request: any;
@@ -59,6 +78,7 @@ export default class SuppressionClient {
59
78
  bounces: typeof Bounce;
60
79
  complaints: typeof Complaint;
61
80
  unsubscribes: typeof Unsubscribe;
81
+ whitelists: typeof WhiteList;
62
82
  };
63
83
 
64
84
  constructor(request: Request) {
@@ -66,7 +86,8 @@ export default class SuppressionClient {
66
86
  this.models = {
67
87
  bounces: Bounce,
68
88
  complaints: Complaint,
69
- unsubscribes: Unsubscribe
89
+ unsubscribes: Unsubscribe,
90
+ whitelists: WhiteList,
70
91
  };
71
92
  }
72
93
 
@@ -106,6 +127,12 @@ export default class SuppressionClient {
106
127
  return new Model(response.body);
107
128
  }
108
129
 
130
+ private createWhiteList(domain: string, data: any) {
131
+ return this.request
132
+ .postWithFD(urljoin('v3', domain, 'whitelists'), data, createOptions)
133
+ .then((response: { body: any }) => response.body);
134
+ }
135
+
109
136
  list(domain: string, type: string, query: any) {
110
137
  const model = (this.models as any)[type];
111
138
 
@@ -125,14 +152,18 @@ export default class SuppressionClient {
125
152
  create(domain: string, type: string, data: any) {
126
153
  // supports adding multiple suppressions by default
127
154
  let postData;
155
+ if (type === 'whitelists') {
156
+ return this.createWhiteList(domain, data);
157
+ }
158
+
128
159
  if (!Array.isArray(data)) {
129
160
  postData = [data];
130
161
  } else {
131
- postData = { ...data };
162
+ postData = [...data];
132
163
  }
133
164
 
134
165
  return this.request
135
- .post(urljoin('v3', domain, type), postData, createOptions)
166
+ .post(urljoin('v3', domain, type), JSON.stringify(postData), createOptions)
136
167
  .then((response: { body: any }) => response.body);
137
168
  }
138
169
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mailgun.js",
3
- "version": "4.0.1",
3
+ "version": "4.1.3",
4
4
  "main": "dist/mailgun.node.js",
5
5
  "browser": "dist/mailgun.web.js",
6
6
  "types": "dist/index.d.ts",
@@ -23,7 +23,7 @@
23
23
  "build:release": "webpack --config ./webpack/webpack.release.config.js --progress --color",
24
24
  "start": "webpack --watch --config ./webpack/webpack.dev.config.js --progress --color",
25
25
  "release": "standard-version -a",
26
- "test": "multi='dot=- xunit=./results.xml' mocha -t 10000 -R mocha-multi -r ts-node/register test/*.test.ts",
26
+ "test": "multi='dot=- xunit=./results.xml' nyc mocha -t 10000 -R mocha-multi -r ts-node/register test/*.test.ts",
27
27
  "test-watch": "mocha -r ts-node/register -w -R dot test/*.test.ts",
28
28
  "docs": "typedoc --tsconfig ./tsconfig.json",
29
29
  "lint": "eslint . && eslint . --ext .ts",
@@ -63,6 +63,7 @@
63
63
  "mocha": "^9.1.3",
64
64
  "mocha-multi": "^1.1.3",
65
65
  "nock": "^13.0.4",
66
+ "nyc": "^15.1.0",
66
67
  "path-browserify": "^1.0.1",
67
68
  "standard-version": "^9.3.1",
68
69
  "terser-webpack-plugin": "^5.2.0",
@@ -14,6 +14,7 @@ import {
14
14
  UpdatedDKIMAuthority,
15
15
  UpdatedDKIMSelectorResponse, UpdatedWebPrefixResponse
16
16
  } from '../lib/interfaces/Domains';
17
+ import DomainTemplatesClient from '../lib/domainsTemplates';
17
18
 
18
19
  // TODO: fix types
19
20
  describe('DomainClient', function () {
@@ -23,7 +24,8 @@ describe('DomainClient', function () {
23
24
  beforeEach(function () {
24
25
  const reqObject = new Request({ url: 'https://api.mailgun.net' } as RequestOptions, formData as InputFormData);
25
26
  const domainCredentialsClient = new DomainCredentialsClient(reqObject);
26
- client = new DomainClient(reqObject, domainCredentialsClient);
27
+ const domainTemplatesClient = new DomainTemplatesClient(reqObject);
28
+ client = new DomainClient(reqObject, domainCredentialsClient, domainTemplatesClient);
27
29
  api = nock('https://api.mailgun.net');
28
30
  });
29
31
 
@@ -0,0 +1,392 @@
1
+ import formData from 'form-data';
2
+
3
+ import nock from 'nock';
4
+ import Request from '../lib/request';
5
+ import RequestOptions from '../lib/interfaces/RequestOptions';
6
+ import { InputFormData } from '../lib/interfaces/IFormData';
7
+ import { DomainCredentialsList, DomainCredentialsResult } from '../lib/interfaces/DomainCredentials';
8
+ import DomainTemplatesClient from '../lib/domainsTemplates';
9
+ import { DomainTemplateUpdateVersionData, DomainTemplateVersionData } from '../lib/interfaces/DomainTemplates';
10
+
11
+ // TODO: fix types
12
+ describe('DomainsTemplatesClient', function () {
13
+ let client: DomainTemplatesClient;
14
+ let api: nock.Scope;
15
+
16
+ beforeEach(function () {
17
+ const reqObject = new Request({ url: 'https://api.mailgun.net' } as RequestOptions, formData as InputFormData);
18
+ client = new DomainTemplatesClient(reqObject);
19
+ api = nock('https://api.mailgun.net');
20
+ });
21
+
22
+ afterEach(function () {
23
+ api.done();
24
+ });
25
+
26
+ describe('list', function () {
27
+ it('fetches all templates for domain', async () => {
28
+ api.get('/v3/testDomain/templates').reply(200, {
29
+ items: [
30
+ {
31
+ name: 'test_template',
32
+ description: 'test_template description',
33
+ createdAt: 'Mon, 20 Dec 2021 14:47:51 UTC',
34
+ createdBy: '',
35
+ id: 'someId'
36
+ }
37
+ ],
38
+ paging: {
39
+ first: 'first page url',
40
+ last: 'last page url',
41
+ next: 'next page url',
42
+ previous: 'previous page url',
43
+ }
44
+ });
45
+
46
+ const templatesList = await client.list('testDomain');
47
+ templatesList.should.be.an('object').to.have.property('items');
48
+ templatesList.items.should.be.an('array').to.have.property('length').to.be.equal(1);
49
+ templatesList.items[0].should.eql({
50
+ name: 'test_template',
51
+ description: 'test_template description',
52
+ createdAt: new Date('Mon, 20 Dec 2021 14:47:51 UTC'),
53
+ createdBy: '',
54
+ id: 'someId'
55
+ });
56
+ });
57
+ });
58
+
59
+ describe('get', function () {
60
+ it('fetches all templates for domain', async () => {
61
+ api.get('/v3/testDomain/templates/testTemplateName').reply(200, {
62
+ template: {
63
+ name: 'test_template',
64
+ description: 'test_template description',
65
+ createdAt: 'Mon, 20 Dec 2021 14:47:51 UTC',
66
+ createdBy: '',
67
+ id: 'someId'
68
+ }
69
+ });
70
+
71
+ const template = await client.get('testDomain', 'testTemplateName');
72
+ template.should.be.an('object');
73
+ template.should.eql({
74
+ name: 'test_template',
75
+ description: 'test_template description',
76
+ createdAt: new Date('Mon, 20 Dec 2021 14:47:51 UTC'),
77
+ createdBy: '',
78
+ id: 'someId'
79
+ });
80
+ });
81
+ });
82
+
83
+ describe('create', function () {
84
+ it('creates domain template', async () => {
85
+ const templateData = {
86
+ name: 'test_template1',
87
+ description: 'test_template1 description',
88
+ template: '%recipient.title%',
89
+ tag: 'v1',
90
+ comment: 'dummy comment'
91
+ };
92
+
93
+ api.post('/v3/testDomain/templates').reply(200, {
94
+ message: 'template has been stored',
95
+ template: {
96
+ name: 'test_template1',
97
+ description: 'test_template description 1',
98
+ createdAt: 'Wed, 22 Dec 2021 08:59:29 UTC',
99
+ createdBy: '',
100
+ id: 'someId',
101
+ version: {
102
+ tag: 'v1',
103
+ template: '%recipient.title%',
104
+ engine: 'handlebars',
105
+ mjml: '',
106
+ createdAt: 'Wed, 22 Dec 2021 08:59:29 UTC',
107
+ comment: 'dummy comment',
108
+ active: true,
109
+ id: 'someId2'
110
+ }
111
+ }
112
+ });
113
+
114
+ const template = await client.create('testDomain', templateData);
115
+ template.should.be.an('object');
116
+ template.should.eql({
117
+ name: 'test_template1',
118
+ description: 'test_template description 1',
119
+ createdAt: new Date('Wed, 22 Dec 2021 08:59:29 UTC'),
120
+ createdBy: '',
121
+ id: 'someId',
122
+ version: {
123
+ tag: 'v1',
124
+ template: '%recipient.title%',
125
+ engine: 'handlebars',
126
+ mjml: '',
127
+ createdAt: new Date('Wed, 22 Dec 2021 08:59:29 UTC'),
128
+ comment: 'dummy comment',
129
+ active: true,
130
+ id: 'someId2'
131
+ }
132
+ });
133
+ });
134
+ });
135
+
136
+ describe('update', function () {
137
+ it('updates domain template', async () => {
138
+ api.put('/v3/testDomain/templates/testTemplateName').reply(200, {
139
+ message: 'template has been updated',
140
+ template: { name: 'test_template1' }
141
+ });
142
+
143
+ const updatedTemplate = await client.update('testDomain', 'testTemplateName', {
144
+ description: 'updated test_template description'
145
+ });
146
+ updatedTemplate.should.be.an('object');
147
+ updatedTemplate.should.eql({
148
+ status: 200,
149
+ message: 'template has been updated',
150
+ templateName: 'test_template1'
151
+ });
152
+ });
153
+ });
154
+
155
+ describe('destroy', function () {
156
+ it('deletes domain template', async () => {
157
+ api.delete('/v3/testDomain/templates/testTemplateName').reply(200, {
158
+ message: 'template has been deleted',
159
+ template: { name: 'test_template1' }
160
+ });
161
+
162
+ const deletedTemplate = await client.destroy('testDomain', 'testTemplateName');
163
+ deletedTemplate.should.eql({
164
+ status: 200,
165
+ message: 'template has been deleted',
166
+ templateName: 'test_template1'
167
+ });
168
+ });
169
+ });
170
+
171
+ describe('destroyAll', function () {
172
+ it('deletes all domain templates', async () => {
173
+ api.delete('/v3/testDomain/templates').reply(200, { message: 'templates have been deleted' });
174
+
175
+ const deletedTemplate = await client.destroyAll('testDomain');
176
+ deletedTemplate.should.eql({ status: 200, message: 'templates have been deleted' });
177
+ });
178
+ });
179
+
180
+ describe('createVersion', function () {
181
+ it('creates new version of domain template', async () => {
182
+ const templateVersionData = {
183
+ template: '%recipient.title%',
184
+ tag: 'v3',
185
+ comment: 'comment',
186
+ active: 'yes'
187
+ } as DomainTemplateVersionData;
188
+
189
+ api.post('/v3/testDomain/templates/testTemplateName/versions').reply(200, {
190
+ message: 'new version of the template has been stored',
191
+ template: {
192
+ name: 'test_template1',
193
+ description: 'test_template description 1',
194
+ createdAt: 'Wed, 22 Dec 2021 08:59:29 UTC',
195
+ createdBy: '',
196
+ id: 'someId',
197
+ version: {
198
+ tag: 'v1',
199
+ template: '%recipient.title%',
200
+ engine: 'handlebars',
201
+ mjml: '',
202
+ createdAt: 'Wed, 22 Dec 2021 08:59:29 UTC',
203
+ comment: 'comment',
204
+ active: true,
205
+ id: 'someId2'
206
+ }
207
+ }
208
+ });
209
+
210
+ const templateVersion = await client.createVersion('testDomain', 'testTemplateName', templateVersionData);
211
+ templateVersion.should.be.an('object');
212
+ templateVersion.should.eql({
213
+ status: 200,
214
+ message: 'new version of the template has been stored',
215
+ template: {
216
+ name: 'test_template1',
217
+ description: 'test_template description 1',
218
+ createdAt: new Date('Wed, 22 Dec 2021 08:59:29 UTC'),
219
+ createdBy: '',
220
+ id: 'someId',
221
+ version: {
222
+ tag: 'v1',
223
+ template: '%recipient.title%',
224
+ engine: 'handlebars',
225
+ mjml: '',
226
+ createdAt: new Date('Wed, 22 Dec 2021 08:59:29 UTC'),
227
+ comment: 'comment',
228
+ active: true,
229
+ id: 'someId2'
230
+ }
231
+ }
232
+ });
233
+ });
234
+ });
235
+
236
+ describe('getVersion', function () {
237
+ it('fetches version of domain template', async () => {
238
+ api.get('/v3/testDomain/templates/testTemplateName/versions/v1').reply(200, {
239
+ template: {
240
+ name: 'test_template',
241
+ description: 'test_template description',
242
+ createdAt: 'Wed, 22 Dec 2021 09:13:27 UTC',
243
+ createdBy: '',
244
+ id: 'someId',
245
+ version: {
246
+ tag: 'v1',
247
+ template: '%recipient.title%',
248
+ engine: 'handlebars',
249
+ mjml: '',
250
+ createdAt: 'Wed, 22 Dec 2021 09:13:27 UTC',
251
+ comment: 'dummy comment',
252
+ active: false,
253
+ id: 'someId2'
254
+ }
255
+ }
256
+ });
257
+
258
+ const template = await client.getVersion('testDomain', 'testTemplateName', 'v1');
259
+ template.should.be.an('object');
260
+ template.should.eql({
261
+ name: 'test_template',
262
+ description: 'test_template description',
263
+ createdAt: new Date('2021-12-22T09:13:27.000Z'),
264
+ createdBy: '',
265
+ id: 'someId',
266
+ version: {
267
+ tag: 'v1',
268
+ template: '%recipient.title%',
269
+ engine: 'handlebars',
270
+ mjml: '',
271
+ createdAt: new Date('2021-12-22T09:13:27.000Z'),
272
+ comment: 'dummy comment',
273
+ active: false,
274
+ id: 'someId2',
275
+ }
276
+ });
277
+ });
278
+ });
279
+
280
+ describe('updateVersion', function () {
281
+ it('updates version of domain template', async () => {
282
+ api.put('/v3/testDomain/templates/testTemplateName/versions/v2').reply(200, {
283
+ message: 'version has been updated',
284
+ template: { name: 'test_template', version: { tag: 'v2' } }
285
+ });
286
+
287
+ const updatedTemplate = await client.updateVersion('testDomain', 'testTemplateName', 'v2', {
288
+ template: '%recipient.title%',
289
+ comment: 'updated comment 2',
290
+ active: 'yes'
291
+ } as DomainTemplateUpdateVersionData);
292
+ updatedTemplate.should.be.an('object');
293
+ updatedTemplate.should.eql({
294
+ status: 200,
295
+ message: 'version has been updated',
296
+ templateName: 'test_template',
297
+ templateVersion: { tag: 'v2' }
298
+ });
299
+ });
300
+ });
301
+
302
+ describe('destroyVersion', function () {
303
+ it('deletes version of domain template', async () => {
304
+ api.delete('/v3/testDomain/templates/testTemplateName/versions/v1').reply(200, {
305
+ message: 'version has been deleted',
306
+ template: { name: 'test_template', version: { tag: 'v1' } }
307
+ });
308
+
309
+ const deletedTemplate = await client.destroyVersion('testDomain', 'testTemplateName', 'v1');
310
+ deletedTemplate.should.eql({
311
+ status: 200,
312
+ message: 'version has been deleted',
313
+ templateName: 'test_template',
314
+ templateVersion: { tag: 'v1' }
315
+ });
316
+ });
317
+ });
318
+
319
+ describe('listVersions', function () {
320
+ it('gets list of version for domain template', async () => {
321
+ api.get('/v3/testDomain/templates/testTemplateName/versions').reply(200, {
322
+ paging: {
323
+ first: 'first page url',
324
+ last: 'last page url',
325
+ next: 'next page url',
326
+ previous: 'previous page url',
327
+ },
328
+ template: {
329
+ name: 'test_template',
330
+ description: 'test_template description',
331
+ createdAt: 'Wed, 22 Dec 2021 09:13:27 UTC',
332
+ createdBy: '',
333
+ id: 'someId',
334
+ versions: [{
335
+ tag: 'v2',
336
+ engine: 'handlebars',
337
+ mjml: '',
338
+ createdAt: 'Wed, 22 Dec 2021 09:51:07 UTC',
339
+ comment: 'updated comment 2',
340
+ active: true,
341
+ id: 'someId1'
342
+ },
343
+ {
344
+ tag: 'v5',
345
+ engine: 'handlebars',
346
+ mjml: '',
347
+ createdAt: 'Mon, 20 Dec 2021 16:36:55 UTC',
348
+ comment: 'updated comment',
349
+ active: false,
350
+ id: 'someId2'
351
+ }]
352
+ }
353
+ });
354
+
355
+ const templatesList = await client.listVersions('testDomain', 'testTemplateName');
356
+ templatesList.should.be.an('object');
357
+ templatesList.should.eql({
358
+ pages: {
359
+ first: 'first page url',
360
+ last: 'last page url',
361
+ next: 'next page url',
362
+ previous: 'previous page url',
363
+ },
364
+ template: {
365
+ name: 'test_template',
366
+ description: 'test_template description',
367
+ createdAt: new Date('Wed, 22 Dec 2021 09:13:27 UTC'),
368
+ createdBy: '',
369
+ id: 'someId',
370
+ versions: [{
371
+ tag: 'v2',
372
+ engine: 'handlebars',
373
+ mjml: '',
374
+ createdAt: new Date('Wed, 22 Dec 2021 09:51:07 UTC'),
375
+ comment: 'updated comment 2',
376
+ active: true,
377
+ id: 'someId1'
378
+ },
379
+ {
380
+ tag: 'v5',
381
+ engine: 'handlebars',
382
+ mjml: '',
383
+ createdAt: new Date('Mon, 20 Dec 2021 16:36:55 UTC'),
384
+ comment: 'updated comment',
385
+ active: false,
386
+ id: 'someId2'
387
+ }]
388
+ }
389
+ });
390
+ });
391
+ });
392
+ });
@@ -4,7 +4,7 @@ import nock from 'nock';
4
4
  import Request from '../lib/request';
5
5
  import StatsClient from '../lib/stats';
6
6
  import RequestOptions from '../lib/interfaces/RequestOptions';
7
- import StatsOptions from '../lib/interfaces/StatsOptions';
7
+ import { StatsOptions } from '../lib/interfaces/StatsOptions';
8
8
  import { InputFormData } from '../lib/interfaces/IFormData';
9
9
 
10
10
  describe('StatsClient', function () {