@mindful-web/marko-newsletters-email-x 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.eslintignore ADDED
@@ -0,0 +1 @@
1
+ *.marko.js
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Parameter1 LLC
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,30 @@
1
+ import { warn } from "@mindful-web/utils";
2
+ import { getAsObject, getAsArray } from "@mindful-web/object-path";
3
+ import fetchData from "../utils/fetch-data";
4
+
5
+ $ const { onError } = input;
6
+ $ const adUnit = getAsObject(input, "adUnit");
7
+ $ const params = getAsObject(input, "params");
8
+ $ const decodedParams = getAsArray(input, "decodedParams");
9
+
10
+ $ const { uri, id } = adUnit;
11
+ $ const {
12
+ email,
13
+ date,
14
+ send,
15
+ includeAdvertiser,
16
+ } = params;
17
+
18
+ $ const promise = fetchData({
19
+ serveUri: uri,
20
+ adUnitId: id,
21
+ date,
22
+ email,
23
+ send,
24
+ includeAdvertiser,
25
+ decodedParams,
26
+ });
27
+
28
+ <marko-web-resolve|{ resolved }| promise=promise>
29
+ <${input.renderBody} response=resolved ad-unit=adUnit />
30
+ </marko-web-resolve>
@@ -0,0 +1,75 @@
1
+ // Compiled using marko@4.20.2 - DO NOT EDIT
2
+ "use strict";
3
+
4
+ var marko_template = module.exports = require("marko/dist/html").t(__filename),
5
+ marko_componentType = "/@mindful-web/marko-newsletters-email-x$1.0.0/components/data.marko",
6
+ marko_component = require("./data.marko"),
7
+ marko_renderer = require("marko/dist/runtime/components/renderer"),
8
+ module_utils_module = require("@mindful-web/utils"),
9
+ utils_module = module_utils_module.default || module_utils_module,
10
+ warn = module_utils_module.warn,
11
+ module_objectPath_module = require("@mindful-web/object-path"),
12
+ objectPath_module = module_objectPath_module.default || module_objectPath_module,
13
+ getAsObject = module_objectPath_module.getAsObject,
14
+ getAsArray = module_objectPath_module.getAsArray,
15
+ module_fetchData = require("../utils/fetch-data"),
16
+ fetchData = module_fetchData.default || module_fetchData,
17
+ marko_dynamicTag = require("marko/dist/runtime/helpers/dynamic-tag"),
18
+ marko_web_resolve_template = require("@mindful-web/marko-core/components/resolve.marko"),
19
+ marko_loadTag = require("marko/dist/runtime/helpers/load-tag"),
20
+ marko_web_resolve_tag = marko_loadTag(marko_web_resolve_template);
21
+
22
+ function render(input, out, __component, component, state) {
23
+ var data = input;
24
+
25
+ const { onError } = input;
26
+
27
+ const adUnit = getAsObject(input, "adUnit");
28
+
29
+ const params = getAsObject(input, "params");
30
+
31
+ const decodedParams = getAsArray(input, "decodedParams");
32
+
33
+ const { uri, id } = adUnit;
34
+
35
+ const {
36
+ email,
37
+ date,
38
+ send,
39
+ includeAdvertiser,
40
+ } = params;
41
+
42
+ const promise = fetchData({
43
+ serveUri: uri,
44
+ adUnitId: id,
45
+ date,
46
+ email,
47
+ send,
48
+ includeAdvertiser,
49
+ decodedParams,
50
+ });
51
+
52
+ marko_web_resolve_tag({
53
+ promise: promise,
54
+ renderBody: function(out, { resolved }) {
55
+ marko_dynamicTag(out, input.renderBody, function() {
56
+ return {
57
+ response: resolved,
58
+ "ad-unit": adUnit
59
+ };
60
+ }, null, null, null, __component, "1");
61
+ }
62
+ }, out, __component, "0");
63
+ }
64
+
65
+ marko_template._ = marko_renderer(render, {
66
+ e_: marko_componentType
67
+ }, marko_component);
68
+
69
+ marko_template.meta = {
70
+ id: "/@mindful-web/marko-newsletters-email-x$1.0.0/components/data.marko",
71
+ component: "./data.marko",
72
+ tags: [
73
+ "@mindful-web/marko-core/components/resolve.marko"
74
+ ]
75
+ };
@@ -0,0 +1,38 @@
1
+ import { warn } from "@mindful-web/utils";
2
+ import { getAsObject, getAsArray } from "@mindful-web/object-path";
3
+ import moment from "moment";
4
+ import buildUrl from "../utils/build-url";
5
+ import buildQuery from "../utils/build-query";
6
+
7
+ $ const adUnit = getAsObject(input, "adUnit");
8
+ $ const params = getAsObject(input, "params");
9
+ $ const decodedParams = getAsArray(input, "decodedParams");
10
+
11
+ $ const { uri, id, width, height, name, alias } = adUnit;
12
+ $ const { email, date, send } = params;
13
+
14
+ $ const momentDate = moment(date);
15
+ $ if (!momentDate.isValid()) warn("The EmailX date parameter is invalid.");
16
+
17
+ $ const canDisplay = Boolean(uri && id && email && momentDate.isValid());
18
+
19
+ $ const query = buildQuery({ momentDate, email, send });
20
+ $ const href = buildUrl({ action: "click", uri, id, query, decodedParams });
21
+ $ const src = buildUrl({ action: "image", uri, id, query, decodedParams });
22
+ $ const imageAttrs = {
23
+ ...getAsObject(input.image, "attrs"),
24
+ width,
25
+ height,
26
+ };
27
+ $ const classNames = [`email-x-${alias}-${name}`, input.class];
28
+
29
+ <if(canDisplay)>
30
+ <div ...input.attrs class=classNames>
31
+ <marko-core-img ...input.image src=src attrs=imageAttrs>
32
+ <@link target="_blank" ...input.link href=href />
33
+ </marko-core-img>
34
+ </div>
35
+ </if>
36
+ <else>
37
+ $ warn("Unable to render EmailX ad unit: a required input property is missing.");
38
+ </else>
@@ -0,0 +1,92 @@
1
+ // Compiled using marko@4.20.2 - DO NOT EDIT
2
+ "use strict";
3
+
4
+ var marko_template = module.exports = require("marko/dist/html").t(__filename),
5
+ marko_componentType = "/@mindful-web/marko-newsletters-email-x$1.0.0/components/display.marko",
6
+ marko_renderer = require("marko/dist/runtime/components/renderer"),
7
+ module_utils_module = require("@mindful-web/utils"),
8
+ utils_module = module_utils_module.default || module_utils_module,
9
+ warn = module_utils_module.warn,
10
+ module_objectPath_module = require("@mindful-web/object-path"),
11
+ objectPath_module = module_objectPath_module.default || module_objectPath_module,
12
+ getAsObject = module_objectPath_module.getAsObject,
13
+ getAsArray = module_objectPath_module.getAsArray,
14
+ module_moment = require("moment"),
15
+ moment = module_moment.default || module_moment,
16
+ module_buildUrl = require("../utils/build-url"),
17
+ buildUrl = module_buildUrl.default || module_buildUrl,
18
+ module_buildQuery = require("../utils/build-query"),
19
+ buildQuery = module_buildQuery.default || module_buildQuery,
20
+ marko_assign = require("marko/dist/runtime/helpers/assign"),
21
+ marko_core_img_template = require("@mindful-web/marko-core/components/elements/img.marko"),
22
+ marko_loadTag = require("marko/dist/runtime/helpers/load-tag"),
23
+ marko_core_img_tag = marko_loadTag(marko_core_img_template),
24
+ marko_mergeAttrs = require("marko/dist/runtime/html/helpers/merge-attrs");
25
+
26
+ function render(input, out, __component, component, state) {
27
+ var data = input;
28
+
29
+ const adUnit = getAsObject(input, "adUnit");
30
+
31
+ const params = getAsObject(input, "params");
32
+
33
+ const decodedParams = getAsArray(input, "decodedParams");
34
+
35
+ const { uri, id, width, height, name, alias } = adUnit;
36
+
37
+ const { email, date, send } = params;
38
+
39
+ const momentDate = moment(date);
40
+
41
+ if (!momentDate.isValid()) warn("The EmailX date parameter is invalid.");
42
+
43
+ const canDisplay = Boolean(uri && id && email && momentDate.isValid());
44
+
45
+ const query = buildQuery({ momentDate, email, send });
46
+
47
+ const href = buildUrl({ action: "click", uri, id, query, decodedParams });
48
+
49
+ const src = buildUrl({ action: "image", uri, id, query, decodedParams });
50
+
51
+ const imageAttrs = {
52
+ ...getAsObject(input.image, "attrs"),
53
+ width,
54
+ height,
55
+ };
56
+
57
+ const classNames = [`email-x-${alias}-${name}`, input.class];
58
+
59
+ if (canDisplay) {
60
+ out.w("<div" +
61
+ marko_mergeAttrs(input.attrs, {
62
+ class: classNames
63
+ }) +
64
+ ">");
65
+
66
+ marko_core_img_tag(marko_assign({}, input.image, {
67
+ src: src,
68
+ attrs: imageAttrs,
69
+ link: marko_assign({
70
+ target: "_blank"
71
+ }, input.link, {
72
+ href: href
73
+ })
74
+ }), out, __component, "1");
75
+
76
+ out.w("</div>");
77
+ } else {
78
+ warn("Unable to render EmailX ad unit: a required input property is missing.");
79
+ }
80
+ }
81
+
82
+ marko_template._ = marko_renderer(render, {
83
+ d_: true,
84
+ e_: marko_componentType
85
+ });
86
+
87
+ marko_template.meta = {
88
+ id: "/@mindful-web/marko-newsletters-email-x$1.0.0/components/display.marko",
89
+ tags: [
90
+ "@mindful-web/marko-core/components/elements/img.marko"
91
+ ]
92
+ };
@@ -0,0 +1,58 @@
1
+ {
2
+ "<marko-newsletters-email-x-display>": {
3
+ "template": "./display.marko",
4
+ "<ad-unit>": {
5
+ "@uri": "string",
6
+ "@id": "string",
7
+ "@width": "number",
8
+ "@height": "number",
9
+ "@name": "string",
10
+ "@alias": "string"
11
+ },
12
+ "<params>": {
13
+ "@email": "string",
14
+ "@date": "date",
15
+ "@send": "string"
16
+ },
17
+ "<link>": {},
18
+ "<image>": {},
19
+ "@decoded-params": "array",
20
+ "@class": "string",
21
+ "@attrs": "object"
22
+ },
23
+ "<marko-newsletters-email-x-data>": {
24
+ "template": "./data.marko",
25
+ "<ad-unit>": {
26
+ "@uri": "string",
27
+ "@id": "string"
28
+ },
29
+ "<params>": {
30
+ "@email": "string",
31
+ "@date": "date",
32
+ "@send": "string",
33
+ "@include-advertiser": "boolean"
34
+ },
35
+ "@decoded-params": "array"
36
+ },
37
+ "<marko-newsletters-email-x-render>": {
38
+ "template": "./render.marko",
39
+ "<response>": {
40
+ "<data>": {
41
+ "<ad>": {
42
+ "@height": "number",
43
+ "@mindfulCreativeId": "string",
44
+ "@width": "number"
45
+ }
46
+ },
47
+ "@imageSrc": "string"
48
+ },
49
+ "<link>": {
50
+ "@href": "string",
51
+ "@attrs": "object"
52
+ },
53
+ "<image>": {
54
+ "@attrs": "object",
55
+ "@class": "string"
56
+ }
57
+ }
58
+ }
@@ -0,0 +1,37 @@
1
+ import { getAsObject, get } from "@mindful-web/object-path";
2
+
3
+ $ const response = getAsObject(input, "response");
4
+ $ const { ad, elements } = response.data;
5
+ $ const imageSrc = get(input, "response.imageSrc");
6
+ $ const adImageSrc = get(ad, "image.src") || imageSrc;
7
+
8
+ $ const imageAttrs = {
9
+ ...getAsObject(input, "image.attrs"),
10
+ width: ad.width,
11
+ height: ad.height,
12
+ };
13
+
14
+ $ const mindfulCreativeId = get(response, "data.ad.mindfulCreativeId");
15
+ $ let mindfulTenant;
16
+ $ try {
17
+ [, mindfulTenant] = imageSrc.match(/^https:\/\/delivery\.mindfulcms\.com\/(\w+\/\w+)\//);
18
+ } catch (e) {
19
+ // Explicitly throw, native email-x delivery is no longer supported.
20
+ throw new Error(`Unable to parse mindful tenant from image url ${imageSrc}`);
21
+ }
22
+ $ if (!mindfulCreativeId) throw new Error('Unable to parse mindful creative id from response.');
23
+
24
+ $ const linkAttrs = {
25
+ href: ad.url,
26
+ ...(getAsObject(input, "link")),
27
+ attrs: {
28
+ ...(getAsObject(input, "link.attrs")),
29
+ "data-mindful-tenant": mindfulTenant,
30
+ "data-mindful-creative-id": mindfulCreativeId,
31
+ ...elements.a,
32
+ },
33
+ };
34
+
35
+ <marko-core-img ...input.image src=adImageSrc attrs=imageAttrs>
36
+ <@link target="_blank" ...linkAttrs />
37
+ </marko-core-img>
@@ -0,0 +1,76 @@
1
+ // Compiled using marko@4.20.2 - DO NOT EDIT
2
+ "use strict";
3
+
4
+ var marko_template = module.exports = require("marko/dist/html").t(__filename),
5
+ marko_componentType = "/@mindful-web/marko-newsletters-email-x$1.0.0/components/render.marko",
6
+ marko_renderer = require("marko/dist/runtime/components/renderer"),
7
+ module_objectPath_module = require("@mindful-web/object-path"),
8
+ objectPath_module = module_objectPath_module.default || module_objectPath_module,
9
+ getAsObject = module_objectPath_module.getAsObject,
10
+ get = module_objectPath_module.get,
11
+ marko_assign = require("marko/dist/runtime/helpers/assign"),
12
+ marko_core_img_template = require("@mindful-web/marko-core/components/elements/img.marko"),
13
+ marko_loadTag = require("marko/dist/runtime/helpers/load-tag"),
14
+ marko_core_img_tag = marko_loadTag(marko_core_img_template);
15
+
16
+ function render(input, out, __component, component, state) {
17
+ var data = input;
18
+
19
+ const response = getAsObject(input, "response");
20
+
21
+ const { ad, elements } = response.data;
22
+
23
+ const imageSrc = get(input, "response.imageSrc");
24
+
25
+ const adImageSrc = get(ad, "image.src") || imageSrc;
26
+
27
+ const imageAttrs = {
28
+ ...getAsObject(input, "image.attrs"),
29
+ width: ad.width,
30
+ height: ad.height,
31
+ };
32
+
33
+ const mindfulCreativeId = get(response, "data.ad.mindfulCreativeId");
34
+
35
+ let mindfulTenant;
36
+
37
+ try {
38
+ [, mindfulTenant] = imageSrc.match(/^https:\/\/delivery\.mindfulcms\.com\/(\w+\/\w+)\//);
39
+ } catch (e) {
40
+ // Explicitly throw, native email-x delivery is no longer supported.
41
+ throw new Error(`Unable to parse mindful tenant from image url ${imageSrc}`);
42
+ }
43
+
44
+ if (!mindfulCreativeId) throw new Error('Unable to parse mindful creative id from response.');
45
+
46
+ const linkAttrs = {
47
+ href: ad.url,
48
+ ...(getAsObject(input, "link")),
49
+ attrs: {
50
+ ...(getAsObject(input, "link.attrs")),
51
+ "data-mindful-tenant": mindfulTenant,
52
+ "data-mindful-creative-id": mindfulCreativeId,
53
+ ...elements.a,
54
+ },
55
+ };
56
+
57
+ marko_core_img_tag(marko_assign({}, input.image, {
58
+ src: adImageSrc,
59
+ attrs: imageAttrs,
60
+ link: marko_assign({
61
+ target: "_blank"
62
+ }, linkAttrs)
63
+ }), out, __component, "0");
64
+ }
65
+
66
+ marko_template._ = marko_renderer(render, {
67
+ d_: true,
68
+ e_: marko_componentType
69
+ });
70
+
71
+ marko_template.meta = {
72
+ id: "/@mindful-web/marko-newsletters-email-x$1.0.0/components/render.marko",
73
+ tags: [
74
+ "@mindful-web/marko-core/components/elements/img.marko"
75
+ ]
76
+ };
package/config.js ADDED
@@ -0,0 +1,80 @@
1
+ const { asArray } = require('@mindful-web/utils');
2
+ const { set, getAsObject } = require('@mindful-web/object-path');
3
+
4
+ class EmailXConfiguration {
5
+ /**
6
+ *
7
+ * @param {string} uri
8
+ * @param {object} params
9
+ */
10
+ constructor(uri) {
11
+ if (!uri) throw new Error('Unable to configure EmailX: no URI was provided.');
12
+ this.uri = uri;
13
+ this.adUnits = {};
14
+ }
15
+
16
+ /**
17
+ *
18
+ * @param {object} params
19
+ * @param {string} params.name The ad unit name, e.g. `primary` or `header`, etc.
20
+ * @param {string} params.alias The ad unit deployment alias, e.g. `some-deployment-alias`.
21
+ */
22
+ getAdUnit({ name, alias } = {}) {
23
+ const foundAdUnit = getAsObject(this.adUnits, `${alias}.${name}`);
24
+ // Ensure ad unit is duplicated so property re-assignment doesn't "stick."
25
+ return {
26
+ uri: this.uri,
27
+ ...foundAdUnit,
28
+ };
29
+ }
30
+
31
+ /**
32
+ *
33
+ * @param {object} params
34
+ * @param {string} params.alias The ad unit deployment alias, e.g. `some-deployment-alias`.
35
+ * @param {string} params.name The ad unit name, e.g. `primary` or `header`, etc.
36
+ * @param {string} params.id The ad unit id, e.g. `5d4b04769f69b200013ab109`.
37
+ * @param {number} params.width The ad unit width.
38
+ * @param {number} params.height The ad unit height.
39
+ * @param {string} [params.uri] A custom delivery URI for this adunit
40
+ */
41
+ setAdUnit({
42
+ alias,
43
+ name,
44
+ id,
45
+ width,
46
+ height,
47
+ uri,
48
+ } = {}) {
49
+ if (!name || !alias || !id) throw new Error('Unable to create EmailX ad unit: the name, alias, and ID are required');
50
+ if (!width || !height) throw new Error('Unable to create EmailX ad unit: the height and width are required');
51
+ const adUnit = {
52
+ id,
53
+ width,
54
+ height,
55
+ name,
56
+ alias,
57
+ };
58
+ if (uri) adUnit.uri = uri;
59
+ set(this.adUnits, `${alias}.${name}`, adUnit);
60
+ return this;
61
+ }
62
+
63
+ /**
64
+ *
65
+ * @param {string} alias
66
+ * @param {object[]} definitions
67
+ */
68
+ setAdUnits(alias, definitions) {
69
+ asArray(definitions).forEach((definition) => {
70
+ this.setAdUnit({ ...definition, alias });
71
+ });
72
+ return this;
73
+ }
74
+
75
+ getUri() {
76
+ return this.uri;
77
+ }
78
+ }
79
+
80
+ module.exports = EmailXConfiguration;
package/marko.json ADDED
@@ -0,0 +1,6 @@
1
+ {
2
+ "taglib-id": "@mindful-web/marko-newsletters-email-x",
3
+ "taglib-imports": [
4
+ "./components/marko.json"
5
+ ]
6
+ }
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "@mindful-web/marko-newsletters-email-x",
3
+ "version": "1.0.0",
4
+ "description": "EmailX Marko components for Mindful Web newsletters.",
5
+ "author": "Jacob Bare <jacob@parameter1.com>",
6
+ "license": "MIT",
7
+ "repository": "https://github.com/parameter1/mindful-web/tree/main/packages/marko-newsletters-email-x",
8
+ "scripts": {
9
+ "lint:fix": "yarn lint --fix",
10
+ "lint": "eslint --ext .js --max-warnings 5 ./",
11
+ "compile": "mindful-web-marko-compile compile",
12
+ "prepublish": "yarn compile --silent",
13
+ "test": "yarn compile --no-clean && yarn lint"
14
+ },
15
+ "dependencies": {
16
+ "@mindful-web/object-path": "^1.0.0",
17
+ "@mindful-web/utils": "^1.0.0",
18
+ "moment": "^2.29.1",
19
+ "node-fetch": "^2.6.9"
20
+ },
21
+ "peerDependencies": {
22
+ "@mindful-web/marko-core": "^0.0.0"
23
+ },
24
+ "publishConfig": {
25
+ "access": "public"
26
+ },
27
+ "gitHead": "0b77cab713eb5841202bb86c7119949866bc68b5"
28
+ }
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Builds the EmailX ad unit query string.
3
+ *
4
+ * @param {object} params
5
+ * @param {moment} params.momentDate The moment date of the deployment
6
+ * @param {string} params.email The email address query var
7
+ * @param {string} [param.send] The send corrlator var
8
+ * @param {bool} [param.includeAdvertiser] Flag indicating if the advertiser data should be
9
+ * returned in the response. Defaults to false.
10
+ */
11
+ module.exports = ({
12
+ momentDate,
13
+ email,
14
+ send,
15
+ includeAdvertiser = false,
16
+ } = {}) => {
17
+ const rand = Math.floor(Math.random() * 100000000);
18
+ return {
19
+ date: momentDate.format('YYYY-MM-DDTHH:mm:ssZ'),
20
+ rand,
21
+ email,
22
+ send,
23
+ ...(includeAdvertiser && { incAdv: true }),
24
+ };
25
+ };
@@ -0,0 +1,20 @@
1
+ const { asArray, asObject } = require('@mindful-web/utils');
2
+
3
+ module.exports = ({
4
+ uri,
5
+ action,
6
+ id,
7
+ query,
8
+ decodedParams,
9
+ } = {}) => {
10
+ const q = asObject(query);
11
+ const noEncode = asArray(decodedParams);
12
+ const search = Object.keys(q).reduce((pairs, key) => {
13
+ const v = q[key];
14
+ if (!v) return pairs;
15
+ const value = noEncode.includes(key) ? v : encodeURIComponent(v);
16
+ pairs.push(`${encodeURIComponent(key)}=${value}`);
17
+ return pairs;
18
+ }, []).join('&');
19
+ return `${uri}/${action}/${id}?${search}`;
20
+ };
@@ -0,0 +1,68 @@
1
+ const fetch = require('node-fetch');
2
+ const moment = require('moment');
3
+ const { asArray } = require('@mindful-web/utils');
4
+ const buildQuery = require('./build-query');
5
+ const buildUrl = require('./build-url');
6
+
7
+ /**
8
+ * Retrieves ad unit data from the EmailX delivery service
9
+ *
10
+ * @param {object} params
11
+ * @param {string} params.serveUri The EmailX base URI, e.g. https://ebm.serve.email-x.io
12
+ * @param {string} params.adUnitId The ad unit to request data for
13
+ * @param {Date} params.date The newsletter deployment date
14
+ * @param {string} params.email The email address for delivery correlation
15
+ * @param {string} [params.send] The send param for delivery correlation
16
+ * @param {string[]} [params.decodedParams] Params to leave unencoded when build the query string
17
+ * @param {object} [params.fetchOptions] Optional params to send to fetch
18
+ */
19
+ module.exports = async ({
20
+ serveUri,
21
+ adUnitId,
22
+ date,
23
+ email,
24
+ send,
25
+ includeAdvertiser = false,
26
+ decodedParams,
27
+ fetchOptions,
28
+ } = {}) => {
29
+ if (!serveUri) throw new Error('The EmailX serveUri param is required.');
30
+ if (!email) throw new Error('The EmailX email param is required.');
31
+ if (!adUnitId || !/^[a-f0-9]{24}$/i.test(adUnitId)) throw new Error(`The provided EmailX adUnitId '${adUnitId}' is invalid.`);
32
+ const momentDate = moment(date);
33
+ if (!momentDate.isValid()) throw new Error(`The provided EmailX date '${date}' is invalid.`);
34
+
35
+ const query = buildQuery({
36
+ momentDate,
37
+ email,
38
+ send,
39
+ includeAdvertiser,
40
+ });
41
+
42
+ const buildParams = {
43
+ uri: serveUri,
44
+ id: adUnitId,
45
+ query,
46
+ decodedParams: asArray(decodedParams),
47
+ };
48
+
49
+ const url = buildUrl({ action: 'data', ...buildParams });
50
+ const clickHref = buildUrl({ action: 'click', ...buildParams });
51
+ const imageSrc = buildUrl({ action: 'image', ...buildParams });
52
+
53
+ const res = await fetch(url, fetchOptions);
54
+ const { status } = res;
55
+ if (status === 204) return null;
56
+ if (!res.ok) {
57
+ const error = new Error(`Unable to load ad unit data: ${res.statusText} (${status})`);
58
+ error.res = res;
59
+ throw error;
60
+ }
61
+ const data = await res.json();
62
+ return {
63
+ fetchUrl: url,
64
+ clickHref,
65
+ imageSrc,
66
+ data,
67
+ };
68
+ };