@financial-times/privacy-legislation-client
Advanced tools
Comparing version 0.2.0 to 0.3.1
{ | ||
"name": "@financial-times/privacy-legislation-client", | ||
"description": "", | ||
"version": "0.2.0", | ||
"version": "0.3.1", | ||
"main": "dist/cjs/main.js", | ||
@@ -10,5 +10,3 @@ "module": "dist/esm/main.js", | ||
"devDependencies": { | ||
"@types/isomorphic-fetch": "0.0.35", | ||
"isomorphic-fetch": "2.2.1", | ||
"nock": "12.0.3", | ||
"jest-fetch-mock": "^3.0.3", | ||
"npm-run-all": "4.1.5" | ||
@@ -15,0 +13,0 @@ }, |
@@ -6,2 +6,31 @@ export const API_URL = "https://privacy.ft.com/api/v1"; | ||
type RegionalCompliance = { | ||
region: string; | ||
legislation: Set<string>; | ||
}; | ||
type Props = { | ||
url: string; | ||
legislation: Set<string>; | ||
referrer?: string; | ||
}; | ||
function formatResponse(obj: { region: string; legislation: "string" }): RegionalCompliance { | ||
return { | ||
region: obj.region, | ||
legislation: new Set<string>(obj.legislation.split(",")), | ||
}; | ||
} | ||
function sessionStorageOk(): boolean { | ||
const test = "test"; | ||
try { | ||
sessionStorage.setItem(test, test); | ||
sessionStorage.removeItem(test); | ||
return true; | ||
} catch (e) { | ||
return false; | ||
} | ||
} | ||
function getHostname(referrer?: string): string { | ||
@@ -26,14 +55,22 @@ if (referrer) { | ||
*/ | ||
export async function fetchLegislation(): Promise<{ | ||
region: string; | ||
legislation: Set<string>; | ||
}> { | ||
try { | ||
const res = await fetch(`${API_URL}${API_ENDPOINTS.complianceRegion}`); | ||
const { region, legislation } = await (res.ok ? res.json() : Promise.reject(res)); | ||
const legislationIds = new Set<string>(legislation.split(",")); | ||
export async function fetchLegislation(): Promise<RegionalCompliance> { | ||
// Look in session storage first | ||
const complianceStr = sessionStorageOk() && sessionStorage.getItem("user-compliance"); | ||
const complianceObj = complianceStr && JSON.parse(complianceStr); | ||
if (complianceObj) { | ||
return Promise.resolve(formatResponse(complianceObj)); | ||
} | ||
// Not found in session storage, fetch from API | ||
else { | ||
try { | ||
const res = await fetch(`${API_URL}${API_ENDPOINTS.complianceRegion}`); | ||
const compliance = await (res.ok ? res.json() : Promise.reject(res)); | ||
if (sessionStorageOk()) { | ||
sessionStorage.setItem("user-compliance", JSON.stringify(compliance)); | ||
} | ||
return { region, legislation: legislationIds }; | ||
} catch (err) { | ||
throw new Error(err.status); | ||
return formatResponse(compliance); | ||
} catch (err) { | ||
throw new Error(err.status); | ||
} | ||
} | ||
@@ -48,7 +85,2 @@ } | ||
*/ | ||
type Props = { | ||
url: string; | ||
legislation: Set<string>; | ||
referrer?: string; | ||
}; | ||
export function buildConsentPageUrl({ url, legislation, referrer }: Props): string { | ||
@@ -55,0 +87,0 @@ const decoratedUrl = new URL(url); |
@@ -1,6 +0,7 @@ | ||
import nock from "nock"; | ||
import "isomorphic-fetch"; | ||
import fetch from "jest-fetch-mock"; | ||
import { API_URL, API_ENDPOINTS, fetchLegislation, buildConsentPageUrl } from "../main"; | ||
fetch.enableMocks(); | ||
import { fetchLegislation, buildConsentPageUrl } from "../main"; | ||
describe("fetchLegislation()", function () { | ||
@@ -14,7 +15,8 @@ const payload = { | ||
beforeEach(() => { | ||
nock(API_URL).get(API_ENDPOINTS.complianceRegion).reply(200, payload); | ||
sessionStorage.clear(); | ||
fetch.mockResponse(JSON.stringify(payload)); | ||
}); | ||
afterEach(() => { | ||
nock.cleanAll(); | ||
fetch.resetMocks(); | ||
}); | ||
@@ -31,5 +33,6 @@ | ||
expect(legislation).toEqual(new Set(["ccpa", "gdpr"])); | ||
expect(consentPageUrl).toBe( | ||
"https://privacy.ft.com/consent?legislation=ccpa&legislation=gdpr" | ||
); | ||
// When running Jest with JSDOM engine, it sometimes appends the localhost referer automatically. | ||
// This regex matches true with or without `&referrer=localhost` | ||
const matchRegex = /(https:\/\/privacy.ft.com\/consent\?legislation=ccpa&legislation=gdpr)(&referrer=localhost)?$/; | ||
expect(matchRegex.test(consentPageUrl)).toBeTruthy(); | ||
}); | ||
@@ -40,10 +43,13 @@ }); | ||
beforeEach(() => { | ||
nock(API_URL).get(API_ENDPOINTS.complianceRegion).reply(200, { | ||
region: "GB", | ||
legislation: "gdpr", | ||
}); | ||
sessionStorage.clear(); | ||
fetch.mockResponse( | ||
JSON.stringify({ | ||
region: "GB", | ||
legislation: "gdpr", | ||
}) | ||
); | ||
}); | ||
afterEach(() => { | ||
nock.cleanAll(); | ||
fetch.resetMocks(); | ||
}); | ||
@@ -84,2 +90,48 @@ | ||
}); | ||
describe("sessionStorage available", () => { | ||
beforeEach(() => { | ||
sessionStorage.clear(); | ||
fetch.mockResponse( | ||
JSON.stringify({ | ||
region: "US-CA", | ||
legislation: "ccpa,gdpr", | ||
}) | ||
); | ||
}); | ||
afterEach(() => { | ||
fetch.resetMocks(); | ||
}); | ||
test("should fetch from sessionStorage if available", async () => { | ||
sessionStorage.setItem( | ||
"user-compliance", | ||
JSON.stringify({ | ||
region: "US-CA", | ||
legislation: "ccpa,gdpr", | ||
}) | ||
); | ||
const { region, legislation } = await fetchLegislation(); | ||
expect(region).toBe("US-CA"); | ||
expect(legislation).toEqual(new Set(["ccpa", "gdpr"])); | ||
expect(fetch.mock.calls.length).toEqual(0); | ||
}); | ||
test("should set sessionStorage item after fetch", async () => { | ||
const { region, legislation } = await fetchLegislation(); | ||
const ssItemStr = sessionStorage.getItem("user-compliance"); | ||
const ssItemObj = ssItemStr && JSON.parse(ssItemStr); | ||
expect(region).toBe("US-CA"); | ||
expect(legislation).toEqual(new Set(["ccpa", "gdpr"])); | ||
expect(fetch.mock.calls.length).toEqual(1); | ||
expect(ssItemObj).toEqual({ | ||
region: "US-CA", | ||
legislation: "ccpa,gdpr", | ||
}); | ||
}); | ||
}); | ||
}); |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
62878
2
13
818
3