Skip to content

First draft of using content token to fetch and render reusable content across spaces. #3173

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
19 changes: 11 additions & 8 deletions packages/gitbook-v2/src/lib/data/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () =>
Expand All @@ -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', () =>
Expand Down
11 changes: 8 additions & 3 deletions packages/gitbook-v2/src/lib/data/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<api.JSONDocument>
>;
getDocument(params: {
spaceId: string;
documentId: string;
/** Optionally override the API token used to fetch the content. */
apiToken?: string;
}): Promise<DataFetcherResponse<api.JSONDocument>>;

/**
* Get a computed document by its space ID and computed source.
Expand All @@ -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<DataFetcherResponse<api.RevisionReusableContent>>;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ export async function ReusableContent(props: BlockProps<DocumentBlockReusableCon
throw new Error('Expected a content context to render a reusable content block');
}

const resolved = await resolveContentRef(block.data.ref, context.contentContext);
const resolved = await resolveContentRef(block.data.ref, context.contentContext, {
apiToken: block.meta?.token,
});

if (!resolved?.reusableContent?.document) {
return null;
}
Expand All @@ -22,6 +25,7 @@ export async function ReusableContent(props: BlockProps<DocumentBlockReusableCon
context.contentContext.dataFetcher.getDocument({
spaceId: context.contentContext.space.id,
documentId: resolved.reusableContent.document,
apiToken: block.meta?.token,
})
);

Expand Down
4 changes: 3 additions & 1 deletion packages/gitbook/src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -671,8 +671,10 @@ export const getRevisionFile = batch<[string, string, string], RevisionFile | nu
export const getReusableContent = async (
spaceId: string,
revisionId: string,
reusableContentId: string
reusableContentId: string,
apiToken?: string
): Promise<RevisionReusableContent | null> => {
ishouldbeimplmeneted;
const hasRevisionInMemory = await getRevision.hasInMemory(spaceId, revisionId, {
metadata: false,
});
Expand Down
8 changes: 7 additions & 1 deletion packages/gitbook/src/lib/references.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ export interface ResolveContentRefOptions {
* @default false
*/
resolveAsAbsoluteURL?: boolean;

/**
* Override the API token used to fetch any content.
*/
apiToken?: string;
}

/**
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion packages/gitbook/src/lib/v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,8 @@ async function getDataFetcherV1(): Promise<GitBookDataFetcher> {
const reusableContent = await getReusableContent(
params.spaceId,
params.revisionId,
params.reusableContentId
params.reusableContentId,
params.apiToken
);

if (!reusableContent) {
Expand Down
Loading