This repository has been archived by the owner on Dec 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #760 from brandonbk/native-x-utilities
Add native-x utilities
- Loading branch information
Showing
5 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'), | ||
}; | ||
}; |
8 changes: 8 additions & 0 deletions
8
packages/marko-newsletters-native-x/utils/date-to-timestamp.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
32
packages/marko-newsletters-native-x/utils/fetch-email-ad.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = (config) => Boolean(config && config.uri && config.enabled !== false); |