|
| 1 | +/** |
| 2 | + * Server-side functions necessary for effective integration with mailchimp |
| 3 | + */ |
| 4 | +import fetch from 'isomorphic-fetch'; |
| 5 | +import config from 'config'; |
| 6 | +import qs from 'qs'; |
| 7 | + |
| 8 | +/** |
| 9 | + * Auxiliary class that handles communication with mailchimp |
| 10 | + * APIs in the same uniform manner. |
| 11 | + */ |
| 12 | +export default class MailchimpService { |
| 13 | + /** |
| 14 | + * Creates a new service instance. |
| 15 | + * @param {String} baseUrl The base API endpoint. |
| 16 | + */ |
| 17 | + constructor(baseUrl) { |
| 18 | + this.private = { baseUrl }; |
| 19 | + const credentials = config.SECRET.MAILCHIMP.default; |
| 20 | + this.mailchimpBaseUrl = credentials.MAILCHIMP_BASE_URL; |
| 21 | + this.apiKey = credentials.API_KEY; |
| 22 | + this.authorization = `Basic ${Buffer.from(`apikey:${this.apiKey}`).toString('base64')}`; |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * Gets data from the specified endpoint. |
| 27 | + * @return {Promise} |
| 28 | + * @param {Object} the request. |
| 29 | + */ |
| 30 | + async checkSubscription(req) { |
| 31 | + const res = await fetch(`${this.mailchimpBaseUrl}/lists/${req.params.listId}/members/${req.params.emailHash}`, { |
| 32 | + method: 'GET', |
| 33 | + headers: { |
| 34 | + 'Content-Type': req.headers['content-type'], |
| 35 | + Authorization: this.authorization, |
| 36 | + }, |
| 37 | + }); |
| 38 | + return res.json(); |
| 39 | + } |
| 40 | + |
| 41 | + async doRegistMember(req) { |
| 42 | + const formData = JSON.stringify(req.body); |
| 43 | + const res = await fetch(`${this.mailchimpBaseUrl}/lists/${req.params.listId}/members`, { |
| 44 | + method: 'POST', |
| 45 | + headers: { |
| 46 | + 'Content-Type': req.headers['content-type'], |
| 47 | + Authorization: this.authorization, |
| 48 | + }, |
| 49 | + body: formData, |
| 50 | + }); |
| 51 | + return res.json(); |
| 52 | + } |
| 53 | + |
| 54 | + async updateMember(req) { |
| 55 | + const formData = JSON.stringify(req.body); |
| 56 | + const res = await fetch(`${this.mailchimpBaseUrl}/lists/${req.params.listId}/members/${req.params.emailHash}`, { |
| 57 | + method: 'PUT', |
| 58 | + headers: { |
| 59 | + 'Content-Type': req.headers['content-type'], |
| 60 | + Authorization: this.authorization, |
| 61 | + }, |
| 62 | + body: formData, |
| 63 | + }); |
| 64 | + return res.json(); |
| 65 | + } |
| 66 | + |
| 67 | + async subscribeTags(req) { |
| 68 | + const formData = JSON.stringify(req.body); |
| 69 | + const res = await fetch(`${this.mailchimpBaseUrl}/lists/${req.params.listId}/members/${req.params.emailHash}/tags`, { |
| 70 | + method: 'POST', |
| 71 | + headers: { |
| 72 | + 'Content-Type': req.headers['content-type'], |
| 73 | + Authorization: this.authorization, |
| 74 | + }, |
| 75 | + body: formData, |
| 76 | + }); |
| 77 | + return { status: res.status }; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Gets campaign-folders endpoint. |
| 82 | + * @return {Promise} |
| 83 | + * @param {Object} the request. |
| 84 | + */ |
| 85 | + async getCampaignFolder(req) { |
| 86 | + const res = await fetch(`${this.mailchimpBaseUrl}/campaign-folders?count=500`, { |
| 87 | + method: 'GET', |
| 88 | + headers: { |
| 89 | + 'Content-Type': req.headers['content-type'], |
| 90 | + Authorization: this.authorization, |
| 91 | + }, |
| 92 | + }); |
| 93 | + return res.json(); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Gets campaigns endpoint. |
| 98 | + * @return {Promise} |
| 99 | + * @param {Object} the request. |
| 100 | + */ |
| 101 | + async getCampaigns(req) { |
| 102 | + const res = await fetch(`${this.mailchimpBaseUrl}/campaigns?${qs.stringify(req.query)}`, { |
| 103 | + method: 'GET', |
| 104 | + headers: { |
| 105 | + 'Content-Type': req.headers['content-type'], |
| 106 | + Authorization: this.authorization, |
| 107 | + }, |
| 108 | + }); |
| 109 | + return res.json(); |
| 110 | + } |
| 111 | +} |
0 commit comments