@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.
package/README.md CHANGED
@@ -2295,20 +2295,25 @@ const client = createClient({
2295
2295
  })
2296
2296
 
2297
2297
  // Basic usage with video asset ID
2298
- const playbackInfo = await client.mediaLibrary.video.getPlaybackInfo('video-30rh9U3GDEK3ToiId1Zje4uvalC-mp4')
2298
+ const playbackInfo = await client.mediaLibrary.video.getPlaybackInfo(
2299
+ 'video-30rh9U3GDEK3ToiId1Zje4uvalC-mp4',
2300
+ )
2299
2301
 
2300
2302
  // With transformations
2301
- const playbackInfo = await client.mediaLibrary.video.getPlaybackInfo('video-30rh9U3GDEK3ToiId1Zje4uvalC-mp4', {
2302
- transformations: {
2303
- thumbnail: { width: 300, format: 'webp', fit: 'smartcrop' },
2304
- animated: { width: 200, fps: 15, format: 'webp' }
2303
+ const playbackInfo = await client.mediaLibrary.video.getPlaybackInfo(
2304
+ 'video-30rh9U3GDEK3ToiId1Zje4uvalC-mp4',
2305
+ {
2306
+ transformations: {
2307
+ thumbnail: {width: 300, format: 'webp', fit: 'smartcrop'},
2308
+ animated: {width: 200, fps: 15, format: 'webp'},
2309
+ },
2310
+ expiration: 3600, // seconds
2305
2311
  },
2306
- expiration: 3600 // seconds
2307
- })
2312
+ )
2308
2313
 
2309
2314
  // Using Global Dataset Reference (GDR)
2310
2315
  const playbackInfo = await client.mediaLibrary.video.getPlaybackInfo({
2311
- _ref: 'media-library:mlZxz9rvqf76:30rh9U3GDEK3ToiId1Zje4uvalC'
2316
+ _ref: 'media-library:mlZxz9rvqf76:30rh9U3GDEK3ToiId1Zje4uvalC',
2312
2317
  })
2313
2318
  ```
2314
2319
 
@@ -2329,19 +2334,19 @@ The response contains playback URLs and metadata:
2329
2334
  // Signed playback response (when video requires authentication)
2330
2335
  {
2331
2336
  id: "30rh9U3GDEK3ToiId1Zje4uvalC",
2332
- stream: {
2337
+ stream: {
2333
2338
  url: "https://stream.m.sanity-cdn.com/...",
2334
2339
  token: "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9..."
2335
2340
  },
2336
- thumbnail: {
2341
+ thumbnail: {
2337
2342
  url: "https://image.m.sanity-cdn.com/...",
2338
2343
  token: "eyJ0a2VuIjoiVGh1bWJuYWlsVG9rZW4tMTIz..."
2339
2344
  },
2340
- animated: {
2345
+ animated: {
2341
2346
  url: "https://image.m.sanity-cdn.com/...",
2342
2347
  token: "eyJ0a2VuIjoiQW5pbWF0ZWRUb2tlbi1kZWY..."
2343
2348
  },
2344
- storyboard: {
2349
+ storyboard: {
2345
2350
  url: "https://storyboard.m.sanity-cdn.com/...",
2346
2351
  token: "eyJ0a2VuIjoiU3Rvcnlib2FyZFRva2VuLTc4..."
2347
2352
  },
@@ -2355,7 +2360,9 @@ The response contains playback URLs and metadata:
2355
2360
  ```js
2356
2361
  import {getPlaybackTokens, isSignedPlaybackInfo} from '@sanity/client/media-library'
2357
2362
 
2358
- const playbackInfo = await client.mediaLibrary.video.getPlaybackInfo('video-30rh9U3GDEK3ToiId1Zje4uvalC-mp4')
2363
+ const playbackInfo = await client.mediaLibrary.video.getPlaybackInfo(
2364
+ 'video-30rh9U3GDEK3ToiId1Zje4uvalC-mp4',
2365
+ )
2359
2366
 
2360
2367
  // Check if the response requires signed URLs
2361
2368
  if (isSignedPlaybackInfo(playbackInfo)) {
@@ -2368,7 +2375,7 @@ if (isSignedPlaybackInfo(playbackInfo)) {
2368
2375
  // animated: "eyJ0a2VuIjoiQW5pbWF0ZWRUb2tlbi1kZWY...",
2369
2376
  // storyboard: "eyJ0a2VuIjoiU3Rvcnlib2FyZFRva2VuLTc4..."
2370
2377
  // }
2371
-
2378
+
2372
2379
  // Use with Mux Player or other compatible players
2373
2380
  // The tokens authenticate access to the video resources
2374
2381
  }
@@ -853,6 +853,33 @@ function requestOptions(config, overrides = {}) {
853
853
  fetch: typeof overrides.fetch == "object" && typeof config.fetch == "object" ? { ...config.fetch, ...overrides.fetch } : overrides.fetch || config.fetch
854
854
  });
855
855
  }
