@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/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,32 @@ 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
+ try {
877
+ return processObjectRecursively(data, decideParameters);
878
+ } catch (error) {
879
+ return console.warn("Failed to process decide fields:", error), data;
880
+ }
881
+ }
856
882
  const encodeQueryString = ({
857
883
  query,
858
884
  params = {},
@@ -877,7 +903,7 @@ function _fetch(client, httpRequest, _stega, query, _params = {}, options = {})
877
903
  const stega = "stega" in options ? {
878
904
  ..._stega || {},
879
905
  ...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 } = {
906
+ } : _stega, params = stega.enabled ? stegaClean.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 } = {
881
907
  // Opt out of setting a `signal` on an internal `fetch` if one isn't provided.
882
908
  // This is necessary in React Server Components to avoid opting out of Request Memoization.
883
909
  useAbortSignal: typeof options.signal < "u",
@@ -902,11 +928,11 @@ function _fetch(client, httpRequest, _stega, query, _params = {}, options = {})
902
928
  ),
903
929
  operators.map(
904
930
  ([res, stegaEncodeSourceMap]) => {
905
- const result = stegaEncodeSourceMap(res.result, res.resultSourceMap, stega);
906
- return mapResponse({ ...res, result });
931
+ const result = stegaEncodeSourceMap(res.result, res.resultSourceMap, stega), mappedResponse = mapResponse({ ...res, result });
932
+ return processDecideResponse(mappedResponse);
907
933
  }
908
934
  )
909
- ) : $request.pipe(operators.map(mapResponse));
935
+ ) : $request.pipe(operators.map(mapResponse), operators.map(processDecideResponse));
910
936
  }
911
937
  function _getDocument(client, httpRequest, id, opts = {}) {
912
938
  const docId = (() => {
@@ -1059,6 +1085,7 @@ function _dataRequest(client, httpRequest, endpoint, body, options = {}) {
1059
1085
  tag,
1060
1086
  returnQuery,
1061
1087
  perspective: options.perspective,
1088
+ decideParameters: options.decideParameters,
1062
1089
  resultSourceMap: options.resultSourceMap,
1063
1090
  lastLiveEventId: Array.isArray(lastLiveEventId) ? lastLiveEventId[0] : lastLiveEventId,
1064
1091
  cacheMode,
@@ -1103,7 +1130,15 @@ function _requestObservable(client, httpRequest, options) {
1103
1130
  perspective: Array.isArray(perspectiveOption) ? perspectiveOption.join(",") : perspectiveOption,
1104
1131
  ...options.query
1105
1132
  }, (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 });
1133
+ perspectiveOption === "previewDrafts" || perspectiveOption === "drafts") && useCdn && (useCdn = !1, printCdnPreviewDraftsWarning()));
1134
+ const decideParametersOption = options.decideParameters || config.decideParameters;
1135
+ decideParametersOption && typeof decideParametersOption == "object" && (options.query = {
1136
+ ...options.query,
1137
+ ...Object.keys(decideParametersOption).reduce(
1138
+ (acc, key) => (acc[`decide.${key}`] = decideParametersOption[key], acc),
1139
+ {}
1140
+ )
1141
+ }), 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
1142
  }
1108
1143
  const reqOptions = requestOptions(
1109
1144
  config,