Skip to content

Commit 1ebce57

Browse files
committed
Handle legacy document list responses
1 parent d6d4682 commit 1ebce57

2 files changed

Lines changed: 45 additions & 1 deletion

File tree

src/resources/__tests__/documents.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,28 @@ describe('Documents Resource', () => {
6767
expect(mockHttpClient.get).toHaveBeenCalledWith('/v1/documents', undefined);
6868
});
6969

70+
it('should synthesize document pagination for legacy API responses', async () => {
71+
mockHttpClient.get.mockResolvedValue({
72+
namespace: 'default',
73+
documents: [
74+
{
75+
documentId: 'doc-123',
76+
namespace: 'default',
77+
status: 'active',
78+
},
79+
],
80+
});
81+
82+
const response = await documents.list();
83+
84+
expect(response.pagination).toEqual({
85+
page: 1,
86+
pageSize: 1,
87+
total: 1,
88+
totalPages: 1,
89+
});
90+
});
91+
7092
it('should get one document by id', async () => {
7193
mockHttpClient.get.mockResolvedValue({
7294
documentId: 'doc-123',

src/resources/documents.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ type RequestConfig = {
1313
params: Record<string, string | number | boolean>;
1414
};
1515

16+
type MaybePaginatedDocumentListResponse = Omit<DocumentListResponse, 'pagination'> &
17+
Partial<Pick<DocumentListResponse, 'pagination'>>;
18+
1619
/**
1720
* Resource for canonical document lifecycle operations.
1821
*/
@@ -21,10 +24,11 @@ export class Documents extends BaseResource {
2124
* List canonical documents in a namespace.
2225
*/
2326
async list(params?: DocumentListParams): Promise<DocumentListResponse> {
24-
return this.httpClient.get<DocumentListResponse>(
27+
const response = await this.httpClient.get<DocumentListResponse>(
2528
'/v1/documents',
2629
this.createDocumentListRequestConfig(params),
2730
);
31+
return this.normalizeDocumentListResponse(response);
2832
}
2933

3034
/**
@@ -87,6 +91,24 @@ export class Documents extends BaseResource {
8791
return Object.keys(queryParams).length > 0 ? { params: queryParams } : undefined;
8892
}
8993

94+
private normalizeDocumentListResponse(response: DocumentListResponse): DocumentListResponse {
95+
const maybePaginatedResponse = response as MaybePaginatedDocumentListResponse;
96+
if (maybePaginatedResponse.pagination) {
97+
return response;
98+
}
99+
100+
const total = response.documents.length;
101+
return {
102+
...response,
103+
pagination: {
104+
page: 1,
105+
pageSize: total,
106+
total,
107+
totalPages: total > 0 ? 1 : 0,
108+
},
109+
};
110+
}
111+
90112
private createChunkListRequestConfig(
91113
params?: DocumentChunkListParams,
92114
): RequestConfig | undefined {

0 commit comments

Comments
 (0)