export const getConsentState:()

in libs/@guardian/libs/src/consent-management-platform/tcfv2/getConsentState.ts [20:52]


export const getConsentState: () => Promise<TCFv2ConsentState> = async () => {
	const [tcData, customVendors] = await Promise.all([
		getTCData(),
		getCustomVendorConsents(),
	]);

	if (typeof tcData === 'undefined') {
		const currentFramework: string = getCurrentFramework() ?? 'undefined';
		throw new Error(
			`No TC Data found with current framework: ${currentFramework}`,
		);
	}
	const consents = {
		...defaultConsents,
		...tcData.purpose.consents,
	};

	const { eventStatus, gdprApplies, tcString, addtlConsent } = tcData;
	const { grants } = customVendors;

	const vendorConsents: TCFv2ConsentList = Object.keys(grants)
		.sort()
		.reduce((acc, cur) => ({ ...acc, [cur]: grants[cur]?.vendorGrant }), {});

	return {
		consents,
		eventStatus,
		vendorConsents,
		addtlConsent,
		gdprApplies,
		tcString,
	};
};