Skip to content

Commit

Permalink
add sessionStorage and cookies, some refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
vlkerag committed Dec 18, 2023
1 parent 1a8d0e8 commit 55bf627
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 89 deletions.
86 changes: 0 additions & 86 deletions src/cmpLocalStorage.ts

This file was deleted.

141 changes: 141 additions & 0 deletions src/cmpStorage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
import { storage, getCookie, setCookie } from '@guardian/libs';
import { onConsent } from '.';

export const UseCaseOptions = [
"Targeted advertising",
"Essential"
] as const;
export type UseCases = typeof UseCaseOptions[number];

const hasConsentForUseCase = async (useCase: UseCases): Promise<boolean> =>
{
/*console.log(`consentState.tcfv2?.consents['1']: ${consentState.tcfv2?.consents['1']}`);
console.log(`consentState.tcfv2?.consents['2']: ${consentState.tcfv2?.consents['2']}`);
console.log(`consentState.tcfv2?.consents['3']: ${consentState.tcfv2?.consents['3']}`);
console.log(`consentState.tcfv2?.consents['4']: ${consentState.tcfv2?.consents['4']}`);
console.log(`consentState.tcfv2?.consents['5']: ${consentState.tcfv2?.consents['5']}`);
console.log(`consentState.tcfv2?.consents['6']: ${consentState.tcfv2?.consents['6']}`);
console.log(`consentState.tcfv2?.consents['7']: ${consentState.tcfv2?.consents['7']}`);
console.log(`consentState.tcfv2?.consents['8']: ${consentState.tcfv2?.consents['8']}`);
console.log(`consentState.tcfv2?.consents['9']: ${consentState.tcfv2?.consents['9']}`);
console.log(`consentState.tcfv2?.consents['10']: ${consentState.tcfv2?.consents['10']}`);
console.log(`consentState.tcfv2?.consents['11']: ${consentState.tcfv2?.consents['11']}`);
console.log(`consentState.canTarget: ${consentState.canTarget}`);
*/

const consentState = await onConsent();

Check warning on line 26 in src/cmpStorage.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

switch(useCase) {
case "Targeted advertising": return(consentState.canTarget)

Check warning on line 29 in src/cmpStorage.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
//could be more granular than this, for example by using explicit tcf purposes:
//(consentState.tcfv2?.consents['1']
// && consentState.tcfv2.consents['2']
// && consentState.tcfv2.consents['3'])//Need the correct list of consents, this is just an example
//|| (!consentState.ccpa?.doNotSell)
//|| (consentState.aus?.personalisedAdvertising)
case "Essential": return(true)

Check warning on line 36 in src/cmpStorage.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
default: return(false)

Check warning on line 37 in src/cmpStorage.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

Check warning on line 38 in src/cmpStorage.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

}

export const cmpGetLocalStorageItem = async (useCase: UseCases, storageItem: string): Promise<unknown> =>
{
console.log('in cmpGetLocalStorageItem');

Check warning on line 44 in src/cmpStorage.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

if(await hasConsentForUseCase(useCase))
{
return storage.local.get(storageItem)

Check warning on line 48 in src/cmpStorage.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}
else
{
console.error('cmp', `Cannot get local storage item ${storageItem} due to missing consent for use-case ${useCase}`)

Check warning on line 52 in src/cmpStorage.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
return(null)

Check warning on line 53 in src/cmpStorage.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

Check warning on line 54 in src/cmpStorage.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
};

export const cmpSetLocalStorageItem = async (useCase: UseCases, storageItem: string, value:unknown, expires?: string | number | Date): Promise<void> =>
{
console.log('in cmpSetLocalStorageItem');

Check warning on line 59 in src/cmpStorage.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

if(await hasConsentForUseCase(useCase))
{
return storage.local.set(storageItem, expires)

Check warning on line 63 in src/cmpStorage.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}
else
{
console.error('cmp', `Cannot set local storage item ${storageItem} due to missing consent for use-case ${useCase}`)

Check warning on line 67 in src/cmpStorage.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

Check warning on line 68 in src/cmpStorage.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
};

export const cmpGetSessionStorageItem = async (useCase: UseCases, storageItem: string): Promise<unknown> =>
{
console.log('in cmpGetSessionStorageItem');

Check warning on line 73 in src/cmpStorage.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

if(await hasConsentForUseCase(useCase))
{
return storage.session.get(storageItem)

Check warning on line 77 in src/cmpStorage.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}
else
{
console.error('cmp', `Cannot get session storage item ${storageItem} due to missing consent for use-case ${useCase}`)

Check warning on line 81 in src/cmpStorage.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
return(null)
}

Check warning on line 83 in src/cmpStorage.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
};

export const cmpSetSessionStorageItem = async (useCase: UseCases, storageItem: string, value:unknown, expires?: string | number | Date): Promise<void> =>
{
console.log('in cmpSetSessionStorageItem');

if(await hasConsentForUseCase(useCase))
{
return storage.session.set(storageItem, expires)
}
else
{
console.error('cmp', `Cannot set session storage item ${storageItem} due to missing consent for use-case ${useCase}`)
}
};

export const cmpGetCookie = async({ useCase, name, shouldMemoize, }: {
useCase: UseCases,
name: string;
shouldMemoize?: boolean | undefined;
}): Promise<string | null> =>
{
console.log('in cmpGetCookie');

if(await hasConsentForUseCase(useCase))
{
return getCookie({name: name, shouldMemoize: shouldMemoize})
}
else
{
console.error('cmp', `Cannot get cookie ${name} due to missing consent for use-case ${useCase}`)
return(null)
}
};

export const cmpSetCookie = async ({ useCase, name, value, daysToLive, isCrossSubdomain, }: {
useCase: UseCases,
name: string;
value: string;
daysToLive?: number | undefined;
isCrossSubdomain?: boolean | undefined;
}): Promise<void> =>
{
console.log('in cmpSetCookie');

if(await hasConsentForUseCase(useCase))
{
setCookie({name:name, value:value, daysToLive:daysToLive, isCrossSubdomain:isCrossSubdomain})
}
else
{
console.error('cmp', `Cannot set cookie ${name} due to missing consent for use-case ${useCase}`)
}
};


//await cmpGetLocalStorageItem("Targeted advertising", "dep")
//await cmpGetLocalStorageItem("invalid", "dep")
5 changes: 2 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ import {
onConsent as serverOnConsent,
onConsentChange as serverOnConsentChange,
} from './server';
import { cmpGetLocalStorageItem as cmpGetLocalStorageItemLocal, cmpSetLocalStorageItem as cmpSetLocalStorageItemLocal } from './cmpLocalStorage';
import type { CMP, InitCMP, WillShowPrivacyMessage } from './types';
import { initVendorDataManager } from './vendorDataManager';

export {cmpGetLocalStorageItem, cmpSetLocalStorageItem, cmpGetSessionStorageItem, cmpSetSessionStorageItem, cmpGetCookie, cmpSetCookie} from './cmpStorage';

// Store some bits in the global scope for reuse, in case there's more
// than one instance of the CMP on the page in different scopes.
if (!isServerSide) {
Expand Down Expand Up @@ -112,5 +113,3 @@ export const onConsentChange = isServerSide
export const getConsentFor = isServerSide
? serverGetConsentFor
: (window.guCmpHotFix.getConsentFor ||= clientGetConsentFor);
export const cmpGetLocalStorageItem = cmpGetLocalStorageItemLocal;
export const cmpSetLocalStorageItem = cmpSetLocalStorageItemLocal;

1 comment on commit 55bf627

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage report

St.
Category Percentage Covered / Total
🟢 Statements 82.95% 253/305
🟡 Branches 73.44% 94/128
🟢 Functions 81.82% 63/77
🟢 Lines 82.43% 244/296

Test suite run success

328 tests passing in 16 suites.

Report generated by 🧪jest coverage report action from 55bf627

Please sign in to comment.