@sanity/client 7.11.2-audience-decide.8 → 7.12.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.
@@ -1,185 +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
- const isValid =
26
- value != null &&
27
- typeof value === 'object' &&
28
- !Array.isArray(value) &&
29
- 'default' in value &&
30
- 'conditions' in value &&
31
- Array.isArray((value as any).conditions)
32
-
33
- return isValid
34
- }
35
-
36
- /**
37
- * Resolves a decide field based on the provided decide parameters
38
- * @internal
39
- */
40
- export function resolveDecideField(
41
- field: DecideField,
42
- decideParameters?: DecideParameters,
43
- ): unknown {
44
- // eslint-disable-next-line no-console
45
- console.log('[client-8] resolveDecideField: starting resolution:', {
46
- field: {
47
- default: field.default,
48
- conditionsCount: field.conditions.length,
49
- conditions: field.conditions.map((c, i) => ({
50
- index: i,
51
- audience: c.audience,
52
- value: c.value,
53
- })),
54
- },
55
- decideParameters,
56
- })
57
-
58
- const audience = decideParameters?.audience
59
-
60
- // eslint-disable-next-line no-console
61
- console.log('[client-8] resolveDecideField: audience processing:', {
62
- audience,
63
- audienceType: typeof audience,
64
- isArray: Array.isArray(audience),
65
- isEmpty: Array.isArray(audience) ? audience.length === 0 : audience === '',
66
- isUndefined: audience === undefined,
67
- isNull: audience === null,
68
- })
69
-
70
- // If no audience defined or empty, return default
71
- if (!audience || (Array.isArray(audience) && audience.length === 0) || audience === '') {
72
- // eslint-disable-next-line no-console
73
- console.log(
74
- '[client-8] resolveDecideField: no valid audience, returning default:',
75
- field.default,
76
- )
77
- return field.default
78
- }
79
-
80
- // eslint-disable-next-line no-console
81
- console.log('[client-8] resolveDecideField: searching for matching condition...')
82
-
83
- // Find matching condition for the audience
84
- const matchingCondition = field.conditions.find((condition, index) => {
85
- const isMatch = Array.isArray(audience)
86
- ? audience.includes(condition.audience)
87
- : condition.audience === audience
88
-
89
- // eslint-disable-next-line no-console
90
- console.log(`[client-8] resolveDecideField: checking condition ${index}:`, {
91
- conditionAudience: condition.audience,
92
- conditionValue: condition.value,
93
- searchAudience: audience,
94
- isArraySearch: Array.isArray(audience),
95
- isMatch,
96
- })
97
-
98
- return isMatch
99
- })
100
-
101
- const resolvedValue = matchingCondition ? matchingCondition.value : field.default
102
-
103
- // eslint-disable-next-line no-console
104
- console.log('[client-8] resolveDecideField: resolution complete:', {
105
- matchingCondition: matchingCondition
106
- ? {
107
- audience: matchingCondition.audience,
108
- value: matchingCondition.value,
109
- }
110
- : null,
111
- resolvedValue,
112
- usedDefault: !matchingCondition,
113
- })
114
-
115
- // Return matching value or fall back to default
116
- return resolvedValue
117
- }
118
-
119
- /**
120
- * Recursively processes an object or array to resolve decide fields
121
- * @internal
122
- */
123
- export function processObjectRecursively(
124
- obj: unknown,
125
- decideParameters?: DecideParameters,
126
- depth = 0,
127
- path = 'root',
128
- ): unknown {
129
- // Handle null, undefined, or primitive values
130
- if (obj === null || obj === undefined || typeof obj !== 'object') {
131
- return obj
132
- }
133
-
134
- // Handle arrays
135
- if (Array.isArray(obj)) {
136
- return obj.map((item, index) =>
137
- processObjectRecursively(item, decideParameters, depth + 1, `${path}[${index}]`),
138
- )
139
- }
140
-
141
- // Handle objects using reduce
142
- return Object.entries(obj).reduce<Record<string, unknown>>((processed, [key, value]) => {
143
- const currentPath = `${path}.${key}`
144
- const isDecide = isDecideField(value)
145
-
146
- try {
147
- if (isDecide) {
148
- // eslint-disable-next-line no-console
149
- console.log(`[client-8] DECIDE FIELD FOUND: Key: '${key}'`)
150
- // Resolve decide field
151
- processed[key] = resolveDecideField(value, decideParameters)
152
- } else {
153
- // Recursively process nested objects/arrays
154
- processed[key] = processObjectRecursively(value, decideParameters, depth + 1, currentPath)
155
- }
156
- } catch (error) {
157
- // On error, preserve original value and continue
158
- processed[key] = value
159
- }
160
- return processed
161
- }, {})
162
- }
163
-
164
- /**
165
- * Main function to process decide fields in API response data
166
- * @internal
167
- */
168
- export function processDecideFields(data: unknown, decideParameters?: DecideParameters): unknown {
169
- // eslint-disable-next-line no-console
170
- console.log('[client-8] === DECIDE FIELD PROCESSING START ===')
171
-
172
- try {
173
- const result = processObjectRecursively(data, decideParameters)
174
- // eslint-disable-next-line no-console
175
- console.log('[client-8] === DECIDE FIELD PROCESSING END ===')
176
- return result
177
- } catch (error) {
178
- // eslint-disable-next-line no-console
179
- console.warn(
180
- '[client-8] processDecideFields: processing failed, returning original data:',
181
- error,
182
- )
183
- return data
184
- }
185
- }