/
types.ts
454 lines (402 loc) · 10.9 KB
/
types.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
import type {ComponentType} from 'react'
import {SanityClient} from '@sanity/client'
import type {Rule} from '../validation'
import type {SanityDocument} from '../documents'
import type {CurrentUser} from '../user'
import type {PreviewConfig} from './preview'
import {SchemaTypeDefinition} from './definition/schemaDefinition'
import {ArrayOptions} from './definition/type/array'
import {
BlockDecoratorDefinition,
BlockOptions,
BooleanOptions,
DateOptions,
DatetimeOptions,
FileOptions,
ImageOptions,
NumberOptions,
ReferenceOptions,
SlugOptions,
StringOptions,
TextOptions,
} from './definition/type'
export {defineType, defineField, defineArrayMember, typed} from './define'
/**
* Note: you probably want `SchemaTypeDefinition` instead
* @see SchemaTypeDefinition
*
* @public
*/
export type SchemaType =
| ArraySchemaType
| BooleanSchemaType
| FileSchemaType
| NumberSchemaType
| ObjectSchemaType
| StringSchemaType
| ReferenceSchemaType
/** @public */
export interface SchemaValidationError {
helpId?: string
message: string
severity: 'error'
}
/** @internal */
export interface SchemaValidationWarning {
helpId?: string
message: string
severity: 'warning'
}
/** @internal */
export type SchemaValidationProblem = SchemaValidationError | SchemaValidationWarning
/** @internal */
export type SchemaValidationProblemPath = Array<
{kind: 'type'; type: string; name: string} | {kind: 'property'; name: string}
>
/** @internal */
export interface SchemaValidationProblemGroup {
path: SchemaValidationProblemPath
problems: SchemaValidationProblem[]
}
/** @public */
export interface Schema {
/** @internal */
_original?: {
name: string
types: SchemaTypeDefinition[]
}
/** @internal */
_registry: {[typeName: string]: any}
/** @internal */
_validation?: SchemaValidationProblemGroup[]
name: string
get: (name: string) => SchemaType | undefined
has: (name: string) => boolean
getTypeNames: () => string[]
}
/** @beta */
export interface SortOrderingItem {
field: string
direction: 'asc' | 'desc'
}
/** @beta */
export type SortOrdering = {
title: string
name: string
by: SortOrderingItem[]
}
/** @public */
export interface ConditionalPropertyCallbackContext {
document: SanityDocument | undefined
// `any` should be fine here. leaving this as `unknown` would cause more
// friction for end users
// eslint-disable-next-line @typescript-eslint/no-explicit-any
parent: any
// eslint-disable-next-line @typescript-eslint/no-explicit-any
value: any
currentUser: Omit<CurrentUser, 'role'> | null
}
/** @public */
export type ConditionalPropertyCallback = (context: ConditionalPropertyCallbackContext) => boolean
/** @public */
export type ConditionalProperty = boolean | ConditionalPropertyCallback | undefined
/** @public */
export interface InitialValueResolverContext {
projectId: string
dataset: string
schema: Schema
currentUser: CurrentUser | null
getClient: (options: {apiVersion: string}) => SanityClient
}
/** @public */
export type InitialValueResolver<Params, Value> = (
params: Params | undefined,
context: InitialValueResolverContext
) => Promise<Value> | Value
/** @public */
export type InitialValueProperty<Params, Value> =
| Value
| InitialValueResolver<Params, Value>
| undefined
/**
* Represents the possible values of a schema type's `validation` field.
*
* If the schema has not been run through `inferFromSchema` from
* `@sanity/validation` then value could be a function.
*
* `inferFromSchema` mutates the schema converts this value to an array of
* `Rule` instances.
*
* @privateRemarks
*
* Usage of the schema inside the studio will almost always be from the compiled
* `createSchema` function. In this case, you can cast the value or throw to
* narrow the type. E.g.:
*
* ```ts
* if (typeof type.validation === 'function') {
* throw new Error(
* `Schema type "${type.name}"'s \`validation\` was not run though \`inferFromSchema\``
* )
* }
* ```
*
* @public
*/
export type SchemaValidationValue =
| false
| undefined
| Rule
| SchemaValidationValue[]
| ((rule: Rule) => SchemaValidationValue)
/** @public */
export interface BaseSchemaType {
name: string
title?: string
description?: string
type?: SchemaType
liveEdit?: boolean
readOnly?: ConditionalProperty
hidden?: ConditionalProperty
icon?: ComponentType
initialValue?: InitialValueProperty<any, any>
validation?: SchemaValidationValue
preview?: PreviewConfig
/** @beta */
components?: {
block?: ComponentType<any>
annotation?: ComponentType<any>
diff?: ComponentType<any>
field?: ComponentType<any>
input?: ComponentType<any>
item?: ComponentType<any>
preview?: ComponentType<any>
}
/**
* @deprecated This will be removed.
*/
placeholder?: string
}
/**
* This is used for string, text, date and datetime.
* This interface represent the compiled version at runtime, when accessed through Schema.
*
* @public
*/
export interface StringSchemaType extends BaseSchemaType {
jsonType: 'string'
options?: StringOptions & TextOptions & DateOptions & DatetimeOptions
initialValue?: InitialValueProperty<any, string>
}
/** @public */
export interface TextSchemaType extends StringSchemaType {
rows?: number
}
/** @public */
export interface NumberSchemaType extends BaseSchemaType {
jsonType: 'number'
options?: NumberOptions
initialValue?: InitialValueProperty<any, number>
}
/** @public */
export interface BooleanSchemaType extends BaseSchemaType {
jsonType: 'boolean'
options?: BooleanOptions
initialValue?: InitialValueProperty<any, boolean>
}
/** @public */
export interface ArraySchemaType<V = unknown> extends BaseSchemaType {
jsonType: 'array'
of: (Exclude<SchemaType, ArraySchemaType> | ReferenceSchemaType)[]
options?: ArrayOptions<V> & {layout?: V extends string ? 'tag' : 'grid'}
}
// Note: this would ideally be a type parameter in `ArraySchemaType` however
// adding one conflicts with the existing definition.
/** @internal */
export type ArraySchemaTypeOf<TSchemaType extends ArraySchemaType['of'][number]> = Omit<
ArraySchemaType,
'of'
> & {of: TSchemaType[]}
/**
* A specific `ObjectField` for `marks` in `SpanSchemaType`
* @see SpanSchemaType
*
* @internal
*/
export type SpanMarksObjectField = {name: 'marks'} & ObjectField<
ArraySchemaTypeOf<StringSchemaType>
>
/**
* A specific `ObjectField` for `text` in `SpanSchemaType`
* @see SpanSchemaType
*
* @internal
*/
export type SpanTextObjectField = {name: 'text'} & ObjectField<TextSchemaType>
/**
* A specific `ObjectField` for `style` in `BlockSchemaType`
* @see BlockSchemaType
*
* @internal
*/
export type BlockStyleObjectField = {name: 'style'} & ObjectField<StringSchemaType>
/**
* A specific `ObjectField` for `list` in `BlockSchemaType`
* @see BlockSchemaType
*
* @internal
*/
export type BlockListObjectField = {name: 'list'} & ObjectField<StringSchemaType>
/**
* The specific `children` field of a `block` type (`BlockSchemaType`)
* @see BlockSchemaType
*
* @internal
*/
export type BlockChildrenObjectField = {name: 'children'} & ObjectField<ArraySchemaType>
/**
* Represents the compiled schema shape for `span`s for portable text.
*
* Note: this does _not_ represent the schema definition shape.
*
* @internal
*/
export interface SpanSchemaType extends Omit<ObjectSchemaType, 'fields'> {
annotations: (ObjectSchemaType & {
icon?: string | ComponentType
components?: {
item?: ComponentType
}
})[]
decorators: BlockDecoratorDefinition[]
// the first field will always be the `marks` field and the second will
// always be the `text` field
fields: [SpanMarksObjectField, SpanTextObjectField]
}
/**
* Represents the compiled schema shape for `block`s for portable text.
*
* Note: this does _not_ represent the schema definition shape.
*
* @internal
*/
export interface BlockSchemaType extends ObjectSchemaType {
fields: [
// the first 3 field are always block children, styles, and lists
BlockChildrenObjectField,
BlockStyleObjectField,
BlockListObjectField,
// then it could be any additional fields the user could add
...ObjectField[]
]
options?: BlockOptions
}
/** @public */
export interface SlugSchemaType extends ObjectSchemaType {
jsonType: 'object'
options?: SlugOptions
}
/** @public */
export type ObjectFieldType<T extends SchemaType = SchemaType> = T & {
hidden?: ConditionalProperty
readOnly?: ConditionalProperty
}
/** @public */
export interface ObjectField<T extends SchemaType = SchemaType> {
name: string
fieldset?: string
group?: string | string[]
type: ObjectFieldType<T>
}
/** @public */
export interface FieldGroup {
name: string
icon?: ComponentType
title?: string
description?: string
hidden?: ConditionalProperty
default?: boolean
fields?: ObjectField[]
}
/** @public */
export interface ObjectSchemaType extends BaseSchemaType {
jsonType: 'object'
fields: ObjectField[]
groups?: FieldGroup[]
fieldsets?: Fieldset[]
initialValue?: InitialValueProperty<any, Record<string, unknown>>
weak?: boolean
// Experimentals
// Note: `path` is a string in the _specification_, but converted to a
// string/number array in the schema normalization/compilation step
// a path segment is a number when specified like array.0.prop in preview config.
/** @alpha */
__experimental_search: {path: (string | number)[]; weight: number; mapWith?: string}[]
/** @alpha */
__experimental_omnisearch_visibility?: boolean
/**
* @beta
*/
orderings?: SortOrdering[]
// @todo
options?: any
}
/** @internal */
export interface ObjectSchemaTypeWithOptions extends Omit<ObjectSchemaType, 'options'> {
options?: CollapseOptions & {
columns?: number
}
}
/** @public */
export interface SingleFieldSet {
single: true
field: ObjectField
hidden?: ConditionalProperty
readOnly?: ConditionalProperty
group?: string | string[]
}
/** @public */
export interface MultiFieldSet {
name: string
title?: string
description?: string
single?: false
group?: string | string[]
options?: CollapseOptions & {
columns?: number
}
fields: ObjectField[]
hidden?: ConditionalProperty
readOnly?: ConditionalProperty
}
/** @public */
export type Fieldset = SingleFieldSet | MultiFieldSet
/** @public */
export interface CollapseOptions {
collapsed?: boolean
collapsible?: boolean
/**
* @deprecated Use `collapsible` instead
*/
collapsable?: boolean
}
/** @public */
export interface ReferenceSchemaType extends Omit<ObjectSchemaType, 'options'> {
jsonType: 'object'
to: ObjectSchemaType[]
weak?: boolean
options?: ReferenceOptions
}
/** @public */
export interface AssetSchemaTypeOptions {
accept?: string
storeOriginalFilename?: boolean
}
/** @public */
export interface FileSchemaType extends Omit<ObjectSchemaType, 'options'> {
options?: FileOptions
}
/** @internal */
export interface ImageSchemaType extends Omit<ObjectSchemaType, 'options'> {
options?: ImageOptions
}