From b46583ee99cf5b50c467c507fece83d9fa4e9519 Mon Sep 17 00:00:00 2001 From: Cody Olsen <81981+stipsan@users.noreply.github.com> Date: Mon, 29 Jan 2024 13:02:30 +0100 Subject: [PATCH] fix(typings): relax query params typings to fix regressions (#510) --- src/SanityClient.ts | 47 ++++++++++++++++++++++++------------------- src/types.ts | 13 ------------ test/client.test-d.ts | 24 +++++++++++----------- 3 files changed, 38 insertions(+), 46 deletions(-) diff --git a/src/SanityClient.ts b/src/SanityClient.ts index 22f47342..7ea8a480 100644 --- a/src/SanityClient.ts +++ b/src/SanityClient.ts @@ -28,7 +28,6 @@ import type { QueryOptions, QueryParams, QueryParamsLikelyByMistake, - QueryParamsParameter, QueryParamsWithoutQueryOptions, RawQueryResponse, RawRequestOptions, @@ -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 = never, - >(query: string): Observable + fetch(query: string): Observable /** * 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(query: string, params: Q): Observable + /** + * Perform a GROQ-query against the configured dataset. + * + * @param query - GROQ-query to perform + * @param params - Query parameters + * @param options - Request options */ fetch( query: string, - params: QueryParamsParameter, - options?: FilteredResponseQueryOptions, + params: Q | undefined, + options: FilteredResponseQueryOptions, ): Observable /** * 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( query: string, - params: QueryParamsParameter, + params: Q | undefined, options: UnfilteredResponseQueryOptions, ): Observable> /** @@ -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 = never, - >(query: string): Promise + fetch(query: string): Promise + /** + * Perform a GROQ-query against the configured dataset. + * + * @param query - GROQ-query to perform + * @param params - Optional query parameters + */ + fetch(query: string, params: Q): Promise /** * 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( query: string, - params: QueryParamsParameter, - options?: FilteredResponseQueryOptions, + params: Q | undefined, + options: FilteredResponseQueryOptions, ): Promise /** * Perform a GROQ-query against the configured dataset. @@ -819,7 +824,7 @@ export class SanityClient { */ fetch( query: string, - params: QueryParamsParameter, + params: Q | undefined, options: UnfilteredResponseQueryOptions, ): Promise> /** diff --git a/src/types.ts b/src/types.ts index 6e3802bf..7bfcd0c9 100644 --- a/src/types.ts +++ b/src/types.ts @@ -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 extends QueryParams - ? QueryParamsParameterType - : QueryParamsParameterType extends Record - ? Record - : QueryParamsParameterType extends undefined - ? undefined | Record - : never - /** @internal */ export type MutationSelection = {query: string; params?: QueryParams} | {id: string | string[]} /** @internal */ diff --git a/test/client.test-d.ts b/test/client.test-d.ts index a002d9ba..f473c7bb 100644 --- a/test/client.test-d.ts +++ b/test/client.test-d.ts @@ -50,9 +50,9 @@ describe('client.fetch', () => { {type: 'post'}, ), ).toMatchTypeOf() - expectTypeOf( - await client.fetch('count(*[_type == $type])'), - ).toMatchTypeOf() + // expectTypeOf( + // await client.fetch('count(*[_type == $type])'), + // ).toMatchTypeOf() expectTypeOf( await client.fetch( 'count(*[_type == $type])', @@ -67,15 +67,15 @@ describe('client.fetch', () => { {}, ), ).toMatchTypeOf() - expectTypeOf( - await client.fetch('count(*[_type == $type])'), - ).toMatchTypeOf() + // expectTypeOf( + // await client.fetch('count(*[_type == $type])'), + // ).toMatchTypeOf() expectTypeOf( await client.fetch('count(*[_type == $type])', undefined), ).toMatchTypeOf() - expectTypeOf( - await client.fetch('count(*[_type == $type])', {}), - ).toMatchTypeOf() + // expectTypeOf( + // await client.fetch('count(*[_type == $type])', {}), + // ).toMatchTypeOf() expectTypeOf( await client.fetch( 'count(*[_type == $type])', @@ -83,9 +83,9 @@ describe('client.fetch', () => { {type: 'post'}, ), ).toMatchTypeOf() - expectTypeOf( - await client.fetch>('count(*[_type == $type])'), - ).toMatchTypeOf() + // expectTypeOf( + // await client.fetch>('count(*[_type == $type])'), + // ).toMatchTypeOf() expectTypeOf( await client.fetch>( 'count(*[_type == $type])',