Skip to content

Commit

Permalink
fix: revert "add support for returnQuery option, default to false" (
Browse files Browse the repository at this point in the history
#545)

This reverts commit dee015b.
  • Loading branch information
rexxars committed Feb 14, 2024
1 parent a65e8cb commit e6b4e1c
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 187 deletions.
26 changes: 0 additions & 26 deletions src/SanityClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,12 @@ import type {
QueryOptions,
QueryParams,
QueryWithoutParams,
RawQuerylessQueryResponse,
RawQueryResponse,
RawRequestOptions,
SanityDocument,
SanityDocumentStub,
SingleMutationResult,
UnfilteredResponseQueryOptions,
UnfilteredResponseWithoutQuery,
} from './types'
import {ObservableUsersClient, UsersClient} from './users/UsersClient'

Expand Down Expand Up @@ -162,18 +160,6 @@ export class ObservableSanityClient {
params: Q extends QueryWithoutParams ? QueryWithoutParams : Q,
options: UnfilteredResponseQueryOptions,
): Observable<RawQueryResponse<R>>
/**
* Perform a GROQ-query against the configured dataset.
*
* @param query - GROQ-query to perform
* @param params - Optional query parameters
* @param options - Request options
*/
fetch<R = Any, Q extends QueryWithoutParams | QueryParams = QueryParams>(
query: string,
params: Q extends QueryWithoutParams ? QueryWithoutParams : Q,
options: UnfilteredResponseWithoutQuery,
): Observable<RawQuerylessQueryResponse<R>>
fetch<R, Q>(
query: string,
params?: Q,
Expand Down Expand Up @@ -813,18 +799,6 @@ export class SanityClient {
params: Q extends QueryWithoutParams ? QueryWithoutParams : Q,
options: UnfilteredResponseQueryOptions,
): Promise<RawQueryResponse<R>>
/**
* Perform a GROQ-query against the configured dataset.
*
* @param query - GROQ-query to perform
* @param params - Optional query parameters
* @param options - Request options
*/
fetch<R = Any, Q extends QueryWithoutParams | QueryParams = QueryParams>(
query: string,
params: Q extends QueryWithoutParams ? QueryWithoutParams : Q,
options: UnfilteredResponseWithoutQuery,
): Promise<RawQuerylessQueryResponse<R>>
fetch<R, Q>(query: string, params?: Q, options?: QueryOptions): Promise<RawQueryResponse<R> | R> {
return lastValueFrom(
dataMethods._fetch<R, Q>(
Expand Down
13 changes: 1 addition & 12 deletions src/data/dataMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,6 @@ export function _fetch<R, Q>(
const params = stega.enabled ? vercelStegaCleanAll(_params) : _params
const mapResponse =
options.filterResponse === false ? (res: Any) => res : (res: Any) => res.result

// Default to not returning the query, unless `filterResponse` is `false`,
// or `returnQuery` is explicitly set. `true` is the default in Content Lake, so skip if truthy
const returnQuery = options.filterResponse === false && options.returnQuery !== false

const {cache, next, ...opts} = {
// Opt out of setting a `signal` on an internal `fetch` if one isn't provided.
// This is necessary in React Server Components to avoid opting out of Request Memoization.
Expand All @@ -98,13 +93,7 @@ export function _fetch<R, Q>(
? {...opts, fetch: {cache, next}}
: opts

const $request = _dataRequest(
client,
httpRequest,
'query',
{query, params, options: {returnQuery}},
reqOpts,
)
const $request = _dataRequest(client, httpRequest, 'query', {query, params}, reqOpts)
return stega.enabled
? $request.pipe(
combineLatestWith(
Expand Down
5 changes: 1 addition & 4 deletions src/data/encodeQueryString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const encodeQueryString = ({
}) => {
const searchParams = new URLSearchParams()
// We generally want tag at the start of the query string
const {tag, returnQuery, ...opts} = options
const {tag, ...opts} = options
// We're using `append` instead of `set` to support React Native: https://github.com/facebook/react-native/blob/1982c4722fcc51aa87e34cf562672ee4aff540f1/packages/react-native/Libraries/Blob/URL.js#L86-L88
if (tag) searchParams.append('tag', tag)
searchParams.append('query', query)
Expand All @@ -26,8 +26,5 @@ export const encodeQueryString = ({
if (value) searchParams.append(key, `${value}`)
}

// `returnQuery` is default `true`, so needs an explicit `false` handling
if (returnQuery === false) searchParams.append('returnQuery', 'false')

return `?${searchParams}`
}
29 changes: 1 addition & 28 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -452,8 +452,6 @@ export interface QueryParams {
/** @deprecated you're using a fetch option as a GROQ parameter, this is likely a mistake */
resultSourceMap?: never
/** @deprecated you're using a fetch option as a GROQ parameter, this is likely a mistake */
returnQuery?: never
/** @deprecated you're using a fetch option as a GROQ parameter, this is likely a mistake */
signal?: never
/** @deprecated you're using a fetch option as a GROQ parameter, this is likely a mistake */
stega?: never
Expand Down Expand Up @@ -723,7 +721,6 @@ export interface ListenOptions {
export interface ResponseQueryOptions extends RequestOptions {
perspective?: ClientPerspective
resultSourceMap?: boolean | 'withKeyArraySelector'
returnQuery?: boolean
useCdn?: boolean
stega?: boolean | StegaConfig
// The `cache` and `next` options are specific to the Next.js App Router integration
Expand All @@ -739,31 +736,10 @@ export interface FilteredResponseQueryOptions extends ResponseQueryOptions {
/** @public */
export interface UnfilteredResponseQueryOptions extends ResponseQueryOptions {
filterResponse: false

/**
* When `filterResponse` is `false`, `returnQuery` also defaults to `true` for
* backwards compatibility (on the client side, not from the content lake API).
* Can also explicitly be set to `true`.
*/
returnQuery?: true
}

/**
* When using `filterResponse: false`, but you do not wish to receive back the query from
* the content lake API.
*
* @public
*/
export interface UnfilteredResponseWithoutQuery extends ResponseQueryOptions {
filterResponse: false
returnQuery: false
}

/** @public */
export type QueryOptions =
| FilteredResponseQueryOptions
| UnfilteredResponseQueryOptions
| UnfilteredResponseWithoutQuery
export type QueryOptions = FilteredResponseQueryOptions | UnfilteredResponseQueryOptions

/** @public */
export interface RawQueryResponse<R> {
Expand All @@ -773,9 +749,6 @@ export interface RawQueryResponse<R> {
resultSourceMap?: ContentSourceMap
}

/** @public */
export type RawQuerylessQueryResponse<R> = Omit<RawQueryResponse<R>, 'query'>

/** @internal */
export type BaseMutationOptions = RequestOptions & {
visibility?: 'sync' | 'async' | 'deferred'
Expand Down
15 changes: 1 addition & 14 deletions test-next/client.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
/// <reference types="next/types/global" />

import {
createClient,
type QueryOptions,
type QueryParams,
type RawQuerylessQueryResponse,
type RawQueryResponse,
} from '@sanity/client'
import {createClient, QueryOptions, QueryParams, RawQueryResponse} from '@sanity/client'
import {describe, expectTypeOf, test} from 'vitest'

describe('client.fetch', () => {
Expand Down Expand Up @@ -39,13 +33,6 @@ describe('client.fetch', () => {
{filterResponse: false, cache: 'force-cache', next: {revalidate: 60, tags: ['post']}},
),
).toMatchTypeOf<RawQueryResponse<any>>()
expectTypeOf(
await client.fetch(
'*[_type == $type]',
{type: 'post'},
{filterResponse: false, returnQuery: false},
),
).toMatchTypeOf<RawQuerylessQueryResponse<any>>()
})
test('generics', async () => {
expectTypeOf(
Expand Down
Loading

0 comments on commit e6b4e1c

Please sign in to comment.