diff --git a/packages/marko-newsletters-native-x/utils/create-headers.js b/packages/marko-newsletters-native-x/utils/create-headers.js new file mode 100644 index 000000000..744ebddc6 --- /dev/null +++ b/packages/marko-newsletters-native-x/utils/create-headers.js @@ -0,0 +1,7 @@ +module.exports = ({ req }) => { + if (!req) return {}; + return { + 'x-forwarded-for': req.ip, + 'user-agent': req.get('user-agent'), + }; +}; diff --git a/packages/marko-newsletters-native-x/utils/date-to-timestamp.js b/packages/marko-newsletters-native-x/utils/date-to-timestamp.js new file mode 100644 index 000000000..895472f0d --- /dev/null +++ b/packages/marko-newsletters-native-x/utils/date-to-timestamp.js @@ -0,0 +1,8 @@ +module.exports = (date) => { + const dateInUTC = new Date(Date.UTC( + date.toDate().getFullYear(), + date.toDate().getMonth(), + date.toDate().getDate(), + )); + return dateInUTC.valueOf(); +}; diff --git a/packages/marko-newsletters-native-x/utils/fetch-email-ad.js b/packages/marko-newsletters-native-x/utils/fetch-email-ad.js new file mode 100644 index 000000000..baa9a65b9 --- /dev/null +++ b/packages/marko-newsletters-native-x/utils/fetch-email-ad.js @@ -0,0 +1,32 @@ +const { isObject } = require('@parameter1/base-cms-utils'); +const { URLSearchParams } = require('url'); +const fetch = require('node-fetch'); +const createHeaders = require('./create-headers'); + +module.exports = async ({ + uri, + placementId, + timestamp, + imageOptions, + logoOptions, + opts, + req, +} = {}) => { + const params = new URLSearchParams({ + ...(typeof timestamp === 'number' && { timestamp }), + ...(isObject(imageOptions) && { imageOptions: JSON.stringify(imageOptions) }), + ...(isObject(logoOptions) && { advertiserLogoOptions: JSON.stringify(logoOptions) }), + ...(isObject(opts) && { opts: JSON.stringify(opts) }), + }); + const query = `${params}`; + const url = `${uri}/email-placement/${placementId}.json${query ? `?${query}` : ''}`; + const response = await fetch(url, { headers: createHeaders({ req }) }); + const json = await response.json(); + if (!response.ok) { + const err = new Error(response.statusMessage); + err.statusCode = response.statusText; + err.body = json; + throw err; + } + return json; +}; diff --git a/packages/marko-newsletters-native-x/utils/index.js b/packages/marko-newsletters-native-x/utils/index.js new file mode 100644 index 000000000..4429fbd68 --- /dev/null +++ b/packages/marko-newsletters-native-x/utils/index.js @@ -0,0 +1,11 @@ +const convertAdToContent = require('./convert-ad-to-content'); +const dateToTimestamp = require('./date-to-timestamp'); +const fetchEmailAd = require('./fetch-email-ad'); +const isEnabled = require('./is-enabled'); + +module.exports = { + convertAdToContent, + dateToTimestamp, + fetchEmailAd, + isEnabled, +}; diff --git a/packages/marko-newsletters-native-x/utils/is-enabled.js b/packages/marko-newsletters-native-x/utils/is-enabled.js new file mode 100644 index 000000000..baa3e982a --- /dev/null +++ b/packages/marko-newsletters-native-x/utils/is-enabled.js @@ -0,0 +1 @@ +module.exports = (config) => Boolean(config && config.uri && config.enabled !== false);