@sanity/client 6.11.3 → 6.11.4-canary.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. package/README.md +85 -4
  2. package/dist/_chunks/{browserMiddleware-1MULg59S.cjs → browserMiddleware-Pe3R-Bkh.cjs} +106 -20
  3. package/dist/_chunks/browserMiddleware-Pe3R-Bkh.cjs.map +1 -0
  4. package/dist/_chunks/{browserMiddleware-heyg-fDk.js → browserMiddleware-wklv-F5c.js} +108 -22
  5. package/dist/_chunks/browserMiddleware-wklv-F5c.js.map +1 -0
  6. package/dist/_chunks/{nodeMiddleware-CHxg1-zH.cjs → nodeMiddleware-7PkLkt-m.cjs} +107 -21
  7. package/dist/_chunks/nodeMiddleware-7PkLkt-m.cjs.map +1 -0
  8. package/dist/_chunks/{nodeMiddleware-u_fUUnxa.js → nodeMiddleware-LGORMZpx.js} +109 -23
  9. package/dist/_chunks/nodeMiddleware-LGORMZpx.js.map +1 -0
  10. package/dist/_chunks/stegaEncodeSourceMap-2y-qTNKB.cjs +230 -0
  11. package/dist/_chunks/stegaEncodeSourceMap-2y-qTNKB.cjs.map +1 -0
  12. package/dist/_chunks/stegaEncodeSourceMap-9L0STMBo.js +475 -0
  13. package/dist/_chunks/stegaEncodeSourceMap-9L0STMBo.js.map +1 -0
  14. package/dist/_chunks/stegaEncodeSourceMap-gXzb3LIV.js +226 -0
  15. package/dist/_chunks/stegaEncodeSourceMap-gXzb3LIV.js.map +1 -0
  16. package/dist/_chunks/stegaEncodeSourceMap-vNYIaMiu.cjs +479 -0
  17. package/dist/_chunks/stegaEncodeSourceMap-vNYIaMiu.cjs.map +1 -0
  18. package/dist/index.browser.cjs +1 -1
  19. package/dist/index.browser.js +2 -2
  20. package/dist/index.cjs +1 -1
  21. package/dist/index.d.ts +133 -0
  22. package/dist/index.js +2 -2
  23. package/dist/stega.browser.cjs +10 -700
  24. package/dist/stega.browser.cjs.map +1 -1
  25. package/dist/stega.browser.js +5 -693
  26. package/dist/stega.browser.js.map +1 -1
  27. package/dist/stega.cjs +10 -451
  28. package/dist/stega.cjs.js +3 -5
  29. package/dist/stega.cjs.map +1 -1
  30. package/dist/stega.d.ts +1302 -1455
  31. package/dist/stega.js +5 -444
  32. package/dist/stega.js.map +1 -1
  33. package/package.json +12 -7
  34. package/src/SanityClient.ts +40 -4
  35. package/src/config.ts +34 -12
  36. package/src/data/dataMethods.ts +37 -9
  37. package/src/stega/index.browser.ts +3 -6
  38. package/src/stega/index.ts +3 -6
  39. package/src/stega/shared.ts +1 -13
  40. package/src/stega/types.ts +11 -18
  41. package/src/types.ts +24 -0
  42. package/umd/sanityClient.js +1073 -20
  43. package/umd/sanityClient.min.js +4 -3
  44. package/dist/_chunks/browserMiddleware-1MULg59S.cjs.map +0 -1
  45. package/dist/_chunks/browserMiddleware-heyg-fDk.js.map +0 -1
  46. package/dist/_chunks/nodeMiddleware-CHxg1-zH.cjs.map +0 -1
  47. package/dist/_chunks/nodeMiddleware-u_fUUnxa.js.map +0 -1
  48. package/src/stega/SanityStegaClient.ts +0 -298
  49. package/src/stega/config.ts +0 -76
package/src/config.ts CHANGED
@@ -8,6 +8,7 @@ export const defaultConfig = {
8
8
  apiHost: 'https://api.sanity.io',
9
9
  apiVersion: '1',
10
10
  useProjectHostname: true,
11
+ stega: {enabled: false},
11
12
  } satisfies ClientConfig
12
13
 
13
14
  const LOCALHOSTS = ['localhost', '127.0.0.1', '0.0.0.0']
