@sanity/client 7.23.2 → 7.24.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.
@@ -719,6 +719,10 @@ export declare interface ClientConfig {
719
719
  * @defaultValue 'published'
720
720
  */
721
721
  perspective?: ClientPerspective
722
+ /**
723
+ * @beta
724
+ */
725
+ variant?: ClientVariant
722
726
  apiHost?: string
723
727
  /**
724
728
  @remarks
@@ -880,6 +884,18 @@ export declare type ClientReturn<
880
884
  */
881
885
  export declare interface ClientStegaConfig extends ClientConfig {}
882
886
 
887
+ /**
888
+ * @public
889
+ * @beta
890
+ */
891
+ export declare type ClientVariant = ClientVariantConditions | string
892
+
893
+ /**
894
+ * @public
895
+ * @beta
896
+ */
897
+ export declare type ClientVariantConditions = Record<string, string>
898
+
883
899
  /**
884
900
  * Sanity API specific EventSource handler shared between the listen and live APIs
885
901
  *
@@ -4526,6 +4542,8 @@ export declare interface QueryParams {
4526
4542
  /** @deprecated you're using a fetch option as a GROQ parameter, this is likely a mistake */
4527
4543
  perspective?: never
4528
4544
  /** @deprecated you're using a fetch option as a GROQ parameter, this is likely a mistake */
4545
+ variant?: never
4546
+ /** @deprecated you're using a fetch option as a GROQ parameter, this is likely a mistake */
4529
4547
  query?: never
4530
4548
  /** @deprecated you're using a fetch option as a GROQ parameter, this is likely a mistake */
4531
4549
  resultSourceMap?: never
@@ -5066,6 +5084,10 @@ export declare interface RequestObservableOptions extends Omit<RequestOptions, '
5066
5084
  returnQuery?: boolean
5067
5085
  resultSourceMap?: boolean | 'withKeyArraySelector'
5068
5086
  perspective?: ClientPerspective
5087
+ /**
5088
+ * @beta
5089
+ */
5090
+ variant?: ClientVariant
5069
5091
  lastLiveEventId?: string
5070
5092
  cacheMode?: 'noStale'
5071
5093
  }
