Skip to content

Commit

Permalink
feat(stega): allow setting stega options on client.fetch (#419)
Browse files Browse the repository at this point in the history
  • Loading branch information
stipsan authored Nov 28, 2023
1 parent 2a9bbe0 commit d38afd8
Show file tree
Hide file tree
Showing 4 changed files with 369 additions and 182 deletions.
5 changes: 5 additions & 0 deletions src/data/dataMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ 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: 34 additions & 12 deletions src/stega/SanityStegaClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,19 @@ import type {
RawQueryResponse,
UnfilteredResponseQueryOptions,
} from '../types'
import {defaultStegaConfig, initStegaConfig, splitConfig} from './config'
import {
defaultStegaConfig,
initStegaConfig,
splitConfig,
splitStegaConfigFromFetchOptions,
} from './config'
import {stegaEncodeSourceMap} from './stegaEncodeSourceMap'
import {ClientStegaConfig, InitializedClientStegaConfig, InitializedStegaConfig} from './types'
import {
ClientStegaConfig,
InitializedClientStegaConfig,
InitializedStegaConfig,
StegaConfig,
} from './types'

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

export const defaultStegaConfig: StegaConfig = {
Expand Down Expand Up @@ -53,3 +57,20 @@ 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

3 comments on commit d38afd8

@tomaszzmudzinski
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey,

There is some ts error. Take a look at it:

image

@tomaszzmudzinski
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Option with 3 parameters is not even visible:

image

@tomaszzmudzinski
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stipsan Could you check this? 😸

Please sign in to comment.