@zerobounce/zero-bounce-sdk 1.0.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/README.md +306 -0
- package/README_es.md +317 -0
- package/babel.config.js +1 -0
- package/dist/zeroBounceSDK.js +1 -0
- package/jest.config.js +5 -0
- package/package.json +28 -0
- package/src/index.js +2 -0
- package/src/utils.js +72 -0
- package/src/zero-bounce.js +341 -0
- package/tests/zero-bounce.test.js +747 -0
- package/webpack.config.js +20 -0
package/src/utils.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
// Constants
|
|
2
|
+
export const API_BASE_URL = "https://api.zerobounce.net/v2";
|
|
3
|
+
export const API_BULK_BASE_URL = "https://bulkapi.zerobounce.net/v2";
|
|
4
|
+
export const HEADERS = {
|
|
5
|
+
Accept: "*/*",
|
|
6
|
+
"Accept-Encoding": "gzip, deflate, br",
|
|
7
|
+
Connection: "keep-alive",
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export async function createRequest({
|
|
11
|
+
requestType,
|
|
12
|
+
body = null,
|
|
13
|
+
params = null,
|
|
14
|
+
path,
|
|
15
|
+
batch = false,
|
|
16
|
+
returnText = false,
|
|
17
|
+
scoring = false,
|
|
18
|
+
}) {
|
|
19
|
+
const url = `${
|
|
20
|
+
batch ? API_BULK_BASE_URL : API_BASE_URL
|
|
21
|
+
}${path}?${new URLSearchParams(params)}`;
|
|
22
|
+
|
|
23
|
+
try {
|
|
24
|
+
const response = await fetch(url, {
|
|
25
|
+
method: requestType,
|
|
26
|
+
headers: HEADERS,
|
|
27
|
+
body,
|
|
28
|
+
});
|
|
29
|
+
if (returnText) {
|
|
30
|
+
const finalResult = await response.text();
|
|
31
|
+
if (!finalResult.includes('"success":"False"')) {
|
|
32
|
+
const blob = new Blob([finalResult], { type: "application/json" });
|
|
33
|
+
return saveFile(blob, `result${scoring ? "-scoring" : ""}.csv`);
|
|
34
|
+
} else {
|
|
35
|
+
return JSON.parse(finalResult);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
const finalResult = await response.json();
|
|
39
|
+
return finalResult;
|
|
40
|
+
} catch (error) {
|
|
41
|
+
throw new Error(error);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function saveFile(blob, filename) {
|
|
46
|
+
if (window.navigator.msSaveOrOpenBlob) {
|
|
47
|
+
// IE support
|
|
48
|
+
window.navigator.msSaveOrOpenBlob(blob, filename);
|
|
49
|
+
} else {
|
|
50
|
+
const a = document.createElement("a");
|
|
51
|
+
document.body.appendChild(a);
|
|
52
|
+
const url = window.URL.createObjectURL(blob);
|
|
53
|
+
a.href = url;
|
|
54
|
+
a.download = filename;
|
|
55
|
+
a.click();
|
|
56
|
+
setTimeout(() => {
|
|
57
|
+
window.URL.revokeObjectURL(url);
|
|
58
|
+
document.body.removeChild(a);
|
|
59
|
+
}, 0);
|
|
60
|
+
return filename;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function notInitialized() {
|
|
65
|
+
console.error("ZeroBounce: Call init function first with a valid api key.");
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function parameterIsMissing(parameter, aditionalInfo = "") {
|
|
69
|
+
console.error(
|
|
70
|
+
`ZeroBounce: ${parameter} parameter is missing. ${aditionalInfo}`
|
|
71
|
+
);
|
|
72
|
+
}
|
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
import { createRequest, notInitialized, parameterIsMissing } from "./utils.js";
|
|
2
|
+
|
|
3
|
+
export class ZeroBounceSDK {
|
|
4
|
+
constructor() {
|
|
5
|
+
this._initialized = false;
|
|
6
|
+
this._api_key = null;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @param apiKey - your private API key
|
|
11
|
+
* */
|
|
12
|
+
init(apiKey) {
|
|
13
|
+
if (!apiKey) {
|
|
14
|
+
parameterIsMissing("Api key", "Please provide a valid API key.");
|
|
15
|
+
} else {
|
|
16
|
+
this._api_key = apiKey;
|
|
17
|
+
this._initialized = true;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
getCredits() {
|
|
22
|
+
if (!this._initialized) {
|
|
23
|
+
notInitialized();
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
const params = {
|
|
27
|
+
api_key: this._api_key,
|
|
28
|
+
};
|
|
29
|
+
return createRequest({ requestType: "GET", params, path: "/getcredits" });
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* @param email - email to be validated
|
|
34
|
+
* @param ip_address
|
|
35
|
+
* */
|
|
36
|
+
validateEmail(email, ip_address = null) {
|
|
37
|
+
if (!this._initialized) {
|
|
38
|
+
notInitialized();
|
|
39
|
+
return;
|
|
40
|
+
} else if (!email) {
|
|
41
|
+
parameterIsMissing("Email");
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
const params = {
|
|
45
|
+
api_key: this._api_key,
|
|
46
|
+
email: email,
|
|
47
|
+
ip_address,
|
|
48
|
+
};
|
|
49
|
+
return createRequest({ requestType: "GET", params, path: "/validate" });
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* @param startDate - The start date
|
|
54
|
+
* @param endDate - The end date
|
|
55
|
+
* */
|
|
56
|
+
getApiUsage(startDate, endDate) {
|
|
57
|
+
if (!this._initialized) {
|
|
58
|
+
notInitialized();
|
|
59
|
+
return;
|
|
60
|
+
} else if (!startDate) {
|
|
61
|
+
parameterIsMissing("Start date", "Format: YYYY-MM-DD");
|
|
62
|
+
return;
|
|
63
|
+
} else if (!endDate) {
|
|
64
|
+
parameterIsMissing("End date", "Format: YYYY-MM-DD");
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const params = {
|
|
69
|
+
api_key: this._api_key,
|
|
70
|
+
start_date: startDate,
|
|
71
|
+
end_date: endDate,
|
|
72
|
+
};
|
|
73
|
+
return createRequest({
|
|
74
|
+
requestType: "GET",
|
|
75
|
+
params,
|
|
76
|
+
path: "/getapiusage",
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* @param emailBatch: an array containing a list of email objects {email_address: "example@example.com"}
|
|
82
|
+
* */
|
|
83
|
+
|
|
84
|
+
validateBatch(emailBatch) {
|
|
85
|
+
if (!this._initialized) {
|
|
86
|
+
notInitialized();
|
|
87
|
+
return;
|
|
88
|
+
} else if (!emailBatch) {
|
|
89
|
+
parameterIsMissing("Email list");
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const body = {
|
|
94
|
+
api_key: this._api_key,
|
|
95
|
+
email_batch: emailBatch,
|
|
96
|
+
};
|
|
97
|
+
return createRequest({
|
|
98
|
+
requestType: "POST",
|
|
99
|
+
path: "/validatebatch",
|
|
100
|
+
body: JSON.stringify(body),
|
|
101
|
+
batch: true,
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* @param email - a valid email address
|
|
107
|
+
* */
|
|
108
|
+
getEmailActivity(email) {
|
|
109
|
+
if (!this._initialized) {
|
|
110
|
+
notInitialized();
|
|
111
|
+
return;
|
|
112
|
+
} else if (!email) {
|
|
113
|
+
parameterIsMissing("Email");
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
const params = {
|
|
117
|
+
api_key: this._api_key,
|
|
118
|
+
email,
|
|
119
|
+
};
|
|
120
|
+
return createRequest({
|
|
121
|
+
requestType: "GET",
|
|
122
|
+
params,
|
|
123
|
+
path: "/activity",
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* @param file: File - The csv or txt file to be submitted.
|
|
129
|
+
* @param email_address_column: number - The column index of the email address in the file. Index starts from 1.
|
|
130
|
+
* @param return_url: str or null - The URL will be used to call back when the validation is completed.
|
|
131
|
+
* @param first_name_column: number or null - The column index of the first name column.
|
|
132
|
+
* @param last_name_column: number or null - The column index of the last name column.
|
|
133
|
+
* @param gender_column: number or null - The column index of the gender column.
|
|
134
|
+
* @param ip_address_column: number or null - The IP Address the email signed up from.
|
|
135
|
+
* @param has_header_row: Boolean - If the first row from the submitted file is a header row.
|
|
136
|
+
* @param remove_duplicate: Boolean - If you want the system to remove duplicate emails.
|
|
137
|
+
* */
|
|
138
|
+
sendFile({
|
|
139
|
+
file,
|
|
140
|
+
email_address_column,
|
|
141
|
+
first_name_column = false,
|
|
142
|
+
return_url = false,
|
|
143
|
+
last_name_column = false,
|
|
144
|
+
gender_column = false,
|
|
145
|
+
ip_address_column = false,
|
|
146
|
+
has_header_row = false,
|
|
147
|
+
remove_duplicate = false,
|
|
148
|
+
}) {
|
|
149
|
+
if (!this._initialized) {
|
|
150
|
+
notInitialized();
|
|
151
|
+
return;
|
|
152
|
+
} else if (!file) {
|
|
153
|
+
parameterIsMissing("file");
|
|
154
|
+
return;
|
|
155
|
+
} else if (!email_address_column) {
|
|
156
|
+
parameterIsMissing("email_address_column");
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const body = new FormData();
|
|
161
|
+
if (return_url) {
|
|
162
|
+
body.append("return_url", return_url);
|
|
163
|
+
}
|
|
164
|
+
if (first_name_column) {
|
|
165
|
+
body.append("first_name_column", first_name_column);
|
|
166
|
+
}
|
|
167
|
+
if (last_name_column) {
|
|
168
|
+
body.append("last_name_column", last_name_column);
|
|
169
|
+
}
|
|
170
|
+
if (gender_column) {
|
|
171
|
+
body.append("gender_column", gender_column);
|
|
172
|
+
}
|
|
173
|
+
if (ip_address_column) {
|
|
174
|
+
body.append("ip_address_column", ip_address_column);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
body.append("email_address_column", email_address_column);
|
|
178
|
+
body.append("file", file);
|
|
179
|
+
body.append("has_header_row", has_header_row);
|
|
180
|
+
body.append("remove_duplicate", remove_duplicate);
|
|
181
|
+
body.append("api_key", this._api_key);
|
|
182
|
+
|
|
183
|
+
return createRequest({
|
|
184
|
+
requestType: "POST",
|
|
185
|
+
path: "/sendfile",
|
|
186
|
+
body,
|
|
187
|
+
batch: true,
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* @param file: File - The csv or txt file to be submitted.
|
|
193
|
+
* @param email_address_column: number - The column index of the email address in the file. Index starts from 1.
|
|
194
|
+
* @param return_url: str or null - The URL will be used to call back when the validation is completed.
|
|
195
|
+
* @param has_header_row: Boolean - If the first row from the submitted file is a header row.
|
|
196
|
+
* @param remove_duplicate: Boolean - If you want the system to remove duplicate emails.
|
|
197
|
+
* */
|
|
198
|
+
sendScoringFile({
|
|
199
|
+
file,
|
|
200
|
+
email_address_column,
|
|
201
|
+
return_url = false,
|
|
202
|
+
has_header_row = false,
|
|
203
|
+
remove_duplicate = false,
|
|
204
|
+
}) {
|
|
205
|
+
if (!this._initialized) {
|
|
206
|
+
notInitialized();
|
|
207
|
+
return;
|
|
208
|
+
} else if (!file) {
|
|
209
|
+
parameterIsMissing("file: File");
|
|
210
|
+
return;
|
|
211
|
+
} else if (!email_address_column) {
|
|
212
|
+
parameterIsMissing("email_address_column: number");
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const body = new FormData();
|
|
217
|
+
|
|
218
|
+
if (return_url) {
|
|
219
|
+
body.append("return_url", return_url);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
body.append("file", file);
|
|
223
|
+
body.append("email_address_column", email_address_column);
|
|
224
|
+
body.append("has_header_row", has_header_row);
|
|
225
|
+
body.append("api_key", this._api_key);
|
|
226
|
+
body.append("remove_duplicate", remove_duplicate);
|
|
227
|
+
|
|
228
|
+
return createRequest({
|
|
229
|
+
requestType: "POST",
|
|
230
|
+
path: "/scoring/sendfile",
|
|
231
|
+
body,
|
|
232
|
+
batch: true,
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
_getStatusUtil(fileId, path) {
|
|
237
|
+
if (!this._initialized) {
|
|
238
|
+
notInitialized();
|
|
239
|
+
return;
|
|
240
|
+
} else if (!fileId) {
|
|
241
|
+
parameterIsMissing("File id");
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const params = {
|
|
246
|
+
api_key: this._api_key,
|
|
247
|
+
file_id: fileId,
|
|
248
|
+
};
|
|
249
|
+
return createRequest({
|
|
250
|
+
requestType: "GET",
|
|
251
|
+
params,
|
|
252
|
+
path,
|
|
253
|
+
batch: true,
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* @param fileId - the id of a previously submmitted and accepted file
|
|
259
|
+
* */
|
|
260
|
+
getFileStatus(fileId) {
|
|
261
|
+
return this._getStatusUtil(fileId, "/filestatus");
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* @param fileId - the id of a previously submmitted and accepted file
|
|
266
|
+
* */
|
|
267
|
+
getScoringFileStatus(fileId) {
|
|
268
|
+
return this._getStatusUtil(fileId, "/scoring/filestatus");
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
_getFileUtil(fileId, path, scoring = false) {
|
|
272
|
+
if (!this._initialized) {
|
|
273
|
+
notInitialized();
|
|
274
|
+
return;
|
|
275
|
+
} else if (!fileId) {
|
|
276
|
+
parameterIsMissing("File id");
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
279
|
+
const params = {
|
|
280
|
+
api_key: this._api_key,
|
|
281
|
+
file_id: fileId,
|
|
282
|
+
};
|
|
283
|
+
return createRequest({
|
|
284
|
+
requestType: "GET",
|
|
285
|
+
params,
|
|
286
|
+
path,
|
|
287
|
+
batch: true,
|
|
288
|
+
returnText: true,
|
|
289
|
+
scoring,
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* @param fileId - the id of a previously submmitted and accepted file
|
|
295
|
+
* */
|
|
296
|
+
getFile(fileId) {
|
|
297
|
+
return this._getFileUtil(fileId, "/getfile");
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* @param fileId - the id of a previously submmitted and accepted file
|
|
302
|
+
* */
|
|
303
|
+
getScoringFile(fileId) {
|
|
304
|
+
return this._getFileUtil(fileId, "/scoring/getfile", true);
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
_deleteFileUtil(fileId, path, scoring = false) {
|
|
308
|
+
if (!this._initialized) {
|
|
309
|
+
notInitialized();
|
|
310
|
+
return;
|
|
311
|
+
} else if (!fileId) {
|
|
312
|
+
parameterIsMissing("File id");
|
|
313
|
+
return;
|
|
314
|
+
}
|
|
315
|
+
const params = {
|
|
316
|
+
api_key: this._api_key,
|
|
317
|
+
file_id: fileId,
|
|
318
|
+
};
|
|
319
|
+
return createRequest({
|
|
320
|
+
requestType: "GET",
|
|
321
|
+
params,
|
|
322
|
+
path,
|
|
323
|
+
batch: true,
|
|
324
|
+
scoring,
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* @param fileId - the id of a previously submmitted and accepted file
|
|
330
|
+
* */
|
|
331
|
+
deleteFile(fileId) {
|
|
332
|
+
return this._deleteFileUtil(fileId, "/deletefile");
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/**
|
|
336
|
+
* @param fileId - the id of a previously submmitted and accepted file
|
|
337
|
+
* */
|
|
338
|
+
deleteScoringFile(fileId) {
|
|
339
|
+
return this._deleteFileUtil(fileId, "/scoring/deletefile", true);
|
|
340
|
+
}
|
|
341
|
+
}
|