Skip to content

Commit

Permalink
fix: Resource.getList.schema args types
Browse files Browse the repository at this point in the history
  • Loading branch information
ntucker committed Feb 23, 2024
1 parent 062c150 commit 4bcd77a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/stupid-houses-itch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@data-client/rest": patch
---

fix: Resource.getList.schema args types
2 changes: 1 addition & 1 deletion packages/react/src/hooks/__tests__/useCache.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}, {});
Expand Down
9 changes: 9 additions & 0 deletions packages/rest/src/RestEndpointTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,15 @@ export type ParamFetchNoBody<P, R = any> =
| ((this: EndpointInstanceInterface) => Promise<R>)
: (this: EndpointInstanceInterface, params: P) => Promise<R>;

// same algorithm, but for Args (aka readonly any[])
export type ParamToArgs<P> =
P extends undefined ? []
: {} extends P ?
keyof P extends never ?
[]
: [] | [P]
: [P];

type IfTypeScriptLooseNull<Y, N> = 1 | undefined extends 1 ? Y : N;

export type KeyofRestEndpoint = keyof RestInstance;
Expand Down
31 changes: 16 additions & 15 deletions packages/rest/src/resourceTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -74,13 +80,11 @@ export interface Resource<
path: ShortenPath<O['path']>;
schema: schema.Collection<
[O['schema']],
[
'searchParams' extends keyof O ?
O['searchParams'] extends undefined ?
PathArgs<ShortenPath<O['path']>>
: O['searchParams'] & PathArgs<ShortenPath<O['path']>>
: PathArgs<ShortenPath<O['path']>>,
]
ParamToArgs<
O['searchParams'] extends undefined ?
KeysToArgs<ShortenPath<O['path']>>
: O['searchParams'] & PathArgs<ShortenPath<O['path']>>
>
>;
body: 'body' extends keyof O ? O['body']
: Partial<Denormalize<O['schema']>>;
Expand All @@ -92,13 +96,10 @@ export interface Resource<
path: ShortenPath<O['path']>;
schema: schema.Collection<
[O['schema']],
[
'searchParams' extends keyof O ?
O['searchParams'] extends undefined ?
PathArgs<ShortenPath<O['path']>>
: O['searchParams'] & PathArgs<ShortenPath<O['path']>>
: PathArgs<ShortenPath<O['path']>>,
]
ParamToArgs<
(Record<string, number | string | boolean> | undefined) &
PathArgs<ShortenPath<O['path']>>
>
>;
body: 'body' extends keyof O ? O['body']
: Partial<Denormalize<O['schema']>>;
Expand Down

0 comments on commit 4bcd77a

Please sign in to comment.