@sanity/client 7.11.2-audience-decide.3 → 7.11.2-audience-decide.4

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.
@@ -854,30 +854,118 @@ function requestOptions(config, overrides = {}) {
854
854
  });
855
855
  }
856
856
  function isDecideField(value) {
857
- return value != null && typeof value == "object" && !Array.isArray(value) && "default" in value && "conditions" in value && Array.isArray(value.conditions);
857
+ console.log("isDecideField: checking value:", {
858
+ value: typeof value == "object" ? JSON.stringify(value, null, 2) : value,
859
+ type: typeof value,
860
+ isNull: value === null,
861
+ isArray: Array.isArray(value)
862
+ });
863
+ const isValid = value != null && typeof value == "object" && !Array.isArray(value) && "default" in value && "conditions" in value && Array.isArray(value.conditions);
864
+ return console.log("isDecideField: result:", {
865
+ isValid,
866
+ hasDefault: value != null && typeof value == "object" && "default" in value,
867
+ hasConditions: value != null && typeof value == "object" && "conditions" in value,
868
+ conditionsIsArray: value != null && typeof value == "object" && "conditions" in value && Array.isArray(value.conditions)
869
+ }), isValid;
858
870
  }
859
871
  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]) => {
872
+ console.log("resolveDecideField: starting resolution:", {
873
+ field: {
874
+ default: field.default,
875
+ conditionsCount: field.conditions.length,
876
+ conditions: field.conditions.map((c, i) => ({
877
+ index: i,
878
+ audience: c.audience,
879
+ value: c.value
880
+ }))
881
+ },
882
+ decideParameters
883
+ });
884
+ const audience = decideParameters?.audience;
885
+ if (console.log("resolveDecideField: audience processing:", {
886
+ audience,
887
+ audienceType: typeof audience,
888
+ isArray: Array.isArray(audience),
889
+ isEmpty: Array.isArray(audience) ? audience.length === 0 : audience === "",
890
+ isUndefined: audience === void 0,
891
+ isNull: audience === null
892
+ }), !audience || Array.isArray(audience) && audience.length === 0 || audience === "")
893
+ return console.log("resolveDecideField: no valid audience, returning default:", field.default), field.default;
894
+ console.log("resolveDecideField: searching for matching condition...");
895
+ const matchingCondition = field.conditions.find((condition, index) => {
896
+ const isMatch = Array.isArray(audience) ? audience.includes(condition.audience) : condition.audience === audience;
897
+ return console.log(`resolveDecideField: checking condition ${index}:`, {
898
+ conditionAudience: condition.audience,
899
+ conditionValue: condition.value,
900
+ searchAudience: audience,
901
+ isArraySearch: Array.isArray(audience),
902
+ isMatch
903
+ }), isMatch;
904
+ }), resolvedValue = matchingCondition ? matchingCondition.value : field.default;
905
+ return console.log("resolveDecideField: resolution complete:", {
906
+ matchingCondition: matchingCondition ? {
907
+ audience: matchingCondition.audience,
908
+ value: matchingCondition.value
909
+ } : null,
910
+ resolvedValue,
911
+ usedDefault: !matchingCondition
912
+ }), resolvedValue;
913
+ }
914
+ function processObjectRecursively(obj, decideParameters, depth = 0, path = "root") {
915
+ const indent = " ".repeat(depth);
916
+ return console.log(`${indent}processObjectRecursively: processing at path '${path}':`, {
917
+ type: typeof obj,
918
+ isNull: obj === null,
919
+ isUndefined: obj === void 0,
920
+ isArray: Array.isArray(obj),
921
+ depth,
922
+ decideParameters
923
+ }), obj == null || typeof obj != "object" ? (console.log(`${indent}processObjectRecursively: returning primitive/null value:`, obj), obj) : Array.isArray(obj) ? (console.log(`${indent}processObjectRecursively: processing array with ${obj.length} items`), obj.map((item, index) => (console.log(`${indent}processObjectRecursively: processing array item ${index}`), processObjectRecursively(item, decideParameters, depth + 1, `${path}[${index}]`)))) : (console.log(`${indent}processObjectRecursively: processing object with keys:`, Object.keys(obj)), Object.entries(obj).reduce((processed, [key, value]) => {
924
+ const currentPath = `${path}.${key}`;
925
+ console.log(`${indent}processObjectRecursively: processing key '${key}' at path '${currentPath}'`);
865
926
  try {
866
- isDecideField(value) ? processed[key] = resolveDecideField(value, decideParameters) : processed[key] = processObjectRecursively(value, decideParameters);
927
+ isDecideField(value) ? (console.log(`${indent}processObjectRecursively: '${key}' is a decide field, resolving...`), processed[key] = resolveDecideField(value, decideParameters), console.log(`${indent}processObjectRecursively: '${key}' resolved to:`, processed[key])) : (console.log(`${indent}processObjectRecursively: '${key}' is not a decide field, recursing...`), processed[key] = processObjectRecursively(value, decideParameters, depth + 1, currentPath));
867
928
  } catch (error) {
868
- console.warn(`Failed to process decide field '${key}':`, error), processed[key] = value;
929
+ console.warn(`${indent}processObjectRecursively: Failed to process decide field '${key}':`, error), processed[key] = value;
869
930
  }
870
931
  return processed;
871
- }, {});
932
+ }, {}));
872
933
  }
