@sanity/client 7.11.1 → 7.11.2-audience-decide.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.
package/dist/index.d.cts CHANGED
@@ -706,6 +706,10 @@ export declare interface ClientConfig {
706
706
  * @defaultValue 'published'
707
707
  */
708
708
  perspective?: ClientPerspective
709
+ /**
710
+ * Parameters to pass to Sanity Decide for personalisation
711
+ */
712
+ decideParameters?: DecideParameters
709
713
  apiHost?: string
710
714
  /**
711
715
  @remarks
@@ -1132,6 +1136,12 @@ export declare type DatasetsResponse = {
1132
1136
  tags: string[]
1133
1137
  }[]
1134
1138
 
1139
+ /** @public */
1140
+ export declare interface DecideParameters {
1141
+ audience: string | string[]
1142
+ [key: string]: unknown
1143
+ }
1144
+
1135
1145
  /**
1136
1146
  * Deletes the published version of a document and optionally some (likely all known) draft versions.
1137
1147
  * If any draft version exists that is not specified for deletion this is an error.
@@ -1863,6 +1873,21 @@ export declare type ImageDescriptionOperation = {
1863
1873
  }
1864
1874
  )
1865
1875
 
1876
+ /** @public */
1877
+ export declare type ImportReleaseAction =
1878
+ | {
1879
+ actionType: 'sanity.action.release.import'
1880
+ attributes: IdentifiedSanityDocumentStub
1881
+ releaseId: string
1882
+ ifExists: 'fail' | 'ignore' | 'replace'
1883
+ }
1884
+ | {
1885
+ actionType: 'sanity.action.release.import'
1886
+ document: IdentifiedSanityDocumentStub
1887
+ releaseId: string
1888
+ ifExists: 'fail' | 'ignore' | 'replace'
1889
+ }
1890
+
1866
1891
  /** @public */
1867
1892
  export declare interface InitializedClientConfig extends ClientConfig {
1868
1893
  apiHost: string
@@ -4118,6 +4143,8 @@ export declare interface QueryParams {
4118
4143
  /** @deprecated you're using a fetch option as a GROQ parameter, this is likely a mistake */
4119
4144
  cache?: 'next' extends keyof RequestInit ? never : any
4120
4145
  /** @deprecated you're using a fetch option as a GROQ parameter, this is likely a mistake */
4146
+ decideParameters?: never
4147
+ /** @deprecated you're using a fetch option as a GROQ parameter, this is likely a mistake */
4121
4148
  filterResponse?: never
4122
4149
  /** @deprecated you're using a fetch option as a GROQ parameter, this is likely a mistake */
4123
4150
  headers?: never
@@ -4229,6 +4256,7 @@ export declare type ReleaseAction =
4229
4256
  | ScheduleReleaseAction
4230
4257
  | UnscheduleReleaseAction
4231
4258
  | DeleteReleaseAction
4259
+ | ImportReleaseAction
4232
4260
 
4233
4261
  /** @public */
4234
4262
  export declare type ReleaseCardinality = 'many' | 'one' | undefined
@@ -4626,6 +4654,7 @@ export declare interface RequestObservableOptions extends Omit<RequestOptions, '
4626
4654
  returnQuery?: boolean
4627
4655
  resultSourceMap?: boolean | 'withKeyArraySelector'
4628
4656
  perspective?: ClientPerspective
4657
+ decideParameters?: DecideParameters
4629
4658
  lastLiveEventId?: string
4630
4659
  cacheMode?: 'noStale'
4631
4660
  }
@@ -4658,6 +4687,7 @@ export declare interface ResponseEvent<T = unknown> {
4658
4687
  /** @public */
4659
4688
  export declare interface ResponseQueryOptions extends RequestOptions {
4660
4689
  perspective?: ClientPerspective
4690
+ decideParameters?: DecideParameters
4661
4691
  resultSourceMap?: boolean | 'withKeyArraySelector'
4662
4692
  returnQuery?: boolean
4663
4693
  useCdn?: boolean
package/dist/index.d.ts CHANGED
@@ -706,6 +706,10 @@ export declare interface ClientConfig {
706
706
  * @defaultValue 'published'
707
707
  */
708
708
  perspective?: ClientPerspective
709
+ /**
710
+ * Parameters to pass to Sanity Decide for personalisation
711
+ */
712
+ decideParameters?: DecideParameters
709
713
  apiHost?: string
710
714
  /**
711
715
  @remarks
@@ -1132,6 +1136,12 @@ export declare type DatasetsResponse = {
1132
1136
  tags: string[]
1133
1137
  }[]
1134
1138
 
1139
+ /** @public */
1140
+ export declare interface DecideParameters {
1141
+ audience: string | string[]
1142
+ [key: string]: unknown
1143
+ }
1144
+
1135
1145
  /**
1136
1146
  * Deletes the published version of a document and optionally some (likely all known) draft versions.
1137
1147
  * If any draft version exists that is not specified for deletion this is an error.
@@ -1863,6 +1873,21 @@ export declare type ImageDescriptionOperation = {
1863
1873
  }
1864
1874
  )
1865
1875
 
1876
+ /** @public */
1877
+ export declare type ImportReleaseAction =
1878
+ | {
1879
+ actionType: 'sanity.action.release.import'
1880
+ attributes: IdentifiedSanityDocumentStub
1881
+ releaseId: string
1882
+ ifExists: 'fail' | 'ignore' | 'replace'
1883
+ }
1884
+ | {
1885
+ actionType: 'sanity.action.release.import'
1886
+ document: IdentifiedSanityDocumentStub
1887
+ releaseId: string
1888
+ ifExists: 'fail' | 'ignore' | 'replace'
1889
+ }
1890
+
1866
1891
  /** @public */
1867
1892
  export declare interface InitializedClientConfig extends ClientConfig {
1868
1893
  apiHost: string
@@ -4118,6 +4143,8 @@ export declare interface QueryParams {
4118
4143
  /** @deprecated you're using a fetch option as a GROQ parameter, this is likely a mistake */
4119
4144
  cache?: 'next' extends keyof RequestInit ? never : any
4120
4145
  /** @deprecated you're using a fetch option as a GROQ parameter, this is likely a mistake */
4146
+ decideParameters?: never
4147
+ /** @deprecated you're using a fetch option as a GROQ parameter, this is likely a mistake */
4121
4148
  filterResponse?: never
4122
4149
  /** @deprecated you're using a fetch option as a GROQ parameter, this is likely a mistake */
4123
4150
  headers?: never
@@ -4229,6 +4256,7 @@ export declare type ReleaseAction =
4229
4256
  | ScheduleReleaseAction
4230
4257
  | UnscheduleReleaseAction
4231
4258
  | DeleteReleaseAction
4259
+ | ImportReleaseAction
4232
4260
 
4233
4261
  /** @public */
4234
4262
  export declare type ReleaseCardinality = 'many' | 'one' | undefined
@@ -4626,6 +4654,7 @@ export declare interface RequestObservableOptions extends Omit<RequestOptions, '
4626
4654
  returnQuery?: boolean
4627
4655
  resultSourceMap?: boolean | 'withKeyArraySelector'
4628
4656
  perspective?: ClientPerspective
4657
+ decideParameters?: DecideParameters
4629
4658
  lastLiveEventId?: string
4630
4659
  cacheMode?: 'noStale'
4631
4660
  }
@@ -4658,6 +4687,7 @@ export declare interface ResponseEvent<T = unknown> {
4658
4687
  /** @public */
4659
4688
  export declare interface ResponseQueryOptions extends RequestOptions {
4660
4689
  perspective?: ClientPerspective
4690
+ decideParameters?: DecideParameters
4661
4691
  resultSourceMap?: boolean | 'withKeyArraySelector'
4662
4692
  returnQuery?: boolean
4663
4693
  useCdn?: boolean
package/dist/index.js CHANGED
@@ -670,6 +670,32 @@ function requestOptions(config, overrides = {}) {
670
670
  fetch: typeof overrides.fetch == "object" && typeof config.fetch == "object" ? { ...config.fetch, ...overrides.fetch } : overrides.fetch || config.fetch
671
671
  });
672
672
  }
673
+ function isDecideField(value) {
674
+ return value != null && typeof value == "object" && !Array.isArray(value) && "default" in value && "conditions" in value && Array.isArray(value.conditions);
675
+ }
676
+ function resolveDecideField(field, decideParameters) {
677
+ const audience = decideParameters.audience, matchingCondition = field.conditions.find((condition) => Array.isArray(audience) ? audience.includes(condition.audience) : condition.audience === audience);
678
+ return matchingCondition ? matchingCondition.value : field.default;
679
+ }
680
+ function processObjectRecursively(obj, decideParameters) {
681
+ return obj == null || typeof obj != "object" ? obj : Array.isArray(obj) ? obj.map((item) => processObjectRecursively(item, decideParameters)) : Object.entries(obj).reduce((processed, [key, value]) => {
682
+ try {
683
+ isDecideField(value) ? processed[key] = resolveDecideField(value, decideParameters) : processed[key] = processObjectRecursively(value, decideParameters);
684
+ } catch (error) {
685
+ console.warn(`Failed to process decide field '${key}':`, error), processed[key] = value;
686
+ }
687
+ return processed;
688
+ }, {});
689
+ }
690
+ function processDecideFields(data, decideParameters) {
691
+ if (!decideParameters || !decideParameters.audience)
692
+ return data;
693
+ try {
694
+ return processObjectRecursively(data, decideParameters);
695
+ } catch (error) {
696
+ return console.warn("Failed to process decide fields:", error), data;
697
+ }
698
+ }
673
699
  const encodeQueryString = ({
674
700
  query,
675
701
  params = {},
@@ -694,7 +720,7 @@ function _fetch(client, httpRequest, _stega, query, _params = {}, options = {})
694
720
  const stega = "stega" in options ? {
695
721
  ..._stega || {},
696
722
  ...typeof options.stega == "boolean" ? { enabled: options.stega } : options.stega || {}
697
- } : _stega, params = stega.enabled ? stegaClean(_params) : _params, mapResponse = options.filterResponse === !1 ? (res) => res : (res) => res.result, { cache, next, ...opts } = {
723
+ } : _stega, params = stega.enabled ? stegaClean(_params) : _params, mapResponse = options.filterResponse === !1 ? (res) => res : (res) => res.result, processDecideResponse = (response) => options.decideParameters && options.decideParameters.audience ? processDecideFields(response, options.decideParameters) : response, { cache, next, ...opts } = {
698
724
  // Opt out of setting a `signal` on an internal `fetch` if one isn't provided.
699
725
  // This is necessary in React Server Components to avoid opting out of Request Memoization.
700
726
  useAbortSignal: typeof options.signal < "u",
@@ -717,11 +743,11 @@ function _fetch(client, httpRequest, _stega, query, _params = {}, options = {})
717
743
  ),
718
744
  map(
719
745
  ([res, stegaEncodeSourceMap]) => {
720
- const result = stegaEncodeSourceMap(res.result, res.resultSourceMap, stega);
721
- return mapResponse({ ...res, result });
746
+ const result = stegaEncodeSourceMap(res.result, res.resultSourceMap, stega), mappedResponse = mapResponse({ ...res, result });
747
+ return processDecideResponse(mappedResponse);
722
748
  }
723
749
  )
724
- ) : $request.pipe(map(mapResponse));
750
+ ) : $request.pipe(map(mapResponse), map(processDecideResponse));
725
751
  }
726
752
  function _getDocument(client, httpRequest, id, opts = {}) {
727
753
  const docId = (() => {
@@ -874,6 +900,7 @@ function _dataRequest(client, httpRequest, endpoint, body, options = {}) {
874
900
  tag,
875
901
  returnQuery,
876
902
  perspective: options.perspective,
903
+ decideParameters: options.decideParameters,
877
904
  resultSourceMap: options.resultSourceMap,
878
905
  lastLiveEventId: Array.isArray(lastLiveEventId) ? lastLiveEventId[0] : lastLiveEventId,
879
906
  cacheMode,
@@ -918,7 +945,15 @@ function _requestObservable(client, httpRequest, options) {
918
945
  perspective: Array.isArray(perspectiveOption) ? perspectiveOption.join(",") : perspectiveOption,
919
946
  ...options.query
920
947
  }, (Array.isArray(perspectiveOption) && perspectiveOption.length > 0 || // previewDrafts was renamed to drafts, but keep for backwards compat
921
- perspectiveOption === "previewDrafts" || perspectiveOption === "drafts") && useCdn && (useCdn = !1, printCdnPreviewDraftsWarning())), options.lastLiveEventId && (options.query = { ...options.query, lastLiveEventId: options.lastLiveEventId }), options.returnQuery === !1 && (options.query = { returnQuery: "false", ...options.query }), useCdn && options.cacheMode == "noStale" && (options.query = { cacheMode: "noStale", ...options.query });
948
+ perspectiveOption === "previewDrafts" || perspectiveOption === "drafts") && useCdn && (useCdn = !1, printCdnPreviewDraftsWarning()));
949
+ const decideParametersOption = options.decideParameters || config.decideParameters;
950
+ decideParametersOption && typeof decideParametersOption == "object" && (options.query = {
951
+ ...options.query,
952
+ ...Object.keys(decideParametersOption).reduce(
953
+ (acc, key) => (acc[`decide.${key}`] = decideParametersOption[key], acc),
954
+ {}
955
+ )
956
+ }), options.lastLiveEventId && (options.query = { ...options.query, lastLiveEventId: options.lastLiveEventId }), options.returnQuery === !1 && (options.query = { returnQuery: "false", ...options.query }), useCdn && options.cacheMode == "noStale" && (options.query = { cacheMode: "noStale", ...options.query });
922
957
  }
923
958
  const reqOptions = requestOptions(
924
959
  config,
@@ -2669,7 +2704,7 @@ function defineDeprecatedCreateClient(createClient2) {
2669
2704
  return printNoDefaultExport(), createClient2(config);
2670
2705
  };
2671
2706
  }
2672
- var name = "@sanity/client", version = "7.11.1";
2707
+ var name = "@sanity/client", version = "7.11.2-audience-decide.0";
2673
2708
  const middleware = [
2674
2709
  debug({ verbose: !0, namespace: "sanity:client" }),
2675
2710
  headers({ "User-Agent": `${name} ${version}` }),