Skip to content

Commit

Permalink
Add support for fetch_visitor_authentication event in runtime package (
Browse files Browse the repository at this point in the history
…#369)

* Add support for fetch_visitor_authentication event

* Update comment
  • Loading branch information
vibhanshub authored Feb 7, 2024
1 parent 8e1985e commit 41fa1ec
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/blue-brooms-cough.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@gitbook/runtime': minor
---

Add support for fetch_visitor_authentication event
16 changes: 14 additions & 2 deletions packages/runtime/src/events.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { Event, FetchEvent, FetchPublishedScriptEvent } from '@gitbook/api';
import {
Event,
FetchEvent,
FetchPublishedScriptEvent,
FetchVisitorAuthenticationEvent,
} from '@gitbook/api';

import { RuntimeCallback, RuntimeContext } from './context';

Expand All @@ -25,7 +30,7 @@ export type EventCallback<
*/
export type NonFetchEvent = Exclude<
EventType,
FetchEvent['type'] | FetchPublishedScriptEvent['type']
FetchEvent['type'] | FetchPublishedScriptEvent['type'] | FetchVisitorAuthenticationEvent['type']
>;

/**
Expand Down Expand Up @@ -53,3 +58,10 @@ export type FetchPublishScriptEventCallback<Context extends RuntimeContext = Run
Response | undefined | Promise<Response | undefined>, // 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<Response>, Context>;
27 changes: 26 additions & 1 deletion packages/runtime/src/integrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -19,6 +24,11 @@ interface IntegrationRuntimeDefinition<Context extends RuntimeContext = RuntimeC
*/
fetch_published_script?: FetchPublishScriptEventCallback<Context>;

/**
* Handler for fetching the visitor auth for an integration.
*/
fetch_visitor_authentication?: FetchVisitorAuthenticationEventCallback<Context>;

/**
* Handler for GitBook events.
*/
Expand Down Expand Up @@ -108,6 +118,21 @@ export function createIntegration<Context extends RuntimeContext = RuntimeContex
return resp;
}

if (
event.type === 'fetch_visitor_authentication' &&
definition.fetch_visitor_authentication
) {
const resp = await definition.fetch_visitor_authentication(event, context);

logger.debug(
`response ${resp.status} ${resp.statusText} Content-Type: ${resp.headers.get(
'content-type'
)}`
);

return resp;
}

if (event.type === 'ui_render') {
const component = components.find((c) => c.componentId === event.componentId);

Expand Down

0 comments on commit 41fa1ec

Please sign in to comment.