873
934
  function processDecideFields(data, decideParameters) {
874
- if (!decideParameters || !decideParameters.audience)
875
- return data;
876
- console.log("Starting decide field processing with parameters:", decideParameters);
935
+ console.log("=== DECIDE FIELD PROCESSING START ==="), console.log("processDecideFields: analyzing response structure:", {
936
+ dataType: typeof data,
937
+ isObject: typeof data == "object" && data !== null,
938
+ hasResultsKey: typeof data == "object" && data !== null && "result" in data,
939
+ hasDocumentsKey: typeof data == "object" && data !== null && "documents" in data,
940
+ topLevelKeys: typeof data == "object" && data !== null ? Object.keys(data) : null
941
+ });
942
+ let decideFieldCount = 0;
943
+ const countDecideFields = (obj, path = "") => {
944
+ obj == null || typeof obj != "object" || (Array.isArray(obj) ? obj.forEach((item, index) => countDecideFields(item, `${path}[${index}]`)) : Object.entries(obj).forEach(([key, value]) => {
945
+ const currentPath = path ? `${path}.${key}` : key;
946
+ isDecideField(value) ? (decideFieldCount++, console.log(`processDecideFields: Found decide field at path '${currentPath}':`, {
947
+ default: value.default,
948
+ conditionsCount: value.conditions.length
949
+ })) : countDecideFields(value, currentPath);
950
+ }));
951
+ };
952
+ console.log("processDecideFields: scanning for decide fields..."), countDecideFields(data), console.log(`processDecideFields: found ${decideFieldCount} decide fields in response`), console.log("processDecideFields: entry parameters:", {
953
+ decideParameters,
954
+ hasAudience: !!decideParameters?.audience,
955
+ audience: decideParameters?.audience,
956
+ audienceType: typeof decideParameters?.audience,
957
+ dataType: typeof data,
958
+ dataPreview: typeof data == "object" && data !== null ? JSON.stringify(data, null, 2).substring(0, 500) + (JSON.stringify(data, null, 2).length > 500 ? "..." : "") : data
959
+ });
877
960
  try {
878
- return processObjectRecursively(data, decideParameters);
961
+ console.log("processDecideFields: starting recursive processing...");
962
+ const result = processObjectRecursively(data, decideParameters);
963
+ return console.log("processDecideFields: processing completed successfully"), console.log("processDecideFields: result preview:", {
964
+ resultType: typeof result,
965
+ resultPreview: typeof result == "object" && result !== null ? JSON.stringify(result, null, 2).substring(0, 500) + (JSON.stringify(result, null, 2).length > 500 ? "..." : "") : result
966
+ }), console.log("=== DECIDE FIELD PROCESSING END ==="), result;
879
967
  } catch (error) {
880
- return console.warn("Failed to process decide fields:", error), data;
968
+ return console.warn("processDecideFields: processing failed, returning original data:", error), console.log("=== DECIDE FIELD PROCESSING END (ERROR) ==="), data;
881
969
  }
882
970
  }
