Initial commit
This commit is contained in:
61
api/hooks/lib/omnisendRestApi.js
Normal file
61
api/hooks/lib/omnisendRestApi.js
Normal file
@@ -0,0 +1,61 @@
|
||||
const { omnisendApiKey } = require("../config.js")
|
||||
const { statusIsValid } = require("./utils.js")
|
||||
/**
|
||||
*
|
||||
* @param {string} email
|
||||
*/
|
||||
function createNewsletterSubscriber(email) {
|
||||
const res = context.http.fetch("https://api.omnisend.com/v5/contacts", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"X-API-KEY": omnisendApiKey,
|
||||
accept: "application/json",
|
||||
"content-type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
createdAt: new Date().toISOString(),
|
||||
identifiers: [
|
||||
{
|
||||
channels: {
|
||||
email: {
|
||||
status: "subscribed",
|
||||
statusDate: new Date().toISOString(),
|
||||
},
|
||||
},
|
||||
consent: {
|
||||
createdAt: new Date().toISOString(),
|
||||
source: "headless-frontend",
|
||||
},
|
||||
id: email,
|
||||
type: "email",
|
||||
},
|
||||
],
|
||||
}),
|
||||
})
|
||||
if (!statusIsValid(res.status)) {
|
||||
throw {
|
||||
status: res.status,
|
||||
error: res.statusText,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} email
|
||||
* @returns {boolean}
|
||||
*/
|
||||
function checkIfNewsletterSubscriber(email) {
|
||||
const res = context.http.fetch("https://api.omnisend.com/v5/contacts", {
|
||||
headers: {
|
||||
"X-API-KEY": omnisendApiKey,
|
||||
accept: "application/json",
|
||||
},
|
||||
})
|
||||
const resJson = res.body.json()
|
||||
return resJson.contacts.some((contact) => contact.email === email)
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
createNewsletterSubscriber,
|
||||
checkIfNewsletterSubscriber,
|
||||
}
|
||||
Reference in New Issue
Block a user