import jsonp from 'jsonp' ; import { validate } from 'email-validator' ; param { String } url return { Promise } const subscribeEmailToMailchimp = url => new Promise ( ( resolve , reject ) => jsonp ( url , { param : 'c' , timeout : 3500 } , ( err , data ) => { if ( err ) reject ( err ) ; if ( data ) resolve ( data ) ; } ) ) ; param { String } email return { Object } export const addToMailchimp = function addToMailchimp ( email ) { const isEmailValid = validate ( email ) ; const emailEncoded = encodeURIComponent ( email ) ; if ( ! isEmailValid ) { return Promise . resolve ( { result : 'error' , msg : 'The email you entered is not valid.' , } ) ; } let endpoint = process . env . MAILCHIMP_ADDRESS ; endpoint = endpoint . replace ( /\/post/g , '/post-json' ) ; const queryParams = `&EMAIL=${ emailEncoded }` ; const url = `${ endpoint }${ queryParams }` ; return subscribeEmailToMailchimp ( url ) ; } ;