-
Notifications
You must be signed in to change notification settings - Fork 31
/
index.js
123 lines (108 loc) · 4.31 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
'use strict';
Object.defineProperty(exports, '__esModule', {
value: true,
});
exports['default'] = void 0;
var _jsonp = _interopRequireDefault(require('jsonp'));
var _emailValidator = require('email-validator');
function _interopRequireDefault(obj) {
return obj && obj.__esModule ? obj : { default: obj };
}
/**
* Make a jsonp request to user's mailchimp list
* `param` object avoids CORS issues
* timeout to 3.5s so user isn't waiting forever
* usually occurs w/ privacy plugins enabled
* 3.5s is a bit longer than the time it would take on a Slow 3G connection
*
* @param {String} url - concatenated string of user's gatsby-config.js
* options, along with any MC list fields as query params.
*
* @return {Promise} - a promise that resolves a data object
* or rejects an error object
*/
var subscribeEmailToMailchimp = function subscribeEmailToMailchimp(_ref) {
var url = _ref.url,
timeout = _ref.timeout;
return new Promise(function(resolve, reject) {
return (0, _jsonp['default'])(
url,
{
param: 'c',
timeout: timeout,
},
function(err, data) {
if (err) reject(err);
if (data) resolve(data);
},
);
});
};
/**
* Build a query string of MC list fields
*
* @param {Object} fields - a list of mailchimp audience field labels
* and their values. We uppercase because that's what MC requires.
* NOTE: GROUPS stay as lowercase (ex: MC uses group field names as `group[21269]`)
*
* @return {String} - `&FIELD1=value1&FIELD2=value2&group[21265][2]=group1`
*/
var convertListFields = function convertListFields(fields) {
var queryParams = '';
for (var field in fields) {
if (Object.prototype.hasOwnProperty.call(fields, field)) {
// If this is a list group, not user field then keep lowercase, as per MC reqs
// https://github.com/benjaminhoffman/gatsby-plugin-mailchimp/blob/master/README.md#groups
var fieldTransformed = field.substring(0, 6) === 'group[' ? field : field.toUpperCase();
queryParams = queryParams.concat(
'&'.concat(fieldTransformed, '=').concat(fields[field]),
);
}
}
return queryParams;
};
/**
* Subscribe an email address to a Mailchimp email list.
* We use ES5 function syntax (instead of arrow) because we need `arguments.length`
*
* @param {String} email - required; the email address you want to subscribe
* @param {Object} fields - optional; add'l info (columns) you want included w/ this subscriber
* @param {String} endpointOverride - optional; if you want to override the default MC mailing list
* that's listed in your gatsby-config, pass the list in here
*
* @return {Object} -
* {
* result: <String>(`success` || `error`)
* msg: <String>(`Thank you for subscribing!` || `The email you entered is not valid.`),
* }
*/
var addToMailchimp = function addToMailchimp(email, fields, endpointOverride) {
var isEmailValid = (0, _emailValidator.validate)(email);
var emailEncoded = encodeURIComponent(email);
if (!isEmailValid) {
return Promise.resolve({
result: 'error',
msg: 'The email you entered is not valid.',
});
}
var endpoint = __GATSBY_PLUGIN_MAILCHIMP_ADDRESS__; // eslint-disable-line no-undef
var timeout = __GATSBY_PLUGIN_MAILCHIMP_TIMEOUT__; // eslint-disable-line no-undef
// The following tests for whether you passed in a `fields` object. If
// there are only two params and the second is a string, then we can safely
// assume the second param is a MC mailing list, and not a fields object.
if (arguments.length < 3 && typeof fields === 'string') {
endpoint = fields;
} else if (typeof endpointOverride === 'string') {
endpoint = endpointOverride;
} // Generates MC endpoint for our jsonp request. We have to
// change `/post` to `/post-json` otherwise, MC returns an error
endpoint = endpoint.replace(/\/post/g, '/post-json');
var queryParams = '&EMAIL='.concat(emailEncoded).concat(convertListFields(fields));
var url = ''.concat(endpoint).concat(queryParams);
return subscribeEmailToMailchimp({
url: url,
timeout: timeout,
});
};
var _default = addToMailchimp;
exports['default'] = _default;