Skip to content
This repository has been archived by the owner on Dec 9, 2024. It is now read-only.

Commit

Permalink
Merge pull request #760 from brandonbk/native-x-utilities
Browse files Browse the repository at this point in the history
Add native-x utilities
  • Loading branch information
brandonbk authored Aug 7, 2023
2 parents a94fc01 + 09bc9d7 commit d471158
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 0 deletions.
7 changes: 7 additions & 0 deletions packages/marko-newsletters-native-x/utils/create-headers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = ({ req }) => {
if (!req) return {};
return {
'x-forwarded-for': req.ip,
'user-agent': req.get('user-agent'),
};
};
Original file line number Diff line number Diff line change
@@ -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();
};
32 changes: 32 additions & 0 deletions packages/marko-newsletters-native-x/utils/fetch-email-ad.js
Original file line number Diff line number Diff line change
@@ -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;
};
11 changes: 11 additions & 0 deletions packages/marko-newsletters-native-x/utils/index.js
Original file line number Diff line number Diff line change
@@ -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,
};
1 change: 1 addition & 0 deletions packages/marko-newsletters-native-x/utils/is-enabled.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = (config) => Boolean(config && config.uri && config.enabled !== false);

0 comments on commit d471158

Please sign in to comment.