This repository has been archived by the owner on Jan 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
174 lines (150 loc) · 4.95 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
const https = require('https');
const querystring = require('querystring');
const API_ENDPOINT = 'https://testi.at/UAPI';
const API_ENDPOINT_CLIENTS_LIST = '/listEmlClients';
const API_ENDPOINT_PROJECT_STATUS = '/projStatus';
const API_ENDPOINT_EMAILTEST_START = '/letsgo';
const STRINGS = {
ERROR: {
MISSING_PROJECT_ID: 'Please provide a valid project ID.',
MISSING_SUBJECT_HTML_CLIENTS: 'Please provide subject, html and client list.',
MISSING_CLIENTS: 'Please provide at least one client as array.'
}
}
module.exports = class Testiat {
constructor(apikey) {
this.apikey = apikey;
this.defaultRequestData = {
API: this.apikey
};
this.api_url = new URL(API_ENDPOINT);
this.defaultRequestDataString = querystring.stringify(this.defaultRequestData);
this.defaultRequestOptions = {
hostname: this.api_url.hostname,
port: 443,
path: this.api_url.pathname,
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': this.defaultRequestDataString.length
}
}
}
getApiEndpoint() {
return API_ENDPOINT;
}
getAvailableClients() {
return new Promise((resolve, reject) => {
const data = this.defaultRequestDataString;
const requestOptions = Object.assign(
{},
this.defaultRequestOptions,
{
path: this.api_url.pathname + API_ENDPOINT_CLIENTS_LIST
}
);
const req = https.request(requestOptions, (res) => {
res.on('data', (data) => {
resolve(
JSON.parse(data.toString('utf-8'))
);
});
}).on('error', (err) => {
reject(err);
});
req.write(data);
req.end();
});
}
getProjectStatus(id) {
return new Promise((resolve, reject) => {
if (
typeof id === 'undefined' ||
typeof id !== 'string'
) {
reject(new Error(STRINGS.ERROR.MISSING_PROJECT_ID));
}
const data = querystring.stringify(
Object.assign(
{},
this.defaultRequestData,
{
ProjID: id
}
)
);
const requestOptions = Object.assign(
{},
this.defaultRequestOptions,
{
path: this.api_url.pathname + API_ENDPOINT_PROJECT_STATUS,
headers: {
...this.defaultRequestOptions.headers,
'Content-Length': data.length
}
}
);
const req = https.request(requestOptions, (res) => {
res.on('data', (data) => {
resolve(
JSON.parse(data.toString('utf-8'))
);
});
}).on('error', (err) => {
reject(err);
});
req.write(data);
req.end();
});
}
startEmailTest(subject, html, clients) {
return new Promise((resolve, reject) => {
if (
!subject ||
!html ||
!clients
) {
reject(new Error(STRINGS.ERROR.MISSING_SUBJECT_HTML_CLIENTS));
}
if (
!Array.isArray(clients) ||
clients.length === 0
) {
reject(new Error(STRINGS.ERROR.MISSING_CLIENTS));
}
const data = querystring.stringify(
Object.assign(
{},
this.defaultRequestData,
{
Subject: subject,
HTML: html,
'ECID[]': clients
}
)
);
const requestOptions = Object.assign(
{},
this.defaultRequestOptions,
{
path: this.api_url.pathname + API_ENDPOINT_EMAILTEST_START,
headers: {
...this.defaultRequestOptions.headers,
'Content-Length': data.length
}
}
);
const req = https.request(requestOptions, (res) => {
res.on('data', (data) => {
resolve(
JSON.parse(data.toString('utf-8'))
);
});
}).on('error', (err) => {
reject(err);
});
req.write(data);
req.end();
});
}
};