From b72fb71bd8fccea13bc9c9759b7001eb49288b6a Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 1 Mar 2026 11:29:34 +0000 Subject: [PATCH] refactor: extract setMeta helper for PaginatedResponseArray --- .../make-paginated-response-query-options.ts | 5 +---- .../utils/make-paginated-response-query.ts | 12 ++++++++---- .../utils/paginated-response-array.test.ts | 15 +++++---------- 3 files changed, 14 insertions(+), 18 deletions(-) diff --git a/packages/nicolium/src/queries/utils/make-paginated-response-query-options.ts b/packages/nicolium/src/queries/utils/make-paginated-response-query-options.ts index d07e24403..32bc55c26 100644 --- a/packages/nicolium/src/queries/utils/make-paginated-response-query-options.ts +++ b/packages/nicolium/src/queries/utils/make-paginated-response-query-options.ts @@ -52,10 +52,7 @@ const makePaginatedResponseQueryOptions = data.pages.flatMap((page) => Array.isArray(page.items) ? page.items : [page.items], ), - ); - - Object.defineProperty(items, 'total', { value: lastPage.total, writable: true, enumerable: false, configurable: true }); - Object.defineProperty(items, 'partial', { value: lastPage.partial, writable: true, enumerable: false, configurable: true }); + ).setMeta(lastPage.total, lastPage.partial); return items as T3; } diff --git a/packages/nicolium/src/queries/utils/make-paginated-response-query.ts b/packages/nicolium/src/queries/utils/make-paginated-response-query.ts index 96f8d5fd3..07d87b9d3 100644 --- a/packages/nicolium/src/queries/utils/make-paginated-response-query.ts +++ b/packages/nicolium/src/queries/utils/make-paginated-response-query.ts @@ -21,6 +21,13 @@ class PaginatedResponseArray extends Array { } return arr; } + + /** Set metadata as non-enumerable to preserve TanStack Query structural sharing. */ + setMeta(total: number | undefined, partial: boolean | undefined): this { + Object.defineProperty(this, 'total', { value: total, writable: true, enumerable: false, configurable: true }); + Object.defineProperty(this, 'partial', { value: partial, writable: true, enumerable: false, configurable: true }); + return this; + } } type PaginatedResponseQueryResult = IsArray extends true @@ -71,10 +78,7 @@ const makePaginatedResponseQuery = data.pages.flatMap((page) => Array.isArray(page.items) ? page.items : [page.items], ), - ); - - Object.defineProperty(items, 'total', { value: lastPage.total, writable: true, enumerable: false, configurable: true }); - Object.defineProperty(items, 'partial', { value: lastPage.partial, writable: true, enumerable: false, configurable: true }); + ).setMeta(lastPage.total, lastPage.partial); return items as T3; } diff --git a/packages/nicolium/tests/queries/utils/paginated-response-array.test.ts b/packages/nicolium/tests/queries/utils/paginated-response-array.test.ts index ea068cd8c..c8bb2d195 100644 --- a/packages/nicolium/tests/queries/utils/paginated-response-array.test.ts +++ b/packages/nicolium/tests/queries/utils/paginated-response-array.test.ts @@ -24,26 +24,21 @@ describe('PaginatedResponseArray', () => { expect(items.length).toBe(100_000); }); - it('supports non-enumerable total and partial properties', () => { - const items = PaginatedResponseArray.from(['a', 'b', 'c']); - Object.defineProperty(items, 'total', { value: 42, writable: true, enumerable: false, configurable: true }); - Object.defineProperty(items, 'partial', { value: true, writable: true, enumerable: false, configurable: true }); + it('supports non-enumerable total and partial properties via setMeta', () => { + const items = PaginatedResponseArray.from(['a', 'b', 'c']).setMeta(42, true); expect(items.total).toBe(42); expect(items.partial).toBe(true); }); it('passes isPlainArray check with non-enumerable properties for structural sharing', () => { - const items = PaginatedResponseArray.from([1, 2, 3]); - Object.defineProperty(items, 'total', { value: 10, writable: true, enumerable: false, configurable: true }); - Object.defineProperty(items, 'partial', { value: false, writable: true, enumerable: false, configurable: true }); + const items = PaginatedResponseArray.from([1, 2, 3]).setMeta(10, false); expect(isPlainArray(items)).toBe(true); }); - it('total and partial properties are writable', () => { - const items = PaginatedResponseArray.from([1]); - Object.defineProperty(items, 'total', { value: 5, writable: true, enumerable: false, configurable: true }); + it('total and partial properties are writable after setMeta', () => { + const items = PaginatedResponseArray.from([1]).setMeta(5, false); items.total = 10; expect(items.total).toBe(10);