mailgun.js 3.7.1 → 4.0.1

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.
Files changed (51) hide show
  1. package/CHANGELOG.md +31 -0
  2. package/README.md +335 -1
  3. package/dist/lib/client.d.ts +0 -4
  4. package/dist/lib/domains.d.ts +19 -13
  5. package/dist/lib/domainsCredentials.d.ts +14 -0
  6. package/dist/lib/events.d.ts +3 -3
  7. package/dist/lib/interfaces/DomainCredentials.d.ts +52 -0
  8. package/dist/lib/interfaces/DomainTracking.d.ts +4 -4
  9. package/dist/lib/interfaces/Domains.d.ts +53 -3
  10. package/dist/lib/interfaces/Events.d.ts +66 -2
  11. package/dist/lib/interfaces/MultipleValidation.d.ts +55 -0
  12. package/dist/lib/interfaces/Validate.d.ts +12 -0
  13. package/dist/lib/interfaces/Webhooks.d.ts +5 -1
  14. package/dist/lib/interfaces/lists.d.ts +1 -1
  15. package/dist/lib/multipleValidation.d.ts +10 -0
  16. package/dist/lib/parse.d.ts +0 -6
  17. package/dist/lib/request.d.ts +4 -1
  18. package/dist/lib/validate.d.ts +5 -2
  19. package/dist/lib/webhooks.d.ts +3 -3
  20. package/dist/mailgun.node.js +2 -2
  21. package/dist/mailgun.node.js.LICENSE.txt +1 -1
  22. package/dist/mailgun.web.js +2 -2
  23. package/dist/mailgun.web.js.LICENSE.txt +1 -1
  24. package/lib/client.ts +6 -12
  25. package/lib/domains.ts +82 -26
  26. package/lib/domainsCredentials.ts +88 -0
  27. package/lib/events.ts +6 -6
  28. package/lib/interfaces/DomainCredentials.ts +68 -0
  29. package/lib/interfaces/DomainTracking.ts +4 -4
  30. package/lib/interfaces/Domains.ts +65 -4
  31. package/lib/interfaces/Events.ts +66 -2
  32. package/lib/interfaces/MultipleValidation.ts +62 -0
  33. package/lib/interfaces/Validate.ts +15 -0
  34. package/lib/interfaces/Webhooks.ts +6 -1
  35. package/lib/interfaces/lists.ts +1 -1
  36. package/lib/multipleValidation.ts +37 -0
  37. package/lib/request.ts +75 -34
  38. package/lib/validate.ts +10 -4
  39. package/lib/webhooks.ts +19 -10
  40. package/package.json +6 -6
  41. package/test/client.test.ts +21 -5
  42. package/test/data/emailsValidation1.csv +3 -0
  43. package/test/domains.test.ts +118 -15
  44. package/test/domainsCredentials.test.ts +97 -0
  45. package/test/events.test.ts +15 -24
  46. package/test/multipleValidation.test.ts +159 -0
  47. package/test/validate.test.ts +7 -4
  48. package/test/webhooks.test.ts +6 -6
  49. package/webpack/webpack.dev.config.js +10 -0
  50. package/lib/parse.ts +0 -27
  51. package/test/parse.test.ts +0 -75
@@ -0,0 +1,62 @@
1
+ /* eslint-disable camelcase */
2
+ export interface MultipleValidationJob {
3
+ created_at: number;
4
+ download_url: {
5
+ csv: string;
6
+ json: string;
7
+ };
8
+ id: string;
9
+ quantity: number;
10
+ records_processed: number;
11
+ status: string;
12
+ summary: {
13
+ result: {
14
+ catch_all: number;
15
+ deliverable: number;
16
+ do_not_send: number;
17
+ undeliverable: number;
18
+ unknown: number;
19
+ };
20
+ risk: {
21
+ high: number;
22
+ low: number;
23
+ medium: number;
24
+ unknown: number;
25
+ }
26
+ }
27
+ }
28
+
29
+ export interface CreatedMultipleValidationJob {
30
+ id: string;
31
+ message: string;
32
+ }
33
+
34
+ export interface PagesList {
35
+ prev: string;
36
+ first: string;
37
+ last: string;
38
+ next: string;
39
+ }
40
+
41
+ export interface MultipleValidationJobsListResult {
42
+ jobs: MultipleValidationJob[];
43
+ paging: PagesList;
44
+ total: number;
45
+ }
46
+
47
+ export interface MultipleValidationJobsListResponse {
48
+ status: 200;
49
+ body: MultipleValidationJobsListResult
50
+ }
51
+
52
+ export interface CanceledMultipleValidationJob {
53
+ body: string;
54
+ status: number;
55
+ }
56
+
57
+ export interface IMultipleValidationClient {
58
+ list() : Promise<MultipleValidationJobsListResult>
59
+ get(listId: string) : Promise<MultipleValidationJob>
60
+ create(listId: string, file: any) : Promise<CreatedMultipleValidationJob>
61
+ destroy(listId: string) : Promise<CanceledMultipleValidationJob>
62
+ }
@@ -0,0 +1,15 @@
1
+ /* eslint-disable camelcase */
2
+
3
+ export interface ValidationResult {
4
+ address: string;
5
+ is_disposable_address: boolean;
6
+ is_role_address: boolean;
7
+ reason: string[];
8
+ result: string;
9
+ risk: string;
10
+ }
11
+
12
+ export interface ValidationResponse {
13
+ status: number;
14
+ body: ValidationResult;
15
+ }
@@ -9,7 +9,7 @@ export interface WebhookResponseBody {
9
9
  }