856
+ function isDecideField(value) {
857
+ return value != null && typeof value == "object" && !Array.isArray(value) && "default" in value && "conditions" in value && Array.isArray(value.conditions);
858
+ }
859
+ function resolveDecideField(field, decideParameters) {
860
+ const audience = decideParameters.audience, matchingCondition = field.conditions.find((condition) => Array.isArray(audience) ? audience.includes(condition.audience) : condition.audience === audience);
861
+ return matchingCondition ? matchingCondition.value : field.default;
862
+ }
863
+ function processObjectRecursively(obj, decideParameters) {
864
+ return obj == null || typeof obj != "object" ? obj : Array.isArray(obj) ? obj.map((item) => processObjectRecursively(item, decideParameters)) : Object.entries(obj).reduce((processed, [key, value]) => {
865
+ try {
866
+ isDecideField(value) ? processed[key] = resolveDecideField(value, decideParameters) : processed[key] = processObjectRecursively(value, decideParameters);
867
+ } catch (error) {
868
+ console.warn(`Failed to process decide field '${key}':`, error), processed[key] = value;
869
+ }
870
+ return processed;
871
+ }, {});
872
+ }
873
+ function processDecideFields(data, decideParameters) {
874
+ if (!decideParameters || !decideParameters.audience)
875
+ return data;
876
+ console.log("Starting decide field processing with parameters:", decideParameters);
877
+ try {
878
+ return processObjectRecursively(data, decideParameters);
879
+ } catch (error) {
880
+ return console.warn("Failed to process decide fields:", error), data;
881
+ }
882
+ }
856
883
  const encodeQueryString = ({
857
884
  query,
858
885
  params = {},
@@ -877,7 +904,10 @@ function _fetch(client, httpRequest, _stega, query, _params = {}, options = {})
877
904
  const stega = "stega" in options ? {
878
905
  ..._stega || {},
879
906
  ...typeof options.stega == "boolean" ? { enabled: options.stega } : options.stega || {}
880
- } : _stega, params = stega.enabled ? stegaClean.stegaClean(_params) : _params, mapResponse = options.filterResponse === !1 ? (res) => res : (res) => res.result, { cache, next, ...opts } = {
907
+ } : _stega, params = stega.enabled ? stegaClean.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:", {
908
+ audience: options.decideParameters.audience,
909
+ decideParameters: options.decideParameters
910
+ }), processDecideFields(response, options.decideParameters)) : response, { cache, next, ...opts } = {
881
911
  // Opt out of setting a `signal` on an internal `fetch` if one isn't provided.
882
912
  // This is necessary in React Server Components to avoid opting out of Request Memoization.
883
913
  useAbortSignal: typeof options.signal < "u",
@@ -902,11 +932,11 @@ function _fetch(client, httpRequest, _stega, query, _params = {}, options = {})
902
932
  ),
903
933
  operators.map(
904
934
  ([res, stegaEncodeSourceMap]) => {
905
- const result = stegaEncodeSourceMap(res.result, res.resultSourceMap, stega);
906
- return mapResponse({ ...res, result });
935
+ const result = stegaEncodeSourceMap(res.result, res.resultSourceMap, stega), mappedResponse = mapResponse({ ...res, result });
936
+ return processDecideResponse(mappedResponse);
907
937
  }
908
938
  )
909
- ) : $request.pipe(operators.map(mapResponse));
939
+ ) : $request.pipe(operators.map(mapResponse), operators.map(processDecideResponse));
910
940
  }
911
941
  function _getDocument(client, httpRequest, id, opts = {}) {
912
942
  const docId = (() => {
@@ -1059,6 +1089,7 @@ function _dataRequest(client, httpRequest, endpoint, body, options = {}) {
1059
1089
  tag,
1060
1090
  returnQuery,
1061
1091
  perspective: options.perspective,
1092
+ decideParameters: options.decideParameters,
1062
1093
  resultSourceMap: options.resultSourceMap,
1063
1094
  lastLiveEventId: Array.isArray(lastLiveEventId) ? lastLiveEventId[0] : lastLiveEventId,
1064
1095
  cacheMode,
@@ -1103,7 +1134,20 @@ function _requestObservable(client, httpRequest, options) {
1103
1134
  perspective: Array.isArray(perspectiveOption) ? perspectiveOption.join(",") : perspectiveOption,
1104
1135
  ...options.query
1105
1136
  }, (Array.isArray(perspectiveOption) && perspectiveOption.length > 0 || // previewDrafts was renamed to drafts, but keep for backwards compat
1106
- 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 });
1137
+ perspectiveOption === "previewDrafts" || perspectiveOption === "drafts") && useCdn && (useCdn = !1, printCdnPreviewDraftsWarning()));
1138
+ const decideParametersOption = options.decideParameters || config.decideParameters;
1139
+ if (decideParametersOption && typeof decideParametersOption == "object") {
1140
+ console.log("DecideParameters received:", decideParametersOption);
1141
+ const decideQueryParams = Object.keys(decideParametersOption).reduce(
1142
+ (acc, key) => (acc[`decide.${key}`] = decideParametersOption[key], acc),
1143
+ {}
1144
+ );
1145
+ console.log("DecideParameters transformed to query params:", decideQueryParams), options.query = {
1146
+ ...options.query,
1147
+ ...decideQueryParams
1148
+ };
1149
+ }
1150
+ 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 });
1107
1151
  }
1108
1152
  const reqOptions = requestOptions(
1109
1153
  config,