@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 +21 -14
- package/dist/index.browser.cjs +40 -5
- package/dist/index.browser.cjs.map +1 -1
- package/dist/index.browser.d.cts +30 -0
- package/dist/index.browser.d.ts +30 -0
- package/dist/index.browser.js +40 -5
- package/dist/index.browser.js.map +1 -1
- package/dist/index.cjs +41 -6
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +30 -0
- package/dist/index.d.ts +30 -0
- package/dist/index.js +41 -6
- package/dist/index.js.map +1 -1
- package/dist/stega.browser.d.cts +30 -0
- package/dist/stega.browser.d.ts +30 -0
- package/dist/stega.d.cts +30 -0
- package/dist/stega.d.ts +30 -0
- package/package.json +1 -1
- package/src/data/dataMethods.ts +31 -2
- package/src/data/decideResponseProcessor.ts +111 -0
- package/src/types.ts +32 -0
- package/umd/sanityClient.js +40 -5
- package/umd/sanityClient.min.js +2 -2
package/dist/index.cjs
CHANGED
|
@@ -682,6 +682,32 @@ function requestOptions(config2, overrides = {}) {
|
|
|
682
682
|
fetch: typeof overrides.fetch == "object" && typeof config2.fetch == "object" ? { ...config2.fetch, ...overrides.fetch } : overrides.fetch || config2.fetch
|
|
683
683
|
});
|
|
684
684
|
}
|
|
685
|
+
function isDecideField(value) {
|
|
686
|
+
return value != null && typeof value == "object" && !Array.isArray(value) && "default" in value && "conditions" in value && Array.isArray(value.conditions);
|
|
687
|
+
}
|
|
688
|
+
function resolveDecideField(field, decideParameters) {
|
|
689
|
+
const audience = decideParameters.audience, matchingCondition = field.conditions.find((condition) => Array.isArray(audience) ? audience.includes(condition.audience) : condition.audience === audience);
|
|
690
|
+
return matchingCondition ? matchingCondition.value : field.default;
|
|
691
|
+
}
|
|
692
|
+
function processObjectRecursively(obj, decideParameters) {
|
|
693
|
+
return obj == null || typeof obj != "object" ? obj : Array.isArray(obj) ? obj.map((item) => processObjectRecursively(item, decideParameters)) : Object.entries(obj).reduce((processed, [key, value]) => {
|
|
694
|
+
try {
|
|
695
|
+
isDecideField(value) ? processed[key] = resolveDecideField(value, decideParameters) : processed[key] = processObjectRecursively(value, decideParameters);
|
|
696
|
+
} catch (error) {
|
|
697
|
+
console.warn(`Failed to process decide field '${key}':`, error), processed[key] = value;
|
|
698
|
+
}
|
|
699
|
+
return processed;
|
|
700
|
+
}, {});
|
|
701
|
+
}
|
|
702
|
+
function processDecideFields(data, decideParameters) {
|
|
703
|
+
if (!decideParameters || !decideParameters.audience)
|
|
704
|
+
return data;
|
|
705
|
+
try {
|
|
706
|
+
return processObjectRecursively(data, decideParameters);
|
|
707
|
+
} catch (error) {
|
|
708
|
+
return console.warn("Failed to process decide fields:", error), data;
|
|
709
|
+
}
|
|
710
|
+
}
|
|
685
711
|
const encodeQueryString = ({
|
|
686
712
|
query,
|
|
687
713
|
params = {},
|
|
@@ -706,7 +732,7 @@ function _fetch(client, httpRequest, _stega, query, _params = {}, options = {})
|
|
|
706
732
|
const stega = "stega" in options ? {
|
|
707
733
|
..._stega || {},
|
|
708
734
|
...typeof options.stega == "boolean" ? { enabled: options.stega } : options.stega || {}
|
|
709
|
-
} : _stega, params = stega.enabled ? stegaClean.stegaClean(_params) : _params, mapResponse = options.filterResponse === !1 ? (res) => res : (res) => res.result, { cache, next, ...opts } = {
|
|
735
|
+
} : _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 } = {
|
|
710
736
|
// Opt out of setting a `signal` on an internal `fetch` if one isn't provided.
|
|
711
737
|
// This is necessary in React Server Components to avoid opting out of Request Memoization.
|
|
712
738
|
useAbortSignal: typeof options.signal < "u",
|
|
@@ -731,11 +757,11 @@ function _fetch(client, httpRequest, _stega, query, _params = {}, options = {})
|
|
|
731
757
|
),
|
|
732
758
|
operators.map(
|
|
733
759
|
([res, stegaEncodeSourceMap]) => {
|
|
734
|
-
const result = stegaEncodeSourceMap(res.result, res.resultSourceMap, stega);
|
|
735
|
-
return
|
|
760
|
+
const result = stegaEncodeSourceMap(res.result, res.resultSourceMap, stega), mappedResponse = mapResponse({ ...res, result });
|
|
761
|
+
return processDecideResponse(mappedResponse);
|
|
736
762
|
}
|
|
737
763
|
)
|
|
738
|
-
) : $request.pipe(operators.map(mapResponse));
|
|
764
|
+
) : $request.pipe(operators.map(mapResponse), operators.map(processDecideResponse));
|
|
739
765
|
}
|
|
740
766
|
function _getDocument(client, httpRequest, id, opts = {}) {
|
|
741
767
|
const docId = (() => {
|
|
@@ -888,6 +914,7 @@ function _dataRequest(client, httpRequest, endpoint, body, options = {}) {
|
|
|
888
914
|
tag,
|
|
889
915
|
returnQuery,
|
|
890
916
|
perspective: options.perspective,
|
|
917
|
+
decideParameters: options.decideParameters,
|
|
891
918
|
resultSourceMap: options.resultSourceMap,
|
|
892
919
|
lastLiveEventId: Array.isArray(lastLiveEventId) ? lastLiveEventId[0] : lastLiveEventId,
|
|
893
920
|
cacheMode,
|
|
@@ -932,7 +959,15 @@ function _requestObservable(client, httpRequest, options) {
|
|
|
932
959
|
perspective: Array.isArray(perspectiveOption) ? perspectiveOption.join(",") : perspectiveOption,
|
|
933
960
|
...options.query
|
|
934
961
|
}, (Array.isArray(perspectiveOption) && perspectiveOption.length > 0 || // previewDrafts was renamed to drafts, but keep for backwards compat
|
|
935
|
-
perspectiveOption === "previewDrafts" || perspectiveOption === "drafts") && useCdn && (useCdn = !1, config.printCdnPreviewDraftsWarning()))
|
|
962
|
+
perspectiveOption === "previewDrafts" || perspectiveOption === "drafts") && useCdn && (useCdn = !1, config.printCdnPreviewDraftsWarning()));
|
|
963
|
+
const decideParametersOption = options.decideParameters || config$1.decideParameters;
|
|
964
|
+
decideParametersOption && typeof decideParametersOption == "object" && (options.query = {
|
|
965
|
+
...options.query,
|
|
966
|
+
...Object.keys(decideParametersOption).reduce(
|
|
967
|
+
(acc, key) => (acc[`decide.${key}`] = decideParametersOption[key], acc),
|
|
968
|
+
{}
|
|
969
|
+
)
|
|
970
|
+
}), 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 });
|
|
936
971
|
}
|
|
937
972
|
const reqOptions = requestOptions(
|
|
938
973
|
config$1,
|
|
@@ -2683,7 +2718,7 @@ function defineDeprecatedCreateClient(createClient2) {
|
|
|
2683
2718
|
return config.printNoDefaultExport(), createClient2(config$1);
|
|
2684
2719
|
};
|
|
2685
2720
|
}
|
|
2686
|
-
var name = "@sanity/client", version = "7.11.
|
|
2721
|
+
var name = "@sanity/client", version = "7.11.2-audience-decide.0";
|
|
2687
2722
|
const middleware = [
|
|
2688
2723
|
middleware$1.debug({ verbose: !0, namespace: "sanity:client" }),
|
|
2689
2724
|
middleware$1.headers({ "User-Agent": `${name} ${version}` }),
|