|
7 | 7 | GitBookAPI,
|
8 | 8 | GitBookAPIError,
|
9 | 9 | HttpResponse,
|
| 10 | + JSONDocument, |
10 | 11 | List,
|
11 | 12 | PublishedSiteContentLookup,
|
12 | 13 | RequestRenderIntegrationUI,
|
@@ -487,6 +488,40 @@ const getRevisionFileById = cache({
|
487 | 488 | },
|
488 | 489 | });
|
489 | 490 |
|
| 491 | +const getRevisionReusableContentById = cache({ |
| 492 | + name: 'api.getRevisionReusableContentById', |
| 493 | + tag: (spaceId, revisionId) => |
| 494 | + getAPICacheTag({ tag: 'revision', space: spaceId, revision: revisionId }), |
| 495 | + get: async ( |
| 496 | + spaceId: string, |
| 497 | + revisionId: string, |
| 498 | + reusableContentId: string, |
| 499 | + options: CacheFunctionOptions, |
| 500 | + ) => { |
| 501 | + try { |
| 502 | + const response = await (async () => { |
| 503 | + return api().spaces.getReusableContentInRevisionById( |
| 504 | + spaceId, |
| 505 | + revisionId, |
| 506 | + reusableContentId, |
| 507 | + { |
| 508 | + ...noCacheFetchOptions, |
| 509 | + signal: options.signal, |
| 510 | + }, |
| 511 | + ); |
| 512 | + })(); |
| 513 | + |
| 514 | + return cacheResponse(response, cacheTtl_7days); |
| 515 | + } catch (error: any) { |
| 516 | + if (error instanceof GitBookAPIError && error.code === 404) { |
| 517 | + return { data: null, ...cacheTtl_7days }; |
| 518 | + } |
| 519 | + |
| 520 | + throw error; |
| 521 | + } |
| 522 | + }, |
| 523 | +}); |
| 524 | + |
490 | 525 | /**
|
491 | 526 | * Get all the files in a revision of a space.
|
492 | 527 | * It should not be used directly, use `getRevisionFile` instead.
|
@@ -578,6 +613,35 @@ export const getRevisionFile = batch<[string, string, string], RevisionFile | nu
|
578 | 613 | },
|
579 | 614 | );
|
580 | 615 |
|
| 616 | +/** |
| 617 | + * Get reusable content in a revision. Also returns the document. |
| 618 | + */ |
| 619 | +export const getReusableContentDocument = async ( |
| 620 | + spaceId: string, |
| 621 | + revisionId: string, |
| 622 | + reusableContentId: string, |
| 623 | +): Promise<JSONDocument | null> => { |
| 624 | + const hasRevisionInMemory = await getRevision.hasInMemory(spaceId, revisionId, { |
| 625 | + metadata: false, |
| 626 | + }); |
| 627 | + |
| 628 | + if (hasRevisionInMemory) { |
| 629 | + const revision = await getRevision(spaceId, revisionId, { metadata: false }); |
| 630 | + const reusableContent = revision.reusableContents[reusableContentId]; |
| 631 | + if (!reusableContent) { |
| 632 | + return null; |
| 633 | + } |
| 634 | + return getDocument(reusableContent.documentId); |
| 635 | + } else { |
| 636 | + const response = await getRevisionReusableContentById( |
| 637 | + spaceId, |
| 638 | + revisionId, |
| 639 | + reusableContentId, |
| 640 | + ); |
| 641 | + return response?.document ?? null; |
| 642 | + } |
| 643 | +}; |
| 644 | + |
581 | 645 | /**
|
582 | 646 | * Get a document by its ID.
|
583 | 647 | */
|
|
0 commit comments