@@ -44,12 +45,24 @@ export const initConfig = (
44
45
  config: Partial<ClientConfig>,
45
46
  prevConfig: Partial<ClientConfig>,
46
47
  ): InitializedClientConfig => {
47
- const specifiedConfig = Object.assign({}, prevConfig, config)
48
+ const specifiedConfig = {
49
+ ...prevConfig,
50
+ ...config,
51
+ stega: {
52
+ ...(typeof prevConfig.stega === 'boolean'
53
+ ? {enabled: prevConfig.stega}
54
+ : prevConfig.stega || defaultConfig.stega),
55
+ ...(typeof config.stega === 'boolean' ? {enabled: config.stega} : config.stega || {}),
56
+ },
57
+ }
48
58
  if (!specifiedConfig.apiVersion) {
49
59
  warnings.printNoApiVersionSpecifiedWarning()
50
60
  }
51
61
 
52
- const newConfig = Object.assign({} as InitializedClientConfig, defaultConfig, specifiedConfig)
62
+ const newConfig = {
63
+ ...defaultConfig,
64
+ ...specifiedConfig,
65
+ } as InitializedClientConfig
53
66
  const projectBased = newConfig.useProjectHostname
54
67
 
55
68
  if (typeof Promise === 'undefined') {
@@ -65,20 +78,29 @@ export const initConfig = (
65
78
  validateApiPerspective(newConfig.perspective)
66
79
  }
67
80
 
68
- if (
69
- 'encodeSourceMapAtPath' in newConfig ||
70
- 'encodeSourceMap' in newConfig ||
71
- 'studioUrl' in newConfig ||
72
- 'logger' in newConfig
73
- ) {
81
+ if ('encodeSourceMap' in newConfig) {
74
82
  throw new Error(
75
- `It looks like you're using options meant for '@sanity/preview-kit/client', such as 'encodeSourceMapAtPath', 'encodeSourceMap', 'studioUrl' and 'logger'. Make sure you're using the right import.`,
83
+ `It looks like you're using options meant for '@sanity/preview-kit/client'. 'encodeSourceMap' is not supported in '@sanity/client'. Did you mean 'stega.enabled'?`,
76
84
  )
77
85
  }
78
-
79
- if ('stega' in newConfig && newConfig['stega'] !== undefined && newConfig['stega'] !== false) {
86
+ if ('encodeSourceMapAtPath' in newConfig) {
87
+ throw new Error(
88
+ `It looks like you're using options meant for '@sanity/preview-kit/client'. 'encodeSourceMapAtPath' is not supported in '@sanity/client'. Did you mean 'stega.filter'?`,
89
+ )
90
+ }
91
+ if (typeof newConfig.stega.enabled !== 'boolean') {
92
+ throw new Error(`stega.enabled must be a boolean, received ${newConfig.stega.enabled}`)
93
+ }
94
+ if (newConfig.stega.enabled && newConfig.stega.studioUrl === undefined) {
95
+ throw new Error(`stega.studioUrl must be defined when stega.enabled is true`)
96
+ }
97
+ if (
98
+ newConfig.stega.enabled &&
99
+ typeof newConfig.stega.studioUrl !== 'string' &&
100
+ typeof newConfig.stega.studioUrl !== 'function'
101
+ ) {
80
102
  throw new Error(
81
- `It looks like you're using options meant for '@sanity/client/stega'. Make sure you're using the right import. Or set 'stega' in 'createClient' to 'false'.`,
103
+ `stega.studioUrl must be a string or a function, received ${newConfig.stega.studioUrl}`,
82
104
  )
83
105
  }
84
106
 
@@ -1,9 +1,10 @@
1
- import {type MonoTypeOperatorFunction, Observable} from 'rxjs'
2
- import {filter, map} from 'rxjs/operators'
1
+ import {from, type MonoTypeOperatorFunction, Observable} from 'rxjs'
2
+ import {combineLatestWith, filter, map} from 'rxjs/operators'
3
3
 
4
4
  import {validateApiPerspective} from '../config'
5
5
  import {requestOptions} from '../http/requestOptions'
6
6
  import type {ObservableSanityClient, SanityClient} from '../SanityClient'
7
+ import {vercelStegaCleanAll} from '../stega/vercelStegaCleanAll'
7
8
  import type {
8
9
  AllDocumentIdsMutationOptions,
9
10
  AllDocumentsMutationOptions,
@@ -15,6 +16,7 @@ import type {
15
16
  HttpRequest,
16
17
  HttpRequestEvent,
17
18
  IdentifiedSanityDocumentStub,
19
+ InitializedStegaConfig,
18
20
  MultipleMutationResult,
19
21
  Mutation,
20
22
  MutationSelection,
@@ -65,21 +67,27 @@ const getQuerySizeLimit = 11264
65
67
  export function _fetch<R, Q extends QueryParams>(
66
68
  client: ObservableSanityClient | SanityClient,
67
69
  httpRequest: HttpRequest,
70
+ _stega: InitializedStegaConfig,
68
71
  query: string,
69
- params?: Q,
72
+ _params?: Q,
70
73
  options: FilteredResponseQueryOptions | UnfilteredResponseQueryOptions = {},
71
74
  ): Observable<RawQueryResponse<R> | R> {
72
- if ('stega' in options && options['stega'] !== undefined && options['stega'] !== false) {
73
- throw new Error(
74
- `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'.`,
75
- )
76
- }
75
+ const stega =
76
+ 'stega' in options
77
+ ? {
78
+ ...(_stega || {}),
79
+ ...(typeof options.stega === 'boolean' ? {enabled: options.stega} : options.stega || {}),
80
+ }
81
+ : _stega
82
+ const params = stega.enabled ? vercelStegaCleanAll(_params) : _params
77
83
  const mapResponse =
78
84
  options.filterResponse === false ? (res: Any) => res : (res: Any) => res.result
79
85
  const {cache, next, ...opts} = {
80
86
  // Opt out of setting a `signal` on an internal `fetch` if one isn't provided.
81
87
  // This is necessary in React Server Components to avoid opting out of Request Memoization.
82
88
  useAbortSignal: typeof options.signal !== 'undefined',
89
+ // Set `resultSourceMap' when stega is enabled, as it's required for encoding.
90
+ resultSourceMap: stega.enabled ? 'withKeyArraySelector' : options.resultSourceMap,
83
91
  ...options,
84
92
  }
85
93
  const reqOpts =
@@ -87,7 +95,27 @@ export function _fetch<R, Q extends QueryParams>(
87
95
  ? {...opts, fetch: {cache, next}}
88
96
  : opts
89
97
 
90
- return _dataRequest(client, httpRequest, 'query', {query, params}, reqOpts).pipe(map(mapResponse))
98
+ const $request = _dataRequest(client, httpRequest, 'query', {query, params}, reqOpts)
99
+ return stega.enabled
100
+ ? $request.pipe(
101
+ combineLatestWith(
102
+ from(
103
+ import('../stega/stegaEncodeSourceMap').then(
104
+ ({stegaEncodeSourceMap}) => stegaEncodeSourceMap,
105
+ ),
106
+ ),
107
+ ),
108
+ map(
109
+ ([res, stegaEncodeSourceMap]: [
110
+ Any,
111
+ (typeof import('../stega/stegaEncodeSourceMap'))['stegaEncodeSourceMap'],
112
+ ]) => {
113
+ const result = stegaEncodeSourceMap(res.result, res.resultSourceMap, stega)
114
+ return mapResponse({...res, result})
115
+ },
116
+ ),
117
+ )
118
+ : $request.pipe(map(mapResponse))
91
119
  }
92
120
 
93
121
  /** @internal */
@@ -2,13 +2,10 @@ export * from '../defineCreateClient'
2
2
 
3
3
  import defineCreateClientExports from '../defineCreateClient'
4
4
  import envMiddleware from '../http/browserMiddleware'
5
- import {SanityStegaClient} from './SanityStegaClient'
6
- import type {ClientStegaConfig} from './types'
5
+ import {SanityClient} from '../SanityClient'
6
+ import type {ClientConfig} from './types'
7
7
 
8
- const exp = defineCreateClientExports<SanityStegaClient, ClientStegaConfig>(
9
- envMiddleware,
10
- SanityStegaClient,
11
- )
8
+ const exp = defineCreateClientExports<SanityClient, ClientConfig>(envMiddleware, SanityClient)
12
9
 
13
10
  export type {ObservableSanityClient, SanityClient} from './shared'
14
11
  export * from './shared'
@@ -2,13 +2,10 @@ export * from '../defineCreateClient'
2
2
 
3
3
  import defineCreateClientExports from '../defineCreateClient'
4
4
  import envMiddleware from '../http/nodeMiddleware'
5
- import {SanityStegaClient} from './SanityStegaClient'
6
- import type {ClientStegaConfig} from './types'
5
+ import {SanityClient} from '../SanityClient'
6
+ import type {ClientConfig} from './types'
7
7
 
8
- const exp = defineCreateClientExports<SanityStegaClient, ClientStegaConfig>(
9
- envMiddleware,
10
- SanityStegaClient,
11
- )
8
+ const exp = defineCreateClientExports<SanityClient, ClientConfig>(envMiddleware, SanityClient)
12
9
 
13
10
  export type {ObservableSanityClient, SanityClient} from './shared'
14
11
  export * from './shared'
@@ -1,17 +1,5 @@
1
+ export * from '../SanityClient'
1
2
  export {encodeIntoResult} from './encodeIntoResult'
2
- export * from './SanityStegaClient'
3
3
  export {stegaEncodeSourceMap} from './stegaEncodeSourceMap'
4
4
  export * from './types'
5
5
  export {vercelStegaCleanAll} from './vercelStegaCleanAll'
6
-
7
- /**
8
- * @deprecated -- Use `import type {SanityClient} from '@sanity/client'` instead
9
- * @public
10
- */
11
- export type SanityClient = never
12
-
13
- /**
14
- * @deprecated -- Use `import type {ObservableSanityClient} from '@sanity/client'` instead
15
- * @public
16
- */
17
- export type ObservableSanityClient = never
@@ -53,21 +53,23 @@ export interface StegaConfig {
53
53
  }
54
54
 
55
55
  /** @public */
56
- export type StegaConfigRequiredKeys = Extract<keyof StegaConfig, 'enabled' | 'filter'>
56
+ export type StegaConfigRequiredKeys = Extract<keyof StegaConfig, 'enabled'>
57
57
 
58
58
  /** @public */
59
59
  export type InitializedStegaConfig = Omit<StegaConfig, StegaConfigRequiredKeys> &
60
60
  Required<Pick<StegaConfig, StegaConfigRequiredKeys>>
61
61
 
62
- /** @public */
63
- export interface ClientStegaConfig extends ClientConfig {
64
- stega?: StegaConfig | boolean
65
- }
62
+ /**
63
+ * @public
64
+ * @deprecated -- use `ClientConfig` instead
65
+ */
66
+ export interface ClientStegaConfig extends ClientConfig {}
66
67
 
67
- /** @public */
68
- export interface InitializedClientStegaConfig extends InitializedClientConfig {
69
- stega: InitializedStegaConfig
70
- }
68
+ /**
69
+ * @public
70
+ * @deprecated -- use `InitializedClientConfig` instead
71
+ */
72
+ export interface InitializedClientStegaConfig extends InitializedClientConfig {}
71
73
 
72
74
  /** @public */
73
75
  export type FilterDefault = (props: {
@@ -134,12 +136,3 @@ export type Encoder = (context: {
134
136
  resultPath: ContentSourceMapParsedPath
135
137
  value: string
136
138
  }) => string
137
-
138
- // Extends the `client.fetch` call with a `stega` option
139
- declare module '../types' {
140
- /** @public */
141
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
142
- export interface ResponseQueryOptions<T = 'next'> extends RequestOptions {
143
- stega?: boolean | StegaConfig
144
- }
145
- }
package/src/types.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  // deno-lint-ignore-file no-empty-interface
2
2
 
3
3
  import type {Requester} from 'get-it'
4
+ import type {InitializedStegaConfig, StegaConfig} from './stega/types'
4
5
 
5
6
  /**
6
7
  * Used to tag types that is set to `any` as a temporary measure, but should be replaced with proper typings in the future
@@ -89,6 +90,10 @@ export interface ClientConfig {
89
90
  *@deprecated set `cache` and `next` options on `client.fetch` instead
90
91
  */
91
92
  fetch?: RequestFetchOptions | boolean
93
+ /**
94
+ * Options for how, if enabled, Content Source Maps are encoded into query results using steganography
95
+ */
96
+ stega?: StegaConfig | boolean
92
97
  }
93
98
 
94
99
  /** @public */
@@ -111,6 +116,10 @@ export interface InitializedClientConfig extends ClientConfig {
111
116
  * @deprecated Internal, don't use
112
117
  */
113
118
  cdnUrl: string
119
+ /**
120
+ * The fully initialized stega config, can be used to check if stega is enabled
121
+ */
122
+ stega: InitializedStegaConfig
114
123
  }
115
124
 
116
125
  /** @public */
@@ -670,6 +679,7 @@ export interface ResponseQueryOptions<T = 'next'> extends RequestOptions {
670
679
  cache?: RequestInit['cache']
671
680
  next?: T extends keyof RequestInit ? RequestInit[T] : never
672
681
  useCdn?: boolean
682
+ stega?: boolean | StegaConfig
673
683
  }
674
684
 
675
685
  /** @public */
@@ -898,3 +908,17 @@ export interface ContentSourceMap {
898
908
  documents: ContentSourceMapDocuments
899
909
  paths: ContentSourceMapPaths
900
910
  }
911
+
912
+ export type {
913
+ ContentSourceMapParsedPath,
914
+ ContentSourceMapParsedPathKeyedSegment,
915
+ FilterDefault,
916
+ InitializedStegaConfig,
917
+ Logger,
918
+ ResolveStudioUrl,
919
+ StegaConfig,
920
+ StegaConfigRequiredKeys,
921
+ StudioBaseRoute,
922
+ StudioBaseUrl,
923
+ StudioUrl,
924
+ } from './stega/types'