Skip to content

Commit

Permalink
fix(typings): relax query params typings to fix regressions (#510)
Browse files Browse the repository at this point in the history
  • Loading branch information
stipsan authored Jan 29, 2024
1 parent 6c98d92 commit b46583e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 46 deletions.
47 changes: 26 additions & 21 deletions src/SanityClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import type {
QueryOptions,
QueryParams,
QueryParamsLikelyByMistake,
QueryParamsParameter,
QueryParamsWithoutQueryOptions,
RawQueryResponse,
RawRequestOptions,
Expand Down Expand Up @@ -134,33 +133,36 @@ export class ObservableSanityClient {
*
* @param query - GROQ-query to perform
*/
fetch<
R = Any,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Q extends never | undefined | Record<string, never> = never,
>(query: string): Observable<R>
fetch<R = Any>(query: string): Observable<R>
/**
* Perform a GROQ-query against the configured dataset.
*
* @param query - GROQ-query to perform
* @param params - Optional query parameters
* @param options - Optional request options
* @param params - Query parameters
*/
fetch<R = Any, Q = QueryParamsWithoutQueryOptions>(query: string, params: Q): Observable<R>
/**
* Perform a GROQ-query against the configured dataset.
*
* @param query - GROQ-query to perform
* @param params - Query parameters
* @param options - Request options
*/
fetch<R = Any, Q = QueryParamsWithoutQueryOptions>(
query: string,
params: QueryParamsParameter<Q>,
options?: FilteredResponseQueryOptions,
params: Q | undefined,
options: FilteredResponseQueryOptions,
): Observable<R>
/**
* Perform a GROQ-query against the configured dataset.
*
* @param query - GROQ-query to perform
* @param params - Optional query parameters
* @param params - Query parameters
* @param options - Request options
*/
fetch<R = Any, Q = QueryParamsWithoutQueryOptions>(
query: string,
params: QueryParamsParameter<Q>,
params: Q | undefined,
options: UnfilteredResponseQueryOptions,
): Observable<RawQueryResponse<R>>
/**
Expand Down Expand Up @@ -793,22 +795,25 @@ export class SanityClient {
*
* @param query - GROQ-query to perform
*/
fetch<
R = Any,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Q extends never | undefined | Record<string, never> = never,
>(query: string): Promise<R>
fetch<R = Any>(query: string): Promise<R>
/**
* Perform a GROQ-query against the configured dataset.
*
* @param query - GROQ-query to perform
* @param params - Optional query parameters
*/
fetch<R = Any, Q = QueryParamsWithoutQueryOptions>(query: string, params: Q): Promise<R>
/**
* Perform a GROQ-query against the configured dataset.
*
* @param query - GROQ-query to perform
* @param params - Optional query parameters
* @param options - Optional request options
* @param options - Request options
*/
fetch<R = Any, Q = QueryParamsWithoutQueryOptions>(
query: string,
params: QueryParamsParameter<Q>,
options?: FilteredResponseQueryOptions,
params: Q | undefined,
options: FilteredResponseQueryOptions,
): Promise<R>
/**
* Perform a GROQ-query against the configured dataset.
Expand All @@ -819,7 +824,7 @@ export class SanityClient {
*/
fetch<R = Any, Q = QueryParamsWithoutQueryOptions>(
query: string,
params: QueryParamsParameter<Q>,
params: Q | undefined,
options: UnfilteredResponseQueryOptions,
): Promise<RawQueryResponse<R>>
/**
Expand Down
13 changes: 0 additions & 13 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -495,19 +495,6 @@ export type QueryParamsWithoutQueryOptions = {
[K in keyof _QueryParamsLikelyByMistake]: never
} & QueryParams

/**
* Transform a QueryParams generic type to a valid parameter type for `client.fetch
* @public
*/
export type QueryParamsParameter<QueryParamsParameterType> =
QueryParamsParameterType extends QueryParams
? QueryParamsParameterType
: QueryParamsParameterType extends Record<string, never>
? Record<string, never>
: QueryParamsParameterType extends undefined
? undefined | Record<string, never>
: never

/** @internal */
export type MutationSelection = {query: string; params?: QueryParams} | {id: string | string[]}
/** @internal */
Expand Down
24 changes: 12 additions & 12 deletions test/client.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ describe('client.fetch', () => {
{type: 'post'},
),
).toMatchTypeOf<number>()
expectTypeOf(
await client.fetch<number, never>('count(*[_type == $type])'),
).toMatchTypeOf<number>()
// expectTypeOf(
// await client.fetch<number, never>('count(*[_type == $type])'),
// ).toMatchTypeOf<number>()
expectTypeOf(
await client.fetch<number, never>(
'count(*[_type == $type])',
Expand All @@ -67,25 +67,25 @@ describe('client.fetch', () => {
{},
),
).toMatchTypeOf<number>()
expectTypeOf(
await client.fetch<number, undefined>('count(*[_type == $type])'),
).toMatchTypeOf<number>()
// expectTypeOf(
// await client.fetch<number, undefined>('count(*[_type == $type])'),
// ).toMatchTypeOf<number>()
expectTypeOf(
await client.fetch<number, undefined>('count(*[_type == $type])', undefined),
).toMatchTypeOf<number>()
expectTypeOf(
await client.fetch<number, undefined>('count(*[_type == $type])', {}),
).toMatchTypeOf<number>()
// expectTypeOf(
// await client.fetch<number, undefined>('count(*[_type == $type])', {}),
// ).toMatchTypeOf<number>()
expectTypeOf(
await client.fetch<number, undefined>(
'count(*[_type == $type])',
// @ts-expect-error -- should fail
{type: 'post'},
),
).toMatchTypeOf<number>()
expectTypeOf(
await client.fetch<number, Record<string, never>>('count(*[_type == $type])'),
).toMatchTypeOf<number>()
// expectTypeOf(
// await client.fetch<number, Record<string, never>>('count(*[_type == $type])'),
// ).toMatchTypeOf<number>()
expectTypeOf(
await client.fetch<number, Record<string, never>>(
'count(*[_type == $type])',
Expand Down

0 comments on commit b46583e

Please sign in to comment.