@schibsted/sps-sdk 3.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/dist/cjs/actions/dismiss.d.ts +6 -0
- package/dist/cjs/actions/dismiss.js +102 -0
- package/dist/cjs/actions/dismiss.js.map +1 -0
- package/dist/cjs/apiClient.d.ts +89 -0
- package/dist/cjs/apiClient.js +98 -0
- package/dist/cjs/apiClient.js.map +1 -0
- package/dist/cjs/index.d.ts +17 -0
- package/dist/cjs/index.js +34 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/sps/endpoint.d.ts +7 -0
- package/dist/cjs/sps/endpoint.js +42 -0
- package/dist/cjs/sps/endpoint.js.map +1 -0
- package/dist/cjs/sps/placement.d.ts +62 -0
- package/dist/cjs/sps/placement.js +3 -0
- package/dist/cjs/sps/placement.js.map +1 -0
- package/dist/cjs/sps/publication.d.ts +23 -0
- package/dist/cjs/sps/publication.js +28 -0
- package/dist/cjs/sps/publication.js.map +1 -0
- package/dist/cjs/sps/queryBuilder.d.ts +21 -0
- package/dist/cjs/sps/queryBuilder.js +31 -0
- package/dist/cjs/sps/queryBuilder.js.map +1 -0
- package/dist/cjs/sps/rewrite-salesposter-variables.d.ts +35 -0
- package/dist/cjs/sps/rewrite-salesposter-variables.js +112 -0
- package/dist/cjs/sps/rewrite-salesposter-variables.js.map +1 -0
- package/dist/cjs/sps.d.ts +234 -0
- package/dist/cjs/sps.js +237 -0
- package/dist/cjs/sps.js.map +1 -0
- package/dist/cjs/tracking/create-tracking-params.d.ts +116 -0
- package/dist/cjs/tracking/create-tracking-params.js +62 -0
- package/dist/cjs/tracking/create-tracking-params.js.map +1 -0
- package/dist/cjs/tracking/poster-data-to-pulse-object.d.ts +22 -0
- package/dist/cjs/tracking/poster-data-to-pulse-object.js +32 -0
- package/dist/cjs/tracking/poster-data-to-pulse-object.js.map +1 -0
- package/dist/cjs/tracking/tracking-to-pulse-origin.d.ts +14 -0
- package/dist/cjs/tracking/tracking-to-pulse-origin.js +15 -0
- package/dist/cjs/tracking/tracking-to-pulse-origin.js.map +1 -0
- package/dist/cjs/tracking/tracking-to-search-params.d.ts +10 -0
- package/dist/cjs/tracking/tracking-to-search-params.js +26 -0
- package/dist/cjs/tracking/tracking-to-search-params.js.map +1 -0
- package/dist/cjs/utils/pulse.d.ts +15 -0
- package/dist/cjs/utils/pulse.js +27 -0
- package/dist/cjs/utils/pulse.js.map +1 -0
- package/dist/cjs/utils/pulseProxy.d.ts +30 -0
- package/dist/cjs/utils/pulseProxy.js +28 -0
- package/dist/cjs/utils/pulseProxy.js.map +1 -0
- package/dist/cjs/utils/source.d.ts +9 -0
- package/dist/cjs/utils/source.js +113 -0
- package/dist/cjs/utils/source.js.map +1 -0
- package/dist/cjs/utils/url.d.ts +3 -0
- package/dist/cjs/utils/url.js +16 -0
- package/dist/cjs/utils/url.js.map +1 -0
- package/dist/cjs/utils/utm.d.ts +19 -0
- package/dist/cjs/utils/utm.js +30 -0
- package/dist/cjs/utils/utm.js.map +1 -0
- package/dist/cjs/utils/xDomainId.d.ts +1 -0
- package/dist/cjs/utils/xDomainId.js +14 -0
- package/dist/cjs/utils/xDomainId.js.map +1 -0
- package/dist/esm/actions/dismiss.d.ts +6 -0
- package/dist/esm/actions/dismiss.js +96 -0
- package/dist/esm/actions/dismiss.js.map +1 -0
- package/dist/esm/apiClient.d.ts +89 -0
- package/dist/esm/apiClient.js +93 -0
- package/dist/esm/apiClient.js.map +1 -0
- package/dist/esm/index.d.ts +17 -0
- package/dist/esm/index.js +18 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/sps/endpoint.d.ts +7 -0
- package/dist/esm/sps/endpoint.js +37 -0
- package/dist/esm/sps/endpoint.js.map +1 -0
- package/dist/esm/sps/placement.d.ts +62 -0
- package/dist/esm/sps/placement.js +2 -0
- package/dist/esm/sps/placement.js.map +1 -0
- package/dist/esm/sps/publication.d.ts +23 -0
- package/dist/esm/sps/publication.js +25 -0
- package/dist/esm/sps/publication.js.map +1 -0
- package/dist/esm/sps/queryBuilder.d.ts +21 -0
- package/dist/esm/sps/queryBuilder.js +26 -0
- package/dist/esm/sps/queryBuilder.js.map +1 -0
- package/dist/esm/sps/rewrite-salesposter-variables.d.ts +35 -0
- package/dist/esm/sps/rewrite-salesposter-variables.js +106 -0
- package/dist/esm/sps/rewrite-salesposter-variables.js.map +1 -0
- package/dist/esm/sps.d.ts +234 -0
- package/dist/esm/sps.js +234 -0
- package/dist/esm/sps.js.map +1 -0
- package/dist/esm/tracking/create-tracking-params.d.ts +116 -0
- package/dist/esm/tracking/create-tracking-params.js +59 -0
- package/dist/esm/tracking/create-tracking-params.js.map +1 -0
- package/dist/esm/tracking/poster-data-to-pulse-object.d.ts +22 -0
- package/dist/esm/tracking/poster-data-to-pulse-object.js +29 -0
- package/dist/esm/tracking/poster-data-to-pulse-object.js.map +1 -0
- package/dist/esm/tracking/tracking-to-pulse-origin.d.ts +14 -0
- package/dist/esm/tracking/tracking-to-pulse-origin.js +12 -0
- package/dist/esm/tracking/tracking-to-pulse-origin.js.map +1 -0
- package/dist/esm/tracking/tracking-to-search-params.d.ts +10 -0
- package/dist/esm/tracking/tracking-to-search-params.js +23 -0
- package/dist/esm/tracking/tracking-to-search-params.js.map +1 -0
- package/dist/esm/utils/pulse.d.ts +15 -0
- package/dist/esm/utils/pulse.js +24 -0
- package/dist/esm/utils/pulse.js.map +1 -0
- package/dist/esm/utils/pulseProxy.d.ts +30 -0
- package/dist/esm/utils/pulseProxy.js +24 -0
- package/dist/esm/utils/pulseProxy.js.map +1 -0
- package/dist/esm/utils/source.d.ts +9 -0
- package/dist/esm/utils/source.js +108 -0
- package/dist/esm/utils/source.js.map +1 -0
- package/dist/esm/utils/url.d.ts +3 -0
- package/dist/esm/utils/url.js +10 -0
- package/dist/esm/utils/url.js.map +1 -0
- package/dist/esm/utils/utm.d.ts +19 -0
- package/dist/esm/utils/utm.js +27 -0
- package/dist/esm/utils/utm.js.map +1 -0
- package/dist/esm/utils/xDomainId.d.ts +1 -0
- package/dist/esm/utils/xDomainId.js +10 -0
- package/dist/esm/utils/xDomainId.js.map +1 -0
- package/dist/public-scripts/pulse-tracker-proxy.v0.1.0.min.js +1 -0
- package/dist/public-scripts/sps-pulse-helpers.v0.2.0.min.js +1 -0
- package/package.json +67 -0
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.rewriteJsonPosterVariables = exports.rewritePlaceholderVariables = void 0;
|
|
4
|
+
exports.appendTrackingParams = appendTrackingParams;
|
|
5
|
+
const url_1 = require("../utils/url.js");
|
|
6
|
+
const pulseProxy_1 = require("../utils/pulseProxy.js");
|
|
7
|
+
const dismiss_1 = require("../actions/dismiss.js");
|
|
8
|
+
/**
|
|
9
|
+
* Enriches the salesposter html links with the correct parameters
|
|
10
|
+
* @returns StructuredResponse with the updated html
|
|
11
|
+
*/
|
|
12
|
+
const rewritePlaceholderVariables = (content, { redirectUrl, loginUrl, searchParams, appendSearchParams = true, }, publication, environment, placementType, pulseTrackingParams) => {
|
|
13
|
+
const htmlContent = new DOMParser().parseFromString(content, "text/html");
|
|
14
|
+
const vevJSON = htmlContent.querySelector("script[type='text/vev']");
|
|
15
|
+
const links = htmlContent.querySelectorAll("a");
|
|
16
|
+
links.forEach((link, index) => {
|
|
17
|
+
const href = link.getAttribute("href") || "";
|
|
18
|
+
const name = link.getAttribute("data-track-id") ||
|
|
19
|
+
`${link.textContent.trim()}_${index + 1}` ||
|
|
20
|
+
`link_${index + 1}`;
|
|
21
|
+
const searchParamsForLink = new URLSearchParams(searchParams.toString());
|
|
22
|
+
searchParamsForLink.set("offer_cta", (0, url_1.sanitizeQueryParam)(`${name.trim()}`));
|
|
23
|
+
const VARIABLES = {
|
|
24
|
+
redirect_uri_url_encoded: redirectUrl ? encodeURIComponent(redirectUrl) : "",
|
|
25
|
+
loginUrl: loginUrl ?? "",
|
|
26
|
+
};
|
|
27
|
+
let updatedHref = replaceVariables(href, VARIABLES);
|
|
28
|
+
if (appendSearchParams) {
|
|
29
|
+
updatedHref = appendSearchParamsToUrl(updatedHref, searchParamsForLink);
|
|
30
|
+
}
|
|
31
|
+
link.setAttribute("href", updatedHref);
|
|
32
|
+
// Update link in VEV JSON if present
|
|
33
|
+
if (vevJSON?.textContent) {
|
|
34
|
+
vevJSON.textContent = vevJSON.textContent.replaceAll(href, updatedHref);
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
if (pulseTrackingParams) {
|
|
38
|
+
(0, pulseProxy_1.injectPulseProxyScripts)(htmlContent.head, pulseTrackingParams);
|
|
39
|
+
}
|
|
40
|
+
const dismissElements = htmlContent.querySelectorAll("[data-dismiss-on-click]");
|
|
41
|
+
if (dismissElements.length > 0) {
|
|
42
|
+
const script = htmlContent.createElement("script");
|
|
43
|
+
script.textContent = (0, dismiss_1.getDismissScript)(publication, environment, placementType);
|
|
44
|
+
htmlContent.head.appendChild(script);
|
|
45
|
+
}
|
|
46
|
+
// Read inside body as DOMParser adds html and body tags automatically
|
|
47
|
+
return htmlContent.head.innerHTML + htmlContent.body.innerHTML;
|
|
48
|
+
};
|
|
49
|
+
exports.rewritePlaceholderVariables = rewritePlaceholderVariables;
|
|
50
|
+
const appendSearchParamsToUrl = (href, searchParams) => {
|
|
51
|
+
// Try to parse the href as a URL. If it fails, return the original href
|
|
52
|
+
try {
|
|
53
|
+
const url = new URL(href);
|
|
54
|
+
// Merge provided search params into the existing URL search params
|
|
55
|
+
searchParams.forEach((v, k) => {
|
|
56
|
+
url.searchParams.set(k, v);
|
|
57
|
+
});
|
|
58
|
+
return url.toString();
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
return href;
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
const rewriteJsonPosterVariables = (content, { searchParams, appendSearchParams = true, }) => {
|
|
65
|
+
if (!appendSearchParams) {
|
|
66
|
+
return content;
|
|
67
|
+
}
|
|
68
|
+
if (Array.isArray(content)) {
|
|
69
|
+
return content.map((item) => {
|
|
70
|
+
const result = {};
|
|
71
|
+
Object.entries(item).forEach(([k, v]) => {
|
|
72
|
+
result[k] = appendSearchParamsToUrl(v, searchParams);
|
|
73
|
+
});
|
|
74
|
+
return result;
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
const result = {};
|
|
78
|
+
Object.entries(content).forEach(([k, v]) => {
|
|
79
|
+
result[k] = appendSearchParamsToUrl(v, searchParams);
|
|
80
|
+
});
|
|
81
|
+
return result;
|
|
82
|
+
};
|
|
83
|
+
exports.rewriteJsonPosterVariables = rewriteJsonPosterVariables;
|
|
84
|
+
/**
|
|
85
|
+
* Rewrite values of a Record<string, string> that
|
|
86
|
+
* are URL-like, adding all tracking params.
|
|
87
|
+
*/
|
|
88
|
+
function appendTrackingParams(jsonContentItem, searchParams) {
|
|
89
|
+
const result = {};
|
|
90
|
+
Object.entries(jsonContentItem).forEach(([key, value]) => {
|
|
91
|
+
try {
|
|
92
|
+
const url = new URL(value);
|
|
93
|
+
// Copy the tracking params
|
|
94
|
+
const params = new URLSearchParams(searchParams);
|
|
95
|
+
// Set all params on URL
|
|
96
|
+
params.forEach((v, k) => url.searchParams.set(k, v));
|
|
97
|
+
result[key] = url.toString();
|
|
98
|
+
}
|
|
99
|
+
catch {
|
|
100
|
+
// Not a valid URL → leave value as-is
|
|
101
|
+
result[key] = value;
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
return result;
|
|
105
|
+
}
|
|
106
|
+
const replaceVariables = (value, variables) => {
|
|
107
|
+
if (!variables) {
|
|
108
|
+
return value;
|
|
109
|
+
}
|
|
110
|
+
return Object.entries(variables).reduce((result, [key, replacement]) => String(result).replaceAll(`{{${key}}}`, replacement), String(value));
|
|
111
|
+
};
|
|
112
|
+
//# sourceMappingURL=rewrite-salesposter-variables.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{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terByDecisionParams, type PulseOrigin, type PulseObject, type PlacementTypeWithOptionalProps } from "./index.js";
|
|
2
|
+
import { type ContentType, type TrackingData, type TrackingParamsProps } from "./tracking/create-tracking-params.js";
|
|
3
|
+
import { SpsPublication } from "./sps/publication.js";
|
|
4
|
+
import { type SPSPosterResponse } from "./apiClient.js";
|
|
5
|
+
import type { PlacementType, PlacementTypeWithOptionalFallback, PlacementTypeWithRequiredFallback } from "./sps/placement.js";
|
|
6
|
+
/**
|
|
7
|
+
* The full poster response enriched with segment names, tracking data, and Pulse event objects.
|
|
8
|
+
* Returned by {@link SPS.getPoster} and {@link SPS.getPosterByDecision}.
|
|
9
|
+
* @typeParam T - The placement type
|
|
10
|
+
*/
|
|
11
|
+
export type Poster<T extends PlacementType> = SPSPosterResponse<T> & {
|
|
12
|
+
segmentNames: string[];
|
|
13
|
+
trackingData: TrackingData;
|
|
14
|
+
pulseOrigin: PulseOrigin;
|
|
15
|
+
pulseObject: PulseObject;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* The decision result from SPS, used to subsequently fetch poster content via
|
|
19
|
+
* {@link SPS.getPosterByDecision}.
|
|
20
|
+
* @typeParam T - The placement type
|
|
21
|
+
*/
|
|
22
|
+
export type Decision<T extends PlacementType> = GetPosterByDecisionParams & {
|
|
23
|
+
placementType: T;
|
|
24
|
+
articleType?: string;
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Configuration options for the {@link SPS} constructor.
|
|
28
|
+
*/
|
|
29
|
+
export type SpsOptions = {
|
|
30
|
+
/** The SPS publication to fetch content from. */
|
|
31
|
+
publication: SpsPublication;
|
|
32
|
+
/**
|
|
33
|
+
* The domain you are requesting from. Must be set for correct cookie forwarding.
|
|
34
|
+
* @defaultValue `"schibsted.tech"`
|
|
35
|
+
*/
|
|
36
|
+
domain?: string;
|
|
37
|
+
/**
|
|
38
|
+
* Determines which SPS endpoint is called.
|
|
39
|
+
* @defaultValue `"production"`
|
|
40
|
+
*/
|
|
41
|
+
environment?: Environment;
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* Options for {@link SPS.getPoster} and {@link SPS.getPosterByDecision}.
|
|
45
|
+
* @typeParam T - The placement type
|
|
46
|
+
*/
|
|
47
|
+
export type SpsRequestOptions<T extends PlacementType> = {
|
|
48
|
+
/** Tracking context for the content item on which the salesposter is displayed. */
|
|
49
|
+
tracking?: Omit<TrackingParamsProps, "window" | "document" | "contentType"> & {
|
|
50
|
+
contentType?: ContentType;
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* Callback that returns the value substituted for the `{{loginUrl}}` placeholder in the poster HTML.
|
|
54
|
+
* Receives the raw SPS poster response as its argument.
|
|
55
|
+
*/
|
|
56
|
+
generateLoginUrl?: (poster: SPSPosterResponse<T>) => Promise<string> | string;
|
|
57
|
+
/**
|
|
58
|
+
* Callback that returns the value substituted for the `{{redirect_uri_url_encoded}}` placeholder.
|
|
59
|
+
* @defaultValue Current page URL
|
|
60
|
+
*/
|
|
61
|
+
generateRedirectUrl?: (poster: SPSPosterResponse<T>) => Promise<string> | string;
|
|
62
|
+
/**
|
|
63
|
+
* Fallback poster to render when SPS returns an error or an invalid response.
|
|
64
|
+
* Also used when `showFallbackPosterOnNoContent` is `true` and SPS returns no content.
|
|
65
|
+
*/
|
|
66
|
+
fallbackPoster?: SPSPosterResponse<T>;
|
|
67
|
+
/** When `true`, renders the `fallbackPoster` even when SPS returns no content. */
|
|
68
|
+
showFallbackPosterOnNoContent?: boolean;
|
|
69
|
+
/**
|
|
70
|
+
* Whether to append tracking search params to links inside the poster content.
|
|
71
|
+
* @defaultValue `true`
|
|
72
|
+
*/
|
|
73
|
+
appendSearchParams?: boolean;
|
|
74
|
+
/** When true, disables pulse proxy tracking scripts (default true). Set to false to enable automatic pulse tracking. */
|
|
75
|
+
disablePulseTracking?: boolean;
|
|
76
|
+
};
|
|
77
|
+
/**
|
|
78
|
+
* Variant of {@link SpsRequestOptions} that requires a `fallbackPoster` and forces
|
|
79
|
+
* `showFallbackPosterOnNoContent` to `true`, guaranteeing a non-null poster is returned.
|
|
80
|
+
* @typeParam T - The placement type
|
|
81
|
+
*/
|
|
82
|
+
export type SpsRequestOptionsWithFallback<T extends PlacementType> = SpsRequestOptions<T> & {
|
|
83
|
+
fallbackPoster: SPSPosterResponse<T>;
|
|
84
|
+
showFallbackPosterOnNoContent: true;
|
|
85
|
+
};
|
|
86
|
+
/**
|
|
87
|
+
* Duration strings controlling how long a dismissed placement stays hidden.
|
|
88
|
+
*/
|
|
89
|
+
export type DismissTimeOptions = "1day" | "2days" | "3days" | "4days" | "5days" | "6days" | "1week" | "2week" | "1month";
|
|
90
|
+
/**
|
|
91
|
+
* Options for the dismiss functionality, enabling a placement to be hidden when
|
|
92
|
+
* an element with `data-dismiss-on-click` is clicked inside the placement.
|
|
93
|
+
*/
|
|
94
|
+
interface DismissOptions {
|
|
95
|
+
/** The HTML `id` attribute of the iframe element containing the placement. */
|
|
96
|
+
elementId: string;
|
|
97
|
+
/** How long the placement should remain suppressed after being dismissed. */
|
|
98
|
+
dismissTime: DismissTimeOptions;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Mapping of placement types to their required parameters.
|
|
102
|
+
* Used to enforce correct parameter requirements for placements with unique needs.
|
|
103
|
+
* Placements not included in this map will accept any parameters.
|
|
104
|
+
*/
|
|
105
|
+
type PlacementTypeToParamsMap = {
|
|
106
|
+
bottombar: DefaultBottomBarParams;
|
|
107
|
+
salesposter: DefaultSalesposterParams;
|
|
108
|
+
};
|
|
109
|
+
/**
|
|
110
|
+
* Required parameters for the `salesposter` placement type.
|
|
111
|
+
*/
|
|
112
|
+
export type DefaultSalesposterParams = {
|
|
113
|
+
articleId: string;
|
|
114
|
+
articleType: string;
|
|
115
|
+
section: string;
|
|
116
|
+
tags: string[];
|
|
117
|
+
} & SpsParams;
|
|
118
|
+
/**
|
|
119
|
+
* Required parameters for the `bottombar` placement type.
|
|
120
|
+
*/
|
|
121
|
+
export type DefaultBottomBarParams = {
|
|
122
|
+
context: "article" | "frontpage";
|
|
123
|
+
} & SpsParams;
|
|
124
|
+
/**
|
|
125
|
+
* Overloaded call signatures for {@link SPS.getPoster}.
|
|
126
|
+
* When a `fallbackPoster` is provided together with `showFallbackPosterOnNoContent: true`,
|
|
127
|
+
* the return type is narrowed to a non-null `Poster<T>`.
|
|
128
|
+
*/
|
|
129
|
+
interface GetPoster {
|
|
130
|
+
<T extends PlacementTypeWithRequiredFallback, U extends ParamsFor<T> = ParamsFor<T>>(placementType: T, props: GetPosterProps<T, U> & SpsRequestOptionsWithFallback<T>, dismiss?: DismissOptions): Promise<Poster<T>>;
|
|
131
|
+
<T extends PlacementTypeWithOptionalFallback, U extends ParamsFor<T> = ParamsFor<T>>(placementType: T, props: GetPosterProps<T, U> & SpsRequestOptionsWithFallback<T>, dismiss?: DismissOptions): Promise<Poster<T>>;
|
|
132
|
+
<T extends PlacementTypeWithOptionalFallback, U extends ParamsFor<T> = ParamsFor<T>>(placementType: T, props: GetPosterProps<T, U> & SpsRequestOptions<T>, dismiss?: DismissOptions): Promise<Poster<T> | null>;
|
|
133
|
+
<T extends PlacementTypeWithOptionalProps, U extends ParamsFor<T> = ParamsFor<T>>(placementType: T, props?: GetPosterProps<T, U> & SpsRequestOptions<T>, dismiss?: DismissOptions): Promise<Poster<T> | null>;
|
|
134
|
+
}
|
|
135
|
+
interface GetDecision {
|
|
136
|
+
<T extends PlacementType, U extends ParamsFor<T> = ParamsFor<T>>(placementType: T, props: GetPosterProps<T, U>, userJwtToken?: string): Promise<Decision<T> | null>;
|
|
137
|
+
<T extends PlacementTypeWithOptionalProps, U extends ParamsFor<T> = ParamsFor<T>>(placementType: T, props?: GetPosterProps<T, U>, userJwtToken?: string): Promise<Decision<T> | null>;
|
|
138
|
+
}
|
|
139
|
+
type ParamsFor<T extends PlacementType> = T extends keyof PlacementTypeToParamsMap ? PlacementTypeToParamsMap[T] : SpsParams;
|
|
140
|
+
type GetPosterProps<T extends PlacementType, U extends ParamsFor<T> = ParamsFor<T>> = T extends keyof PlacementTypeToParamsMap ? {
|
|
141
|
+
params: U;
|
|
142
|
+
} : {
|
|
143
|
+
params?: U;
|
|
144
|
+
};
|
|
145
|
+
/**
|
|
146
|
+
* Overloaded call signatures for {@link SPS.getPosterByDecision}.
|
|
147
|
+
* When a `fallbackPoster` is provided together with `showFallbackPosterOnNoContent: true`,
|
|
148
|
+
* the return type is narrowed to a non-null `Poster<T>`.
|
|
149
|
+
*/
|
|
150
|
+
interface GetPosterByDecision {
|
|
151
|
+
<T extends PlacementTypeWithRequiredFallback>(decision: Omit<Decision<T>, "contentKey"> & {
|
|
152
|
+
contentKey: string;
|
|
153
|
+
}, props: SpsRequestOptionsWithFallback<T>, dismiss?: DismissOptions): Promise<Poster<T>>;
|
|
154
|
+
<T extends PlacementTypeWithOptionalFallback>(decision: Omit<Decision<T>, "contentKey"> & {
|
|
155
|
+
contentKey: string;
|
|
156
|
+
}, props: SpsRequestOptionsWithFallback<T>, dismiss?: DismissOptions): Promise<Poster<T>>;
|
|
157
|
+
<T extends PlacementTypeWithOptionalFallback>(decision: Omit<Decision<T>, "contentKey"> & {
|
|
158
|
+
contentKey: string;
|
|
159
|
+
}, props?: SpsRequestOptions<T>, dismiss?: DismissOptions): Promise<Poster<T> | null>;
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* JSON content type for placements that return structured data objects instead of raw HTML strings.
|
|
163
|
+
*/
|
|
164
|
+
export type JsonContent = Record<string, string>[] | Record<string, string>;
|
|
165
|
+
/**
|
|
166
|
+
* Main entry point for the SPS SDK. Provides methods to fetch posters and decisions
|
|
167
|
+
* from the SPS API for a given publication and environment.
|
|
168
|
+
*/
|
|
169
|
+
export declare class SPS {
|
|
170
|
+
/** The publication identifier used in SPS API requests. */
|
|
171
|
+
publication: string;
|
|
172
|
+
/** The resolved SPS API base endpoint URL. */
|
|
173
|
+
endpoint: string;
|
|
174
|
+
/** The active environment this instance targets. */
|
|
175
|
+
environment: Environment;
|
|
176
|
+
/** The CDN base URL for the active environment. */
|
|
177
|
+
cdnUrl: string;
|
|
178
|
+
/**
|
|
179
|
+
* Creates a new SPS SDK instance.
|
|
180
|
+
* @param options - Configuration options including publication, domain and environment.
|
|
181
|
+
*/
|
|
182
|
+
constructor({ publication, domain, environment }: SpsOptions);
|
|
183
|
+
private validateClientEnvironment;
|
|
184
|
+
private validateServerEnvironment;
|
|
185
|
+
/**
|
|
186
|
+
* Fetches a poster for the given placement type from the SPS API.
|
|
187
|
+
* Must be called from a browser environment.
|
|
188
|
+
*
|
|
189
|
+
* Resolves placeholder variables (`{{loginUrl}}`, `{{redirect_uri_url_encoded}}`) in the
|
|
190
|
+
* returned HTML content and injects Pulse proxy tracking scripts unless disabled.
|
|
191
|
+
*
|
|
192
|
+
* Returns `null` when SPS returns no content and no fallback is configured,
|
|
193
|
+
* or when the placement has been dismissed by the user.
|
|
194
|
+
*
|
|
195
|
+
* @param placementType - The SPS placement to request (e.g. `"salesposter"`, `"bottombar"`).
|
|
196
|
+
* @param props - Request options including decisioning params and tracking context.
|
|
197
|
+
* @param dismiss - Optional dismiss configuration for hiding the placement on user interaction.
|
|
198
|
+
* @returns The enriched poster response, or `null` when no content is available.
|
|
199
|
+
*/
|
|
200
|
+
getPoster: GetPoster;
|
|
201
|
+
/**
|
|
202
|
+
* Fetches a placement decision from the SPS API.
|
|
203
|
+
* Must be called from a **server** environment (no browser `window` or `document`).
|
|
204
|
+
*
|
|
205
|
+
* The returned {@link Decision} object can be passed directly to
|
|
206
|
+
* {@link SPS.getPosterByDecision} on the client side to fetch the actual poster content.
|
|
207
|
+
*
|
|
208
|
+
* Falls back to a `contentKey` of `"sps:not-working"` if the request fails,
|
|
209
|
+
* so that the caller can still invoke `getPosterByDecision` safely.
|
|
210
|
+
*
|
|
211
|
+
* @param placementType - The SPS placement to request a decision for.
|
|
212
|
+
* @param props - Optional decisioning params such as `accessCheck`, `pids`, and `source`.
|
|
213
|
+
* @param userJwtToken - JWT token identifying the current user for access-check decisioning.
|
|
214
|
+
* @returns A {@link Decision} object containing the resolved `contentKey`.
|
|
215
|
+
*/
|
|
216
|
+
getDecision: GetDecision;
|
|
217
|
+
/**
|
|
218
|
+
* Fetches poster content for a previously obtained {@link Decision}.
|
|
219
|
+
* Must be called from a browser environment.
|
|
220
|
+
*
|
|
221
|
+
* Resolves placeholder variables and injects Pulse proxy tracking scripts
|
|
222
|
+
* in the same way as {@link SPS.getPoster}.
|
|
223
|
+
*
|
|
224
|
+
* Returns `null` when the decision has no `contentKey`, the placement has been
|
|
225
|
+
* dismissed, or SPS returns no content without a fallback configured.
|
|
226
|
+
*
|
|
227
|
+
* @param decision - The decision object returned by {@link SPS.getDecision}.
|
|
228
|
+
* @param props - Request options including tracking context and optional fallback poster.
|
|
229
|
+
* @param dismiss - Optional dismiss configuration for hiding the placement on user interaction.
|
|
230
|
+
* @returns The enriched poster response, or `null` when no content is available.
|
|
231
|
+
*/
|
|
232
|
+
getPosterByDecision: GetPosterByDecision;
|
|
233
|
+
}
|
|
234
|
+
export {};
|
package/dist/cjs/sps.js
ADDED
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SPS = void 0;
|
|
4
|
+
const _1 = require("./index.js");
|
|
5
|
+
const create_tracking_params_1 = require("./tracking/create-tracking-params.js");
|
|
6
|
+
const rewrite_salesposter_variables_1 = require("./sps/rewrite-salesposter-variables.js");
|
|
7
|
+
const apiClient_1 = require("./apiClient.js");
|
|
8
|
+
const dismiss_1 = require("./actions/dismiss.js");
|
|
9
|
+
/**
|
|
10
|
+
* Main entry point for the SPS SDK. Provides methods to fetch posters and decisions
|
|
11
|
+
* from the SPS API for a given publication and environment.
|
|
12
|
+
*/
|
|
13
|
+
class SPS {
|
|
14
|
+
/** The publication identifier used in SPS API requests. */
|
|
15
|
+
publication;
|
|
16
|
+
/** The resolved SPS API base endpoint URL. */
|
|
17
|
+
endpoint;
|
|
18
|
+
/** The active environment this instance targets. */
|
|
19
|
+
environment;
|
|
20
|
+
/** The CDN base URL for the active environment. */
|
|
21
|
+
cdnUrl;
|
|
22
|
+
/**
|
|
23
|
+
* Creates a new SPS SDK instance.
|
|
24
|
+
* @param options - Configuration options including publication, domain and environment.
|
|
25
|
+
*/
|
|
26
|
+
constructor({ publication, domain = "schibsted.tech", environment = "production" }) {
|
|
27
|
+
this.environment = environment;
|
|
28
|
+
this.publication = publication;
|
|
29
|
+
this.endpoint = (0, _1.getEndpoint)(environment, domain);
|
|
30
|
+
this.cdnUrl = (0, _1.getCdnUrl)(environment);
|
|
31
|
+
}
|
|
32
|
+
async validateClientEnvironment() {
|
|
33
|
+
if (typeof window === "undefined" || typeof document === "undefined") {
|
|
34
|
+
throw new Error("This library can only be used in a browser environment with access to window and document.");
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
async validateServerEnvironment() {
|
|
38
|
+
if (typeof window !== "undefined" || typeof document !== "undefined") {
|
|
39
|
+
throw new Error("This library can only be used in a server environment");
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Fetches a poster for the given placement type from the SPS API.
|
|
44
|
+
* Must be called from a browser environment.
|
|
45
|
+
*
|
|
46
|
+
* Resolves placeholder variables (`{{loginUrl}}`, `{{redirect_uri_url_encoded}}`) in the
|
|
47
|
+
* returned HTML content and injects Pulse proxy tracking scripts unless disabled.
|
|
48
|
+
*
|
|
49
|
+
* Returns `null` when SPS returns no content and no fallback is configured,
|
|
50
|
+
* or when the placement has been dismissed by the user.
|
|
51
|
+
*
|
|
52
|
+
* @param placementType - The SPS placement to request (e.g. `"salesposter"`, `"bottombar"`).
|
|
53
|
+
* @param props - Request options including decisioning params and tracking context.
|
|
54
|
+
* @param dismiss - Optional dismiss configuration for hiding the placement on user interaction.
|
|
55
|
+
* @returns The enriched poster response, or `null` when no content is available.
|
|
56
|
+
*/
|
|
57
|
+
getPoster = async (placementType, props = {}, dismissOptions) => {
|
|
58
|
+
await this.validateClientEnvironment();
|
|
59
|
+
if (dismissOptions && (0, dismiss_1.isDismissed)(placementType, dismissOptions.elementId)) {
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
const poster = await (0, apiClient_1.fetchPoster)(`${this.endpoint}/${this.publication}/${placementType}`, props.params || {}, props.fallbackPoster);
|
|
63
|
+
if (!poster) {
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
const posterResponse = poster.posterResponse;
|
|
67
|
+
const { trackingData, pulseOrigin, pulseObject, searchParams, offerData } = await (0, create_tracking_params_1.createTrackingParams)({
|
|
68
|
+
contentId: props.tracking?.contentId || "",
|
|
69
|
+
contentType: props.tracking?.contentType || "article",
|
|
70
|
+
window,
|
|
71
|
+
document,
|
|
72
|
+
...props.tracking,
|
|
73
|
+
}, {
|
|
74
|
+
placementType,
|
|
75
|
+
campaign: posterResponse.campaign,
|
|
76
|
+
articleType: props.params?.posterType ||
|
|
77
|
+
props.params?.articleType ||
|
|
78
|
+
undefined,
|
|
79
|
+
section: props.params?.section,
|
|
80
|
+
tags: props.params?.tags,
|
|
81
|
+
failReason: poster?.failReason,
|
|
82
|
+
abTestGroup: posterResponse.abTestGroup || undefined,
|
|
83
|
+
});
|
|
84
|
+
let content;
|
|
85
|
+
const generateLoginUrl = props.generateLoginUrl;
|
|
86
|
+
const generateRedirectUrl = props.generateRedirectUrl;
|
|
87
|
+
const { disablePulseTracking = true } = props;
|
|
88
|
+
if (typeof posterResponse.content === "string") {
|
|
89
|
+
content = (0, rewrite_salesposter_variables_1.rewritePlaceholderVariables)(posterResponse.content, {
|
|
90
|
+
loginUrl: (await generateLoginUrl?.(posterResponse)) ?? "",
|
|
91
|
+
redirectUrl: (await generateRedirectUrl?.(posterResponse)) ??
|
|
92
|
+
`${window.location.origin}${window.location.pathname}`,
|
|
93
|
+
searchParams,
|
|
94
|
+
appendSearchParams: props.appendSearchParams ?? true,
|
|
95
|
+
}, this.publication, this.environment, placementType, !disablePulseTracking
|
|
96
|
+
? {
|
|
97
|
+
pulseOrigin,
|
|
98
|
+
publication: this.publication,
|
|
99
|
+
placementType: placementType,
|
|
100
|
+
cdnUrl: this.cdnUrl,
|
|
101
|
+
offerData,
|
|
102
|
+
experiment: posterResponse.experiment,
|
|
103
|
+
}
|
|
104
|
+
: undefined);
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
content = (0, rewrite_salesposter_variables_1.rewriteJsonPosterVariables)(posterResponse.content, {
|
|
108
|
+
searchParams,
|
|
109
|
+
appendSearchParams: props.appendSearchParams ?? true,
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
if (dismissOptions) {
|
|
113
|
+
(0, dismiss_1.addDismiss)(placementType, dismissOptions.elementId, dismissOptions.dismissTime);
|
|
114
|
+
}
|
|
115
|
+
return {
|
|
116
|
+
...posterResponse,
|
|
117
|
+
segmentNames: posterResponse.segmentNames || [],
|
|
118
|
+
content,
|
|
119
|
+
trackingData,
|
|
120
|
+
pulseOrigin,
|
|
121
|
+
pulseObject,
|
|
122
|
+
};
|
|
123
|
+
};
|
|
124
|
+
/**
|
|
125
|
+
* Fetches a placement decision from the SPS API.
|
|
126
|
+
* Must be called from a **server** environment (no browser `window` or `document`).
|
|
127
|
+
*
|
|
128
|
+
* The returned {@link Decision} object can be passed directly to
|
|
129
|
+
* {@link SPS.getPosterByDecision} on the client side to fetch the actual poster content.
|
|
130
|
+
*
|
|
131
|
+
* Falls back to a `contentKey` of `"sps:not-working"` if the request fails,
|
|
132
|
+
* so that the caller can still invoke `getPosterByDecision` safely.
|
|
133
|
+
*
|
|
134
|
+
* @param placementType - The SPS placement to request a decision for.
|
|
135
|
+
* @param props - Optional decisioning params such as `accessCheck`, `pids`, and `source`.
|
|
136
|
+
* @param userJwtToken - JWT token identifying the current user for access-check decisioning.
|
|
137
|
+
* @returns A {@link Decision} object containing the resolved `contentKey`.
|
|
138
|
+
*/
|
|
139
|
+
getDecision = async (placementType, props = {}, userJwtToken) => {
|
|
140
|
+
this.validateServerEnvironment();
|
|
141
|
+
try {
|
|
142
|
+
const decisionResponse = await (0, apiClient_1.fetchDecision)(`${this.endpoint}/${this.publication}/${placementType}/decision`, props.params || {}, userJwtToken);
|
|
143
|
+
return {
|
|
144
|
+
...decisionResponse,
|
|
145
|
+
placementType,
|
|
146
|
+
articleType: props.params?.articleType || props.params?.posterType,
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
catch {
|
|
150
|
+
return {
|
|
151
|
+
contentKey: "sps:not-working",
|
|
152
|
+
placementType,
|
|
153
|
+
articleType: props.params?.articleType || props.params?.posterType,
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
/**
|
|
158
|
+
* Fetches poster content for a previously obtained {@link Decision}.
|
|
159
|
+
* Must be called from a browser environment.
|
|
160
|
+
*
|
|
161
|
+
* Resolves placeholder variables and injects Pulse proxy tracking scripts
|
|
162
|
+
* in the same way as {@link SPS.getPoster}.
|
|
163
|
+
*
|
|
164
|
+
* Returns `null` when the decision has no `contentKey`, the placement has been
|
|
165
|
+
* dismissed, or SPS returns no content without a fallback configured.
|
|
166
|
+
*
|
|
167
|
+
* @param decision - The decision object returned by {@link SPS.getDecision}.
|
|
168
|
+
* @param props - Request options including tracking context and optional fallback poster.
|
|
169
|
+
* @param dismiss - Optional dismiss configuration for hiding the placement on user interaction.
|
|
170
|
+
* @returns The enriched poster response, or `null` when no content is available.
|
|
171
|
+
*/
|
|
172
|
+
getPosterByDecision = async ({ placementType, articleType, contentKey, segmentNames }, props = {}, dismissOptions) => {
|
|
173
|
+
this.validateClientEnvironment();
|
|
174
|
+
if (!contentKey) {
|
|
175
|
+
return null;
|
|
176
|
+
}
|
|
177
|
+
if (dismissOptions && (0, dismiss_1.isDismissed)(placementType, dismissOptions.elementId)) {
|
|
178
|
+
return null;
|
|
179
|
+
}
|
|
180
|
+
const poster = await (0, apiClient_1.fetchPoster)(`${this.endpoint}/poster/${encodeURIComponent(contentKey)}`, {}, props.fallbackPoster);
|
|
181
|
+
if (!poster) {
|
|
182
|
+
return null;
|
|
183
|
+
}
|
|
184
|
+
const { trackingData, pulseOrigin, pulseObject, searchParams, offerData } = await (0, create_tracking_params_1.createTrackingParams)({
|
|
185
|
+
contentId: props.tracking?.contentId || "",
|
|
186
|
+
contentType: props.tracking?.contentType || "article",
|
|
187
|
+
...props.tracking,
|
|
188
|
+
window,
|
|
189
|
+
document,
|
|
190
|
+
}, {
|
|
191
|
+
placementType,
|
|
192
|
+
campaign: poster.posterResponse.campaign,
|
|
193
|
+
failReason: poster?.failReason,
|
|
194
|
+
articleType,
|
|
195
|
+
abTestGroup: poster.posterResponse.abTestGroup || undefined,
|
|
196
|
+
});
|
|
197
|
+
let content = poster.posterResponse.content;
|
|
198
|
+
const { disablePulseTracking = true } = props;
|
|
199
|
+
if (typeof poster.posterResponse.content === "string") {
|
|
200
|
+
content = (0, rewrite_salesposter_variables_1.rewritePlaceholderVariables)(poster.posterResponse.content, {
|
|
201
|
+
loginUrl: (await props.generateLoginUrl?.(poster.posterResponse)) ?? "",
|
|
202
|
+
redirectUrl: (await props.generateRedirectUrl?.(poster.posterResponse)) ??
|
|
203
|
+
window.location.origin + window.location.pathname,
|
|
204
|
+
searchParams,
|
|
205
|
+
appendSearchParams: props.appendSearchParams ?? true,
|
|
206
|
+
}, this.publication, this.environment, placementType, !disablePulseTracking
|
|
207
|
+
? {
|
|
208
|
+
pulseOrigin,
|
|
209
|
+
publication: this.publication,
|
|
210
|
+
placementType,
|
|
211
|
+
cdnUrl: this.cdnUrl,
|
|
212
|
+
offerData,
|
|
213
|
+
experiment: poster.posterResponse.experiment,
|
|
214
|
+
}
|
|
215
|
+
: undefined);
|
|
216
|
+
}
|
|
217
|
+
else {
|
|
218
|
+
content = (0, rewrite_salesposter_variables_1.rewriteJsonPosterVariables)(poster.posterResponse.content, {
|
|
219
|
+
searchParams,
|
|
220
|
+
appendSearchParams: props.appendSearchParams ?? true,
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
if (dismissOptions) {
|
|
224
|
+
(0, dismiss_1.addDismiss)(placementType, dismissOptions.elementId, dismissOptions.dismissTime);
|
|
225
|
+
}
|
|
226
|
+
return {
|
|
227
|
+
...poster.posterResponse,
|
|
228
|
+
segmentNames,
|
|
229
|
+
content,
|
|
230
|
+
trackingData,
|
|
231
|
+
pulseOrigin,
|
|
232
|
+
pulseObject,
|
|
233
|
+
};
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
exports.SPS = SPS;
|
|
237
|
+
//# sourceMappingURL=sps.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sps.js","sourceRoot":"","sources":["../../src/sps.ts"],"names":[],"mappings":";;;AAAA,wBAUW;AACX,8EAK2C;AAC3C,uFAG6C;AAE7C,2CAAiF;AAMjF,+CAA4D;AA6N5D;;;GAGG;AACH,MAAa,GAAG;IACd,2DAA2D;IAC3D,WAAW,CAAS;IACpB,8CAA8C;IAC9C,QAAQ,CAAS;IACjB,oDAAoD;IACpD,WAAW,CAAc;IACzB,mDAAmD;IACnD,MAAM,CAAS;IAEf;;;OAGG;IACH,YAAY,EAAE,WAAW,EAAE,MAAM,GAAG,gBAAgB,EAAE,WAAW,GAAG,YAAY,EAAc;QAC5F,IAAI,CAAC,WAAW,GAAG,WAAW,CAAC;QAC/B,IAAI,CAAC,WAAW,GAAG,WAAW,CAAC;QAC/B,IAAI,CAAC,QAAQ,GAAG,IAAA,cAAW,EAAC,WAAW,EAAE,MAAM,CAAC,CAAC;QACjD,IAAI,CAAC,MAAM,GAAG,IAAA,YAAS,EAAC,WAAW,CAAC,CAAC;IACvC,CAAC;IAEO,KAAK,CAAC,yBAAyB;QACrC,IAAI,OAAO,MAAM,KAAK,WAAW,IAAI,OAAO,QAAQ,KAAK,WAAW,EAAE,CAAC;YACrE,MAAM,IAAI,KAAK,CACb,4FAA4F,CAC7F,CAAC;Q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