From 09bc9d74ea72740bb42831f30b3a78f949c6cd08 Mon Sep 17 00:00:00 2001 From: Brandon Krigbaum Date: Mon, 7 Aug 2023 08:25:15 -0500 Subject: [PATCH] Add native-x utilities --- .../utils/create-headers.js | 7 ++++ .../utils/date-to-timestamp.js | 8 +++++ .../utils/fetch-email-ad.js | 32 +++++++++++++++++++ .../marko-newsletters-native-x/utils/index.js | 11 +++++++ .../utils/is-enabled.js | 1 + 5 files changed, 59 insertions(+) create mode 100644 packages/marko-newsletters-native-x/utils/create-headers.js create mode 100644 packages/marko-newsletters-native-x/utils/date-to-timestamp.js create mode 100644 packages/marko-newsletters-native-x/utils/fetch-email-ad.js create mode 100644 packages/marko-newsletters-native-x/utils/index.js create mode 100644 packages/marko-newsletters-native-x/utils/is-enabled.js 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);