Skip to content

Commit 4b90635

Browse files
committed
First draft of using content token to fetch and render reusable content across spaces.
1 parent ebc39e9 commit 4b90635

File tree

6 files changed

+36
-15
lines changed

6 files changed

+36
-15
lines changed

packages/gitbook-v2/src/lib/data/api.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,14 @@ export function createDataFetcher(
125125
);
126126
},
127127
getReusableContent(params) {
128-
return trace('getReusableContent', () =>
129-
getReusableContent(input, {
128+
return trace('getReusableContent', () => {
129+
const withToken = params.apiToken ? { ...input, apiToken: params.apiToken } : input;
130+
return getReusableContent(withToken, {
130131
spaceId: params.spaceId,
131132
revisionId: params.revisionId,
132133
reusableContentId: params.reusableContentId,
133-
})
134-
);
134+
});
135+
});
135136
},
136137
getLatestOpenAPISpecVersionContent(params) {
137138
return trace('getLatestOpenAPISpecVersionContent', () =>
@@ -158,12 +159,14 @@ export function createDataFetcher(
158159
);
159160
},
160161
getDocument(params) {
161-
return trace('getDocument', () =>
162-
getDocument(input, {
162+
return trace('getDocument', () => {
163+
const withToken = params.apiToken ? { ...input, apiToken: params.apiToken } : input;
164+
165+
return getDocument(withToken, {
163166
spaceId: params.spaceId,
164167
documentId: params.documentId,
165-
})
166-
);
168+
});
169+
});
167170
},
168171
getComputedDocument(params) {
169172
return trace('getComputedDocument', () =>

packages/gitbook-v2/src/lib/data/types.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,12 @@ export interface GitBookDataFetcher {
109109
/**
110110
* Get a document by its space ID and document ID.
111111
*/
112-
getDocument(params: { spaceId: string; documentId: string }): Promise<
113-
DataFetcherResponse<api.JSONDocument>
114-
>;
112+
getDocument(params: {
113+
spaceId: string;
114+
documentId: string;
115+
/** Optionally override the API token used to fetch the content. */
116+
apiToken?: string;
117+
}): Promise<DataFetcherResponse<api.JSONDocument>>;
115118

116119
/**
117120
* Get a computed document by its space ID and computed source.
@@ -130,6 +133,8 @@ export interface GitBookDataFetcher {
130133
spaceId: string;
131134
revisionId: string;
132135
reusableContentId: string;
136+
/** Optionally override the API token used to fetch the content. */
137+
apiToken?: string;
133138
}): Promise<DataFetcherResponse<api.RevisionReusableContent>>;
134139

135140
/**

packages/gitbook/src/components/DocumentView/ReusableContent.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ export async function ReusableContent(props: BlockProps<DocumentBlockReusableCon
1313
throw new Error('Expected a content context to render a reusable content block');
1414
}
1515

16-
const resolved = await resolveContentRef(block.data.ref, context.contentContext);
16+
const resolved = await resolveContentRef(block.data.ref, context.contentContext, {
17+
apiToken: block.meta?.token,
18+
});
19+
1720
if (!resolved?.reusableContent?.document) {
1821
return null;
1922
}
@@ -22,6 +25,7 @@ export async function ReusableContent(props: BlockProps<DocumentBlockReusableCon
2225
context.contentContext.dataFetcher.getDocument({
2326
spaceId: context.contentContext.space.id,
2427
documentId: resolved.reusableContent.document,
28+
apiToken: block.meta?.token,
2529
})
2630
);
2731

packages/gitbook/src/lib/api.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -671,8 +671,10 @@ export const getRevisionFile = batch<[string, string, string], RevisionFile | nu
671671
export const getReusableContent = async (
672672
spaceId: string,
673673
revisionId: string,
674-
reusableContentId: string
674+
reusableContentId: string,
675+
apiToken?: string
675676
): Promise<RevisionReusableContent | null> => {
677+
ishouldbeimplmeneted;
676678
const hasRevisionInMemory = await getRevision.hasInMemory(spaceId, revisionId, {
677679
metadata: false,
678680
});

packages/gitbook/src/lib/references.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ export interface ResolveContentRefOptions {
6464
* @default false
6565
*/
6666
resolveAsAbsoluteURL?: boolean;
67+
68+
/**
69+
* Override the API token used to fetch any content.
70+
*/
71+
apiToken?: string;
6772
}
6873

6974
/**
@@ -233,9 +238,10 @@ export async function resolveContentRef(
233238
case 'reusable-content': {
234239
const reusableContent = await getDataOrNull(
235240
dataFetcher.getReusableContent({
236-
spaceId: space.id,
241+
spaceId: contentRef.space ?? space.id,
237242
revisionId,
238243
reusableContentId: contentRef.reusableContent,
244+
apiToken: options.apiToken,
239245
})
240246
);
241247
if (!reusableContent) {

packages/gitbook/src/lib/v1.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,8 @@ async function getDataFetcherV1(): Promise<GitBookDataFetcher> {
210210
const reusableContent = await getReusableContent(
211211
params.spaceId,
212212
params.revisionId,
213-
params.reusableContentId
213+
params.reusableContentId,
214+
params.apiToken
214215
);
215216

216217
if (!reusableContent) {

0 commit comments

Comments
 (0)