@financial-times/privacy-legislation-client 2.0.0 → 2.0.3

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/main.cjs ADDED
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const API_URL = "https://privacy.ft.com/api/v1";
4
+ const API_ENDPOINTS = {
5
+ complianceRegion: "/compliance-region.json"
6
+ };
7
+ function formatResponse(obj) {
8
+ return {
9
+ region: obj.region,
10
+ legislation: new Set(obj.legislation.split(","))
11
+ };
12
+ }
13
+ function sessionStorageOk() {
14
+ const test = "test";
15
+ try {
16
+ sessionStorage.setItem(test, test);
17
+ sessionStorage.removeItem(test);
18
+ return true;
19
+ } catch (e) {
20
+ return false;
21
+ }
22
+ }
23
+ function getHostname(referrer) {
24
+ if (referrer) {
25
+ try {
26
+ return new URL(referrer).hostname;
27
+ } catch (err) {
28
+ return referrer;
29
+ }
30
+ }
31
+ return typeof window === "undefined" ? "" : window.location.hostname;
32
+ }
33
+ async function fetchLegislation() {
34
+ const complianceStr = sessionStorageOk() && sessionStorage.getItem("user-compliance");
35
+ const complianceObj = complianceStr && JSON.parse(complianceStr);
36
+ if (complianceObj) {
37
+ return Promise.resolve(formatResponse(complianceObj));
38
+ } else {
39
+ const url = `${API_URL}${API_ENDPOINTS.complianceRegion}`;
40
+ const res = await fetch(url);
41
+ const compliance = await (res.ok ? res.json() : Promise.reject(res));
42
+ if (sessionStorageOk()) {
43
+ sessionStorage.setItem("user-compliance", JSON.stringify(compliance));
44
+ }
45
+ return formatResponse(compliance);
46
+ }
47
+ }
48
+ function buildConsentPageUrl({
49
+ url,
50
+ legislation,
51
+ referrer
52
+ }) {
53
+ const decoratedUrl = new URL(url);
54
+ const hostname = getHostname(referrer);
55
+ for (const id of legislation) {
56
+ decoratedUrl.searchParams.append("legislation", id);
57
+ }
58
+ if (hostname) {
59
+ decoratedUrl.searchParams.set("referrer", hostname.toLowerCase());
60
+ }
61
+ return decoratedUrl.toString();
62
+ }
63
+ exports.API_ENDPOINTS = API_ENDPOINTS;
64
+ exports.API_URL = API_URL;
65
+ exports.buildConsentPageUrl = buildConsentPageUrl;
66
+ exports.fetchLegislation = fetchLegislation;
package/dist/main.d.ts ADDED
@@ -0,0 +1,32 @@
1
+ export declare const API_ENDPOINTS: {
2
+ complianceRegion: string;
3
+ };
4
+
5
+ export declare const API_URL = "https://privacy.ft.com/api/v1";
6
+
7
+ /**
8
+ * Embed legislationIds and referrer as params to `url`
9
+ * Allows customisation (e.g. skinning, granularity of consent, etc) of the destination page
10
+ *
11
+ * This method provides a standardised way to interface with x-privacy-manager
12
+ */
13
+ export declare function buildConsentPageUrl({ url, legislation, referrer, }: {
14
+ url: string;
15
+ legislation: Set<string>;
16
+ referrer?: string;
17
+ }): string;
18
+
19
+ /**
20
+ * Return a promise resolving to an object containing information about the
21
+ * user's legal protections: *
22
+ * - region: A string consisting of a country code or country-region ("GB", "US-CA")
23
+ * - legislation: A Set of the applicable legislationIds
24
+ */
25
+ export declare function fetchLegislation(): Promise<RegionalCompliance>;
26
+
27
+ export declare type RegionalCompliance = {
28
+ region: string;
29
+ legislation: Set<string>;
30
+ };
31
+
32
+ export { }
package/dist/main.js ADDED
@@ -0,0 +1,66 @@
1
+ const API_URL = "https://privacy.ft.com/api/v1";
2
+ const API_ENDPOINTS = {
3
+ complianceRegion: "/compliance-region.json"
4
+ };
5
+ function formatResponse(obj) {
6
+ return {
7
+ region: obj.region,
8
+ legislation: new Set(obj.legislation.split(","))
9
+ };
10
+ }
11
+ function sessionStorageOk() {
12
+ const test = "test";
13
+ try {
14
+ sessionStorage.setItem(test, test);
15
+ sessionStorage.removeItem(test);
16
+ return true;
17
+ } catch (e) {
18
+ return false;
19
+ }
20
+ }
21
+ function getHostname(referrer) {
22
+ if (referrer) {
23
+ try {
24
+ return new URL(referrer).hostname;
25
+ } catch (err) {
26
+ return referrer;
27
+ }
28
+ }
29
+ return typeof window === "undefined" ? "" : window.location.hostname;
30
+ }
31
+ async function fetchLegislation() {
32
+ const complianceStr = sessionStorageOk() && sessionStorage.getItem("user-compliance");
33
+ const complianceObj = complianceStr && JSON.parse(complianceStr);
34
+ if (complianceObj) {
35
+ return Promise.resolve(formatResponse(complianceObj));
36
+ } else {
37
+ const url = `${API_URL}${API_ENDPOINTS.complianceRegion}`;
38
+ const res = await fetch(url);
39
+ const compliance = await (res.ok ? res.json() : Promise.reject(res));
40
+ if (sessionStorageOk()) {
41
+ sessionStorage.setItem("user-compliance", JSON.stringify(compliance));
42
+ }
43
+ return formatResponse(compliance);
44
+ }
45
+ }
46
+ function buildConsentPageUrl({
47
+ url,
48
+ legislation,
49
+ referrer
50
+ }) {
51
+ const decoratedUrl = new URL(url);
52
+ const hostname = getHostname(referrer);
53
+ for (const id of legislation) {
54
+ decoratedUrl.searchParams.append("legislation", id);
55
+ }
56
+ if (hostname) {
57
+ decoratedUrl.searchParams.set("referrer", hostname.toLowerCase());
58
+ }
59
+ return decoratedUrl.toString();
60
+ }
61
+ export {
62
+ API_ENDPOINTS,
63
+ API_URL,
64
+ buildConsentPageUrl,
65
+ fetchLegislation
66
+ };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@financial-times/privacy-legislation-client",
3
3
  "description": "",
4
- "version": "2.0.0",
4
+ "version": "2.0.3",
5
5
  "private": false,
6
6
  "type": "module",
7
7
  "types": "./dist/main.d.ts",