Skip to content

Commit

Permalink
Revert "feat(stega): allow setting stega options on client.fetch (#…
Browse files Browse the repository at this point in the history
…419)"

This reverts commit d38afd8.
  • Loading branch information
stipsan authored Nov 28, 2023
1 parent 7726326 commit 7e03f49
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 369 deletions.
5 changes: 0 additions & 5 deletions src/data/dataMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,6 @@ export function _fetch<R, Q extends QueryParams>(
params?: Q,
options: FilteredResponseQueryOptions | UnfilteredResponseQueryOptions = {},
): Observable<RawQueryResponse<R> | R> {
if ('stega' in options && options['stega'] !== undefined && options['stega'] !== false) {
throw new Error(
`It looks like you're using options meant for '@sanity/client/stega'. Make sure you're using the right import. Or set 'stega' in 'fetch' to 'false'.`,
)
}
const mapResponse =
options.filterResponse === false ? (res: Any) => res : (res: Any) => res.result
const {cache, next, ...opts} = {
Expand Down
46 changes: 12 additions & 34 deletions src/stega/SanityStegaClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,9 @@ import type {
RawQueryResponse,
UnfilteredResponseQueryOptions,
} from '../types'
import {
defaultStegaConfig,
initStegaConfig,
splitConfig,
splitStegaConfigFromFetchOptions,
} from './config'
import {defaultStegaConfig, initStegaConfig, splitConfig} from './config'
import {stegaEncodeSourceMap} from './stegaEncodeSourceMap'
import {
ClientStegaConfig,
InitializedClientStegaConfig,
InitializedStegaConfig,
StegaConfig,
} from './types'
import {ClientStegaConfig, InitializedClientStegaConfig, InitializedStegaConfig} from './types'

/** @public */
export class ObservableSanityStegaClient extends ObservableSanityClient {
Expand Down Expand Up @@ -110,7 +100,7 @@ export class ObservableSanityStegaClient extends ObservableSanityClient {
fetch<R = Any, Q = QueryParams>(
query: string,
params: Q | undefined,
options: FilteredResponseQueryOptions & {stega?: boolean | StegaConfig},
options: FilteredResponseQueryOptions,
): Observable<R>
/**
* Perform a GROQ-query against the configured dataset.
Expand All @@ -122,20 +112,14 @@ export class ObservableSanityStegaClient extends ObservableSanityClient {
fetch<R = Any, Q = QueryParams>(
query: string,
params: Q | undefined,
options: UnfilteredResponseQueryOptions & {stega?: boolean | StegaConfig},
options: UnfilteredResponseQueryOptions,
): Observable<RawQueryResponse<R>>
fetch<R, Q extends QueryParams>(
query: string,
params?: Q,
_options: (FilteredResponseQueryOptions | UnfilteredResponseQueryOptions) & {
stega?: boolean | StegaConfig
} = {},
options: FilteredResponseQueryOptions | UnfilteredResponseQueryOptions = {},
): Observable<RawQueryResponse<R> | R> {
const {stegaConfig, fetchOptions: options} = splitStegaConfigFromFetchOptions(
_options,
this.stegaConfig,
)
if (!stegaConfig.enabled) {
if (!this.stegaConfig.enabled) {
return super.fetch<R, Q>(query, params, options as Any)
}
const {filterResponse: originalFilterResponse = true} = options
Expand All @@ -151,7 +135,7 @@ export class ObservableSanityStegaClient extends ObservableSanityClient {
.pipe(
map((res: Any) => {
const {result: _result, resultSourceMap} = res as RawQueryResponse<R>
const result = stegaEncodeSourceMap(_result, resultSourceMap, stegaConfig)
const result = stegaEncodeSourceMap(_result, resultSourceMap, this.stegaConfig)
return originalFilterResponse ? result : {...res, result}
}),
)
Expand Down Expand Up @@ -249,7 +233,7 @@ export class SanityStegaClient extends SanityClient {
fetch<R = Any, Q = QueryParams>(
query: string,
params: Q | undefined,
options: FilteredResponseQueryOptions & {stega?: boolean | StegaConfig},
options: FilteredResponseQueryOptions,
): Promise<R>
/**
* Perform a GROQ-query against the configured dataset.
Expand All @@ -261,20 +245,14 @@ export class SanityStegaClient extends SanityClient {
fetch<R = Any, Q = QueryParams>(
query: string,
params: Q | undefined,
options: UnfilteredResponseQueryOptions & {stega?: boolean | StegaConfig},
options: UnfilteredResponseQueryOptions,
): Promise<RawQueryResponse<R>>
fetch<R, Q extends QueryParams>(
query: string,
params?: Q,
_options: (FilteredResponseQueryOptions | UnfilteredResponseQueryOptions) & {
stega?: boolean | StegaConfig
} = {},
options: FilteredResponseQueryOptions | UnfilteredResponseQueryOptions = {},
): Promise<RawQueryResponse<R> | R> {
const {stegaConfig, fetchOptions: options} = splitStegaConfigFromFetchOptions(
_options,
this.stegaConfig,
)
if (!stegaConfig.enabled) {
if (!this.stegaConfig.enabled) {
return super.fetch<R, Q>(query, params, options as Any)
}
const {filterResponse: originalFilterResponse = true} = options
Expand All @@ -289,7 +267,7 @@ export class SanityStegaClient extends SanityClient {
)
.then((res: Any) => {
const {result: _result, resultSourceMap} = res as RawQueryResponse<R>
const result = stegaEncodeSourceMap(_result, resultSourceMap, stegaConfig)
const result = stegaEncodeSourceMap(_result, resultSourceMap, this.stegaConfig)
return originalFilterResponse ? result : {...res, result}
})
}
Expand Down
23 changes: 1 addition & 22 deletions src/stega/config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import type {
ClientConfig,
FilteredResponseQueryOptions,
UnfilteredResponseQueryOptions,
} from '../types'
import type {ClientConfig} from '../types'
import type {ClientStegaConfig, InitializedStegaConfig, StegaConfig} from './types'

export const defaultStegaConfig: StegaConfig = {
Expand Down Expand Up @@ -57,20 +53,3 @@ export const initStegaConfig = (

return newConfig
}

export function splitStegaConfigFromFetchOptions(
options: (FilteredResponseQueryOptions | UnfilteredResponseQueryOptions) & {
stega?: boolean | StegaConfig
},
initializedStegaConfig: InitializedStegaConfig,
): {
fetchOptions: FilteredResponseQueryOptions | UnfilteredResponseQueryOptions
stegaConfig: InitializedStegaConfig
} {
const {stega = {}, ...fetchOptions} = options
const stegaConfig = initStegaConfig(
typeof stega === 'boolean' ? {enabled: stega} : stega,
initializedStegaConfig,
)
return {fetchOptions, stegaConfig}
}
Loading

0 comments on commit 7e03f49

Please sign in to comment.