@sanity/client 7.11.1 → 7.11.2-audience-decide.1

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,33 @@ 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
+ console.log("Starting decide field processing with parameters:", decideParameters);
863
+ try {
864
+ return processObjectRecursively(data, decideParameters);
865
+ } catch (error) {
866
+ return console.warn("Failed to process decide fields:", error), data;
867
+ }
868
+ }
842
869
  const encodeQueryString = ({
843
870
  query,
844
871
  params = {},
@@ -863,7 +890,10 @@ function _fetch(client, httpRequest, _stega, query, _params = {}, options = {})
863
890
  const stega = "stega" in options ? {
864
891
  ..._stega || {},
865
892
  ...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 } = {
893
+ } : _stega, params = stega.enabled ? stegaClean(_params) : _params, mapResponse = options.filterResponse === !1 ? (res) => res : (res) => res.result, processDecideResponse = (response) => options.decideParameters && options.decideParameters.audience ? (console.log("Processing response with decideParameters:", {
894
+ audience: options.decideParameters.audience,
895
+ decideParameters: options.decideParameters
896
+ }), processDecideFields(response, options.decideParameters)) : response, { cache, next, ...opts } = {
867
897
  // Opt out of setting a `signal` on an internal `fetch` if one isn't provided.
868
898
  // This is necessary in React Server Components to avoid opting out of Request Memoization.
869
899
  useAbortSignal: typeof options.signal < "u",
@@ -886,11 +916,11 @@ function _fetch(client, httpRequest, _stega, query, _params = {}, options = {})
886
916
  ),
887
917
  map(
888
918
  ([res, stegaEncodeSourceMap]) => {
889
- const result = stegaEncodeSourceMap(res.result, res.resultSourceMap, stega);
890
- return mapResponse({ ...res, result });
919
+ const result = stegaEncodeSourceMap(res.result, res.resultSourceMap, stega), mappedResponse = mapResponse({ ...res, result });
920
+ return processDecideResponse(mappedResponse);
891
921
  }
892
922
  )
893
- ) : $request.pipe(map(mapResponse));
923
+ ) : $request.pipe(map(mapResponse), map(processDecideResponse));
894
924
  }
895
925
  function _getDocument(client, httpRequest, id, opts = {}) {
896
926
  const docId = (() => {
@@ -1043,6 +1073,7 @@ function _dataRequest(client, httpRequest, endpoint, body, options = {}) {
1043
1073
  tag,
1044
1074
  returnQuery,
1045
1075
  perspective: options.perspective,
1076
+ decideParameters: options.decideParameters,
1046
1077
  resultSourceMap: options.resultSourceMap,
1047
1078
  lastLiveEventId: Array.isArray(lastLiveEventId) ? lastLiveEventId[0] : lastLiveEventId,
1048
1079
  cacheMode,
@@ -1087,7 +1118,20 @@ function _requestObservable(client, httpRequest, options) {
1087
1118
  perspective: Array.isArray(perspectiveOption) ? perspectiveOption.join(",") : perspectiveOption,
1088
1119
  ...options.query
1089
1120
  }, (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 });
1121
+ perspectiveOption === "previewDrafts" || perspectiveOption === "drafts") && useCdn && (useCdn = !1, printCdnPreviewDraftsWarning()));
1122
+ const decideParametersOption = options.decideParameters || config.decideParameters;
1123
+ if (decideParametersOption && typeof decideParametersOption == "object") {
1124
+ console.log("DecideParameters received:", decideParametersOption);
1125
+ const decideQueryParams = Object.keys(decideParametersOption).reduce(
1126
+ (acc, key) => (acc[`decide.${key}`] = decideParametersOption[key], acc),
1127
+ {}
1128
+ );
1129
+ console.log("DecideParameters transformed to query params:", decideQueryParams), options.query = {
1130
+ ...options.query,
1131
+ ...decideQueryParams
1132
+ };
1133
+ }
1134
+ 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
1135
  }
1092
1136
  const reqOptions = requestOptions(
1093
1137
  config,