-
-
Notifications
You must be signed in to change notification settings - Fork 8.5k
/
Copy pathapiSetupHelpers.ts
515 lines (483 loc) · 13.6 KB
/
apiSetupHelpers.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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
import {
type IfAny,
type LooseRequired,
type Prettify,
type UnionToIntersection,
extend,
isArray,
isFunction,
isPromise,
} from '@vue/shared'
import {
type SetupContext,
createSetupContext,
getCurrentInstance,
setCurrentInstance,
unsetCurrentInstance,
} from './component'
import type { EmitFn, EmitsOptions, ObjectEmitsOptions } from './componentEmits'
import type {
ComponentOptionsBase,
ComponentOptionsMixin,
ComputedOptions,
MethodOptions,
} from './componentOptions'
import type {
ComponentObjectPropsOptions,
ComponentPropsOptions,
ExtractPropTypes,
PropOptions,
} from './componentProps'
import { warn } from './warning'
import type { SlotsType, StrictUnwrapSlotsType } from './componentSlots'
import type { Ref } from '@vue/reactivity'
// dev only
const warnRuntimeUsage = (method: string) =>
warn(
`${method}() is a compiler-hint helper that is only usable inside ` +
`<script setup> of a single file component. Its arguments should be ` +
`compiled away and passing it at runtime has no effect.`,
)
/**
* Vue `<script setup>` compiler macro for declaring component props. The
* expected argument is the same as the component `props` option.
*
* Example runtime declaration:
* ```js
* // using Array syntax
* const props = defineProps(['foo', 'bar'])
* // using Object syntax
* const props = defineProps({
* foo: String,
* bar: {
* type: Number,
* required: true
* }
* })
* ```
*
* Equivalent type-based declaration:
* ```ts
* // will be compiled into equivalent runtime declarations
* const props = defineProps<{
* foo?: string
* bar: number
* }>()
* ```
*
* @see {@link https://vuejs.org/api/sfc-script-setup.html#defineprops-defineemits}
*
* This is only usable inside `<script setup>`, is compiled away in the
* output and should **not** be actually called at runtime.
*/
// overload 1: runtime props w/ array
export function defineProps<PropNames extends string = string>(
props: PropNames[],
): Prettify<Readonly<{ [key in PropNames]?: any }>>
// overload 2: runtime props w/ object
export function defineProps<
PP extends ComponentObjectPropsOptions = ComponentObjectPropsOptions,
>(props: PP): Prettify<Readonly<ExtractPropTypes<PP>>>
// overload 3: typed-based declaration
export function defineProps<TypeProps>(): DefineProps<
LooseRequired<TypeProps>,
BooleanKey<TypeProps>
>
// implementation
export function defineProps() {
if (__DEV__) {
warnRuntimeUsage(`defineProps`)
}
return null as any
}
export type DefineProps<T, BKeys extends keyof T> = Readonly<T> & {
readonly [K in BKeys]-?: boolean
}
type BooleanKey<T, K extends keyof T = keyof T> = K extends any
? [T[K]] extends [boolean | undefined]
? K
: never
: never
/**
* Vue `<script setup>` compiler macro for declaring a component's emitted
* events. The expected argument is the same as the component `emits` option.
*
* Example runtime declaration:
* ```js
* const emit = defineEmits(['change', 'update'])
* ```
*
* Example type-based declaration:
* ```ts
* const emit = defineEmits<{
* // <eventName>: <expected arguments>
* change: []
* update: [value: number] // named tuple syntax
* }>()
*
* emit('change')
* emit('update', 1)
* ```
*
* This is only usable inside `<script setup>`, is compiled away in the
* output and should **not** be actually called at runtime.
*
* @see {@link https://vuejs.org/api/sfc-script-setup.html#defineprops-defineemits}
*/
// overload 1: runtime emits w/ array
export function defineEmits<EE extends string = string>(
emitOptions: EE[],
): EmitFn<EE[]>
export function defineEmits<E extends EmitsOptions = EmitsOptions>(
emitOptions: E,
): EmitFn<E>
export function defineEmits<T extends ComponentTypeEmits>(): T extends (
...args: any[]
) => any
? T
: ShortEmits<T>
// implementation
export function defineEmits() {
if (__DEV__) {
warnRuntimeUsage(`defineEmits`)
}
return null as any
}
export type ComponentTypeEmits = ((...args: any[]) => any) | Record<string, any>
type RecordToUnion<T extends Record<string, any>> = T[keyof T]
type ShortEmits<T extends Record<string, any>> = UnionToIntersection<
RecordToUnion<{
[K in keyof T]: (evt: K, ...args: T[K]) => void
}>
>
/**
* Vue `<script setup>` compiler macro for declaring a component's exposed
* instance properties when it is accessed by a parent component via template
* refs.
*
* `<script setup>` components are closed by default - i.e. variables inside
* the `<script setup>` scope is not exposed to parent unless explicitly exposed
* via `defineExpose`.
*
* This is only usable inside `<script setup>`, is compiled away in the
* output and should **not** be actually called at runtime.
*
* @see {@link https://vuejs.org/api/sfc-script-setup.html#defineexpose}
*/
export function defineExpose<
Exposed extends Record<string, any> = Record<string, any>,
>(exposed?: Exposed): void {
if (__DEV__) {
warnRuntimeUsage(`defineExpose`)
}
}
/**
* Vue `<script setup>` compiler macro for declaring a component's additional
* options. This should be used only for options that cannot be expressed via
* Composition API - e.g. `inheritAttrs`.
*
* @see {@link https://vuejs.org/api/sfc-script-setup.html#defineoptions}
*/
export function defineOptions<
RawBindings = {},
D = {},
C extends ComputedOptions = {},
M extends MethodOptions = {},
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
>(
options?: ComponentOptionsBase<
{},
RawBindings,
D,
C,
M,
Mixin,
Extends,
{}
> & {
/**
* props should be defined via defineProps().
*/
props?: never
/**
* emits should be defined via defineEmits().
*/
emits?: never
/**
* expose should be defined via defineExpose().
*/
expose?: never
/**
* slots should be defined via defineSlots().
*/
slots?: never
},
): void {
if (__DEV__) {
warnRuntimeUsage(`defineOptions`)
}
}
export function defineSlots<
S extends Record<string, any> = Record<string, any>,
>(): StrictUnwrapSlotsType<SlotsType<S>> {
if (__DEV__) {
warnRuntimeUsage(`defineSlots`)
}
return null as any
}
export type ModelRef<T, M extends PropertyKey = string, G = T, S = T> = Ref<
G,
S
> &
[ModelRef<T, M, G, S>, Record<M, true | undefined>]
export type DefineModelOptions<T = any, G = T, S = T> = {
get?: (v: T) => G
set?: (v: S) => any
}
/**
* Vue `<script setup>` compiler macro for declaring a
* two-way binding prop that can be consumed via `v-model` from the parent
* component. This will declare a prop with the same name and a corresponding
* `update:propName` event.
*
* If the first argument is a string, it will be used as the prop name;
* Otherwise the prop name will default to "modelValue". In both cases, you
* can also pass an additional object which will be used as the prop's options.
*
* The returned ref behaves differently depending on whether the parent
* provided the corresponding v-model props or not:
* - If yes, the returned ref's value will always be in sync with the parent
* prop.
* - If not, the returned ref will behave like a normal local ref.
*
* @example
* ```ts
* // default model (consumed via `v-model`)
* const modelValue = defineModel<string>()
* modelValue.value = "hello"
*
* // default model with options
* const modelValue = defineModel<string>({ required: true })
*
* // with specified name (consumed via `v-model:count`)
* const count = defineModel<number>('count')
* count.value++
*
* // with specified name and default value
* const count = defineModel<number>('count', { default: 0 })
* ```
*/
export function defineModel<T, M extends PropertyKey = string, G = T, S = T>(
options: ({ default: any } | { required: true }) &
PropOptions<T> &
DefineModelOptions<T, G, S>,
): ModelRef<T, M, G, S>
export function defineModel<T, M extends PropertyKey = string, G = T, S = T>(
options?: PropOptions<T> & DefineModelOptions<T, G, S>,
): ModelRef<T | undefined, M, G | undefined, S | undefined>
export function defineModel<T, M extends PropertyKey = string, G = T, S = T>(
name: string,
options: ({ default: any } | { required: true }) &
PropOptions<T> &
DefineModelOptions<T, G, S>,
): ModelRef<T, M, G, S>
export function defineModel<T, M extends PropertyKey = string, G = T, S = T>(
name: string,
options?: PropOptions<T> & DefineModelOptions<T, G, S>,
): ModelRef<T | undefined, M, G | undefined, S | undefined>
export function defineModel(): any {
if (__DEV__) {
warnRuntimeUsage('defineModel')
}
}
type NotUndefined<T> = T extends undefined ? never : T
type MappedOmit<T, K extends keyof any> = {
[P in keyof T as P extends K ? never : P]: T[P]
}
type InferDefaults<T> = {
[K in keyof T]?: InferDefault<T, T[K]>
}
type NativeType = null | number | string | boolean | symbol | Function
type InferDefault<P, T> =
| ((props: P) => T & {})
| (T extends NativeType ? T : never)
type PropsWithDefaults<
T,
Defaults extends InferDefaults<T>,
BKeys extends keyof T,
> = T extends unknown
? Readonly<MappedOmit<T, keyof Defaults>> & {
readonly [K in keyof Defaults as K extends keyof T
? K
: never]-?: K extends keyof T
? Defaults[K] extends undefined
? IfAny<Defaults[K], NotUndefined<T[K]>, T[K]>
: NotUndefined<T[K]>
: never
} & {
readonly [K in BKeys]-?: K extends keyof Defaults
? Defaults[K] extends undefined
? boolean | undefined
: boolean
: boolean
}
: never
/**
* Vue `<script setup>` compiler macro for providing props default values when
* using type-based `defineProps` declaration.
*
* Example usage:
* ```ts
* withDefaults(defineProps<{
* size?: number
* labels?: string[]
* }>(), {
* size: 3,
* labels: () => ['default label']
* })
* ```
*
* This is only usable inside `<script setup>`, is compiled away in the output
* and should **not** be actually called at runtime.
*
* @see {@link https://vuejs.org/guide/typescript/composition-api.html#typing-component-props}
*/
export function withDefaults<
T,
BKeys extends keyof T,
Defaults extends InferDefaults<T>,
>(
props: DefineProps<T, BKeys>,
defaults: Defaults,
): PropsWithDefaults<T, Defaults, BKeys> {
if (__DEV__) {
warnRuntimeUsage(`withDefaults`)
}
return null as any
}
export function useSlots(): SetupContext['slots'] {
return getContext().slots
}
export function useAttrs(): SetupContext['attrs'] {
return getContext().attrs
}
function getContext(): SetupContext {
const i = getCurrentInstance()!
if (__DEV__ && !i) {
warn(`useContext() called without active instance.`)
}
return i.setupContext || (i.setupContext = createSetupContext(i))
}
/**
* @internal
*/
export function normalizePropsOrEmits(
props: ComponentPropsOptions | EmitsOptions,
): ComponentObjectPropsOptions | ObjectEmitsOptions {
return isArray(props)
? props.reduce(
(normalized, p) => ((normalized[p] = null), normalized),
{} as ComponentObjectPropsOptions | ObjectEmitsOptions,
)
: props
}
/**
* Runtime helper for merging default declarations. Imported by compiled code
* only.
* @internal
*/
export function mergeDefaults(
raw: ComponentPropsOptions,
defaults: Record<string, any>,
): ComponentObjectPropsOptions {
const props = normalizePropsOrEmits(raw)
for (const key in defaults) {
if (key.startsWith('__skip')) continue
let opt = props[key]
if (opt) {
if (isArray(opt) || isFunction(opt)) {
opt = props[key] = { type: opt, default: defaults[key] }
} else {
opt.default = defaults[key]
}
} else if (opt === null) {
opt = props[key] = { default: defaults[key] }
} else if (__DEV__) {
warn(`props default key "${key}" has no corresponding declaration.`)
}
if (opt && defaults[`__skip_${key}`]) {
opt.skipFactory = true
}
}
return props
}
/**
* Runtime helper for merging model declarations.
* Imported by compiled code only.
* @internal
*/
export function mergeModels(
a: ComponentPropsOptions | EmitsOptions,
b: ComponentPropsOptions | EmitsOptions,
): ComponentPropsOptions | EmitsOptions {
if (!a || !b) return a || b
if (isArray(a) && isArray(b)) return a.concat(b)
return extend({}, normalizePropsOrEmits(a), normalizePropsOrEmits(b))
}
/**
* Used to create a proxy for the rest element when destructuring props with
* defineProps().
* @internal
*/
export function createPropsRestProxy(
props: any,
excludedKeys: string[],
): Record<string, any> {
const ret: Record<string, any> = {}
for (const key in props) {
if (!excludedKeys.includes(key)) {
Object.defineProperty(ret, key, {
enumerable: true,
get: () => props[key],
})
}
}
return ret
}
/**
* `<script setup>` helper for persisting the current instance context over
* async/await flows.
*
* `@vue/compiler-sfc` converts the following:
*
* ```ts
* const x = await foo()
* ```
*
* into:
*
* ```ts
* let __temp, __restore
* const x = (([__temp, __restore] = withAsyncContext(() => foo())),__temp=await __temp,__restore(),__temp)
* ```
* @internal
*/
export function withAsyncContext(getAwaitable: () => any): [any, () => void] {
const ctx = getCurrentInstance()!
if (__DEV__ && !ctx) {
warn(
`withAsyncContext called without active current instance. ` +
`This is likely a bug.`,
)
}
let awaitable = getAwaitable()
unsetCurrentInstance()
if (isPromise(awaitable)) {
awaitable = awaitable.catch(e => {
setCurrentInstance(ctx)
throw e
})
}
return [awaitable, () => setCurrentInstance(ctx)]
}