@financial-times/privacy-legislation-client 0.0.0-beta.1
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/README.md +45 -0
- package/dist/main.cjs +66 -0
- package/dist/main.d.ts +32 -0
- package/dist/main.js +66 -0
- package/package.json +32 -0
package/README.md
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# Privacy Legislation Client
|
|
2
|
+
|
|
3
|
+
## What it is
|
|
4
|
+
|
|
5
|
+
A module that reports which privacy-related legislation applies in the user's current
|
|
6
|
+
location.
|
|
7
|
+
|
|
8
|
+
Available for both server and client: see [example](../../examples/legislation-client/README.md)
|
|
9
|
+
|
|
10
|
+
## Usage
|
|
11
|
+
|
|
12
|
+
Typical usage would be to add the module to your app, call the imported method
|
|
13
|
+
and then update the UI in line with any applicable legal requirements:
|
|
14
|
+
|
|
15
|
+
```js
|
|
16
|
+
import { fetchLegislation, buildConsentPageUrl } from "@financial-times/privacy-legislation-client";
|
|
17
|
+
|
|
18
|
+
function onPageLoad() {
|
|
19
|
+
/**
|
|
20
|
+
* `legislation`: a Set of applicable laws: "ccpa", "gdpr", etc
|
|
21
|
+
* `region`: Identifies countries or subdivisions - "GB", "US-CA"
|
|
22
|
+
*/
|
|
23
|
+
const { legislation, region } = await fetchLegislation();
|
|
24
|
+
|
|
25
|
+
// Contruct a standardised URL for the Consent Page, embedding legislation & referrer
|
|
26
|
+
const url = buildConsentPageUrl({ url: "...", legislation })
|
|
27
|
+
|
|
28
|
+
if(legislation.has("ccpa")) {
|
|
29
|
+
insertCCPALink(url)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if(legislation.has("gdpr")) {
|
|
33
|
+
...
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## How it works
|
|
39
|
+
|
|
40
|
+
This module is a lightweight wrapper over the Fetch API that hits
|
|
41
|
+
`https://privacy.ft.com/api/v1/compliance-region.json`, and exposes the comma-delimted response as a property called `legislation` in the form of a `Set`.
|
|
42
|
+
|
|
43
|
+
The abstraction allows us to change implementation details like the API's URL, version, etc. without impacting consumers
|
|
44
|
+
|
|
45
|
+
See the [legislation-api](../../vcl/legislation-api/README.md) for more details
|
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
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@financial-times/privacy-legislation-client",
|
|
3
|
+
"description": "",
|
|
4
|
+
"version": "0.0.0-beta.1",
|
|
5
|
+
"private": false,
|
|
6
|
+
"type": "module",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist"
|
|
9
|
+
],
|
|
10
|
+
"types": "./dist/main.d.ts",
|
|
11
|
+
"main": "./dist/main.cjs",
|
|
12
|
+
"module": "./dist/main.js",
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"import": "./dist/main.js",
|
|
16
|
+
"require": "./dist/main.cjs"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"jest-fetch-mock": "^3.0.3",
|
|
21
|
+
"jsdom": "^22.1.0",
|
|
22
|
+
"msw": "^1.3.2",
|
|
23
|
+
"npm-run-all": "4.1.5",
|
|
24
|
+
"vite": "^4.5.0",
|
|
25
|
+
"vite-plugin-dts": "^3.6.3",
|
|
26
|
+
"vitest": "^0.34.6"
|
|
27
|
+
},
|
|
28
|
+
"scripts": {
|
|
29
|
+
"build": "vite build",
|
|
30
|
+
"test": "vitest"
|
|
31
|
+
}
|
|
32
|
+
}
|