10
10
 
11
11
  export interface WebhookResponse {
12
- status: string;
12
+ status: number;
13
13
  body: WebhookResponseBody;
14
14
  }
15
15
 
@@ -23,3 +23,8 @@ export interface WebhooksQuery {
23
23
  limit?: number;
24
24
  skip?: number;
25
25
  }
26
+
27
+ export interface ValidationResponse {
28
+ code: number;
29
+ message: string;
30
+ }
@@ -9,7 +9,7 @@ export interface CreateUpdateList {
9
9
  address: string;
10
10
  name?: string;
11
11
  description?: string;
12
- access_level?: 'read-only' | 'members'| 'everyone';
12
+ access_level?: 'readonly' | 'members'| 'everyone';
13
13
  reply_preference?: 'list' | 'sender';
14
14
  }
15
15
 
@@ -0,0 +1,37 @@
1
+ import {
2
+ CanceledMultipleValidationJob,
3
+ CreatedMultipleValidationJob,
4
+ IMultipleValidationClient,
5
+ MultipleValidationJob,
6
+ MultipleValidationJobsListResult
7
+ }
8
+ from './interfaces/MultipleValidation';
9
+ import Request from './request';
10
+
11
+ export default class MultipleValidationClient implements IMultipleValidationClient {
12
+ request: Request;
13
+
14
+ constructor(request: Request) {
15
+ this.request = request;
16
+ }
17
+
18
+ list(): Promise<MultipleValidationJobsListResult> {
19
+ return this.request.get('/v4/address/validate/bulk')
20
+ .then((response) => response.body as MultipleValidationJobsListResult);
21
+ }
22
+
23
+ get(listId: string): Promise<MultipleValidationJob> {
24
+ return this.request.get(`/v4/address/validate/bulk/${listId}`)
25
+ .then((response) => response.body);
26
+ }
27
+
28
+ create(listId: string, file: any): Promise<CreatedMultipleValidationJob> {
29
+ return this.request.postWithFD(`/v4/address/validate/bulk/${listId}`, file)
30
+ .then((response) => response.body);
31
+ }
32
+
33
+ destroy(listId: string): Promise<CanceledMultipleValidationJob> {
34
+ return this.request.delete(`/v4/address/validate/bulk/${listId}`)
35
+ .then((response) => response);
36
+ }
37
+ }
package/lib/request.ts CHANGED
@@ -50,7 +50,7 @@ class Request {
50
50
  private url: string;
51
51
  private timeout: number;
52
52
  private headers: any;
53
- private formData: InputFormData;
53
+ private FormDataConstructor: InputFormData;
54
54
 
55
55
  constructor(options: RequestOptions, formData: InputFormData) {
56
56
  this.username = options.username;
@@ -58,7 +58,7 @@ class Request {
58
58
  this.url = options.url as string;
59
59
  this.timeout = options.timeout;
60
60
  this.headers = options.headers || {};
61
- this.formData = formData;
61
+ this.FormDataConstructor = formData;
62
62
  }
63
63
 
64
64
  async request(method: string, url: string, inputOptions?: any): Promise<APIResponse> {
@@ -107,10 +107,12 @@ class Request {
107
107
  } as APIErrorOptions);
108
108
  }
109
109
 
110
- return {
110
+ const res = {
111
111
  body: await response?.json(),
112
112
  status: response?.status
113
113
  };
114
+
115
+ return res;
114
116
  }
115
117
 
116
118
  query(method: string, url: string, query: any, options?: any) : Promise<APIResponse> {
@@ -142,6 +144,9 @@ class Request {
142
144
  }
143
145
 
144
146
  postWithFD(url: string, data: any): Promise<APIResponse> {
147
+ if (!data) {
148
+ throw new Error('Please provide data object');
149
+ }
145
150
  const params: any = {
146
151
  headers: { 'Content-Type': null }
147
152
  };
@@ -150,6 +155,9 @@ class Request {
150
155
  }
151
156
 
152
157
  putWithFD(url: string, data: any): Promise<APIResponse> {
158
+ if (!data) {
159
+ throw new Error('Please provide data object');
160
+ }
153
161
  const params: any = {
154
162
  headers: { 'Content-Type': null }
155
163
  };
@@ -158,49 +166,82 @@ class Request {
158
166
  }
159
167
 
160
168
  createFormData(data: any): NodeFormData | FormData {
169
+ const formData: NodeFormData | FormData = Object.keys(data)
170
+ .filter(function (key) { return data[key]; })
171
+ .reduce((formDataAcc: NodeFormData | FormData, key) => {
172
+ const fileKeys = ['attachment', 'inline', 'file'];
173
+ if (fileKeys.includes(key)) {
174
+ this.addFilesToFD(key, data[key], formDataAcc);
175
+ return formDataAcc;
176
+ }
177
+
178
+ if (key === 'message') { // mime message
179
+ this.addMimeDataToFD(key, data[key], formDataAcc);
180
+ return formDataAcc;
181
+ }
182
+
183
+ this.addCommonPropertyToFD(key, data[key], formDataAcc);
184
+ return formDataAcc;
185
+ }, new this.FormDataConstructor());
186
+ return formData;
187
+ }
188
+
189
+ private addMimeDataToFD(
190
+ key: string,
191
+ data: Buffer | Blob,
192
+ formDataInstance: NodeFormData | FormData
193
+ ): void {
194
+ if (isNodeFormData(formDataInstance)) {
195
+ if (Buffer.isBuffer(data)) {
196
+ formDataInstance.append(key, data, { filename: 'MimeMessage' });
197
+ }
198
+ } else {
199
+ formDataInstance.append(key, data as Blob, 'MimeMessage');
200
+ }
201
+ }
202
+
203
+ private addFilesToFD(
204
+ propertyName: string,
205
+ value: any,
206
+ formDataInstance: NodeFormData | FormData
207
+ ): void {
161
208
  const appendFileToFD = (
162
209
  key: string,
163
210
  obj: any,
164
- formDataInstance: NodeFormData | FormData
211
+ formData: NodeFormData | FormData
165
212
  ): void => {
166
213
  const isStreamData = isStream(obj);
167
214
  const objData = isStreamData ? obj : obj.data;
215
+ // getAttachmentOptions should be called with obj parameter to prevent loosing filename
168
216
  const options = getAttachmentOptions(obj);
169
- if (isNodeFormData(formDataInstance)) {
170
- formDataInstance.append(key, objData, options);
217
+ if (isNodeFormData(formData)) {
218
+ formData.append(key, objData, options);
171
219
  return;
172
220
  }
173
- formDataInstance.append(key, objData, options.filename);
221
+ formData.append(key, objData, options.filename);
174
222
  };
175
223
 
176
- const formData: NodeFormData | FormData = Object.keys(data)
177
- .filter(function (key) { return data[key]; })
178
- .reduce((formDataAcc: NodeFormData | FormData, key) => {
179
- if (key === 'attachment' || key === 'inline') {
180
- const obj = data[key];
181
-
182
- if (Array.isArray(obj)) {
183
- obj.forEach(function (item) {
184
- appendFileToFD(key, item, formDataAcc);
185
- });
186
- } else {
187
- appendFileToFD(key, obj, formDataAcc);
188
- }
189
-
190
- return formDataAcc;
191
- }
224
+ if (Array.isArray(value)) {
225
+ value.forEach(function (item) {
226
+ appendFileToFD(propertyName, item, formDataInstance);
227
+ });
228
+ } else {
229
+ appendFileToFD(propertyName, value, formDataInstance);
230
+ }
231
+ }
192
232
 
193
- if (Array.isArray(data[key])) {
194
- data[key].forEach(function (item: any) {
195
- formDataAcc.append(key, item);
196
- });
197
- } else if (data[key] != null) {
198
- formDataAcc.append(key, data[key]);
199
- }
200
- return formDataAcc;
201
- // eslint-disable-next-line new-cap
202
- }, new this.formData());
203
- return formData;
233
+ private addCommonPropertyToFD(
234
+ key: string,
235
+ value: any,
236
+ formDataAcc: NodeFormData | FormData
237
+ ): void {
238
+ if (Array.isArray(value)) {
239
+ value.forEach(function (item: any) {
240
+ formDataAcc.append(key, item);
241
+ });
242
+ } else if (value != null) {
243
+ formDataAcc.append(key, value);
244
+ }
204
245
  }
205
246
 
206
247
  put(url: string, data: any, options?: any): Promise<APIResponse> {
package/lib/validate.ts CHANGED
@@ -1,14 +1,20 @@
1
+ import APIResponse from './interfaces/ApiResponse';
2
+ import { IMultipleValidationClient } from './interfaces/MultipleValidation';
3
+ import { ValidationResult, ValidationResponse } from './interfaces/Validate';
1
4
  import Request from './request';
2
5
 
3
6
  export default class ValidateClient {
7
+ public multipleValidation;
4
8
  request: Request;
5
9
 
6
- constructor(request: Request) {
10
+ constructor(request: Request, multipleValidationClient: IMultipleValidationClient) {
7
11
  this.request = request;
12
+ this.multipleValidation = multipleValidationClient;
8
13
  }
9
14
 
10
- get(address: string) {
11
- return this.request.get('/v3/address/validate', { address })
12
- .then((response) => response.body);
15
+ get(address: string): Promise<ValidationResult> {
16
+ return this.request.get('/v4/address/validate', { address })
17
+ .then((response : APIResponse) => response)
18
+ .then((res : ValidationResponse) => res.body as ValidationResult);
13
19
  }
14
20
  }
package/lib/webhooks.ts CHANGED
@@ -1,5 +1,11 @@
1
1
  import urljoin from 'url-join';
2
- import { WebhookList, WebhookResponse, WebhooksQuery } from './interfaces/Webhooks';
2
+
3
+ import {
4
+ ValidationResponse,
5
+ WebhookList,
6
+ WebhookResponse,
7
+ WebhooksQuery
8
+ } from './interfaces/Webhooks';
3
9
  import Request from './request';
4
10
 
5
11
  class Webhook {
@@ -13,7 +19,7 @@ class Webhook {
13
19
  }
14
20
 
15
21
  export default class WebhookClient {
16
- request: any;
22
+ request: Request;
17
23
 
18
24
  constructor(request: Request) {
19
25
  this.request = request;
@@ -36,36 +42,39 @@ export default class WebhookClient {
36
42
 
37
43
  _parseWebhookTest(response: { body: { code: number, message: string } })
38
44
  : {code: number, message:string} {
39
- return { code: response.body.code, message: response.body.message };
45
+ return { code: response.body.code, message: response.body.message } as ValidationResponse;
40
46
  }
41
47
 
42
48
  list(domain: string, query: WebhooksQuery): Promise<WebhookList> {
43
- return this.request.get(urljoin('/v2/domains', domain, 'webhooks'), query)
49
+ return this.request.get(urljoin('/v3/domains', domain, 'webhooks'), query)
44
50
  .then(this._parseWebhookList);
45
51
  }
46
52
 
47
53
  get(domain: string, id: string): Promise<Webhook> {
48
- return this.request.get(urljoin('/v2/domains', domain, 'webhooks', id))
54
+ return this.request.get(urljoin('/v3/domains', domain, 'webhooks', id))
49
55
  .then(this._parseWebhookWithID(id));
50
56
  }
51
57
 
52
- create(domain: string, id: string, url: string, test = false): Promise<Webhook> {
58
+ create(domain: string,
59
+ id: string,
60
+ url: string,
61
+ test = false): Promise<Webhook | ValidationResponse> {
53
62
  if (test) {
54
- return this.request.putWithFD(urljoin('/v2/domains', domain, 'webhooks', id, 'test'), { url })
63
+ return this.request.putWithFD(urljoin('/v3/domains', domain, 'webhooks', id, 'test'), { url })
55
64
  .then(this._parseWebhookTest);
56
65
  }
57
66
 
58
- return this.request.postWithFD(urljoin('/v2/domains', domain, 'webhooks'), { id, url })
67
+ return this.request.postWithFD(urljoin('/v3/domains', domain, 'webhooks'), { id, url })
59
68
  .then(this._parseWebhookWithID(id));
60
69
  }
61
70
 
62
71
  update(domain: string, id: string, url: string): Promise<Webhook> {
63
- return this.request.putWithFD(urljoin('/v2/domains', domain, 'webhooks', id), { url })
72
+ return this.request.putWithFD(urljoin('/v3/domains', domain, 'webhooks', id), { url })
64
73
  .then(this._parseWebhookWithID(id));
65
74
  }
66
75
 
67
76
  destroy(domain: string, id: string) : Promise<Webhook> {
68
- return this.request.delete(urljoin('/v2/domains', domain, 'webhooks', id))
77
+ return this.request.delete(urljoin('/v3/domains', domain, 'webhooks', id))
69
78
  .then(this._parseWebhookWithID(id));
70
79
  }
71
80
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mailgun.js",
3
- "version": "3.7.1",
3
+ "version": "4.0.1",
4
4
  "main": "dist/mailgun.node.js",
5
5
  "browser": "dist/mailgun.web.js",
6
6
  "types": "dist/index.d.ts",
@@ -12,12 +12,12 @@
12
12
  ],
13
13
  "repository": {
14
14
  "type": "git",
15
- "url": "git://github.com/mailgun/mailgun-js.git"
15
+ "url": "git://github.com/mailgun/mailgun.js.git"
16
16
  },
17
17
  "bugs": {
18
- "url": "https://github.com/mailgun/mailgun-js/issues"
18
+ "url": "https://github.com/mailgun/mailgun.js/issues"
19
19
  },
20
- "homepage": "https://github.com/mailgun/mailgun-js#readme",
20
+ "homepage": "https://github.com/mailgun/mailgun.js#readme",
21
21
  "scripts": {
22
22
  "build": "webpack --config ./webpack/webpack.dev.config.js --progress --color",
23
23
  "build:release": "webpack --config ./webpack/webpack.release.config.js --progress --color",
@@ -102,8 +102,8 @@
102
102
  "section": "Other changes"
103
103
  }
104
104
  ],
105
- "commitUrlFormat": "https://github.com/mailgun/mailgun-js/commits/{{hash}}",
106
- "compareUrlFormat": "https://github.com/mailgun/mailgun-js/compare/{{previousTag}}...{{currentTag}}",
105
+ "commitUrlFormat": "https://github.com/mailgun/mailgun.js/commits/{{hash}}",
106
+ "compareUrlFormat": "https://github.com/mailgun/mailgun.js/compare/{{previousTag}}...{{currentTag}}",
107
107
  "scripts": {
108
108
  "prerelease": "npm test && webpack --config ./webpack/webpack.release.config.js --progress --color && git add -A dist",
109
109
  "posttag": "git push && git push --tags && rm -rf build"
@@ -11,8 +11,12 @@ import SuppressionsClient from '../lib/suppressions';
11
11
  import MessagesClient from '../lib/messages';
12
12
  import RoutesClient from '../lib/routes';
13
13
  import ValidateClient from '../lib/validate';
14
- import ParseClient from '../lib/parse';
14
+
15
15
  import { InputFormData } from '../lib/interfaces/IFormData';
16
+ import StatsClient from '../lib/stats';
17
+ import ListsClient from '../lib/lists';
18
+ import IpPoolsClient from '../lib/ip-pools';
19
+ import IpsClient from '../lib/ips';
16
20
 
17
21
  describe('Client', function () {
18
22
  let client: any;
@@ -62,6 +66,10 @@ describe('Client', function () {
62
66
  client.suppressions.should.be.instanceOf(SuppressionsClient);
63
67
  });
64
68
 
69
+ it('creates stats client', function () {
70
+ client.stats.should.be.instanceOf(StatsClient);
71
+ });
72
+
65
73
  it('creates messages client', function () {
66
74
  client.messages.should.be.instanceOf(MessagesClient);
67
75
  });
@@ -70,11 +78,19 @@ describe('Client', function () {
70
78
  client.routes.should.be.instanceOf(RoutesClient);
71
79
  });
72
80
 
73
- it('creates address validate client', function () {
74
- client.validate.should.be.instanceOf(ValidateClient);
81
+ it('creates ips client', function () {
82
+ client.ips.should.be.instanceOf(IpsClient);
75
83
  });
76
84
 
77
- it('creates address parse client', function () {
78
- client.parse.should.be.instanceOf(ParseClient);
85
+ it('creates ip_pools client', function () {
86
+ client.ip_pools.should.be.instanceOf(IpPoolsClient);
87
+ });
88
+
89
+ it('creates lists client', function () {
90
+ client.lists.should.be.instanceOf(ListsClient);
91
+ });
92
+
93
+ it('creates address validate client', function () {
94
+ client.validate.should.be.instanceOf(ValidateClient);
79
95
  });
80
96
  });
@@ -0,0 +1,3 @@
1
+ email
2
+ 1testEmailAdressForCheck@test.com
3
+ 2testEmailAdressForCheck@test.com