From 41fa1ec8d1951fe623b4140f83aebb394ea8ffc8 Mon Sep 17 00:00:00 2001 From: vibhanshub <140522517+vibhanshub@users.noreply.github.com> Date: Wed, 7 Feb 2024 19:33:50 +0000 Subject: [PATCH] Add support for fetch_visitor_authentication event in runtime package (#369) * Add support for fetch_visitor_authentication event * Update comment --- .changeset/blue-brooms-cough.md | 5 +++++ packages/runtime/src/events.ts | 16 ++++++++++++++-- packages/runtime/src/integrations.ts | 27 ++++++++++++++++++++++++++- 3 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 .changeset/blue-brooms-cough.md diff --git a/.changeset/blue-brooms-cough.md b/.changeset/blue-brooms-cough.md new file mode 100644 index 000000000..241349f51 --- /dev/null +++ b/.changeset/blue-brooms-cough.md @@ -0,0 +1,5 @@ +--- +'@gitbook/runtime': minor +--- + +Add support for fetch_visitor_authentication event diff --git a/packages/runtime/src/events.ts b/packages/runtime/src/events.ts index be0eb596d..a291f1cbd 100644 --- a/packages/runtime/src/events.ts +++ b/packages/runtime/src/events.ts @@ -1,4 +1,9 @@ -import { Event, FetchEvent, FetchPublishedScriptEvent } from '@gitbook/api'; +import { + Event, + FetchEvent, + FetchPublishedScriptEvent, + FetchVisitorAuthenticationEvent, +} from '@gitbook/api'; import { RuntimeCallback, RuntimeContext } from './context'; @@ -25,7 +30,7 @@ export type EventCallback< */ export type NonFetchEvent = Exclude< EventType, - FetchEvent['type'] | FetchPublishedScriptEvent['type'] + FetchEvent['type'] | FetchPublishedScriptEvent['type'] | FetchVisitorAuthenticationEvent['type'] >; /** @@ -53,3 +58,10 @@ export type FetchPublishScriptEventCallback, // Scripts can return undefined, in which case a no-op script will be sent back to the caller. Context >; + +/** + * Callback to handle visitor authentication fetch event. + */ +export type FetchVisitorAuthenticationEventCallback< + Context extends RuntimeContext = RuntimeContext +> = RuntimeCallback<[FetchVisitorAuthenticationEvent], Response | Promise, Context>; diff --git a/packages/runtime/src/integrations.ts b/packages/runtime/src/integrations.ts index 19d82583c..6aa943aea 100644 --- a/packages/runtime/src/integrations.ts +++ b/packages/runtime/src/integrations.ts @@ -2,7 +2,12 @@ import { Event, IntegrationEnvironment } from '@gitbook/api'; import { ComponentDefinition } from './components'; import { createContext, RuntimeContext } from './context'; -import { EventCallbackMap, FetchEventCallback, FetchPublishScriptEventCallback } from './events'; +import { + EventCallbackMap, + FetchEventCallback, + FetchPublishScriptEventCallback, + FetchVisitorAuthenticationEventCallback, +} from './events'; import { Logger } from './logger'; const logger = Logger('integrations'); @@ -19,6 +24,11 @@ interface IntegrationRuntimeDefinition; + /** + * Handler for fetching the visitor auth for an integration. + */ + fetch_visitor_authentication?: FetchVisitorAuthenticationEventCallback; + /** * Handler for GitBook events. */ @@ -108,6 +118,21 @@ export function createIntegration c.componentId === event.componentId);