@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 +21 -14
- package/dist/index.browser.cjs +49 -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 +49 -5
- package/dist/index.browser.js.map +1 -1
- package/dist/index.cjs +50 -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 +50 -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 +44 -2
- package/src/data/decideResponseProcessor.ts +114 -0
- package/src/types.ts +32 -0
- package/umd/sanityClient.js +49 -5
- package/umd/sanityClient.min.js +2 -2
package/dist/index.d.cts
CHANGED
|
@@ -706,6 +706,10 @@ export declare interface ClientConfig {
|
|
|
706
706
|
* @defaultValue 'published'
|
|
707
707
|
*/
|
|
708
708
|
perspective?: ClientPerspective
|
|
709
|
+
/**
|
|
710
|
+
* Parameters to pass to Sanity Decide for personalisation
|
|
711
|
+
*/
|
|
712
|
+
decideParameters?: DecideParameters
|
|
709
713
|
apiHost?: string
|
|
710
714
|
/**
|
|
711
715
|
@remarks
|
|
@@ -1132,6 +1136,12 @@ export declare type DatasetsResponse = {
|
|
|
1132
1136
|
tags: string[]
|
|
1133
1137
|
}[]
|
|
1134
1138
|
|
|
1139
|
+
/** @public */
|
|
1140
|
+
export declare interface DecideParameters {
|
|
1141
|
+
audience: string | string[]
|
|
1142
|
+
[key: string]: unknown
|
|
1143
|
+
}
|
|
1144
|
+
|
|
1135
1145
|
/**
|
|
1136
1146
|
* Deletes the published version of a document and optionally some (likely all known) draft versions.
|
|
1137
1147
|
* If any draft version exists that is not specified for deletion this is an error.
|
|
@@ -1863,6 +1873,21 @@ export declare type ImageDescriptionOperation = {
|
|
|
1863
1873
|
}
|
|
1864
1874
|
)
|
|
1865
1875
|
|
|
1876
|
+
/** @public */
|
|
1877
|
+
export declare type ImportReleaseAction =
|
|
1878
|
+
| {
|
|
1879
|
+
actionType: 'sanity.action.release.import'
|
|
1880
|
+
attributes: IdentifiedSanityDocumentStub
|
|
1881
|
+
releaseId: string
|
|
1882
|
+
ifExists: 'fail' | 'ignore' | 'replace'
|
|
1883
|
+
}
|
|
1884
|
+
| {
|
|
1885
|
+
actionType: 'sanity.action.release.import'
|
|
1886
|
+
document: IdentifiedSanityDocumentStub
|
|
1887
|
+
releaseId: string
|
|
1888
|
+
ifExists: 'fail' | 'ignore' | 'replace'
|
|
1889
|
+
}
|
|
1890
|
+
|
|
1866
1891
|
/** @public */
|
|
1867
1892
|
export declare interface InitializedClientConfig extends ClientConfig {
|
|
1868
1893
|
apiHost: string
|
|
@@ -4118,6 +4143,8 @@ export declare interface QueryParams {
|
|
|
4118
4143
|
/** @deprecated you're using a fetch option as a GROQ parameter, this is likely a mistake */
|
|
4119
4144
|
cache?: 'next' extends keyof RequestInit ? never : any
|
|
4120
4145
|
/** @deprecated you're using a fetch option as a GROQ parameter, this is likely a mistake */
|
|
4146
|
+
decideParameters?: never
|
|
4147
|
+
/** @deprecated you're using a fetch option as a GROQ parameter, this is likely a mistake */
|
|
4121
4148
|
filterResponse?: never
|
|
4122
4149
|
/** @deprecated you're using a fetch option as a GROQ parameter, this is likely a mistake */
|
|
4123
4150
|
headers?: never
|
|
@@ -4229,6 +4256,7 @@ export declare type ReleaseAction =
|
|
|
4229
4256
|
| ScheduleReleaseAction
|
|
4230
4257
|
| UnscheduleReleaseAction
|
|
4231
4258
|
| DeleteReleaseAction
|
|
4259
|
+
| ImportReleaseAction
|
|
4232
4260
|
|
|
4233
4261
|
/** @public */
|
|
4234
4262
|
export declare type ReleaseCardinality = 'many' | 'one' | undefined
|
|
@@ -4626,6 +4654,7 @@ export declare interface RequestObservableOptions extends Omit<RequestOptions, '
|
|
|
4626
4654
|
returnQuery?: boolean
|
|
4627
4655
|
resultSourceMap?: boolean | 'withKeyArraySelector'
|
|
4628
4656
|
perspective?: ClientPerspective
|
|
4657
|
+
decideParameters?: DecideParameters
|
|
4629
4658
|
lastLiveEventId?: string
|
|
4630
4659
|
cacheMode?: 'noStale'
|
|
4631
4660
|
}
|
|
@@ -4658,6 +4687,7 @@ export declare interface ResponseEvent<T = unknown> {
|
|
|
4658
4687
|
/** @public */
|
|
4659
4688
|
export declare interface ResponseQueryOptions extends RequestOptions {
|
|
4660
4689
|
perspective?: ClientPerspective
|
|
4690
|
+
decideParameters?: DecideParameters
|
|
4661
4691
|
resultSourceMap?: boolean | 'withKeyArraySelector'
|
|
4662
4692
|
returnQuery?: boolean
|
|
4663
4693
|
useCdn?: boolean
|
package/dist/index.d.ts
CHANGED
|
@@ -706,6 +706,10 @@ export declare interface ClientConfig {
|
|
|
706
706
|
* @defaultValue 'published'
|
|
707
707
|
*/
|
|
708
708
|
perspective?: ClientPerspective
|
|
709
|
+
/**
|
|
710
|
+
* Parameters to pass to Sanity Decide for personalisation
|
|
711
|
+
*/
|
|
712
|
+
decideParameters?: DecideParameters
|
|
709
713
|
apiHost?: string
|
|
710
714
|
/**
|
|
711
715
|
@remarks
|
|
@@ -1132,6 +1136,12 @@ export declare type DatasetsResponse = {
|
|
|
1132
1136
|
tags: string[]
|
|
1133
1137
|
}[]
|
|
1134
1138
|
|
|
1139
|
+
/** @public */
|
|
1140
|
+
export declare interface DecideParameters {
|
|
1141
|
+
audience: string | string[]
|
|
1142
|
+
[key: string]: unknown
|
|
1143
|
+
}
|
|
1144
|
+
|
|
1135
1145
|
/**
|
|
1136
1146
|
* Deletes the published version of a document and optionally some (likely all known) draft versions.
|
|
1137
1147
|
* If any draft version exists that is not specified for deletion this is an error.
|
|
@@ -1863,6 +1873,21 @@ export declare type ImageDescriptionOperation = {
|
|
|
1863
1873
|
}
|
|
1864
1874
|
)
|
|
1865
1875
|
|
|
1876
|
+
/** @public */
|
|
1877
|
+
export declare type ImportReleaseAction =
|
|
1878
|
+
| {
|
|
1879
|
+
actionType: 'sanity.action.release.import'
|
|
1880
|
+
attributes: IdentifiedSanityDocumentStub
|
|
1881
|
+
releaseId: string
|
|
1882
|
+
ifExists: 'fail' | 'ignore' | 'replace'
|
|
1883
|
+
}
|
|
1884
|
+
| {
|
|
1885
|
+
actionType: 'sanity.action.release.import'
|
|
1886
|
+
document: IdentifiedSanityDocumentStub
|
|
1887
|
+
releaseId: string
|
|
1888
|
+
ifExists: 'fail' | 'ignore' | 'replace'
|
|
1889
|
+
}
|
|
1890
|
+
|
|
1866
1891
|
/** @public */
|
|
1867
1892
|
export declare interface InitializedClientConfig extends ClientConfig {
|
|
1868
1893
|
apiHost: string
|
|
@@ -4118,6 +4143,8 @@ export declare interface QueryParams {
|
|
|
4118
4143
|
/** @deprecated you're using a fetch option as a GROQ parameter, this is likely a mistake */
|
|
4119
4144
|
cache?: 'next' extends keyof RequestInit ? never : any
|
|
4120
4145
|
/** @deprecated you're using a fetch option as a GROQ parameter, this is likely a mistake */
|
|
4146
|
+
decideParameters?: never
|
|
4147
|
+
/** @deprecated you're using a fetch option as a GROQ parameter, this is likely a mistake */
|
|
4121
4148
|
filterResponse?: never
|
|
4122
4149
|
/** @deprecated you're using a fetch option as a GROQ parameter, this is likely a mistake */
|
|
4123
4150
|
headers?: never
|
|
@@ -4229,6 +4256,7 @@ export declare type ReleaseAction =
|
|
|
4229
4256
|
| ScheduleReleaseAction
|
|
4230
4257
|
| UnscheduleReleaseAction
|
|
4231
4258
|
| DeleteReleaseAction
|
|
4259
|
+
| ImportReleaseAction
|
|
4232
4260
|
|
|
4233
4261
|
/** @public */
|
|
4234
4262
|
export declare type ReleaseCardinality = 'many' | 'one' | undefined
|
|
@@ -4626,6 +4654,7 @@ export declare interface RequestObservableOptions extends Omit<RequestOptions, '
|
|
|
4626
4654
|
returnQuery?: boolean
|
|
4627
4655
|
resultSourceMap?: boolean | 'withKeyArraySelector'
|
|
4628
4656
|
perspective?: ClientPerspective
|
|
4657
|
+
decideParameters?: DecideParameters
|
|
4629
4658
|
lastLiveEventId?: string
|
|
4630
4659
|
cacheMode?: 'noStale'
|
|
4631
4660
|
}
|
|
@@ -4658,6 +4687,7 @@ export declare interface ResponseEvent<T = unknown> {
|
|
|
4658
4687
|
/** @public */
|
|
4659
4688
|
export declare interface ResponseQueryOptions extends RequestOptions {
|
|
4660
4689
|
perspective?: ClientPerspective
|
|
4690
|
+
decideParameters?: DecideParameters
|
|
4661
4691
|
resultSourceMap?: boolean | 'withKeyArraySelector'
|
|
4662
4692
|
returnQuery?: boolean
|
|
4663
4693
|
useCdn?: boolean
|
package/dist/index.js
CHANGED
|
@@ -670,6 +670,33 @@ function requestOptions(config, overrides = {}) {
|
|
|
670
670
|
fetch: typeof overrides.fetch == "object" && typeof config.fetch == "object" ? { ...config.fetch, ...overrides.fetch } : overrides.fetch || config.fetch
|
|
671
671
|
});
|
|
672
672
|
}
|
|
673
|
+
function isDecideField(value) {
|
|
674
|
+
return value != null && typeof value == "object" && !Array.isArray(value) && "default" in value && "conditions" in value && Array.isArray(value.conditions);
|
|
675
|
+
}
|
|
676
|
+
function resolveDecideField(field, decideParameters) {
|
|
677
|
+
const audience = decideParameters.audience, matchingCondition = field.conditions.find((condition) => Array.isArray(audience) ? audience.includes(condition.audience) : condition.audience === audience);
|
|
678
|
+
return matchingCondition ? matchingCondition.value : field.default;
|
|
679
|
+
}
|
|
680
|
+
function processObjectRecursively(obj, decideParameters) {
|
|
681
|
+
return obj == null || typeof obj != "object" ? obj : Array.isArray(obj) ? obj.map((item) => processObjectRecursively(item, decideParameters)) : Object.entries(obj).reduce((processed, [key, value]) => {
|
|
682
|
+
try {
|
|
683
|
+
isDecideField(value) ? processed[key] = resolveDecideField(value, decideParameters) : processed[key] = processObjectRecursively(value, decideParameters);
|
|
684
|
+
} catch (error) {
|
|
685
|
+
console.warn(`Failed to process decide field '${key}':`, error), processed[key] = value;
|
|
686
|
+
}
|
|
687
|
+
return processed;
|
|
688
|
+
}, {});
|
|
689
|
+
}
|
|
690
|
+
function processDecideFields(data, decideParameters) {
|
|
691
|
+
if (!decideParameters || !decideParameters.audience)
|
|
692
|
+
return data;
|
|
693
|
+
console.log("Starting decide field processing with parameters:", decideParameters);
|
|
694
|
+
try {
|
|
695
|
+
return processObjectRecursively(data, decideParameters);
|
|
696
|
+
} catch (error) {
|
|
697
|
+
return console.warn("Failed to process decide fields:", error), data;
|
|
698
|
+
}
|
|
699
|
+
}
|
|
673
700
|
const encodeQueryString = ({
|
|
674
701
|
query,
|
|
675
702
|
params = {},
|
|
@@ -694,7 +721,10 @@ function _fetch(client, httpRequest, _stega, query, _params = {}, options = {})
|
|
|
694
721
|
const stega = "stega" in options ? {
|
|
695
722
|
..._stega || {},
|
|
696
723
|
...typeof options.stega == "boolean" ? { enabled: options.stega } : options.stega || {}
|
|
697
|
-
} : _stega, params = stega.enabled ? stegaClean(_params) : _params, mapResponse = options.filterResponse === !1 ? (res) => res : (res) => res.result,
|
|
724
|
+
} : _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:", {
|
|
725
|
+
audience: options.decideParameters.audience,
|
|
726
|
+
decideParameters: options.decideParameters
|
|
727
|
+
}), processDecideFields(response, options.decideParameters)) : response, { cache, next, ...opts } = {
|
|
698
728
|
// Opt out of setting a `signal` on an internal `fetch` if one isn't provided.
|
|
699
729
|
// This is necessary in React Server Components to avoid opting out of Request Memoization.
|
|
700
730
|
useAbortSignal: typeof options.signal < "u",
|
|
@@ -717,11 +747,11 @@ function _fetch(client, httpRequest, _stega, query, _params = {}, options = {})
|
|
|
717
747
|
),
|
|
718
748
|
map(
|
|
719
749
|
([res, stegaEncodeSourceMap]) => {
|
|
720
|
-
const result = stegaEncodeSourceMap(res.result, res.resultSourceMap, stega);
|
|
721
|
-
return
|
|
750
|
+
const result = stegaEncodeSourceMap(res.result, res.resultSourceMap, stega), mappedResponse = mapResponse({ ...res, result });
|
|
751
|
+
return processDecideResponse(mappedResponse);
|
|
722
752
|
}
|
|
723
753
|
)
|
|
724
|
-
) : $request.pipe(map(mapResponse));
|
|
754
|
+
) : $request.pipe(map(mapResponse), map(processDecideResponse));
|
|
725
755
|
}
|
|
726
756
|
function _getDocument(client, httpRequest, id, opts = {}) {
|
|
727
757
|
const docId = (() => {
|
|
@@ -874,6 +904,7 @@ function _dataRequest(client, httpRequest, endpoint, body, options = {}) {
|
|
|
874
904
|
tag,
|
|
875
905
|
returnQuery,
|
|
876
906
|
perspective: options.perspective,
|
|
907
|
+
decideParameters: options.decideParameters,
|
|
877
908
|
resultSourceMap: options.resultSourceMap,
|
|
878
909
|
lastLiveEventId: Array.isArray(lastLiveEventId) ? lastLiveEventId[0] : lastLiveEventId,
|
|
879
910
|
cacheMode,
|
|
@@ -918,7 +949,20 @@ function _requestObservable(client, httpRequest, options) {
|
|
|
918
949
|
perspective: Array.isArray(perspectiveOption) ? perspectiveOption.join(",") : perspectiveOption,
|
|
919
950
|
...options.query
|
|
920
951
|
}, (Array.isArray(perspectiveOption) && perspectiveOption.length > 0 || // previewDrafts was renamed to drafts, but keep for backwards compat
|
|
921
|
-
perspectiveOption === "previewDrafts" || perspectiveOption === "drafts") && useCdn && (useCdn = !1, printCdnPreviewDraftsWarning()))
|
|
952
|
+
perspectiveOption === "previewDrafts" || perspectiveOption === "drafts") && useCdn && (useCdn = !1, printCdnPreviewDraftsWarning()));
|
|
953
|
+
const decideParametersOption = options.decideParameters || config.decideParameters;
|
|
954
|
+
if (decideParametersOption && typeof decideParametersOption == "object") {
|
|
955
|
+
console.log("DecideParameters received:", decideParametersOption);
|
|
956
|
+
const decideQueryParams = Object.keys(decideParametersOption).reduce(
|
|
957
|
+
(acc, key) => (acc[`decide.${key}`] = decideParametersOption[key], acc),
|
|
958
|
+
{}
|
|
959
|
+
);
|
|
960
|
+
console.log("DecideParameters transformed to query params:", decideQueryParams), options.query = {
|
|
961
|
+
...options.query,
|
|
962
|
+
...decideQueryParams
|
|
963
|
+
};
|
|
964
|
+
}
|
|
965
|
+
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 });
|
|
922
966
|
}
|
|
923
967
|
const reqOptions = requestOptions(
|
|
924
968
|
config,
|
|
@@ -2669,7 +2713,7 @@ function defineDeprecatedCreateClient(createClient2) {
|
|
|
2669
2713
|
return printNoDefaultExport(), createClient2(config);
|
|
2670
2714
|
};
|
|
2671
2715
|
}
|
|
2672
|
-
var name = "@sanity/client", version = "7.11.1";
|
|
2716
|
+
var name = "@sanity/client", version = "7.11.2-audience-decide.1";
|
|
2673
2717
|
const middleware = [
|
|
2674
2718
|
debug({ verbose: !0, namespace: "sanity:client" }),
|
|
2675
2719
|
headers({ "User-Agent": `${name} ${version}` }),
|