@sanity/client 7.11.2-audience-decide.0 → 7.11.2
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/dist/index.browser.cjs +5 -40
- package/dist/index.browser.cjs.map +1 -1
- package/dist/index.browser.d.cts +0 -14
- package/dist/index.browser.d.ts +0 -14
- package/dist/index.browser.js +5 -40
- package/dist/index.browser.js.map +1 -1
- package/dist/index.cjs +6 -41
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +0 -14
- package/dist/index.d.ts +0 -14
- package/dist/index.js +6 -41
- package/dist/index.js.map +1 -1
- package/dist/stega.browser.d.cts +0 -14
- package/dist/stega.browser.d.ts +0 -14
- package/dist/stega.d.cts +0 -14
- package/dist/stega.d.ts +0 -14
- package/package.json +1 -1
- package/src/data/dataMethods.ts +2 -31
- package/src/types.ts +0 -16
- package/umd/sanityClient.js +5 -40
- package/umd/sanityClient.min.js +2 -2
- package/src/data/decideResponseProcessor.ts +0 -111
|
@@ -1,111 +0,0 @@
|
|
|
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
|
-
}
|