-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
73 lines (66 loc) · 1.94 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Require fetch
// Note: fetch is required here for this package to work well
const fetch = require("node-fetch");
/* Makes a PATCH request with given data and return response
** param {string}
** param {string}
** return {object || string}
*/
const subscribe = async (email, projectApi) => {
// Validation
if (typeof email !== "string" && typeof projectApi !== "string") {
throw new Error("Both arguments should be typeof string.");
} else if (typeof email !== undefined && typeof projectApi !== undefined) {
throw new Error("Both arguments should be passed.");
}
const res = await fetch(
"https://cknewsletter.herokuapp.com/api/v1/projects/subscribe",
{
method: "PATCH",
headers: {
"Content-Type": "application/json;charset=utf-8",
},
body: JSON.stringify({ subscriber: email, apiKey: projectApi }),
}
);
// Return response
if (res.status === 200) {
return res;
} else if (res.status === 409) {
throw new Error("Already subscribed.");
} else {
throw new Error("There's an error.");
}
};
/* Makes a PATCH request with given data and return response
** param {string}
** param {string}
** return {object || string}
*/
const pushSlug = async (slug, projectApi) => {
// Validation
if (typeof slug !== "string" && typeof projectApi !== "string") {
throw new Error("Both arguments should be typeof string.");
} else if (typeof slug !== undefined && typeof projectApi !== undefined) {
throw new Error("Both arguments should be passed.");
}
const res = await fetch(
"https://cknewsletter.herokuapp.com/api/v1/projects/slug",
{
method: "PATCH",
headers: {
"Content-Type": "application/json;charset=utf-8",
},
body: JSON.stringify({ slug, apiKey: projectApi }),
}
);
// Return response
if (res.status === 200) {
return res;
} else if (res.status === 409) {
throw new Error("Already pushed the slug.");
} else {
throw new Error("There's an error.");
}
};
module.exports = { subscribe, pushSlug };