/
prepareForPreview.ts
300 lines (270 loc) · 9.15 KB
/
prepareForPreview.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import {
isTitledListValue,
PrepareViewOptions,
PreviewValue,
SchemaType,
TitledListValue,
} from '@sanity/types'
import {debounce, flatten, get, isPlainObject, pick, uniqBy} from 'lodash'
import {INVALID_PREVIEW_FALLBACK} from '../constants'
import {PreviewableType} from '../types'
import {isRecord} from '../../util'
import {extractTextFromBlocks, isPortableTextPreviewValue} from './portableText'
import {keysOf} from './keysOf'
const PRESERVE_KEYS = ['_id', '_type', '_upload', '_createdAt', '_updatedAt']
const EMPTY: never[] = []
type SelectedValue = Record<string, unknown>
export type PrepareInvocationResult = {
selectedValue?: SelectedValue
returnValue: null | PreviewValue
errors: Error[]
}
const errorCollector = (() => {
let errorsByType: Record<string, {error: Error; type: SchemaType; value: SelectedValue}[]> = {}
return {
add: (type: SchemaType, value: SelectedValue, error: Error) => {
if (!errorsByType[type.name]) {
errorsByType[type.name] = []
}
errorsByType[type.name].push({error: error, type: type, value})
},
getAll() {
return errorsByType
},
clear() {
errorsByType = {}
},
}
})()
const reportErrors = debounce(() => {
/* eslint-disable no-console */
const errorsByType = errorCollector.getAll()
const uniqueErrors = flatten(
Object.keys(errorsByType).map((typeName) => {
const entries = errorsByType[typeName]
return uniqBy(entries, (entry) => entry.error.message)
})
)
const errorCount = uniqueErrors.length
if (errorCount === 0) {
return
}
console.groupCollapsed(
`%cHeads up! Got ${
errorCount === 1 ? 'error' : `${errorCount} errors`
} while preparing data for preview. Click for details.`,
'color: #ff7e7c'
)
Object.keys(errorsByType).forEach((typeName) => {
const entries = errorsByType[typeName]
const first = entries[0]
console.group(`Check the preview config for schema type "${typeName}": %o`, first.type.preview)
const uniqued = uniqBy(entries, (entry) => entry.error.message)
uniqued.forEach((entry) => {
if ((entry.error as any).type === 'returnValueError') {
const hasPrepare = typeof entry.type.preview?.prepare === 'function'
const {value, error} = entry
console.log(
`Encountered an invalid ${
hasPrepare
? 'return value when calling prepare(%o)'
: 'value targeted by preview.select'
}:`,
value
)
console.error(error)
}
if ((entry.error as any).type === 'prepareError') {
const {value, error} = entry
console.log('Encountered an error when calling prepare(%o):', value)
console.error(error)
}
})
console.groupEnd()
})
console.groupEnd()
errorCollector.clear()
/* eslint-enable no-console */
}, 1000)
const isRenderable =
(fieldName: string) =>
(value: unknown): Error[] => {
const type = typeof value
if (
value === null ||
type === 'undefined' ||
type === 'string' ||
type === 'number' ||
type === 'boolean'
) {
return EMPTY
}
return [
assignType(
'returnValueError',
new Error(
`The "${fieldName}" field should be a string, number, boolean, undefined or null, instead saw ${inspect(
value
)}`
)
),
]
}
const FIELD_NAME_VALIDATORS: Record<string, (value: unknown) => Error[]> = {
media: () => {
// not sure how to validate media as it would possibly involve executing a function and check the
// return value
return EMPTY
},
title: isRenderable('title'),
subtitle: isRenderable('subtitle'),
description: isRenderable('description'),
imageUrl: isRenderable('imageUrl'),
date: isRenderable('date'),
}
function inspect(val: unknown, prefixType = true): string {
if (isRecord(val)) {
const keys = Object.keys(val)
const ellipse = keys.length > 3 ? '...' : ''
const prefix = `object with keys `
return `${prefixType ? prefix : ''}{${keys.slice(0, 3).join(', ')}${ellipse}}`
}
if (Array.isArray(val)) {
const ellipse = val.length > 3 ? '...' : ''
const prefix = `array with `
return `${prefixType ? prefix : ''}[${val.map((v) => inspect(v, false))}${ellipse}]`
}
return `the ${typeof val} ${val}`
}
function validateFieldValue(fieldName: string, fieldValue: unknown) {
if (typeof fieldValue === 'undefined') {
return EMPTY
}
const validator = FIELD_NAME_VALIDATORS[fieldName]
return (validator && validator(fieldValue)) || EMPTY
}
function assignType(type: string, error: Error) {
return Object.assign(error, {type})
}
function validatePreparedValue(preparedValue: PreviewValue | null) {
if (!isPlainObject(preparedValue) || preparedValue === null) {
return [
assignType(
'returnValueError',
new Error(
`Invalid return value. Expected a plain object with at least a 'title' field, instead saw ${inspect(
preparedValue
)}`
)
),
]
}
return Object.entries(preparedValue).reduce<Error[]>((acc, [fieldName, fieldValue]) => {
return [...acc, ...validateFieldValue(fieldName, fieldValue)]
}, EMPTY)
}
function validateReturnedPreview(result: PrepareInvocationResult) {
return {
...result,
errors: [...(result.errors || []), ...validatePreparedValue(result.returnValue)],
}
}
function defaultPrepare(value: SelectedValue) {
return keysOf(value).reduce((acc: SelectedValue, fieldName: keyof SelectedValue) => {
const val = value[fieldName]
return {
...acc,
[fieldName]: isPortableTextPreviewValue(val) ? extractTextFromBlocks(val) : val,
}
}, {})
}
export function invokePrepare(
type: PreviewableType,
value: SelectedValue,
viewOptions: PrepareViewOptions = {}
): PrepareInvocationResult {
const prepare = type.preview?.prepare
try {
return {
returnValue: prepare
? (prepare(value, viewOptions) as Record<string, unknown>)
: defaultPrepare(value),
errors: EMPTY,
}
} catch (error) {
return {
returnValue: null,
errors: [assignType('prepareError', error)],
}
}
}
function withErrors(
result: {errors: Error[]},
type: SchemaType,
selectedValue: SelectedValue
): PreviewValue {
result.errors.forEach((error) => errorCollector.add(type, selectedValue, error))
reportErrors()
return INVALID_PREVIEW_FALLBACK
}
interface EnumListOptions {
list: TitledListValue[] | unknown[]
}
function hasEnumListOptions(
type: SchemaType
): type is SchemaType & {options: SchemaType['options'] & EnumListOptions} {
const options = type.options && typeof type.options === 'object' ? type.options : false
if (!options || !('list' in options)) {
return false
}
const listOptions = (options as EnumListOptions).list
return Array.isArray(listOptions)
}
function getListOptions(type: SchemaType): TitledListValue[] | undefined {
if (!hasEnumListOptions(type)) {
return undefined
}
const listOptions = type.options.list as EnumListOptions['list']
return listOptions.map((option) =>
isTitledListValue(option) ? option : ({title: option, value: option} as TitledListValue)
)
}
/** @internal */
export function prepareForPreview(
rawValue: unknown,
type: SchemaType,
viewOptions: PrepareViewOptions = {}
): PreviewValue & {_createdAt?: string; _updatedAt?: string} {
const hasCustomPrepare = typeof type.preview?.prepare === 'function'
const selection: Record<string, string> = type.preview?.select || {}
const targetKeys = Object.keys(selection)
const selectedValue = targetKeys.reduce<Record<string, unknown>>((acc, key) => {
// Find the field the value belongs to
const typeWithFields = 'fields' in type ? type : null
const targetFieldName = selection[key]
const valueField = typeWithFields?.fields?.find((f) => f.name === targetFieldName)
const listOptions = valueField && getListOptions(valueField.type)
// If the user has _not_ specified a `prepare()` function for the preview, and the
// field type has an `options.list`, we want to use the title of the selected item
// as the preview value. If, however, there _is_ a custom `prepare()`, we leave this
// mapping up to the user to perform should they want to. This is both to maintain
// backwards compatiblity, but also to allow using the raw value for prepare operations
if (!hasCustomPrepare && listOptions) {
// Find the selected option that matches the raw value
const selectedOption =
listOptions && listOptions.find((opt) => opt.value === get(rawValue, selection[key]))
acc[key] = selectedOption ? selectedOption.value : get(rawValue, selection[key])
} else {
acc[key] = get(rawValue, selection[key])
}
return acc
}, {})
const prepareResult = invokePrepare(type, selectedValue, viewOptions)
if (prepareResult.errors.length > 0) {
return withErrors(prepareResult, type, selectedValue)
}
const returnValueResult = validateReturnedPreview(invokePrepare(type, selectedValue, viewOptions))
return returnValueResult.errors.length > 0
? withErrors(returnValueResult, type, selectedValue)
: {...pick(rawValue, PRESERVE_KEYS), ...prepareResult.returnValue}
}