@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/stega.browser.d.cts
CHANGED
|
@@ -699,6 +699,10 @@ export declare interface ClientConfig {
|
|
|
699
699
|
* @defaultValue 'published'
|
|
700
700
|
*/
|
|
701
701
|
perspective?: ClientPerspective
|
|
702
|
+
/**
|
|
703
|
+
* Parameters to pass to Sanity Decide for personalisation
|
|
704
|
+
*/
|
|
705
|
+
decideParameters?: DecideParameters
|
|
702
706
|
apiHost?: string
|
|
703
707
|
/**
|
|
704
708
|
@remarks
|
|
@@ -1226,6 +1230,12 @@ export declare type DatasetsResponse = {
|
|
|
1226
1230
|
tags: string[]
|
|
1227
1231
|
}[]
|
|
1228
1232
|
|
|
1233
|
+
/** @public */
|
|
1234
|
+
export declare interface DecideParameters {
|
|
1235
|
+
audience: string | string[]
|
|
1236
|
+
[key: string]: unknown
|
|
1237
|
+
}
|
|
1238
|
+
|
|
1229
1239
|
/**
|
|
1230
1240
|
* Deletes the published version of a document and optionally some (likely all known) draft versions.
|
|
1231
1241
|
* If any draft version exists that is not specified for deletion this is an error.
|
|
@@ -2018,6 +2028,21 @@ export declare type ImageDescriptionOperation = {
|
|
|
2018
2028
|
}
|
|
2019
2029
|
)
|
|
2020
2030
|
|
|
2031
|
+
/** @public */
|
|
2032
|
+
export declare type ImportReleaseAction =
|
|
2033
|
+
| {
|
|
2034
|
+
actionType: 'sanity.action.release.import'
|
|
2035
|
+
attributes: IdentifiedSanityDocumentStub
|
|
2036
|
+
releaseId: string
|
|
2037
|
+
ifExists: 'fail' | 'ignore' | 'replace'
|
|
2038
|
+
}
|
|
2039
|
+
| {
|
|
2040
|
+
actionType: 'sanity.action.release.import'
|
|
2041
|
+
document: IdentifiedSanityDocumentStub
|
|
2042
|
+
releaseId: string
|
|
2043
|
+
ifExists: 'fail' | 'ignore' | 'replace'
|
|
2044
|
+
}
|
|
2045
|
+
|
|
2021
2046
|
/** @public */
|
|
2022
2047
|
export declare interface InitializedClientConfig extends ClientConfig {
|
|
2023
2048
|
apiHost: string
|
|
@@ -4296,6 +4321,8 @@ export declare interface QueryParams {
|
|
|
4296
4321
|
/** @deprecated you're using a fetch option as a GROQ parameter, this is likely a mistake */
|
|
4297
4322
|
cache?: 'next' extends keyof RequestInit ? never : any
|
|
4298
4323
|
/** @deprecated you're using a fetch option as a GROQ parameter, this is likely a mistake */
|
|
4324
|
+
decideParameters?: never
|
|
4325
|
+
/** @deprecated you're using a fetch option as a GROQ parameter, this is likely a mistake */
|
|
4299
4326
|
filterResponse?: never
|
|
4300
4327
|
/** @deprecated you're using a fetch option as a GROQ parameter, this is likely a mistake */
|
|
4301
4328
|
headers?: never
|
|
@@ -4407,6 +4434,7 @@ export declare type ReleaseAction =
|
|
|
4407
4434
|
| ScheduleReleaseAction
|
|
4408
4435
|
| UnscheduleReleaseAction
|
|
4409
4436
|
| DeleteReleaseAction
|
|
4437
|
+
| ImportReleaseAction
|
|
4410
4438
|
|
|
4411
4439
|
/** @public */
|
|
4412
4440
|
export declare type ReleaseCardinality = 'many' | 'one' | undefined
|
|
@@ -4807,6 +4835,7 @@ export declare interface RequestObservableOptions extends Omit<RequestOptions, '
|
|
|
4807
4835
|
returnQuery?: boolean
|
|
4808
4836
|
resultSourceMap?: boolean | 'withKeyArraySelector'
|
|
4809
4837
|
perspective?: ClientPerspective
|
|
4838
|
+
decideParameters?: DecideParameters
|
|
4810
4839
|
lastLiveEventId?: string
|
|
4811
4840
|
cacheMode?: 'noStale'
|
|
4812
4841
|
}
|
|
@@ -4842,6 +4871,7 @@ export declare interface ResponseEvent<T = unknown> {
|
|
|
4842
4871
|
/** @public */
|
|
4843
4872
|
export declare interface ResponseQueryOptions extends RequestOptions {
|
|
4844
4873
|
perspective?: ClientPerspective
|
|
4874
|
+
decideParameters?: DecideParameters
|
|
4845
4875
|
resultSourceMap?: boolean | 'withKeyArraySelector'
|
|
4846
4876
|
returnQuery?: boolean
|
|
4847
4877
|
useCdn?: boolean
|
package/dist/stega.browser.d.ts
CHANGED
|
@@ -699,6 +699,10 @@ export declare interface ClientConfig {
|
|
|
699
699
|
* @defaultValue 'published'
|
|
700
700
|
*/
|
|
701
701
|
perspective?: ClientPerspective
|
|
702
|
+
/**
|
|
703
|
+
* Parameters to pass to Sanity Decide for personalisation
|
|
704
|
+
*/
|
|
705
|
+
decideParameters?: DecideParameters
|
|
702
706
|
apiHost?: string
|
|
703
707
|
/**
|
|
704
708
|
@remarks
|
|
@@ -1226,6 +1230,12 @@ export declare type DatasetsResponse = {
|
|
|
1226
1230
|
tags: string[]
|
|
1227
1231
|
}[]
|
|
1228
1232
|
|
|
1233
|
+
/** @public */
|
|
1234
|
+
export declare interface DecideParameters {
|
|
1235
|
+
audience: string | string[]
|
|
1236
|
+
[key: string]: unknown
|
|
1237
|
+
}
|
|
1238
|
+
|
|
1229
1239
|
/**
|
|
1230
1240
|
* Deletes the published version of a document and optionally some (likely all known) draft versions.
|
|
1231
1241
|
* If any draft version exists that is not specified for deletion this is an error.
|
|
@@ -2018,6 +2028,21 @@ export declare type ImageDescriptionOperation = {
|
|
|
2018
2028
|
}
|
|
2019
2029
|
)
|
|
2020
2030
|
|
|
2031
|
+
/** @public */
|
|
2032
|
+
export declare type ImportReleaseAction =
|
|
2033
|
+
| {
|
|
2034
|
+
actionType: 'sanity.action.release.import'
|
|
2035
|
+
attributes: IdentifiedSanityDocumentStub
|
|
2036
|
+
releaseId: string
|
|
2037
|
+
ifExists: 'fail' | 'ignore' | 'replace'
|
|
2038
|
+
}
|
|
2039
|
+
| {
|
|
2040
|
+
actionType: 'sanity.action.release.import'
|
|
2041
|
+
document: IdentifiedSanityDocumentStub
|
|
2042
|
+
releaseId: string
|
|
2043
|
+
ifExists: 'fail' | 'ignore' | 'replace'
|
|
2044
|
+
}
|
|
2045
|
+
|
|
2021
2046
|
/** @public */
|
|
2022
2047
|
export declare interface InitializedClientConfig extends ClientConfig {
|
|
2023
2048
|
apiHost: string
|
|
@@ -4296,6 +4321,8 @@ export declare interface QueryParams {
|
|
|
4296
4321
|
/** @deprecated you're using a fetch option as a GROQ parameter, this is likely a mistake */
|
|
4297
4322
|
cache?: 'next' extends keyof RequestInit ? never : any
|
|
4298
4323
|
/** @deprecated you're using a fetch option as a GROQ parameter, this is likely a mistake */
|
|
4324
|
+
decideParameters?: never
|
|
4325
|
+
/** @deprecated you're using a fetch option as a GROQ parameter, this is likely a mistake */
|
|
4299
4326
|
filterResponse?: never
|
|
4300
4327
|
/** @deprecated you're using a fetch option as a GROQ parameter, this is likely a mistake */
|
|
4301
4328
|
headers?: never
|
|
@@ -4407,6 +4434,7 @@ export declare type ReleaseAction =
|
|
|
4407
4434
|
| ScheduleReleaseAction
|
|
4408
4435
|
| UnscheduleReleaseAction
|
|
4409
4436
|
| DeleteReleaseAction
|
|
4437
|
+
| ImportReleaseAction
|
|
4410
4438
|
|
|
4411
4439
|
/** @public */
|
|
4412
4440
|
export declare type ReleaseCardinality = 'many' | 'one' | undefined
|
|
@@ -4807,6 +4835,7 @@ export declare interface RequestObservableOptions extends Omit<RequestOptions, '
|
|
|
4807
4835
|
returnQuery?: boolean
|
|
4808
4836
|
resultSourceMap?: boolean | 'withKeyArraySelector'
|
|
4809
4837
|
perspective?: ClientPerspective
|
|
4838
|
+
decideParameters?: DecideParameters
|
|
4810
4839
|
lastLiveEventId?: string
|
|
4811
4840
|
cacheMode?: 'noStale'
|
|
4812
4841
|
}
|
|
@@ -4842,6 +4871,7 @@ export declare interface ResponseEvent<T = unknown> {
|
|
|
4842
4871
|
/** @public */
|
|
4843
4872
|
export declare interface ResponseQueryOptions extends RequestOptions {
|
|
4844
4873
|
perspective?: ClientPerspective
|
|
4874
|
+
decideParameters?: DecideParameters
|
|
4845
4875
|
resultSourceMap?: boolean | 'withKeyArraySelector'
|
|
4846
4876
|
returnQuery?: boolean
|
|
4847
4877
|
useCdn?: boolean
|
package/dist/stega.d.cts
CHANGED
|
@@ -699,6 +699,10 @@ export declare interface ClientConfig {
|
|
|
699
699
|
* @defaultValue 'published'
|
|
700
700
|
*/
|
|
701
701
|
perspective?: ClientPerspective
|
|
702
|
+
/**
|
|
703
|
+
* Parameters to pass to Sanity Decide for personalisation
|
|
704
|
+
*/
|
|
705
|
+
decideParameters?: DecideParameters
|
|
702
706
|
apiHost?: string
|
|
703
707
|
/**
|
|
704
708
|
@remarks
|
|
@@ -1226,6 +1230,12 @@ export declare type DatasetsResponse = {
|
|
|
1226
1230
|
tags: string[]
|
|
1227
1231
|
}[]
|
|
1228
1232
|
|
|
1233
|
+
/** @public */
|
|
1234
|
+
export declare interface DecideParameters {
|
|
1235
|
+
audience: string | string[]
|
|
1236
|
+
[key: string]: unknown
|
|
1237
|
+
}
|
|
1238
|
+
|
|
1229
1239
|
/**
|
|
1230
1240
|
* Deletes the published version of a document and optionally some (likely all known) draft versions.
|
|
1231
1241
|
* If any draft version exists that is not specified for deletion this is an error.
|
|
@@ -2018,6 +2028,21 @@ export declare type ImageDescriptionOperation = {
|
|
|
2018
2028
|
}
|
|
2019
2029
|
)
|
|
2020
2030
|
|
|
2031
|
+
/** @public */
|
|
2032
|
+
export declare type ImportReleaseAction =
|
|
2033
|
+
| {
|
|
2034
|
+
actionType: 'sanity.action.release.import'
|
|
2035
|
+
attributes: IdentifiedSanityDocumentStub
|
|
2036
|
+
releaseId: string
|
|
2037
|
+
ifExists: 'fail' | 'ignore' | 'replace'
|
|
2038
|
+
}
|
|
2039
|
+
| {
|
|
2040
|
+
actionType: 'sanity.action.release.import'
|
|
2041
|
+
document: IdentifiedSanityDocumentStub
|
|
2042
|
+
releaseId: string
|
|
2043
|
+
ifExists: 'fail' | 'ignore' | 'replace'
|
|
2044
|
+
}
|
|
2045
|
+
|
|
2021
2046
|
/** @public */
|
|
2022
2047
|
export declare interface InitializedClientConfig extends ClientConfig {
|
|
2023
2048
|
apiHost: string
|
|
@@ -4296,6 +4321,8 @@ export declare interface QueryParams {
|
|
|
4296
4321
|
/** @deprecated you're using a fetch option as a GROQ parameter, this is likely a mistake */
|
|
4297
4322
|
cache?: 'next' extends keyof RequestInit ? never : any
|
|
4298
4323
|
/** @deprecated you're using a fetch option as a GROQ parameter, this is likely a mistake */
|
|
4324
|
+
decideParameters?: never
|
|
4325
|
+
/** @deprecated you're using a fetch option as a GROQ parameter, this is likely a mistake */
|
|
4299
4326
|
filterResponse?: never
|
|
4300
4327
|
/** @deprecated you're using a fetch option as a GROQ parameter, this is likely a mistake */
|
|
4301
4328
|
headers?: never
|
|
@@ -4407,6 +4434,7 @@ export declare type ReleaseAction =
|
|
|
4407
4434
|
| ScheduleReleaseAction
|
|
4408
4435
|
| UnscheduleReleaseAction
|
|
4409
4436
|
| DeleteReleaseAction
|
|
4437
|
+
| ImportReleaseAction
|
|
4410
4438
|
|
|
4411
4439
|
/** @public */
|
|
4412
4440
|
export declare type ReleaseCardinality = 'many' | 'one' | undefined
|
|
@@ -4807,6 +4835,7 @@ export declare interface RequestObservableOptions extends Omit<RequestOptions, '
|
|
|
4807
4835
|
returnQuery?: boolean
|
|
4808
4836
|
resultSourceMap?: boolean | 'withKeyArraySelector'
|
|
4809
4837
|
perspective?: ClientPerspective
|
|
4838
|
+
decideParameters?: DecideParameters
|
|
4810
4839
|
lastLiveEventId?: string
|
|
4811
4840
|
cacheMode?: 'noStale'
|
|
4812
4841
|
}
|
|
@@ -4842,6 +4871,7 @@ export declare interface ResponseEvent<T = unknown> {
|
|
|
4842
4871
|
/** @public */
|
|
4843
4872
|
export declare interface ResponseQueryOptions extends RequestOptions {
|
|
4844
4873
|
perspective?: ClientPerspective
|
|
4874
|
+
decideParameters?: DecideParameters
|
|
4845
4875
|
resultSourceMap?: boolean | 'withKeyArraySelector'
|
|
4846
4876
|
returnQuery?: boolean
|
|
4847
4877
|
useCdn?: boolean
|
package/dist/stega.d.ts
CHANGED
|
@@ -699,6 +699,10 @@ export declare interface ClientConfig {
|
|
|
699
699
|
* @defaultValue 'published'
|
|
700
700
|
*/
|
|
701
701
|
perspective?: ClientPerspective
|
|
702
|
+
/**
|
|
703
|
+
* Parameters to pass to Sanity Decide for personalisation
|
|
704
|
+
*/
|
|
705
|
+
decideParameters?: DecideParameters
|
|
702
706
|
apiHost?: string
|
|
703
707
|
/**
|
|
704
708
|
@remarks
|
|
@@ -1226,6 +1230,12 @@ export declare type DatasetsResponse = {
|
|
|
1226
1230
|
tags: string[]
|
|
1227
1231
|
}[]
|
|
1228
1232
|
|
|
1233
|
+
/** @public */
|
|
1234
|
+
export declare interface DecideParameters {
|
|
1235
|
+
audience: string | string[]
|
|
1236
|
+
[key: string]: unknown
|
|
1237
|
+
}
|
|
1238
|
+
|
|
1229
1239
|
/**
|
|
1230
1240
|
* Deletes the published version of a document and optionally some (likely all known) draft versions.
|
|
1231
1241
|
* If any draft version exists that is not specified for deletion this is an error.
|
|
@@ -2018,6 +2028,21 @@ export declare type ImageDescriptionOperation = {
|
|
|
2018
2028
|
}
|
|
2019
2029
|
)
|
|
2020
2030
|
|
|
2031
|
+
/** @public */
|
|
2032
|
+
export declare type ImportReleaseAction =
|
|
2033
|
+
| {
|
|
2034
|
+
actionType: 'sanity.action.release.import'
|
|
2035
|
+
attributes: IdentifiedSanityDocumentStub
|
|
2036
|
+
releaseId: string
|
|
2037
|
+
ifExists: 'fail' | 'ignore' | 'replace'
|
|
2038
|
+
}
|
|
2039
|
+
| {
|
|
2040
|
+
actionType: 'sanity.action.release.import'
|
|
2041
|
+
document: IdentifiedSanityDocumentStub
|
|
2042
|
+
releaseId: string
|
|
2043
|
+
ifExists: 'fail' | 'ignore' | 'replace'
|
|
2044
|
+
}
|
|
2045
|
+
|
|
2021
2046
|
/** @public */
|
|
2022
2047
|
export declare interface InitializedClientConfig extends ClientConfig {
|
|
2023
2048
|
apiHost: string
|
|
@@ -4296,6 +4321,8 @@ export declare interface QueryParams {
|
|
|
4296
4321
|
/** @deprecated you're using a fetch option as a GROQ parameter, this is likely a mistake */
|
|
4297
4322
|
cache?: 'next' extends keyof RequestInit ? never : any
|
|
4298
4323
|
/** @deprecated you're using a fetch option as a GROQ parameter, this is likely a mistake */
|
|
4324
|
+
decideParameters?: never
|
|
4325
|
+
/** @deprecated you're using a fetch option as a GROQ parameter, this is likely a mistake */
|
|
4299
4326
|
filterResponse?: never
|
|
4300
4327
|
/** @deprecated you're using a fetch option as a GROQ parameter, this is likely a mistake */
|
|
4301
4328
|
headers?: never
|
|
@@ -4407,6 +4434,7 @@ export declare type ReleaseAction =
|
|
|
4407
4434
|
| ScheduleReleaseAction
|
|
4408
4435
|
| UnscheduleReleaseAction
|
|
4409
4436
|
| DeleteReleaseAction
|
|
4437
|
+
| ImportReleaseAction
|
|
4410
4438
|
|
|
4411
4439
|
/** @public */
|
|
4412
4440
|
export declare type ReleaseCardinality = 'many' | 'one' | undefined
|
|
@@ -4807,6 +4835,7 @@ export declare interface RequestObservableOptions extends Omit<RequestOptions, '
|
|
|
4807
4835
|
returnQuery?: boolean
|
|
4808
4836
|
resultSourceMap?: boolean | 'withKeyArraySelector'
|
|
4809
4837
|
perspective?: ClientPerspective
|
|
4838
|
+
decideParameters?: DecideParameters
|
|
4810
4839
|
lastLiveEventId?: string
|
|
4811
4840
|
cacheMode?: 'noStale'
|
|
4812
4841
|
}
|
|
@@ -4842,6 +4871,7 @@ export declare interface ResponseEvent<T = unknown> {
|
|
|
4842
4871
|
/** @public */
|
|
4843
4872
|
export declare interface ResponseQueryOptions extends RequestOptions {
|
|
4844
4873
|
perspective?: ClientPerspective
|
|
4874
|
+
decideParameters?: DecideParameters
|
|
4845
4875
|
resultSourceMap?: boolean | 'withKeyArraySelector'
|
|
4846
4876
|
returnQuery?: boolean
|
|
4847
4877
|
useCdn?: boolean
|
package/package.json
CHANGED
package/src/data/dataMethods.ts
CHANGED
|
@@ -44,6 +44,7 @@ import {
|
|
|
44
44
|
printCreateVersionWithBaseIdWarning,
|
|
45
45
|
printPreviewDraftsDeprecationWarning,
|
|
46
46
|
} from '../warnings'
|
|
47
|
+
import {processDecideFields} from './decideResponseProcessor'
|
|
47
48
|
import {encodeQueryString} from './encodeQueryString'
|
|
48
49
|
import {ObservablePatch, Patch} from './patch'
|
|
49
50
|
import {ObservableTransaction, Transaction} from './transaction'
|
|
@@ -97,6 +98,15 @@ export function _fetch<R, Q>(
|
|
|
97
98
|
const mapResponse =
|
|
98
99
|
options.filterResponse === false ? (res: Any) => res : (res: Any) => res.result
|
|
99
100
|
|
|
101
|
+
// Helper function to apply decide processing to response data
|
|
102
|
+
const processDecideResponse = (response: Any): Any => {
|
|
103
|
+
if (options.decideParameters && options.decideParameters.audience) {
|
|
104
|
+
const processedData = processDecideFields(response, options.decideParameters)
|
|
105
|
+
return processedData
|
|
106
|
+
}
|
|
107
|
+
return response
|
|
108
|
+
}
|
|
109
|
+
|
|
100
110
|
const {cache, next, ...opts} = {
|
|
101
111
|
// Opt out of setting a `signal` on an internal `fetch` if one isn't provided.
|
|
102
112
|
// This is necessary in React Server Components to avoid opting out of Request Memoization.
|
|
@@ -129,11 +139,12 @@ export function _fetch<R, Q>(
|
|
|
129
139
|
(typeof import('../stega/stegaEncodeSourceMap'))['stegaEncodeSourceMap'],
|
|
130
140
|
]) => {
|
|
131
141
|
const result = stegaEncodeSourceMap(res.result, res.resultSourceMap, stega)
|
|
132
|
-
|
|
142
|
+
const mappedResponse = mapResponse({...res, result})
|
|
143
|
+
return processDecideResponse(mappedResponse)
|
|
133
144
|
},
|
|
134
145
|
),
|
|
135
146
|
)
|
|
136
|
-
: $request.pipe(map(mapResponse))
|
|
147
|
+
: $request.pipe(map(mapResponse), map(processDecideResponse))
|
|
137
148
|
}
|
|
138
149
|
|
|
139
150
|
/** @internal */
|
|
@@ -504,6 +515,7 @@ export function _dataRequest(
|
|
|
504
515
|
tag,
|
|
505
516
|
returnQuery,
|
|
506
517
|
perspective: options.perspective,
|
|
518
|
+
decideParameters: options.decideParameters,
|
|
507
519
|
resultSourceMap: options.resultSourceMap,
|
|
508
520
|
lastLiveEventId: Array.isArray(lastLiveEventId) ? lastLiveEventId[0] : lastLiveEventId,
|
|
509
521
|
cacheMode: cacheMode,
|
|
@@ -646,6 +658,23 @@ export function _requestObservable<R>(
|
|
|
646
658
|
}
|
|
647
659
|
}
|
|
648
660
|
|
|
661
|
+
// Process decideParameters
|
|
662
|
+
const decideParametersOption = options.decideParameters || config.decideParameters
|
|
663
|
+
if (decideParametersOption && typeof decideParametersOption === 'object') {
|
|
664
|
+
// Add decide parameters to query string
|
|
665
|
+
options.query = {
|
|
666
|
+
...options.query,
|
|
667
|
+
...Object.keys(decideParametersOption).reduce(
|
|
668
|
+
(acc, key) => {
|
|
669
|
+
// Prefix decide parameters to avoid conflicts
|
|
670
|
+
acc[`decide.${key}`] = decideParametersOption[key]
|
|
671
|
+
return acc
|
|
672
|
+
},
|
|
673
|
+
{} as Record<string, unknown>,
|
|
674
|
+
),
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
|
|
649
678
|
if (options.lastLiveEventId) {
|
|
650
679
|
options.query = {...options.query, lastLiveEventId: options.lastLiveEventId}
|
|
651
680
|
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import type {DecideParameters} from '../types'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @internal
|
|
5
|
+
*/
|
|
6
|
+
export interface DecideCondition {
|
|
7
|
+
audience: string
|
|
8
|
+
value: unknown
|
|
9
|
+
[key: string]: unknown
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @internal
|
|
14
|
+
*/
|
|
15
|
+
export interface DecideField {
|
|
16
|
+
default: unknown
|
|
17
|
+
conditions: DecideCondition[]
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Checks if a value is a decide field with the expected structure
|
|
22
|
+
* @internal
|
|
23
|
+
*/
|
|
24
|
+
export function isDecideField(value: unknown): value is DecideField {
|
|
25
|
+
return (
|
|
26
|
+
value != null &&
|
|
27
|
+
typeof value === 'object' &&
|
|
28
|
+
!Array.isArray(value) &&
|
|
29
|
+
'default' in value &&
|
|
30
|
+
'conditions' in value &&
|
|
31
|
+
Array.isArray(value.conditions)
|
|
32
|
+
)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Resolves a decide field based on the provided decide parameters
|
|
37
|
+
* @internal
|
|
38
|
+
*/
|
|
39
|
+
export function resolveDecideField(
|
|
40
|
+
field: DecideField,
|
|
41
|
+
decideParameters: DecideParameters,
|
|
42
|
+
): unknown {
|
|
43
|
+
const audience = decideParameters.audience
|
|
44
|
+
|
|
45
|
+
// Find matching condition for the audience
|
|
46
|
+
const matchingCondition = field.conditions.find((condition) => {
|
|
47
|
+
return Array.isArray(audience)
|
|
48
|
+
? audience.includes(condition.audience)
|
|
49
|
+
: condition.audience === audience
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
// Return matching value or fall back to default
|
|
53
|
+
return matchingCondition ? matchingCondition.value : field.default
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Recursively processes an object or array to resolve decide fields
|
|
58
|
+
* @internal
|
|
59
|
+
*/
|
|
60
|
+
export function processObjectRecursively(
|
|
61
|
+
obj: unknown,
|
|
62
|
+
decideParameters: DecideParameters,
|
|
63
|
+
): unknown {
|
|
64
|
+
// Handle null, undefined, or primitive values
|
|
65
|
+
if (obj === null || obj === undefined || typeof obj !== 'object') {
|
|
66
|
+
return obj
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Handle arrays
|
|
70
|
+
if (Array.isArray(obj)) {
|
|
71
|
+
return obj.map((item) => processObjectRecursively(item, decideParameters))
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Handle objects using reduce
|
|
75
|
+
return Object.entries(obj).reduce<Record<string, unknown>>((processed, [key, value]) => {
|
|
76
|
+
try {
|
|
77
|
+
if (isDecideField(value)) {
|
|
78
|
+
// Resolve decide field
|
|
79
|
+
processed[key] = resolveDecideField(value, decideParameters)
|
|
80
|
+
} else {
|
|
81
|
+
// Recursively process nested objects/arrays
|
|
82
|
+
processed[key] = processObjectRecursively(value, decideParameters)
|
|
83
|
+
}
|
|
84
|
+
} catch (error) {
|
|
85
|
+
// On error, preserve original value and continue
|
|
86
|
+
// eslint-disable-next-line no-console
|
|
87
|
+
console.warn(`Failed to process decide field '${key}':`, error)
|
|
88
|
+
processed[key] = value
|
|
89
|
+
}
|
|
90
|
+
return processed
|
|
91
|
+
}, {})
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Main function to process decide fields in API response data
|
|
96
|
+
* @internal
|
|
97
|
+
*/
|
|
98
|
+
export function processDecideFields(data: unknown, decideParameters: DecideParameters): unknown {
|
|
99
|
+
if (!decideParameters || !decideParameters.audience) {
|
|
100
|
+
return data
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
try {
|
|
104
|
+
return processObjectRecursively(data, decideParameters)
|
|
105
|
+
} catch (error) {
|
|
106
|
+
// On any processing error, return original data
|
|
107
|
+
// eslint-disable-next-line no-console
|
|
108
|
+
console.warn('Failed to process decide fields:', error)
|
|
109
|
+
return data
|
|
110
|
+
}
|
|
111
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -53,6 +53,12 @@ export type ClientPerspective =
|
|
|
53
53
|
| 'raw'
|
|
54
54
|
| StackablePerspective[]
|
|
55
55
|
|
|
56
|
+
/** @public */
|
|
57
|
+
export interface DecideParameters {
|
|
58
|
+
audience: string | string[]
|
|
59
|
+
[key: string]: unknown
|
|
60
|
+
}
|
|
61
|
+
|
|
56
62
|
type ClientConfigResource =
|
|
57
63
|
| {
|
|
58
64
|
type: 'canvas'
|
|
@@ -89,6 +95,12 @@ export interface ClientConfig {
|
|
|
89
95
|
* @defaultValue 'published'
|
|
90
96
|
*/
|
|
91
97
|
perspective?: ClientPerspective
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Parameters to pass to Sanity Decide for personalisation
|
|
101
|
+
*/
|
|
102
|
+
decideParameters?: DecideParameters
|
|
103
|
+
|
|
92
104
|
apiHost?: string
|
|
93
105
|
|
|
94
106
|
/**
|
|
@@ -396,6 +408,7 @@ export interface RequestObservableOptions extends Omit<RequestOptions, 'url'> {
|
|
|
396
408
|
returnQuery?: boolean
|
|
397
409
|
resultSourceMap?: boolean | 'withKeyArraySelector'
|
|
398
410
|
perspective?: ClientPerspective
|
|
411
|
+
decideParameters?: DecideParameters
|
|
399
412
|
lastLiveEventId?: string
|
|
400
413
|
cacheMode?: 'noStale'
|
|
401
414
|
}
|
|
@@ -553,6 +566,8 @@ export interface QueryParams {
|
|
|
553
566
|
/** @deprecated you're using a fetch option as a GROQ parameter, this is likely a mistake */
|
|
554
567
|
cache?: 'next' extends keyof RequestInit ? never : any
|
|
555
568
|
/** @deprecated you're using a fetch option as a GROQ parameter, this is likely a mistake */
|
|
569
|
+
decideParameters?: never
|
|
570
|
+
/** @deprecated you're using a fetch option as a GROQ parameter, this is likely a mistake */
|
|
556
571
|
filterResponse?: never
|
|
557
572
|
/** @deprecated you're using a fetch option as a GROQ parameter, this is likely a mistake */
|
|
558
573
|
headers?: never
|
|
@@ -622,6 +637,7 @@ export type ReleaseAction =
|
|
|
622
637
|
| ScheduleReleaseAction
|
|
623
638
|
| UnscheduleReleaseAction
|
|
624
639
|
| DeleteReleaseAction
|
|
640
|
+
| ImportReleaseAction
|
|
625
641
|
|
|
626
642
|
/** @public */
|
|
627
643
|
export type VersionAction =
|
|
@@ -642,6 +658,21 @@ export type Action =
|
|
|
642
658
|
| VersionAction
|
|
643
659
|
| ReleaseAction
|
|
644
660
|
|
|
661
|
+
/** @public */
|
|
662
|
+
export type ImportReleaseAction =
|
|
663
|
+
| {
|
|
664
|
+
actionType: 'sanity.action.release.import'
|
|
665
|
+
attributes: IdentifiedSanityDocumentStub
|
|
666
|
+
releaseId: string
|
|
667
|
+
ifExists: 'fail' | 'ignore' | 'replace'
|
|
668
|
+
}
|
|
669
|
+
| {
|
|
670
|
+
actionType: 'sanity.action.release.import'
|
|
671
|
+
document: IdentifiedSanityDocumentStub
|
|
672
|
+
releaseId: string
|
|
673
|
+
ifExists: 'fail' | 'ignore' | 'replace'
|
|
674
|
+
}
|
|
675
|
+
|
|
645
676
|
/**
|
|
646
677
|
* Creates a new release under the given id, with metadata.
|
|
647
678
|
*
|
|
@@ -1215,6 +1246,7 @@ export interface ListenOptions {
|
|
|
1215
1246
|
/** @public */
|
|
1216
1247
|
export interface ResponseQueryOptions extends RequestOptions {
|
|
1217
1248
|
perspective?: ClientPerspective
|
|
1249
|
+
decideParameters?: DecideParameters
|
|
1218
1250
|
resultSourceMap?: boolean | 'withKeyArraySelector'
|
|
1219
1251
|
returnQuery?: boolean
|
|
1220
1252
|
useCdn?: boolean
|
package/umd/sanityClient.js
CHANGED
|
@@ -2813,6 +2813,32 @@ ${selectionOpts}`);
|
|
|
2813
2813
|
fetch: typeof overrides.fetch == "object" && typeof config.fetch == "object" ? { ...config.fetch, ...overrides.fetch } : overrides.fetch || config.fetch
|
|
2814
2814
|
});
|
|
2815
2815
|
}
|
|
2816
|
+
function isDecideField(value) {
|
|
2817
|
+
return value != null && typeof value == "object" && !Array.isArray(value) && "default" in value && "conditions" in value && Array.isArray(value.conditions);
|
|
2818
|
+
}
|
|
2819
|
+
function resolveDecideField(field, decideParameters) {
|
|
2820
|
+
const audience = decideParameters.audience, matchingCondition = field.conditions.find((condition) => Array.isArray(audience) ? audience.includes(condition.audience) : condition.audience === audience);
|
|
2821
|
+
return matchingCondition ? matchingCondition.value : field.default;
|
|
2822
|
+
}
|
|
2823
|
+
function processObjectRecursively(obj, decideParameters) {
|
|
2824
|
+
return obj == null || typeof obj != "object" ? obj : Array.isArray(obj) ? obj.map((item) => processObjectRecursively(item, decideParameters)) : Object.entries(obj).reduce((processed, [key, value]) => {
|
|
2825
|
+
try {
|
|
2826
|
+
isDecideField(value) ? processed[key] = resolveDecideField(value, decideParameters) : processed[key] = processObjectRecursively(value, decideParameters);
|
|
2827
|
+
} catch (error) {
|
|
2828
|
+
console.warn(`Failed to process decide field '${key}':`, error), processed[key] = value;
|
|
2829
|
+
}
|
|
2830
|
+
return processed;
|
|
2831
|
+
}, {});
|
|
2832
|
+
}
|
|
2833
|
+
function processDecideFields(data, decideParameters) {
|
|
2834
|
+
if (!decideParameters || !decideParameters.audience)
|
|
2835
|
+
return data;
|
|
2836
|
+
try {
|
|
2837
|
+
return processObjectRecursively(data, decideParameters);
|
|
2838
|
+
} catch (error) {
|
|
2839
|
+
return console.warn("Failed to process decide fields:", error), data;
|
|
2840
|
+
}
|
|
2841
|
+
}
|
|
2816
2842
|
const encodeQueryString = ({
|
|
2817
2843
|
query,
|
|
2818
2844
|
params = {},
|
|
@@ -2837,7 +2863,7 @@ ${selectionOpts}`);
|
|
|
2837
2863
|
const stega = "stega" in options ? {
|
|
2838
2864
|
..._stega || {},
|
|
2839
2865
|
...typeof options.stega == "boolean" ? { enabled: options.stega } : options.stega || {}
|
|
2840
|
-
} : _stega, params = stega.enabled ? stegaClean(_params) : _params, mapResponse = options.filterResponse === false ? (res) => res : (res) => res.result, { cache, next, ...opts } = {
|
|
2866
|
+
} : _stega, params = stega.enabled ? stegaClean(_params) : _params, mapResponse = options.filterResponse === false ? (res) => res : (res) => res.result, processDecideResponse = (response) => options.decideParameters && options.decideParameters.audience ? processDecideFields(response, options.decideParameters) : response, { cache, next, ...opts } = {
|
|
2841
2867
|
// Opt out of setting a `signal` on an internal `fetch` if one isn't provided.
|
|
2842
2868
|
// This is necessary in React Server Components to avoid opting out of Request Memoization.
|
|
2843
2869
|
useAbortSignal: typeof options.signal < "u",
|
|
@@ -2860,11 +2886,11 @@ ${selectionOpts}`);
|
|
|
2860
2886
|
),
|
|
2861
2887
|
map(
|
|
2862
2888
|
([res, stegaEncodeSourceMap]) => {
|
|
2863
|
-
const result = stegaEncodeSourceMap(res.result, res.resultSourceMap, stega);
|
|
2864
|
-
return
|
|
2889
|
+
const result = stegaEncodeSourceMap(res.result, res.resultSourceMap, stega), mappedResponse = mapResponse({ ...res, result });
|
|
2890
|
+
return processDecideResponse(mappedResponse);
|
|
2865
2891
|
}
|
|
2866
2892
|
)
|
|
2867
|
-
) : $request.pipe(map(mapResponse));
|
|
2893
|
+
) : $request.pipe(map(mapResponse), map(processDecideResponse));
|
|
2868
2894
|
}
|
|
2869
2895
|
function _getDocument(client, httpRequest, id, opts = {}) {
|
|
2870
2896
|
const docId = (() => {
|
|
@@ -3017,6 +3043,7 @@ ${selectionOpts}`);
|
|
|
3017
3043
|
tag,
|
|
3018
3044
|
returnQuery,
|
|
3019
3045
|
perspective: options.perspective,
|
|
3046
|
+
decideParameters: options.decideParameters,
|
|
3020
3047
|
resultSourceMap: options.resultSourceMap,
|
|
3021
3048
|
lastLiveEventId: Array.isArray(lastLiveEventId) ? lastLiveEventId[0] : lastLiveEventId,
|
|
3022
3049
|
cacheMode,
|
|
@@ -3061,7 +3088,15 @@ ${selectionOpts}`);
|
|
|
3061
3088
|
perspective: Array.isArray(perspectiveOption) ? perspectiveOption.join(",") : perspectiveOption,
|
|
3062
3089
|
...options.query
|
|
3063
3090
|
}, (Array.isArray(perspectiveOption) && perspectiveOption.length > 0 || // previewDrafts was renamed to drafts, but keep for backwards compat
|
|
3064
|
-
perspectiveOption === "previewDrafts" || perspectiveOption === "drafts") && useCdn && (useCdn = false, printCdnPreviewDraftsWarning()))
|
|
3091
|
+
perspectiveOption === "previewDrafts" || perspectiveOption === "drafts") && useCdn && (useCdn = false, printCdnPreviewDraftsWarning()));
|
|
3092
|
+
const decideParametersOption = options.decideParameters || config.decideParameters;
|
|
3093
|
+
decideParametersOption && typeof decideParametersOption == "object" && (options.query = {
|
|
3094
|
+
...options.query,
|
|
3095
|
+
...Object.keys(decideParametersOption).reduce(
|
|
3096
|
+
(acc, key) => (acc[`decide.${key}`] = decideParametersOption[key], acc),
|
|
3097
|
+
{}
|
|
3098
|
+
)
|
|
3099
|
+
}), options.lastLiveEventId && (options.query = { ...options.query, lastLiveEventId: options.lastLiveEventId }), options.returnQuery === false && (options.query = { returnQuery: "false", ...options.query }), useCdn && options.cacheMode == "noStale" && (options.query = { cacheMode: "noStale", ...options.query });
|
|
3065
3100
|
}
|
|
3066
3101
|
const reqOptions = requestOptions(
|
|
3067
3102
|
config,
|