@zerobounce/zero-bounce-sdk 1.0.0 → 1.1.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/src/utils.js CHANGED
@@ -35,8 +35,12 @@ export async function createRequest({
35
35
  return JSON.parse(finalResult);
36
36
  }
37
37
  }
38
- const finalResult = await response.json();
39
- return finalResult;
38
+ if (response.status === 403) {
39
+ throw new Error('[Error]: api_key is invalid');
40
+ } else {
41
+ const finalResult = await response.json();
42
+ return finalResult;
43
+ }
40
44
  } catch (error) {
41
45
  throw new Error(error);
42
46
  }
@@ -338,4 +338,35 @@ export class ZeroBounceSDK {
338
338
  deleteScoringFile(fileId) {
339
339
  return this._deleteFileUtil(fileId, "/scoring/deletefile", true);
340
340
  }
341
+
342
+ // EMAIL FINDER
343
+
344
+ guessFormat ({
345
+ domain,
346
+ first_name = null,
347
+ middle_name = null,
348
+ last_name = null
349
+ }) {
350
+ if (!this._initialized) {
351
+ notInitialized();
352
+ return;
353
+ } else if (!domain) {
354
+ parameterIsMissing("domain");
355
+ return;
356
+ }
357
+
358
+ const params = {
359
+ api_key: this._api_key,
360
+ domain: domain,
361
+ first_name: first_name,
362
+ middle_name: middle_name,
363
+ last_name: last_name,
364
+ };
365
+
366
+ return createRequest({
367
+ requestType: "GET",
368
+ params,
369
+ path: "/guessformat",
370
+ });
371
+ }
341
372
  }
@@ -1,5 +1,6 @@
1
1
  import { ZeroBounceSDK } from "../src/zero-bounce.js";
2
2
  import * as utils from "../src/utils.js";
3
+ import 'whatwg-fetch'
3
4
 
