From 4bcd77a56c411a760e463eb4382fbd0a69784198 Mon Sep 17 00:00:00 2001 From: Nathaniel Tucker Date: Fri, 23 Feb 2024 12:07:24 +0000 Subject: [PATCH] fix: Resource.getList.schema args types --- .changeset/stupid-houses-itch.md | 5 +++ .../react/src/hooks/__tests__/useCache.tsx | 2 +- packages/rest/src/RestEndpointTypes.ts | 9 ++++++ packages/rest/src/resourceTypes.ts | 31 ++++++++++--------- 4 files changed, 31 insertions(+), 16 deletions(-) create mode 100644 .changeset/stupid-houses-itch.md diff --git a/.changeset/stupid-houses-itch.md b/.changeset/stupid-houses-itch.md new file mode 100644 index 000000000000..5536f19d743e --- /dev/null +++ b/.changeset/stupid-houses-itch.md @@ -0,0 +1,5 @@ +--- +"@data-client/rest": patch +--- + +fix: Resource.getList.schema args types diff --git a/packages/react/src/hooks/__tests__/useCache.tsx b/packages/react/src/hooks/__tests__/useCache.tsx index 8b26053c13bd..42d6e5dc32ae 100644 --- a/packages/react/src/hooks/__tests__/useCache.tsx +++ b/packages/react/src/hooks/__tests__/useCache.tsx @@ -22,7 +22,7 @@ describe('useCache()', () => { renderDataClient = makeRenderDataClient(CacheProvider); }); - it('should be null with empty state', () => { + it('should be undefined with empty state', () => { const { result } = renderDataClient(() => { return useCache(CoolerArticleResource.get, { id: payload.id }); }, {}); diff --git a/packages/rest/src/RestEndpointTypes.ts b/packages/rest/src/RestEndpointTypes.ts index 98bb2f382712..6354ab84494e 100644 --- a/packages/rest/src/RestEndpointTypes.ts +++ b/packages/rest/src/RestEndpointTypes.ts @@ -560,6 +560,15 @@ export type ParamFetchNoBody = | ((this: EndpointInstanceInterface) => Promise) : (this: EndpointInstanceInterface, params: P) => Promise; +// same algorithm, but for Args (aka readonly any[]) +export type ParamToArgs

= + P extends undefined ? [] + : {} extends P ? + keyof P extends never ? + [] + : [] | [P] + : [P]; + type IfTypeScriptLooseNull = 1 | undefined extends 1 ? Y : N; export type KeyofRestEndpoint = keyof RestInstance; diff --git a/packages/rest/src/resourceTypes.ts b/packages/rest/src/resourceTypes.ts index cbbcf1434ecf..1a2d400f74a0 100644 --- a/packages/rest/src/resourceTypes.ts +++ b/packages/rest/src/resourceTypes.ts @@ -2,11 +2,17 @@ import type { Collection, schema } from '@data-client/endpoint'; import type { Schema } from '@data-client/endpoint'; import type { Denormalize } from '@data-client/endpoint'; -import type { PathArgs, ResourcePath, ShortenPath } from './pathTypes.js'; +import type { + KeysToArgs, + PathArgs, + ResourcePath, + ShortenPath, +} from './pathTypes.js'; import { Extendable } from './resourceExtendable.js'; import RestEndpoint, { GetEndpoint, MutateEndpoint, + ParamToArgs, RestInstanceBase, RestTypeNoBody, } from './RestEndpoint.js'; @@ -74,13 +80,11 @@ export interface Resource< path: ShortenPath; schema: schema.Collection< [O['schema']], - [ - 'searchParams' extends keyof O ? - O['searchParams'] extends undefined ? - PathArgs> - : O['searchParams'] & PathArgs> - : PathArgs>, - ] + ParamToArgs< + O['searchParams'] extends undefined ? + KeysToArgs> + : O['searchParams'] & PathArgs> + > >; body: 'body' extends keyof O ? O['body'] : Partial>; @@ -92,13 +96,10 @@ export interface Resource< path: ShortenPath; schema: schema.Collection< [O['schema']], - [ - 'searchParams' extends keyof O ? - O['searchParams'] extends undefined ? - PathArgs> - : O['searchParams'] & PathArgs> - : PathArgs>, - ] + ParamToArgs< + (Record | undefined) & + PathArgs> + > >; body: 'body' extends keyof O ? O['body'] : Partial>;