@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.
@@ -705,6 +705,10 @@ export declare interface ClientConfig {
705
705
  * @defaultValue 'published'
706
706
  */
707
707
  perspective?: ClientPerspective
708
+ /**
709
+ * Parameters to pass to Sanity Decide for personalisation
710
+ */
711
+ decideParameters?: DecideParameters
708
712
  apiHost?: string
709
713
  /**
710
714
  @remarks
@@ -1135,6 +1139,12 @@ export declare type DatasetsResponse = {
1135
1139
  tags: string[]
1136
1140
  }[]
1137
1141
 
1142
+ /** @public */
1143
+ export declare interface DecideParameters {
1144
+ audience: string | string[]
1145
+ [key: string]: unknown
1146
+ }
1147
+
1138
1148
  /**
1139
1149
  * Deletes the published version of a document and optionally some (likely all known) draft versions.
1140
1150
  * If any draft version exists that is not specified for deletion this is an error.
@@ -1871,6 +1881,21 @@ export declare type ImageDescriptionOperation = {
1871
1881
  }
1872
1882
  )
1873
1883
 
1884
+ /** @public */
1885
+ export declare type ImportReleaseAction =
1886
+ | {
1887
+ actionType: 'sanity.action.release.import'
1888
+ attributes: IdentifiedSanityDocumentStub
1889
+ releaseId: string
1890
+ ifExists: 'fail' | 'ignore' | 'replace'
1891
+ }
1892
+ | {
1893
+ actionType: 'sanity.action.release.import'
1894
+ document: IdentifiedSanityDocumentStub
1895
+ releaseId: string
1896
+ ifExists: 'fail' | 'ignore' | 'replace'
1897
+ }
1898
+
1874
1899
  /** @public */
1875
1900
  export declare interface InitializedClientConfig extends ClientConfig {
1876
1901
  apiHost: string
@@ -4126,6 +4151,8 @@ export declare interface QueryParams {
4126
4151
  /** @deprecated you're using a fetch option as a GROQ parameter, this is likely a mistake */
4127
4152
  cache?: 'next' extends keyof RequestInit ? never : any
4128
4153
  /** @deprecated you're using a fetch option as a GROQ parameter, this is likely a mistake */
4154
+ decideParameters?: never
4155
+ /** @deprecated you're using a fetch option as a GROQ parameter, this is likely a mistake */
4129
4156
  filterResponse?: never
4130
4157
  /** @deprecated you're using a fetch option as a GROQ parameter, this is likely a mistake */
4131
4158
  headers?: never
@@ -4237,6 +4264,7 @@ export declare type ReleaseAction =
4237
4264
  | ScheduleReleaseAction
4238
4265
  | UnscheduleReleaseAction
4239
4266
  | DeleteReleaseAction
4267
+ | ImportReleaseAction
4240
4268
 
4241
4269
  /** @public */
4242
4270
  export declare type ReleaseCardinality = 'many' | 'one' | undefined
@@ -4634,6 +4662,7 @@ export declare interface RequestObservableOptions extends Omit<RequestOptions, '
4634
4662
  returnQuery?: boolean
4635
4663
  resultSourceMap?: boolean | 'withKeyArraySelector'
4636
4664
  perspective?: ClientPerspective
4665
+ decideParameters?: DecideParameters
4637
4666
  lastLiveEventId?: string
4638
4667
  cacheMode?: 'noStale'
4639
4668
  }
@@ -4666,6 +4695,7 @@ export declare interface ResponseEvent<T = unknown> {
4666
4695
  /** @public */
4667
4696
  export declare interface ResponseQueryOptions extends RequestOptions {
4668
4697
  perspective?: ClientPerspective
4698
+ decideParameters?: DecideParameters
4669
4699
  resultSourceMap?: boolean | 'withKeyArraySelector'
4670
4700
  returnQuery?: boolean
4671
4701
  useCdn?: boolean
@@ -705,6 +705,10 @@ export declare interface ClientConfig {
705
705
  * @defaultValue 'published'
706
706
  */
707
707
  perspective?: ClientPerspective
708
+ /**
709
+ * Parameters to pass to Sanity Decide for personalisation
710
+ */
711
+ decideParameters?: DecideParameters
708
712
  apiHost?: string
709
713
  /**
710
714
  @remarks
@@ -1135,6 +1139,12 @@ export declare type DatasetsResponse = {
1135
1139
  tags: string[]
1136
1140
  }[]
1137
1141
 
1142
+ /** @public */
1143
+ export declare interface DecideParameters {
1144
+ audience: string | string[]
1145
+ [key: string]: unknown
1146
+ }
1147
+
1138
1148
  /**
1139
1149
  * Deletes the published version of a document and optionally some (likely all known) draft versions.
1140
1150
  * If any draft version exists that is not specified for deletion this is an error.
@@ -1871,6 +1881,21 @@ export declare type ImageDescriptionOperation = {
1871
1881
  }
1872
1882
  )
1873
1883
 
1884
+ /** @public */
1885
+ export declare type ImportReleaseAction =
1886
+ | {
1887
+ actionType: 'sanity.action.release.import'
1888
+ attributes: IdentifiedSanityDocumentStub
1889
+ releaseId: string
1890
+ ifExists: 'fail' | 'ignore' | 'replace'
1891
+ }
1892
+ | {
1893
+ actionType: 'sanity.action.release.import'
1894
+ document: IdentifiedSanityDocumentStub
1895
+ releaseId: string
1896
+ ifExists: 'fail' | 'ignore' | 'replace'
1897
+ }
1898
+
1874
1899
  /** @public */
1875
1900
  export declare interface InitializedClientConfig extends ClientConfig {
1876
1901
  apiHost: string
@@ -4126,6 +4151,8 @@ export declare interface QueryParams {
4126
4151
  /** @deprecated you're using a fetch option as a GROQ parameter, this is likely a mistake */
4127
4152
  cache?: 'next' extends keyof RequestInit ? never : any
4128
4153
  /** @deprecated you're using a fetch option as a GROQ parameter, this is likely a mistake */
4154
+ decideParameters?: never
4155
+ /** @deprecated you're using a fetch option as a GROQ parameter, this is likely a mistake */
4129
4156
  filterResponse?: never
4130
4157
  /** @deprecated you're using a fetch option as a GROQ parameter, this is likely a mistake */
4131
4158
  headers?: never
@@ -4237,6 +4264,7 @@ export declare type ReleaseAction =
4237
4264
  | ScheduleReleaseAction
4238
4265
  | UnscheduleReleaseAction
4239
4266
  | DeleteReleaseAction
4267
+ | ImportReleaseAction
4240
4268
 
4241
4269
  /** @public */
4242
4270
  export declare type ReleaseCardinality = 'many' | 'one' | undefined
@@ -4634,6 +4662,7 @@ export declare interface RequestObservableOptions extends Omit<RequestOptions, '
4634
4662
  returnQuery?: boolean
4635
4663
  resultSourceMap?: boolean | 'withKeyArraySelector'
4636
4664
  perspective?: ClientPerspective
4665
+ decideParameters?: DecideParameters
4637
4666
  lastLiveEventId?: string
4638
4667
  cacheMode?: 'noStale'
4639
4668
  }
@@ -4666,6 +4695,7 @@ export declare interface ResponseEvent<T = unknown> {
4666
4695
  /** @public */
4667
4696
  export declare interface ResponseQueryOptions extends RequestOptions {
4668
4697
  perspective?: ClientPerspective
4698
+ decideParameters?: DecideParameters
4669
4699
  resultSourceMap?: boolean | 'withKeyArraySelector'
4670
4700
  returnQuery?: boolean
4671
4701
  useCdn?: boolean
@@ -839,6 +839,32 @@ function requestOptions(config, overrides = {}) {
839
839
  fetch: typeof overrides.fetch == "object" && typeof config.fetch == "object" ? { ...config.fetch, ...overrides.fetch } : overrides.fetch || config.fetch
840
840
  });
841
841
  }
842
+ function isDecideField(value) {
843
+ return value != null && typeof value == "object" && !Array.isArray(value) && "default" in value && "conditions" in value && Array.isArray(value.conditions);
844
+ }
845
+ function resolveDecideField(field, decideParameters) {
846
+ const audience = decideParameters.audience, matchingCondition = field.conditions.find((condition) => Array.isArray(audience) ? audience.includes(condition.audience) : condition.audience === audience);
847
+ return matchingCondition ? matchingCondition.value : field.default;
848
+ }
849
+ function processObjectRecursively(obj, decideParameters) {
850
+ return obj == null || typeof obj != "object" ? obj : Array.isArray(obj) ? obj.map((item) => processObjectRecursively(item, decideParameters)) : Object.entries(obj).reduce((processed, [key, value]) => {
851
+ try {
852
+ isDecideField(value) ? processed[key] = resolveDecideField(value, decideParameters) : processed[key] = processObjectRecursively(value, decideParameters);
853
+ } catch (error) {
854
+ console.warn(`Failed to process decide field '${key}':`, error), processed[key] = value;
855
+ }
856
+ return processed;
857
+ }, {});
858
+ }
859
+ function processDecideFields(data, decideParameters) {
860
+ if (!decideParameters || !decideParameters.audience)
861
+ return data;
862
+ try {
863
+ return processObjectRecursively(data, decideParameters);
864
+ } catch (error) {
865
+ return console.warn("Failed to process decide fields:", error), data;
866
+ }
867
+ }
842
868
  const encodeQueryString = ({
843
869
  query,
844
870
  params = {},
@@ -863,7 +889,7 @@ function _fetch(client, httpRequest, _stega, query, _params = {}, options = {})
863
889
  const stega = "stega" in options ? {
864
890
  ..._stega || {},
865
891
  ...typeof options.stega == "boolean" ? { enabled: options.stega } : options.stega || {}
866
- } : _stega, params = stega.enabled ? stegaClean(_params) : _params, mapResponse = options.filterResponse === !1 ? (res) => res : (res) => res.result, { cache, next, ...opts } = {
892
+ } : _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 } = {
867
893
  // Opt out of setting a `signal` on an internal `fetch` if one isn't provided.
868
894
  // This is necessary in React Server Components to avoid opting out of Request Memoization.
869
895
  useAbortSignal: typeof options.signal < "u",
@@ -886,11 +912,11 @@ function _fetch(client, httpRequest, _stega, query, _params = {}, options = {})
886
912
  ),
887
913
  map(
888
914
  ([res, stegaEncodeSourceMap]) => {
889
- const result = stegaEncodeSourceMap(res.result, res.resultSourceMap, stega);
890
- return mapResponse({ ...res, result });
915
+ const result = stegaEncodeSourceMap(res.result, res.resultSourceMap, stega), mappedResponse = mapResponse({ ...res, result });
916
+ return processDecideResponse(mappedResponse);
891
917
  }
892
918
  )
893
- ) : $request.pipe(map(mapResponse));
919
+ ) : $request.pipe(map(mapResponse), map(processDecideResponse));
894
920
  }
895
921
  function _getDocument(client, httpRequest, id, opts = {}) {
896
922
  const docId = (() => {
@@ -1043,6 +1069,7 @@ function _dataRequest(client, httpRequest, endpoint, body, options = {}) {
1043
1069
  tag,
1044
1070
  returnQuery,
1045
1071
  perspective: options.perspective,
1072
+ decideParameters: options.decideParameters,
1046
1073
  resultSourceMap: options.resultSourceMap,
1047
1074
  lastLiveEventId: Array.isArray(lastLiveEventId) ? lastLiveEventId[0] : lastLiveEventId,
1048
1075
  cacheMode,
@@ -1087,7 +1114,15 @@ function _requestObservable(client, httpRequest, options) {
1087
1114
  perspective: Array.isArray(perspectiveOption) ? perspectiveOption.join(",") : perspectiveOption,
1088
1115
  ...options.query
1089
1116
  }, (Array.isArray(perspectiveOption) && perspectiveOption.length > 0 || // previewDrafts was renamed to drafts, but keep for backwards compat
1090
- 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 });
1117
+ perspectiveOption === "previewDrafts" || perspectiveOption === "drafts") && useCdn && (useCdn = !1, printCdnPreviewDraftsWarning()));
1118
+ const decideParametersOption = options.decideParameters || config.decideParameters;
1119
+ decideParametersOption && typeof decideParametersOption == "object" && (options.query = {
1120
+ ...options.query,
1121
+ ...Object.keys(decideParametersOption).reduce(
1122
+ (acc, key) => (acc[`decide.${key}`] = decideParametersOption[key], acc),
1123
+ {}
1124
+ )
1125
+ }), 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 });
1091
1126
  }
1092
1127
  const reqOptions = requestOptions(
1093
1128
  config,