Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add onFidesEvent method for an alternative way to subscribe to Fides events #5297

Merged
merged 4 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ The types of changes are:
- Add ability to edit dataset YAML from dataset view [#5262](https://github.com/ethyca/fides/pull/5262)
- Added support for "in progress" status in classification [#5248](https://github.com/ethyca/fides/pull/5248)
- Clarify GCP service account permissions when setting up Google Cloud SQL for Postgres in Admin-UI [#5245](https://github.com/ethyca/fides/pull/5266)
- Add onFidesEvent method for an alternative way to subscribe to Fides events [#5297](https://github.com/ethyca/fides/pull/5297)

### Changed
- Validate no path in `server_host` var for CLI config; if there is one then take only up until the first forward slash
Expand Down
36 changes: 36 additions & 0 deletions clients/fides-js/docs/interfaces/Fides.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,42 @@ such as the `fides_overrides` global or the query params.

***

### onFidesEvent()

> **onFidesEvent**: (`type`, `callback`) => () => `void`

An alternative way to subscribe to Fides events. The same events are supported, except the callback
receives the event details directly. This is useful in restricted environments where you can't
directly access `window.addEventListener`.

Returns an unsubscribe function that can be called to remove the event listener.

#### Example

```ts
const unsubscribe = Fides.onFidesEvent("FidesUpdated", (detail) => {
console.log(detail.consent);
unsubscribe();
});
```

#### Parameters

| Parameter | Type | Description |
| ------ | ------ | ------ |
| `type` | `any` | The type of event to listen for, such as `FidesInitialized`, `FidesUpdated`, etc. |
| `callback` | (`detail`) => `void` | The callback function to call when the event is triggered |

#### Returns

`Function`

##### Returns

`void`

***

### ~~reinitialize()~~

> **reinitialize**: () => `Promise`\<`void`\>
Expand Down
20 changes: 20 additions & 0 deletions clients/fides-js/src/docs/fides.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,26 @@ export interface Fides {
*/
init: (config?: any) => Promise<void>;

/**
* An alternative way to subscribe to Fides events. The same events are supported, except the callback
* receives the event details directly. This is useful in restricted environments where you can't
* directly access `window.addEventListener`.
*
* Returns an unsubscribe function that can be called to remove the event listener.
*
* @example
* ```ts
* const unsubscribe = Fides.onFidesEvent("FidesUpdated", (detail) => {
* console.log(detail.consent);
* unsubscribe();
* });
* ```
*
* @param type The type of event to listen for, such as `FidesInitialized`, `FidesUpdated`, etc.
* @param callback The callback function to call when the event is triggered
*/
onFidesEvent: (type: any, callback: (detail: any) => void) => () => void;

/**
* @deprecated
* `Fides.init()` can now be used directly instead of `Fides.reinitialize()`.
Expand Down
3 changes: 2 additions & 1 deletion clients/fides-js/src/fides-tcf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
OverrideType,
PrivacyExperience,
} from "./lib/consent-types";
import { dispatchFidesEvent } from "./lib/events";
import { dispatchFidesEvent, onFidesEvent } from "./lib/events";
import type { GppFunction } from "./lib/gpp/types";
import { DEFAULT_MODAL_LINK_LABEL } from "./lib/i18n";
import {
Expand Down Expand Up @@ -290,6 +290,7 @@ const _Fides: FidesGlobal = {
);
},
initialized: false,
onFidesEvent,
meta,
shopify,
showModal: defaultShowModal,
Expand Down
3 changes: 2 additions & 1 deletion clients/fides-js/src/fides.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
consentCookieObjHasSomeConsentSet,
updateExperienceFromCookieConsentNotices,
} from "./lib/cookie";
import { dispatchFidesEvent } from "./lib/events";
import { dispatchFidesEvent, onFidesEvent } from "./lib/events";
import { DEFAULT_MODAL_LINK_LABEL } from "./lib/i18n";
import {
getInitialCookie,
Expand Down Expand Up @@ -212,6 +212,7 @@ const _Fides: FidesGlobal = {
return this.init();
},
initialized: false,
onFidesEvent,
shouldShowExperience() {
if (!isPrivacyExperience(this.experience)) {
// Nothing to show if there's no experience
Expand Down
7 changes: 6 additions & 1 deletion clients/fides-js/src/lib/consent-types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { Fides, FidesOptions } from "../docs";
import type { Fides, FidesEventType, FidesOptions } from "../docs";
import type { gtm } from "../integrations/gtm";
import type { meta } from "../integrations/meta";
import type { shopify } from "../integrations/shopify";
import type { FidesEventDetail } from "./events";
import type { GPPFieldMapping, GPPSettings } from "./gpp/types";
import type {
GVLJson,
Expand Down Expand Up @@ -150,6 +151,10 @@ export interface FidesGlobal extends Fides {
gtm: typeof gtm;
init: (config?: FidesConfig) => Promise<void>;
meta: typeof meta;
onFidesEvent: (
type: FidesEventType,
callback: (evt: FidesEventDetail) => void,
) => () => void;
reinitialize: () => Promise<void>;
shopify: typeof shopify;
shouldShowExperience: () => boolean;
Expand Down
18 changes: 18 additions & 0 deletions clients/fides-js/src/lib/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,21 @@ export const dispatchFidesEvent = (
window.dispatchEvent(event);
}
};

/**
* An alternative way to subscribe to Fides events. The same events are supported, except the callback
* receives the event details directly. This is useful in restricted environments where you can't
* directly access `window.addEventListener`.
*
* Returns an unsubscribe function that can be called to remove the event listener.
guncha marked this conversation as resolved.
Show resolved Hide resolved
*/
guncha marked this conversation as resolved.
Show resolved Hide resolved
export const onFidesEvent = (
type: FidesEventType,
callback: (evt: FidesEventDetail) => void,
): (() => void) => {
const listener = (evt: FidesEvent) => callback(evt.detail);
window.addEventListener(type, listener);
return () => {
window.removeEventListener(type, listener);
};
};
Loading