@@ -5114,6 +5136,10 @@ export declare interface ResponseEvent<T = unknown> {
5114
5136
  /** @public */
5115
5137
  export declare interface ResponseQueryOptions extends RequestOptions {
5116
5138
  perspective?: ClientPerspective
5139
+ /**
5140
+ * @beta
5141
+ */
5142
+ variant?: ClientVariant
5117
5143
  resultSourceMap?: boolean | 'withKeyArraySelector'
5118
5144
  returnQuery?: boolean
5119
5145
  useCdn?: boolean
@@ -719,6 +719,10 @@ export declare interface ClientConfig {
719
719
  * @defaultValue 'published'
720
720
  */
721
721
  perspective?: ClientPerspective
722
+ /**
723
+ * @beta
724
+ */
725
+ variant?: ClientVariant
722
726
  apiHost?: string
723
727
  /**
724
728
  @remarks
@@ -880,6 +884,18 @@ export declare type ClientReturn<
880
884
  */
881
885
  export declare interface ClientStegaConfig extends ClientConfig {}
882
886
 
887
+ /**
888
+ * @public
889
+ * @beta
890
+ */
891
+ export declare type ClientVariant = ClientVariantConditions | string
892
+
893
+ /**
894
+ * @public
895
+ * @beta
896
+ */
897
+ export declare type ClientVariantConditions = Record<string, string>
898
+
883
899
  /**
884
900
  * Sanity API specific EventSource handler shared between the listen and live APIs
885
901
  *
@@ -4526,6 +4542,8 @@ export declare interface QueryParams {
4526
4542
  /** @deprecated you're using a fetch option as a GROQ parameter, this is likely a mistake */
4527
4543
  perspective?: never
4528
4544
  /** @deprecated you're using a fetch option as a GROQ parameter, this is likely a mistake */
4545
+ variant?: never
4546
+ /** @deprecated you're using a fetch option as a GROQ parameter, this is likely a mistake */
4529
4547
  query?: never
4530
4548
  /** @deprecated you're using a fetch option as a GROQ parameter, this is likely a mistake */
4531
4549
  resultSourceMap?: never
@@ -5066,6 +5084,10 @@ export declare interface RequestObservableOptions extends Omit<RequestOptions, '
5066
5084
  returnQuery?: boolean
5067
5085
  resultSourceMap?: boolean | 'withKeyArraySelector'
5068
5086
  perspective?: ClientPerspective
5087
+ /**
5088
+ * @beta
5089
+ */
5090
+ variant?: ClientVariant
5069
5091
  lastLiveEventId?: string
5070
5092
  cacheMode?: 'noStale'
5071
5093
  }
@@ -5114,6 +5136,10 @@ export declare interface ResponseEvent<T = unknown> {
5114
5136
  /** @public */
5115
5137
  export declare interface ResponseQueryOptions extends RequestOptions {
5116
5138
  perspective?: ClientPerspective
5139
+ /**
5140
+ * @beta
5141
+ */
5142
+ variant?: ClientVariant
5117
5143
  resultSourceMap?: boolean | 'withKeyArraySelector'
5118
5144
  returnQuery?: boolean
5119
5145
  useCdn?: boolean
package/dist/stega.d.cts CHANGED
@@ -719,6 +719,10 @@ export declare interface ClientConfig {
719
719
  * @defaultValue 'published'
720
720
  */
721
721
  perspective?: ClientPerspective
722
+ /**
723
+ * @beta
724
+ */
725
+ variant?: ClientVariant
722
726
  apiHost?: string
723
727
  /**
724
728
  @remarks
@@ -880,6 +884,18 @@ export declare type ClientReturn<
880
884
  */
881
885
  export declare interface ClientStegaConfig extends ClientConfig {}
882
886
 
887
+ /**
888
+ * @public
889
+ * @beta
890
+ */
891
+ export declare type ClientVariant = ClientVariantConditions | string
892
+
893
+ /**
894
+ * @public
895
+ * @beta
896
+ */
897
+ export declare type ClientVariantConditions = Record<string, string>
898
+
883
899
  /**
884
900
  * Sanity API specific EventSource handler shared between the listen and live APIs
885
901
  *
@@ -4526,6 +4542,8 @@ export declare interface QueryParams {
4526
4542
  /** @deprecated you're using a fetch option as a GROQ parameter, this is likely a mistake */
4527
4543
  perspective?: never
4528
4544
  /** @deprecated you're using a fetch option as a GROQ parameter, this is likely a mistake */
4545
+ variant?: never
4546
+ /** @deprecated you're using a fetch option as a GROQ parameter, this is likely a mistake */
4529
4547
  query?: never
4530
4548
  /** @deprecated you're using a fetch option as a GROQ parameter, this is likely a mistake */
4531
4549
  resultSourceMap?: never
@@ -5066,6 +5084,10 @@ export declare interface RequestObservableOptions extends Omit<RequestOptions, '
5066
5084
  returnQuery?: boolean
5067
5085
  resultSourceMap?: boolean | 'withKeyArraySelector'
5068
5086
  perspective?: ClientPerspective
5087
+ /**
5088
+ * @beta
5089
+ */
5090
+ variant?: ClientVariant
5069
5091
  lastLiveEventId?: string
5070
5092
  cacheMode?: 'noStale'
5071
5093
  }
@@ -5114,6 +5136,10 @@ export declare interface ResponseEvent<T = unknown> {
5114
5136
  /** @public */
5115
5137
  export declare interface ResponseQueryOptions extends RequestOptions {
5116
5138
  perspective?: ClientPerspective
5139
+ /**
5140
+ * @beta
5141
+ */
5142
+ variant?: ClientVariant
5117
5143
  resultSourceMap?: boolean | 'withKeyArraySelector'
5118
5144
  returnQuery?: boolean
5119
5145
  useCdn?: boolean
package/dist/stega.d.ts CHANGED
@@ -719,6 +719,10 @@ export declare interface ClientConfig {
719
719
  * @defaultValue 'published'
720
720
  */
721
721
  perspective?: ClientPerspective
722
+ /**
723
+ * @beta
724
+ */
725
+ variant?: ClientVariant
722
726
  apiHost?: string
723
727
  /**
724
728
  @remarks
@@ -880,6 +884,18 @@ export declare type ClientReturn<
880
884
  */
881
885
  export declare interface ClientStegaConfig extends ClientConfig {}
882
886
 
887
+ /**
888
+ * @public
889
+ * @beta
890
+ */
891
+ export declare type ClientVariant = ClientVariantConditions | string
892
+
893
+ /**
894
+ * @public
895
+ * @beta
896
+ */
897
+ export declare type ClientVariantConditions = Record<string, string>
898
+
883
899
  /**
884
900
  * Sanity API specific EventSource handler shared between the listen and live APIs
885
901
  *
@@ -4526,6 +4542,8 @@ export declare interface QueryParams {
4526
4542
  /** @deprecated you're using a fetch option as a GROQ parameter, this is likely a mistake */
4527
4543
  perspective?: never
4528
4544
  /** @deprecated you're using a fetch option as a GROQ parameter, this is likely a mistake */
4545
+ variant?: never
4546
+ /** @deprecated you're using a fetch option as a GROQ parameter, this is likely a mistake */
4529
4547
  query?: never
4530
4548
  /** @deprecated you're using a fetch option as a GROQ parameter, this is likely a mistake */
4531
4549
  resultSourceMap?: never
@@ -5066,6 +5084,10 @@ export declare interface RequestObservableOptions extends Omit<RequestOptions, '
5066
5084
  returnQuery?: boolean
5067
5085
  resultSourceMap?: boolean | 'withKeyArraySelector'
5068
5086
  perspective?: ClientPerspective
5087
+ /**
5088
+ * @beta
5089
+ */
5090
+ variant?: ClientVariant
5069
5091
  lastLiveEventId?: string
5070
5092
  cacheMode?: 'noStale'
5071
5093
  }
@@ -5114,6 +5136,10 @@ export declare interface ResponseEvent<T = unknown> {
5114
5136
  /** @public */
5115
5137
  export declare interface ResponseQueryOptions extends RequestOptions {
5116
5138
  perspective?: ClientPerspective
5139
+ /**
5140
+ * @beta
5141
+ */
5142
+ variant?: ClientVariant
5117
5143
  resultSourceMap?: boolean | 'withKeyArraySelector'
5118
5144
  returnQuery?: boolean
5119
5145
  useCdn?: boolean
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sanity/client",
3
- "version": "7.23.2",
3
+ "version": "7.24.0",
4
4
  "description": "Client for retrieving, creating and patching data from Sanity.io",
5
5
  "keywords": [
6
6
  "sanity",
@@ -549,6 +549,7 @@ export function _dataRequest(
549
549
  tag,
550
550
  returnQuery,
551
551
  perspective: options.perspective,
552
+ variant: options.variant,
552
553
  resultSourceMap: options.resultSourceMap,
553
554
  lastLiveEventId: Array.isArray(lastLiveEventId) ? lastLiveEventId[0] : lastLiveEventId,
554
555
  cacheMode: cacheMode,
@@ -700,6 +701,36 @@ export function _requestObservable<R>(
700
701
  }
701
702
  }
702
703
 
704
+ const variantOption = options.variant || config.variant
705
+
706
+ if (typeof variantOption !== 'undefined') {
707
+ if (typeof variantOption === 'string') {
708
+ options.query = {
709
+ variant: variantOption,
710
+ ...options.query,
711
+ }
712
+ }
713
+
714
+ if (typeof variantOption === 'object') {
715
+ const variantConditions = Object.entries(variantOption)
716
+ const searchParams = variantConditionPairsToSearchParams(variantConditions).slice(0, 1)
717
+
718
+ if (variantConditions.length > 1) {
719
+ const formatter = new Intl.ListFormat('en')
720
+
721
+ // eslint-disable-next-line no-console -- will be removed in an upcoming version; it's better this behaviour is noisy and obvious
722
+ console.warn(
723
+ `The Sanity client's beta \`variant\` option currently only supports one condition. Dropped: ${formatter.format(variantConditions.slice(1).map(([subject]) => JSON.stringify(subject)))}.`,
724
+ )
725
+ }
726
+
727
+ options.query = {
728
+ ...Object.fromEntries(searchParams),
729
+ ...options.query,
730
+ }
731
+ }
732
+ }
733
+
703
734
  if (options.lastLiveEventId) {
704
735
  options.query = {...options.query, lastLiveEventId: options.lastLiveEventId}
705
736
  }
@@ -843,3 +874,12 @@ const resourceDataBase = (config: InitializedClientConfig): string => {
843
874
  throw new Error(`Unsupported resource type: ${type.toString()}`)
844
875
  }
845
876
  }
877
+
878
+ function variantConditionPairsToSearchParams(
879
+ variantConditionPairs: string[][],
880
+ ): ['variantCondition', `${string}:${string}`][] {
881
+ return variantConditionPairs.map(([condition, value]) => [
882
+ 'variantCondition',
883
+ `${condition}:${value}`,
884
+ ])
885
+ }
package/src/types.ts CHANGED
@@ -55,6 +55,18 @@ export type ClientPerspective =
55
55
  | 'raw'
56
56
  | StackablePerspective[]
57
57
 
58
+ /**
59
+ * @public
60
+ * @beta
61
+ */
62
+ export type ClientVariantConditions = Record<string, string>
63
+
64
+ /**
65
+ * @public
66
+ * @beta
67
+ */
68
+ export type ClientVariant = ClientVariantConditions | string
69
+
58
70
  type ClientConfigResource =
59
71
  | {
60
72
  type: 'canvas'
@@ -111,6 +123,10 @@ export interface ClientConfig {
111
123
  * @defaultValue 'published'
112
124
  */
113
125
  perspective?: ClientPerspective
126
+ /**
127
+ * @beta
128
+ */
129
+ variant?: ClientVariant
114
130
  apiHost?: string
115
131
 
116
132
  /**
@@ -484,6 +500,10 @@ export interface RequestObservableOptions extends Omit<RequestOptions, 'url'> {
484
500
  returnQuery?: boolean
485
501
  resultSourceMap?: boolean | 'withKeyArraySelector'
486
502
  perspective?: ClientPerspective
503
+ /**
504
+ * @beta
505
+ */
506
+ variant?: ClientVariant
487
507
  lastLiveEventId?: string
488
508
  cacheMode?: 'noStale'
489
509
  }
@@ -679,6 +699,8 @@ export interface QueryParams {
679
699
  /** @deprecated you're using a fetch option as a GROQ parameter, this is likely a mistake */
680
700
  perspective?: never
681
701
  /** @deprecated you're using a fetch option as a GROQ parameter, this is likely a mistake */
702
+ variant?: never
703
+ /** @deprecated you're using a fetch option as a GROQ parameter, this is likely a mistake */
682
704
  query?: never
683
705
  /** @deprecated you're using a fetch option as a GROQ parameter, this is likely a mistake */
684
706
  resultSourceMap?: never
@@ -1424,6 +1446,10 @@ export interface ResumableListenOptions extends Omit<ListenOptions, 'events' | '
1424
1446
  /** @public */
1425
1447
  export interface ResponseQueryOptions extends RequestOptions {
1426
1448
  perspective?: ClientPerspective
1449
+ /**
1450
+ * @beta
1451
+ */
1452
+ variant?: ClientVariant
1427
1453
  resultSourceMap?: boolean | 'withKeyArraySelector'
1428
1454
  returnQuery?: boolean
1429
1455
  useCdn?: boolean
@@ -3096,6 +3096,7 @@ ${selectionOpts}`);
3096
3096
  tag,
3097
3097
  returnQuery,
3098
3098
  perspective: options.perspective,
3099
+ variant: options.variant,
3099
3100
  resultSourceMap: options.resultSourceMap,
3100
3101
  lastLiveEventId: Array.isArray(lastLiveEventId) ? lastLiveEventId[0] : lastLiveEventId,
3101
3102
  cacheMode,
@@ -3143,7 +3144,25 @@ ${selectionOpts}`);
3143
3144
  perspective: Array.isArray(perspectiveOption) ? perspectiveOption.join(",") : perspectiveOption,
3144
3145
  ...options.query
3145
3146
  }, (Array.isArray(perspectiveOption) && perspectiveOption.length > 0 || // previewDrafts was renamed to drafts, but keep for backwards compat
3146
- perspectiveOption === "previewDrafts" || perspectiveOption === "drafts") && useCdn && (useCdn = false, printCdnPreviewDraftsWarning())), options.lastLiveEventId && (options.query = { ...options.query, lastLiveEventId: options.lastLiveEventId }), options.returnQuery === false && (options.query = { returnQuery: "false", ...options.query }), useCdn && options.cacheMode == "noStale" && (options.query = { cacheMode: "noStale", ...options.query });
3147
+ perspectiveOption === "previewDrafts" || perspectiveOption === "drafts") && useCdn && (useCdn = false, printCdnPreviewDraftsWarning()));
3148
+ const variantOption = options.variant || config.variant;
3149
+ if (typeof variantOption < "u" && (typeof variantOption == "string" && (options.query = {
3150
+ variant: variantOption,
3151
+ ...options.query
3152
+ }), typeof variantOption == "object")) {
3153
+ const variantConditions = Object.entries(variantOption), searchParams = variantConditionPairsToSearchParams(variantConditions).slice(0, 1);
3154
+ if (variantConditions.length > 1) {
3155
+ const formatter = new Intl.ListFormat("en");
3156
+ console.warn(
3157
+ `The Sanity client's beta \`variant\` option currently only supports one condition. Dropped: ${formatter.format(variantConditions.slice(1).map(([subject]) => JSON.stringify(subject)))}.`
3158
+ );
3159
+ }
3160
+ options.query = {
3161
+ ...Object.fromEntries(searchParams),
3162
+ ...options.query
3163
+ };
3164
+ }
3165
+ options.lastLiveEventId && (options.query = { ...options.query, lastLiveEventId: options.lastLiveEventId }), options.returnQuery === false && (options.query = { returnQuery: "false", ...options.query }), useCdn && options.cacheMode == "noStale" && (options.query = { cacheMode: "noStale", ...options.query });
3147
3166
  }
3148
3167
  const reqOptions = requestOptions(
3149
3168
  config,
@@ -3218,6 +3237,12 @@ ${selectionOpts}`);
3218
3237
  throw new Error(`Unsupported resource type: ${type.toString()}`);
3219
3238
  }
3220
3239
  };
3240
+ function variantConditionPairsToSearchParams(variantConditionPairs) {
3241
+ return variantConditionPairs.map(([condition, value]) => [
3242
+ "variantCondition",
3243
+ `${condition}:${value}`
3244
+ ]);
3245
+ }
3221
3246
  function _generate(client, httpRequest, request) {
3222
3247
  const dataset2 = hasDataset(client.config());
3223
3248
  return _request(client, httpRequest, {