4
5
  describe("ZeroBounceSDK", () => {
5
6
  let zeroBounceSDK;
@@ -31,8 +32,11 @@ describe("ZeroBounceSDK", () => {
31
32
 
32
33
  it("should return error response with invalid API key", async () => {
33
34
  zeroBounceSDK.init("invalid-api-key");
34
- const response = await zeroBounceSDK.getCredits();
35
- expect(response).toEqual({ "Credits": "-1" });
35
+ try {
36
+ await zeroBounceSDK.getCredits();
37
+ } catch (error) {
38
+ expect(error.message).toEqual('TypeError: Network request failed');
39
+ }
36
40
  });
37
41
 
38
42
  it("should return the correct number of credits", async () => {
@@ -62,7 +66,7 @@ describe("ZeroBounceSDK", () => {
62
66
  });
63
67
 
64
68
  it("should throw an error if stardDate is missing", async () => {
65
- zeroBounceSDK.init("invalid-api-key");
69
+ zeroBounceSDK.init("valid-api-key");
66
70
  await zeroBounceSDK.getApiUsage(null, endDate);
67
71
  expect(console.error).toHaveBeenCalledWith(
68
72
  expect.stringContaining(missingParamMessage)
@@ -70,7 +74,7 @@ describe("ZeroBounceSDK", () => {
70
74
  });
71
75
 
72
76
  it("should throw an error if endDate is missing", async () => {
73
- zeroBounceSDK.init("invalid-api-key");
77
+ zeroBounceSDK.init("valid-api-key");
74
78
  await zeroBounceSDK.getApiUsage(startDate, null);
75
79
  expect(console.error).toHaveBeenCalledWith(
76
80
  expect.stringContaining(missingParamMessage)
@@ -79,8 +83,11 @@ describe("ZeroBounceSDK", () => {
79
83
 
80
84
  it("should return error response with invalid API key", async () => {
81
85
  zeroBounceSDK.init("invalid-api-key");
82
- const response = await zeroBounceSDK.getApiUsage(startDate, endDate);
83
- expect(response).toEqual({ "error": "Invalid API key" });
86
+ try {
87
+ await zeroBounceSDK.getApiUsage(startDate, endDate);
88
+ } catch (error) {
89
+ expect(error.message).toEqual('TypeError: Network request failed');
90
+ }
84
91
  });
85
92
 
86
93
  it("should return API usage data", async () => {
@@ -143,7 +150,7 @@ describe("ZeroBounceSDK", () => {
143
150
  });
144
151
 
145
152
  it("should throw an error if email is missing", async () => {
146
- zeroBounceSDK.init("invalid-api-key");
153
+ zeroBounceSDK.init("valid-api-key");
147
154
  await zeroBounceSDK.validateEmail(null);
148
155
  expect(console.error).toHaveBeenCalledWith(
149
156
  expect.stringContaining(missingParamMessage)
@@ -152,10 +159,11 @@ describe("ZeroBounceSDK", () => {
152
159
 
153
160
  it("should return error response with invalid API key", async () => {
154
161
  zeroBounceSDK.init("invalid-api-key");
155
- const response = await zeroBounceSDK.validateEmail(email, ip_address);
156
- expect(response).toEqual({
157
- "error": "Invalid API key or your account ran out of credits"
158
- });
162
+ try {
163
+ await zeroBounceSDK.validateEmail(email, ip_address);
164
+ } catch (error) {
165
+ expect(error.message).toEqual('TypeError: Network request failed');
166
+ }
159
167
  });
160
168
 
161
169
  it("should return the validated email data", async () => {
@@ -205,7 +213,7 @@ describe("ZeroBounceSDK", () => {
205
213
  });
206
214
 
207
215
  it("should throw an error if email list is missing", async () => {
208
- zeroBounceSDK.init("invalid-api-key");
216
+ zeroBounceSDK.init("valid-api-key");
209
217
  await zeroBounceSDK.validateBatch(null);
210
218
  expect(console.error).toHaveBeenCalledWith(
211
219
  expect.stringContaining(missingParamMessage)
@@ -214,16 +222,11 @@ describe("ZeroBounceSDK", () => {
214
222
 
215
223
  it("should return error response with invalid API key", async () => {
216
224
  zeroBounceSDK.init("invalid-api-key");
217
- const response = await zeroBounceSDK.validateBatch(emailBatch);
218
- expect(response).toEqual({
219
- "email_batch": [],
220
- "errors": [
221
- {
222
- "email_address": "all",
223
- "error": "Invalid API Key or your account ran out of credits"
224
- }
225
- ]
226
- });
225
+ try {
226
+ await zeroBounceSDK.validateBatch(emailBatch);
227
+ } catch (error) {
228
+ expect(error.message).toEqual('TypeError: Network request failed');
229
+ }
227
230
  });
228
231
 
229
232
  it("should return the validated emails data", async () => {
@@ -296,7 +299,7 @@ describe("ZeroBounceSDK", () => {
296
299
  });
297
300
 
298
301
  it("should throw an error if email is missing", async () => {
299
- zeroBounceSDK.init("invalid-api-key");
302
+ zeroBounceSDK.init("valid-api-key");
300
303
  await zeroBounceSDK.getEmailActivity(null);
301
304
  expect(console.error).toHaveBeenCalledWith(
302
305
  expect.stringContaining(missingParamMessage)
@@ -305,10 +308,11 @@ describe("ZeroBounceSDK", () => {
305
308
 
306
309
  it("should return error response with invalid API key", async () => {
307
310
  zeroBounceSDK.init("invalid-api-key");
308
- const response = await zeroBounceSDK.getEmailActivity(email);
309
- expect(response).toEqual({
310
- "error": "Invalid API key or your account ran out of credits"
311
- });
311
+ try {
312
+ await zeroBounceSDK.getEmailActivity(email);
313
+ } catch (error) {
314
+ expect(error.message).toEqual('TypeError: Network request failed');
315
+ }
312
316
  });
313
317
 
314
318
  it("should return the email activity data", async () => {
@@ -348,7 +352,7 @@ describe("ZeroBounceSDK", () => {
348
352
  email_address_column: 1,
349
353
  };
350
354
 
351
- zeroBounceSDK.init("invalid-api-key");
355
+ zeroBounceSDK.init("valid-api-key");
352
356
  await zeroBounceSDK.sendFile(payload);
353
357
  expect(console.error).toHaveBeenCalledWith(
354
358
  expect.stringContaining(missingParamMessage)
@@ -360,7 +364,7 @@ describe("ZeroBounceSDK", () => {
360
364
  file: file,
361
365
  };
362
366
 
363
- zeroBounceSDK.init("invalid-api-key");
367
+ zeroBounceSDK.init("valid-api-key");
364
368
  await zeroBounceSDK.sendFile(payload);
365
369
  expect(console.error).toHaveBeenCalledWith(
366
370
  expect.stringContaining(missingParamMessage)
@@ -417,7 +421,7 @@ describe("ZeroBounceSDK", () => {
417
421
  email_address_column: 1,
418
422
  };
419
423
 
420
- zeroBounceSDK.init("invalid-api-key");
424
+ zeroBounceSDK.init("valid-api-key");
421
425
  await zeroBounceSDK.sendScoringFile(payload);
422
426
  expect(console.error).toHaveBeenCalledWith(
423
427
  expect.stringContaining(missingParamMessage)
@@ -429,7 +433,7 @@ describe("ZeroBounceSDK", () => {
429
433
  file: file,
430
434
  };
431
435
 
432
- zeroBounceSDK.init("invalid-api-key");
436
+ zeroBounceSDK.init("valid-api-key");
433
437
  await zeroBounceSDK.sendScoringFile(payload);
434
438
  expect(console.error).toHaveBeenCalledWith(
435
439
  expect.stringContaining(missingParamMessage)
@@ -476,7 +480,7 @@ describe("ZeroBounceSDK", () => {
476
480
  });
477
481
 
478
482
  it("should throw an error if file id is missing", async () => {
479
- zeroBounceSDK.init("invalid-api-key");
483
+ zeroBounceSDK.init("valid-api-key");
480
484
  await zeroBounceSDK.getFileStatus(null);
481
485
  expect(console.error).toHaveBeenCalledWith(
482
486
  expect.stringContaining(missingParamMessage)
@@ -485,13 +489,11 @@ describe("ZeroBounceSDK", () => {
485
489
 
486
490
  it("should return error response with invalid API key", async () => {
487
491
  zeroBounceSDK.init("invalid-api-key");
488
- const response = await zeroBounceSDK.getFileStatus(fileId);
489
- expect(response).toEqual({
490
- "success": "False",
491
- "message": [
492
- "api_key is invalid"
493
- ]
494
- });
492
+ try {
493
+ await zeroBounceSDK.getFileStatus(fileId);
494
+ } catch (error) {
495
+ expect(error.message).toEqual('TypeError: Network request failed');
496
+ }
495
497
  });
496
498
 
497
499
  it("should return the completion status of a previously sent file", async () => {
@@ -527,7 +529,7 @@ describe("ZeroBounceSDK", () => {
527
529
  });
528
530
 
529
531
  it("should throw an error if file id is missing", async () => {
530
- zeroBounceSDK.init("invalid-api-key");
532
+ zeroBounceSDK.init("valid-api-key");
531
533
  await zeroBounceSDK.getScoringFileStatus(null);
532
534
  expect(console.error).toHaveBeenCalledWith(
533
535
  expect.stringContaining(missingParamMessage)
@@ -536,13 +538,11 @@ describe("ZeroBounceSDK", () => {
536
538
 
537
539
  it("should return error response with invalid API key", async () => {
538
540
  zeroBounceSDK.init("invalid-api-key");
539
- const response = await zeroBounceSDK.getScoringFileStatus(fileId);
540
- expect(response).toEqual({
541
- "success": "False",
542
- "message": [
543
- "api_key is invalid"
544
- ]
545
- });
541
+ try {
542
+ await zeroBounceSDK.getScoringFileStatus(fileId);
543
+ } catch (error) {
544
+ expect(error.message).toEqual('TypeError: Network request failed');
545
+ }
546
546
  });
547
547
 
548
548
  it("should return the completion status of a previously sent scoring file", async () => {
@@ -577,7 +577,7 @@ describe("ZeroBounceSDK", () => {
577
577
  });
578
578
 
579
579
  it("should throw an error if file id is missing", async () => {
580
- zeroBounceSDK.init("invalid-api-key");
580
+ zeroBounceSDK.init("valid-api-key");
581
581
  await zeroBounceSDK.getFile(null);
582
582
  expect(console.error).toHaveBeenCalledWith(
583
583
  expect.stringContaining(missingParamMessage)
@@ -586,13 +586,11 @@ describe("ZeroBounceSDK", () => {
586
586
 
587
587
  it("should return error response with invalid API key", async () => {
588
588
  zeroBounceSDK.init("invalid-api-key");
589
- const response = await zeroBounceSDK.getFile(fileId);
590
- expect(response).toEqual({
591
- "success": "False",
592
- "message": [
593
- "api_key is invalid"
594
- ]
595
- });
589
+ try {
590
+ await zeroBounceSDK.getFile(fileId);
591
+ } catch (error) {
592
+ expect(error.message).toEqual('TypeError: Network request failed');
593
+ }
596
594
  });
597
595
 
598
596
  it("should return the previously sent file", async () => {
@@ -619,7 +617,7 @@ describe("ZeroBounceSDK", () => {
619
617
  });
620
618
 
621
619
  it("should throw an error if file id is missing", async () => {
622
- zeroBounceSDK.init("invalid-api-key");
620
+ zeroBounceSDK.init("valid-api-key");
623
621
  await zeroBounceSDK.getScoringFile(null);
624
622
  expect(console.error).toHaveBeenCalledWith(
625
623
  expect.stringContaining(missingParamMessage)
@@ -628,13 +626,11 @@ describe("ZeroBounceSDK", () => {
628
626
 
629
627
  it("should return error response with invalid API key", async () => {
630
628
  zeroBounceSDK.init("invalid-api-key");
631
- const response = await zeroBounceSDK.getScoringFile(fileId);
632
- expect(response).toEqual({
633
- "success": "False",
634
- "message": [
635
- "api_key is invalid"
636
- ]
637
- });
629
+ try {
630
+ await zeroBounceSDK.getScoringFile(fileId);
631
+ } catch (error) {
632
+ expect(error.message).toEqual('TypeError: Network request failed');
633
+ }
638
634
  });
639
635
 
640
636
  it("should return the previously sent scoring file", async () => {
@@ -661,7 +657,7 @@ describe("ZeroBounceSDK", () => {
661
657
  });
662
658
 
663
659
  it("should throw an error if file id is missing", async () => {
664
- zeroBounceSDK.init("invalid-api-key");
660
+ zeroBounceSDK.init("valid-api-key");
665
661
  await zeroBounceSDK.deleteFile(null);
666
662
  expect(console.error).toHaveBeenCalledWith(
667
663
  expect.stringContaining(missingParamMessage)
@@ -670,13 +666,11 @@ describe("ZeroBounceSDK", () => {
670
666
 
671
667
  it("should return error response with invalid API key", async () => {
672
668
  zeroBounceSDK.init("invalid-api-key");
673
- const response = await zeroBounceSDK.deleteFile(fileId);
674
- expect(response).toEqual({
675
- "success": "False",
676
- "message": [
677
- "api_key is invalid"
678
- ]
679
- });
669
+ try {
670
+ await zeroBounceSDK.deleteFile(fileId);
671
+ } catch (error) {
672
+ expect(error.message).toEqual('TypeError: Network request failed');
673
+ }
680
674
  });
681
675
 
682
676
  it("should delete previously sent file", async () => {
@@ -708,7 +702,7 @@ describe("ZeroBounceSDK", () => {
708
702
  });
709
703
 
710
704
  it("should throw an error if file id is missing", async () => {
711
- zeroBounceSDK.init("invalid-api-key");
705
+ zeroBounceSDK.init("valid-api-key");
712
706
  await zeroBounceSDK.deleteScoringFile(null);
713
707
  expect(console.error).toHaveBeenCalledWith(
714
708
  expect.stringContaining(missingParamMessage)
@@ -717,13 +711,11 @@ describe("ZeroBounceSDK", () => {
717
711
 
718
712
  it("should return error response with invalid API key", async () => {
719
713
  zeroBounceSDK.init("invalid-api-key");
720
- const response = await zeroBounceSDK.deleteScoringFile(fileId);
721
- expect(response).toEqual({
722
- "success": "False",
723
- "message": [
724
- "api_key is invalid"
725
- ]
726
- });
714
+ try {
715
+ await zeroBounceSDK.deleteScoringFile(fileId);
716
+ } catch (error) {
717
+ expect(error.message).toEqual('TypeError: Network request failed');
718
+ }
727
719
  });
728
720
 
729
721
  it("should delete previously sent scoring file", async () => {
@@ -744,4 +736,58 @@ describe("ZeroBounceSDK", () => {
744
736
  expect(response).toEqual(expectedResponse);
745
737
  });
746
738
  });
739
+
740
+ // EMAIL FINDER
741
+ describe("guessFormat", () => {
742
+ const payload = {
743
+ domain: "example.com",
744
+ first_name: "John",
745
+ last_name: "Doe",
746
+ }
747
+
748
+ it("should throw an error if not initialized", async () => {
749
+ await zeroBounceSDK.guessFormat(payload);
750
+ expect(console.error).toHaveBeenCalledWith(initErrorMessage);
751
+ });
752
+
753
+ it("should throw an error if domain is missing", async () => {
754
+ zeroBounceSDK.init("valid-api-key");
755
+ await zeroBounceSDK.guessFormat({domain: null});
756
+ expect(console.error).toHaveBeenCalledWith(
757
+ expect.stringContaining(missingParamMessage)
758
+ );
759
+ });
760
+
761
+ it("should return error response with invalid API key", async () => {
762
+ zeroBounceSDK.init("invalid-api-key");
763
+ try {
764
+ await zeroBounceSDK.guessFormat(payload);
765
+ } catch (error) {
766
+ expect(error.message).toEqual('TypeError: Network request failed');
767
+ }
768
+ });
769
+
770
+ it("should return the validated format data", async () => {
771
+ const expectedResponse = {
772
+ "email": "john.doe@example.com",
773
+ "domain": "",
774
+ "format": "first.last",
775
+ "status": "valid",
776
+ "sub_status": "",
777
+ "confidence": "high",
778
+ "did_you_mean": "",
779
+ "failure_reason": "",
780
+ "other_domain_formats": []
781
+ }
782
+
783
+ jest.spyOn(global, "fetch").mockImplementationOnce(() => Promise.resolve({
784
+ json: () => Promise.resolve(expectedResponse),
785
+ text: () => Promise.resolve(JSON.stringify(expectedResponse)),
786
+ }));
787
+
788
+ zeroBounceSDK.init("valid-api-key");
789
+ const response = await zeroBounceSDK.guessFormat(payload);
790
+ expect(response).toEqual(expectedResponse);
791
+ });
792
+ });
747
793
  });