Skip to content

Commit

Permalink
PROD-1452- Refactor: extract out getting custom preferences from gett…
Browse files Browse the repository at this point in the history
…ing override options (#4466)
  • Loading branch information
eastandwestwind authored Dec 4, 2023
1 parent ed32558 commit 05b5bfd
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 31 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ The types of changes are:
### Fixed
- Fix type errors when TCF vendors have no dataDeclaration [#4465](https://github.com/ethyca/fides/pull/4465)
- Fixed an error where editing an AC system would mistakenly lock it for GVL [#4471](https://github.com/ethyca/fides/pull/4471)
- Refactor custom Get Preferences function to occur after our CMP API initialization [#4466](https://github.com/ethyca/fides/pull/4466)

## [2.25.0](https://github.com/ethyca/fides/compare/2.24.1...2.25.0)

Expand Down
26 changes: 20 additions & 6 deletions clients/fides-js/src/fides-tcf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ import { shopify } from "./integrations/shopify";

import {
FidesConfig,
FidesOptionsOverrides,
FidesOverrides,
GetPreferencesFnResp,
OverrideOptions,
PrivacyExperience,
UserConsentPreference,
Expand All @@ -62,7 +64,7 @@ import { generateFidesString, initializeTcfCmpApi } from "./lib/tcf";
import {
getInitialCookie,
getInitialFides,
getOverrides,
getOptionsOverrides,
initialize,
} from "./lib/initialize";
import type { Fides } from "./lib/initialize";
Expand Down Expand Up @@ -91,6 +93,7 @@ import {
} from "./lib/tcf/utils";
import type { GppFunction } from "./lib/gpp/types";
import { makeStub } from "./lib/tcf/stub";
import { customGetConsentPreferences } from "./services/external/preferences";

declare global {
interface Window {
Expand Down Expand Up @@ -242,22 +245,33 @@ const updateFidesCookieFromString = (
* Initialize the global Fides object with the given configuration values
*/
const init = async (config: FidesConfig) => {
const overrides: Partial<FidesOverrides> = await getOverrides(config);
const optionsOverrides: Partial<FidesOptionsOverrides> =
getOptionsOverrides();
makeStub({
gdprAppliesDefault: overrides.overrideOptions?.fidesTcfGdprApplies,
gdprAppliesDefault: optionsOverrides?.fidesTcfGdprApplies,
});
const consentPrefsOverrides: GetPreferencesFnResp | null =
await customGetConsentPreferences(config);
// if we don't already have a fidesString override, use fidesString from consent prefs if they exist
if (!optionsOverrides.fidesString && consentPrefsOverrides?.fides_string) {
optionsOverrides.fidesString = consentPrefsOverrides.fides_string;
}
const overrides: Partial<FidesOverrides> = {
optionsOverrides,
consentPrefsOverrides,
};
// eslint-disable-next-line no-param-reassign
config.options = { ...config.options, ...overrides.overrideOptions };
config.options = { ...config.options, ...overrides.optionsOverrides };
const cookie = {
...getInitialCookie(config),
...overrides.overrideConsentPrefs?.consent,
...overrides.consentPrefsOverrides?.consent,
};
if (config.options.fidesString) {
const { cookie: updatedCookie, success } = updateFidesCookieFromString(
cookie,
config.options.fidesString,
config.options.debug,
overrides.overrideConsentPrefs?.version_hash
overrides.consentPrefsOverrides?.version_hash
);
if (success) {
Object.assign(cookie, updatedCookie);
Expand Down
19 changes: 15 additions & 4 deletions clients/fides-js/src/fides.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ import {
} from "./lib/cookie";
import {
FidesConfig,
FidesOptionsOverrides,
FidesOverrides,
GetPreferencesFnResp,
OverrideOptions,
PrivacyExperience,
} from "./lib/consent-types";
Expand All @@ -67,12 +69,13 @@ import {
initialize,
getInitialCookie,
getInitialFides,
getOverrides,
getOptionsOverrides,
} from "./lib/initialize";
import type { Fides } from "./lib/initialize";

import { renderOverlay } from "./lib/renderOverlay";
import { getConsentContext } from "./lib/consent-context";
import { customGetConsentPreferences } from "./services/external/preferences";

declare global {
interface Window {
Expand Down Expand Up @@ -123,12 +126,20 @@ const updateCookie = async (
* Initialize the global Fides object with the given configuration values
*/
const init = async (config: FidesConfig) => {
const overrides: Partial<FidesOverrides> = await getOverrides(config);
const optionsOverrides: Partial<FidesOptionsOverrides> =
getOptionsOverrides();
const consentPrefsOverrides: GetPreferencesFnResp | null =
await customGetConsentPreferences(config);
// DEFER: not implemented - ability to override notice-based consent with the consentPrefsOverrides.consent obj
const overrides: Partial<FidesOverrides> = {
optionsOverrides,
consentPrefsOverrides,
};
// eslint-disable-next-line no-param-reassign
config.options = { ...config.options, ...overrides.overrideOptions };
config.options = { ...config.options, ...overrides.optionsOverrides };
const cookie = {
...getInitialCookie(config),
...overrides.overrideConsentPrefs?.consent,
...overrides.consentPrefsOverrides?.consent,
};
const initialFides = getInitialFides({ ...config, cookie });
if (initialFides) {
Expand Down
4 changes: 2 additions & 2 deletions clients/fides-js/src/lib/consent-constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FidesOptionOverrides, OverrideOptions } from "./consent-types";
import { FidesOptionsOverrides, OverrideOptions } from "./consent-types";

// Regex to validate a location string, which must:
// 1) Start with a 2-3 character country code (e.g. "US")
Expand All @@ -7,7 +7,7 @@ import { FidesOptionOverrides, OverrideOptions } from "./consent-types";
export const VALID_ISO_3166_LOCATION_REGEX = /^\w{2,3}(-\w{2,3})?$/;

export const FIDES_OVERRIDE_OPTIONS_VALIDATOR_MAP: {
fidesOption: keyof FidesOptionOverrides;
fidesOption: keyof FidesOptionsOverrides;
fidesOptionType: "string" | "boolean";
fidesOverrideKey: keyof OverrideOptions;
validationRegex: RegExp;
Expand Down
6 changes: 3 additions & 3 deletions clients/fides-js/src/lib/consent-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ export type OverrideOptions = {
fides_tcf_gdpr_applies: boolean;
};

export type FidesOptionOverrides = Pick<
export type FidesOptionsOverrides = Pick<
FidesOptions,
| "fidesString"
| "fidesDisableSaveApi"
Expand All @@ -403,8 +403,8 @@ export type FidesOptionOverrides = Pick<
>;

export type FidesOverrides = {
overrideOptions: Partial<FidesOptionOverrides>;
overrideConsentPrefs: GetPreferencesFnResp | null;
optionsOverrides: Partial<FidesOptionsOverrides>;
consentPrefsOverrides: GetPreferencesFnResp | null;
};

export enum ButtonType {
Expand Down
21 changes: 5 additions & 16 deletions clients/fides-js/src/lib/initialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ import {
ConsentMethod,
EmptyExperience,
FidesConfig,
FidesOptionOverrides,
FidesOptionsOverrides,
FidesOptions,
FidesOverrides,
GetPreferencesFnResp,
PrivacyExperience,
SaveConsentPreference,
UserGeolocation,
Expand All @@ -44,7 +42,6 @@ import { resolveConsentValue } from "./consent-value";
import { initOverlay } from "./consent";
import { TcfCookieConsent } from "./tcf/types";
import { FIDES_OVERRIDE_OPTIONS_VALIDATOR_MAP } from "./consent-constants";
import { customGetConsentPreferences } from "../services/external/preferences";
import { setupExtensions } from "./extensions";

export type Fides = {
Expand Down Expand Up @@ -147,19 +144,16 @@ const automaticallyApplyGPCPreferences = ({
};

/**
* Gets and validates override options provided through URL query params, cookie, or window obj,
* and optionally retrieves consent preference overrides if a custom fn was defined in the config.
* Gets and validates override options provided through URL query params, cookie, or window obj
*
*
* If the same override option is provided in multiple ways, load the value in this order:
* 1) query param (top priority)
* 2) window obj (second priority)
* 3) cookie value (last priority)
*/
export const getOverrides = async (
config: FidesConfig
): Promise<Partial<FidesOverrides>> => {
const overrideOptions: Partial<FidesOptionOverrides> = {};
export const getOptionsOverrides = (): Partial<FidesOptionsOverrides> => {
const overrideOptions: Partial<FidesOptionsOverrides> = {};
if (typeof window !== "undefined") {
// Grab query params if provided in the URL (e.g. "?fides_string=123...")
const queryParams = new URLSearchParams(window.location.search);
Expand Down Expand Up @@ -188,12 +182,7 @@ export const getOverrides = async (
}
);
}
const overrideConsentPrefs: GetPreferencesFnResp | null =
await customGetConsentPreferences(config);
if (!overrideOptions.fidesString && overrideConsentPrefs?.fides_string) {
overrideOptions.fidesString = overrideConsentPrefs.fides_string;
}
return { overrideOptions, overrideConsentPrefs };
return overrideOptions;
};

/**
Expand Down

0 comments on commit 05b5bfd

Please sign in to comment.