From 4b90635d66c0121ddb763ca292c3f2e8a4fd8493 Mon Sep 17 00:00:00 2001 From: Steven Hall Date: Fri, 18 Apr 2025 17:57:01 +0100 Subject: [PATCH] First draft of using content token to fetch and render reusable content across spaces. --- packages/gitbook-v2/src/lib/data/api.ts | 19 +++++++++++-------- packages/gitbook-v2/src/lib/data/types.ts | 11 ++++++++--- .../DocumentView/ReusableContent.tsx | 6 +++++- packages/gitbook/src/lib/api.ts | 4 +++- packages/gitbook/src/lib/references.tsx | 8 +++++++- packages/gitbook/src/lib/v1.ts | 3 ++- 6 files changed, 36 insertions(+), 15 deletions(-) diff --git a/packages/gitbook-v2/src/lib/data/api.ts b/packages/gitbook-v2/src/lib/data/api.ts index 20e495656e..33266bba1e 100644 --- a/packages/gitbook-v2/src/lib/data/api.ts +++ b/packages/gitbook-v2/src/lib/data/api.ts @@ -125,13 +125,14 @@ export function createDataFetcher( ); }, getReusableContent(params) { - return trace('getReusableContent', () => - getReusableContent(input, { + return trace('getReusableContent', () => { + const withToken = params.apiToken ? { ...input, apiToken: params.apiToken } : input; + return getReusableContent(withToken, { spaceId: params.spaceId, revisionId: params.revisionId, reusableContentId: params.reusableContentId, - }) - ); + }); + }); }, getLatestOpenAPISpecVersionContent(params) { return trace('getLatestOpenAPISpecVersionContent', () => @@ -158,12 +159,14 @@ export function createDataFetcher( ); }, getDocument(params) { - return trace('getDocument', () => - getDocument(input, { + return trace('getDocument', () => { + const withToken = params.apiToken ? { ...input, apiToken: params.apiToken } : input; + + return getDocument(withToken, { spaceId: params.spaceId, documentId: params.documentId, - }) - ); + }); + }); }, getComputedDocument(params) { return trace('getComputedDocument', () => diff --git a/packages/gitbook-v2/src/lib/data/types.ts b/packages/gitbook-v2/src/lib/data/types.ts index 178a0ba77d..6d9076206f 100644 --- a/packages/gitbook-v2/src/lib/data/types.ts +++ b/packages/gitbook-v2/src/lib/data/types.ts @@ -109,9 +109,12 @@ export interface GitBookDataFetcher { /** * Get a document by its space ID and document ID. */ - getDocument(params: { spaceId: string; documentId: string }): Promise< - DataFetcherResponse - >; + getDocument(params: { + spaceId: string; + documentId: string; + /** Optionally override the API token used to fetch the content. */ + apiToken?: string; + }): Promise>; /** * Get a computed document by its space ID and computed source. @@ -130,6 +133,8 @@ export interface GitBookDataFetcher { spaceId: string; revisionId: string; reusableContentId: string; + /** Optionally override the API token used to fetch the content. */ + apiToken?: string; }): Promise>; /** diff --git a/packages/gitbook/src/components/DocumentView/ReusableContent.tsx b/packages/gitbook/src/components/DocumentView/ReusableContent.tsx index e95ffc7545..97a2fe868e 100644 --- a/packages/gitbook/src/components/DocumentView/ReusableContent.tsx +++ b/packages/gitbook/src/components/DocumentView/ReusableContent.tsx @@ -13,7 +13,10 @@ export async function ReusableContent(props: BlockProps => { + ishouldbeimplmeneted; const hasRevisionInMemory = await getRevision.hasInMemory(spaceId, revisionId, { metadata: false, }); diff --git a/packages/gitbook/src/lib/references.tsx b/packages/gitbook/src/lib/references.tsx index ab941e43c5..078033e1dc 100644 --- a/packages/gitbook/src/lib/references.tsx +++ b/packages/gitbook/src/lib/references.tsx @@ -64,6 +64,11 @@ export interface ResolveContentRefOptions { * @default false */ resolveAsAbsoluteURL?: boolean; + + /** + * Override the API token used to fetch any content. + */ + apiToken?: string; } /** @@ -233,9 +238,10 @@ export async function resolveContentRef( case 'reusable-content': { const reusableContent = await getDataOrNull( dataFetcher.getReusableContent({ - spaceId: space.id, + spaceId: contentRef.space ?? space.id, revisionId, reusableContentId: contentRef.reusableContent, + apiToken: options.apiToken, }) ); if (!reusableContent) { diff --git a/packages/gitbook/src/lib/v1.ts b/packages/gitbook/src/lib/v1.ts index ea5219816b..51e50642b4 100644 --- a/packages/gitbook/src/lib/v1.ts +++ b/packages/gitbook/src/lib/v1.ts @@ -210,7 +210,8 @@ async function getDataFetcherV1(): Promise { const reusableContent = await getReusableContent( params.spaceId, params.revisionId, - params.reusableContentId + params.reusableContentId, + params.apiToken ); if (!reusableContent) {