forked from bobvanluijt/mailchimp-v3-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmailchimp-v3-api.js
241 lines (218 loc) · 5.92 KB
/
mailchimp-v3-api.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
'use strict';
/**
* Mailchimps API V3 integration
*/
var HTTPS = require('https'),
Q = require('q'),
STRINGDECODER = require('string_decoder').StringDecoder,
DECODER = new STRINGDECODER('utf8');
/**
* The Mailchimp v3 API integration for nodejs
* Detailed information can be found in the readme.md file
*
* @author Bob van Luijt
* @version 0.1
*/
class MailChimpV3 {
/**
* Constructor function
*
* @param {string} i Object with key and optional host information
*/
constructor(i) {
/**
* Report error when key is not set, otherwise set key to this.key
*/
if(typeof i.key === 'undefined'){
console.warn('WARN: Key is undefined, add your API KEY');
} else {
this.key = i.key;
}
/**
* Check if custom server location is set, if not, set to 12
*/
if(typeof i.location === 'undefined'){
this.location = 'us12';
} else {
this.location = i.location;
}
/**
* Check if debug is set, if not, set to false
*/
if(typeof i.debug === 'boolean'){
this.debug = false;
} else {
this.debug = true;
}
}
/**
* Connect()
* Makes an https connection to the Mailchimp server
*
* @param {string} endpoint Based on http://goo.gl/hAZnhM
* @param {string} method Method set for request type
* @return {Object} returns the promises then() and error()
*/
connect(endpoint, method, data){
var decodedData = JSON.stringify(data);
/**
* Using Q for promises
*/
var deferred = Q.defer();
/**
* Set request options
*/
var options = {
headers: {
'Content-Type': 'application/json'
},
auth: 'anystring:' + this.key,
hostname: this.location + '.api.mailchimp.com',
port: 443,
path: '/3.0' + endpoint,
method: method
};
/**
* If data is set, add to POST
*/
if(typeof data !== 'undefined'){
options['headers'] = {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(decodedData)
};
} else {
if(this.debug === true){
console.log('** No data is set (sometimes this is ok, for example with a GET request)');
}
}
/**
* Do the actual request, console.logs if debug === true
*/
var resRaw = [];
var req = HTTPS.request(options, (res) => {
if(this.debug === true){
console.log('** statusCode: ', res.statusCode);
console.log('** headers: ', res.headers);
console.log('** response: ' + res);
}
res.on('data', (d) => {
resRaw.push(DECODER.write(d));
});
res.on('end', () => {
/**
* Sending the response as a deffer
*/
if(resRaw[0] !== undefined && resRaw[0] !== null){
var jsonRes = JSON.parse(resRaw.join(''));
deferred.resolve(jsonRes);
}
});
});
/**
* If data is set, add to POST or PATCH
*/
if(method === 'POST' || method === 'PATCH'){
req.write(decodedData);
}
/**
* Send error promise if error occured
*/
req.on('error', (e) => {
if(this.debug === true){
console.error('ERROR: ' + e);
}
deferred.reject(e);
});
req.end();
/**
* Return the promise
*/
return deferred.promise;
}
/**
* Get()
* Used for all GET related calls
*
* @param {endpoint} endpoint Based on http://goo.gl/hAZnhM
* @return {Object} returns the promises then() and error()
*/
get(endpoint){
/**
* Using Q for promises
*/
var deferred = Q.defer();
/**
* Do the request and prepare promise
*/
this
.connect(endpoint, 'GET')
.then(d => {
deferred.resolve(d);
});
return deferred.promise;
}
post(endpoint, data){
/**
* Using Q for promises
*/
var deferred = Q.defer();
/**
* Do the request and prepare promise
*/
this
.connect(endpoint, 'POST', data)
.then(d => {
deferred.resolve(d);
});
return deferred.promise;
}
patch(endpoint, data){
/**
* Using Q for promises
*/
var deferred = Q.defer();
/**
* Do the request and prepare promise
*/
this
.connect(endpoint, 'PATCH', data)
.then(d => {
deferred.resolve(d);
});
return deferred.promise;
}
put(endpoint, data){
/**
* Using Q for promises
*/
var deferred = Q.defer();
/**
* Do the request and prepare promise
*/
this
.connect(endpoint, 'PUT', data)
.then(d => {
deferred.resolve(d);
});
return deferred.promise;
}
delete(endpoint){
/**
* Using Q for promises
*/
var deferred = Q.defer();
/**
* Do the request and prepare promise
*/
this
.connect(endpoint, 'DELETE')
.then(d => {
deferred.resolve(d);
});
return deferred.promise;
}
}
/**
* Return the module
*/
module.exports = MailChimpV3;