883
971
  const encodeQueryString = ({
@@ -913,13 +1001,18 @@ function _fetch(client, httpRequest, _stega, query, _params = {}, options = {})
913
1001
  const stega = "stega" in options ? {
914
1002
  ..._stega || {},
915
1003
  ...typeof options.stega == "boolean" ? { enabled: options.stega } : options.stega || {}
916
- } : _stega, params = stega.enabled ? stegaClean.stegaClean(_params) : _params, mapResponse = options.filterResponse === !1 ? (res) => res : (res) => res.result, processDecideResponse = (response) => (console.log("inside processDecideResponse:", {
917
- audience: options.decideParameters?.audience,
918
- decideParameters: options.decideParameters?.audience
919
- }), options.decideParameters && options.decideParameters.audience ? (console.log("Processing response with decideParameters:", {
920
- audience: options.decideParameters.audience,
921
- decideParameters: options.decideParameters
922
- }), processDecideFields(response, options.decideParameters)) : response), { cache, next, ...opts } = {
1004
+ } : _stega, params = stega.enabled ? stegaClean.stegaClean(_params) : _params, mapResponse = options.filterResponse === !1 ? (res) => res : (res) => res.result, processDecideResponse = (response) => {
1005
+ console.log("=== HTTP RESPONSE TO DECIDE PROCESSING ==="), console.log("processDecideResponse: received response for processing:", {
1006
+ responseType: typeof response,
1007
+ responseKeys: typeof response == "object" && response !== null ? Object.keys(response) : null,
1008
+ responsePreview: typeof response == "object" && response !== null ? JSON.stringify(response, null, 2).substring(0, 300) + (JSON.stringify(response, null, 2).length > 300 ? "..." : "") : response,
1009
+ audience: options.decideParameters?.audience,
1010
+ decideParameters: options.decideParameters,
1011
+ hasDecideParameters: !!options.decideParameters
1012
+ }), console.log("processDecideResponse: calling processDecideFields...");
1013
+ const processedData = processDecideFields(response, options.decideParameters);
1014
+ return console.log("processDecideResponse: decide processing complete, returning processed data"), console.log("=== HTTP RESPONSE TO DECIDE PROCESSING COMPLETE ==="), processedData;
1015
+ }, { cache, next, ...opts } = {
923
1016
  // Opt out of setting a `signal` on an internal `fetch` if one isn't provided.
924
1017
  // This is necessary in React Server Components to avoid opting out of Request Memoization.
925
1018
  useAbortSignal: typeof options.signal < "u",
@@ -930,7 +1023,7 @@ function _fetch(client, httpRequest, _stega, query, _params = {}, options = {})
930
1023
  // or `returnQuery` is explicitly set. `true` is the default in Content Lake, so skip if truthy
931
1024
  returnQuery: options.filterResponse === !1 && options.returnQuery !== !1
932
1025
  }, reqOpts = typeof cache < "u" || typeof next < "u" ? { ...opts, fetch: { cache, next } } : opts, $request = _dataRequest(client, httpRequest, "query", { query, params }, reqOpts);
933
- return stega.enabled ? $request.pipe(
1026
+ return console.log("_fetch: pipeline setup, stega.enabled:", stega.enabled), stega.enabled ? $request.pipe(
934
1027
  operators.combineLatestWith(
935
1028
  rxjs.from(
936
1029
  Promise.resolve().then(function() {
@@ -944,11 +1037,18 @@ function _fetch(client, httpRequest, _stega, query, _params = {}, options = {})
944
1037
  ),
945
1038
  operators.map(
946
1039
  ([res, stegaEncodeSourceMap]) => {
1040
+ console.log("_fetch: stega path - processing response");
947
1041
  const result = stegaEncodeSourceMap(res.result, res.resultSourceMap, stega), mappedResponse = mapResponse({ ...res, result });
948
- return processDecideResponse(mappedResponse);
1042
+ return console.log("_fetch: stega path - calling processDecideResponse"), processDecideResponse(mappedResponse);
949
1043
  }
950
1044
  )
951
- ) : $request.pipe(operators.map(mapResponse), operators.map(processDecideResponse));
1045
+ ) : $request.pipe(
1046
+ operators.map((res) => {
1047
+ console.log("_fetch: non-stega path - processing response");
1048
+ const mappedResponse = mapResponse(res);
1049
+ return console.log("_fetch: non-stega path - calling processDecideResponse"), processDecideResponse(mappedResponse);
1050
+ })
1051
+ );
952
1052
  }
953
1053
  function _getDocument(client, httpRequest, id, opts = {}) {
954
1054
  const docId = (() => {