From 314e5795ebbf7abe0c895f4c48acfaa84941b16c Mon Sep 17 00:00:00 2001 From: darkskygit Date: Tue, 25 Mar 2025 03:25:41 +0000 Subject: [PATCH] feat(server): add full content support for doc (#11149) --- .../doc-service/__tests__/controller.spec.ts | 21 +++++++++++++++++++ .../server/src/core/doc-service/controller.ts | 8 +++++-- .../backend/server/src/core/doc/reader.ts | 11 +++++++--- 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/packages/backend/server/src/core/doc-service/__tests__/controller.spec.ts b/packages/backend/server/src/core/doc-service/__tests__/controller.spec.ts index b56a006c8a..b9923d4a52 100644 --- a/packages/backend/server/src/core/doc-service/__tests__/controller.spec.ts +++ b/packages/backend/server/src/core/doc-service/__tests__/controller.spec.ts @@ -183,6 +183,27 @@ test('should get doc content in json format', async t => { t.pass(); }); +test('should get full doc content in json format', async t => { + const { app } = t.context; + mock.method(t.context.databaseDocReader, 'getFullDocContent', async () => { + return { + title: 'test title', + summary: 'test summary', + }; + }); + + const docId = randomUUID(); + await app + .GET(`/rpc/workspaces/${workspace.id}/docs/${docId}/content?full=true`) + .set('x-access-token', t.context.crypto.sign(docId)) + .expect({ + title: 'test title', + summary: 'test summary', + }) + .expect(200); + t.pass(); +}); + test('should 404 when workspace content not found', async t => { const { app } = t.context; diff --git a/packages/backend/server/src/core/doc-service/controller.ts b/packages/backend/server/src/core/doc-service/controller.ts index 9bf3d9aa75..09a2db7322 100644 --- a/packages/backend/server/src/core/doc-service/controller.ts +++ b/packages/backend/server/src/core/doc-service/controller.ts @@ -7,6 +7,7 @@ import { RawBody, Res, } from '@nestjs/common'; +import { Args } from '@nestjs/graphql'; import type { Response } from 'express'; import { NotFound, SkipThrottle } from '../../base'; @@ -76,9 +77,12 @@ export class DocRpcController { @Get('/workspaces/:workspaceId/docs/:docId/content') async getDocContent( @Param('workspaceId') workspaceId: string, - @Param('docId') docId: string + @Param('docId') docId: string, + @Args('full', { nullable: true }) fullContent?: boolean ) { - const content = await this.docReader.getDocContent(workspaceId, docId); + const content = fullContent + ? await this.docReader.getFullDocContent(workspaceId, docId) + : await this.docReader.getDocContent(workspaceId, docId); if (!content) { throw new NotFound('Doc not found'); } diff --git a/packages/backend/server/src/core/doc/reader.ts b/packages/backend/server/src/core/doc/reader.ts index 50326fcab7..d35fe7cd8b 100644 --- a/packages/backend/server/src/core/doc/reader.ts +++ b/packages/backend/server/src/core/doc/reader.ts @@ -347,9 +347,10 @@ export class RpcDocReader extends DatabaseDocReader { protected override async getDocContentWithoutCache( workspaceId: string, - docId: string + docId: string, + fullContent = false ): Promise { - const url = `${this.config.docService.endpoint}/rpc/workspaces/${workspaceId}/docs/${docId}/content`; + const url = `${this.config.docService.endpoint}/rpc/workspaces/${workspaceId}/docs/${docId}/content?full=${fullContent}`; const accessToken = this.crypto.sign(docId); try { const res = await this.fetch(accessToken, url, 'GET'); @@ -366,7 +367,11 @@ export class RpcDocReader extends DatabaseDocReader { `Failed to fetch doc content ${url}, fallback to database doc reader`, err ); - return await super.getDocContentWithoutCache(workspaceId, docId); + return await super.getDocContentWithoutCache( + workspaceId, + docId, + fullContent + ); } }