Skip to content

Commit 85f6a18

Browse files
authored
fix: optional Valibot schema (#287)
1 parent 4c7604a commit 85f6a18

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

src/runtime/types.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,14 @@ export type EmptyOptionsSchema = typeof _emptyOptions
138138

139139
type ScriptInput = ScriptBase & DataKeys & SchemaAugmentations['script']
140140

141+
export type InferIfSchema<T> = T extends ObjectSchema<any, any> ? InferInput<T> : T
141142
export type RegistryScriptInput<
142-
T extends ObjectSchema<any, any> = EmptyOptionsSchema,
143+
T = EmptyOptionsSchema,
143144
Bundelable extends boolean = true,
144145
Usable extends boolean = false,
145146
CanBypassOptions extends boolean = true,
146147
> =
147-
(InferInput<T>
148+
(InferIfSchema<T>
148149
& {
149150
/**
150151
* A unique key to use for the script, this can be used to load multiple of the same script with different options.
@@ -153,7 +154,7 @@ export type RegistryScriptInput<
153154
scriptInput?: ScriptInput
154155
scriptOptions?: Omit<NuxtUseScriptOptions, Bundelable extends true ? '' : 'bundle' | Usable extends true ? '' : 'use'>
155156
})
156-
| Partial<InferInput<T>> & (
157+
| Partial<InferIfSchema<T>> & (
157158
CanBypassOptions extends true ? {
158159
/**
159160
* A unique key to use for the script, this can be used to load multiple of the same script with different options.

src/runtime/utils.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { parse } from '#nuxt-scripts-validator'
66
import { useRuntimeConfig } from '#imports'
77
import type {
88
EmptyOptionsSchema,
9+
InferIfSchema,
910
NuxtUseScriptOptions,
1011
RegistryScriptInput,
1112
ScriptRegistry,
@@ -27,21 +28,21 @@ function validateScriptInputSchema<T extends GenericSchema>(key: string, schema:
2728
}
2829
}
2930

30-
type OptionsFn<O extends ObjectSchema<any, any>> = (options: InferInput<O>) => ({
31+
type OptionsFn<O> = (options: InferIfSchema<O>) => ({
3132
scriptInput?: UseScriptInput
3233
scriptOptions?: NuxtUseScriptOptions
33-
schema?: O
34+
schema?: O extends ObjectSchema<any, any> ? O : undefined
3435
clientInit?: () => void
3536
})
3637

3738
export function scriptRuntimeConfig<T extends keyof ScriptRegistry>(key: T) {
3839
return ((useRuntimeConfig().public.scripts || {}) as ScriptRegistry)[key]
3940
}
4041

41-
export function useRegistryScript<T extends Record<string | symbol, any>, O extends ObjectSchema<any, any> = EmptyOptionsSchema, U = {}>(registryKey: keyof ScriptRegistry | string, optionsFn: OptionsFn<O>, _userOptions?: RegistryScriptInput<O>) {
42+
export function useRegistryScript<T extends Record<string | symbol, any>, O = EmptyOptionsSchema, U = {}>(registryKey: keyof ScriptRegistry | string, optionsFn: OptionsFn<O>, _userOptions?: RegistryScriptInput<O>) {
4243
const scriptConfig = scriptRuntimeConfig(registryKey as keyof ScriptRegistry)
4344
const userOptions = Object.assign(_userOptions || {}, typeof scriptConfig === 'object' ? scriptConfig : {})
44-
const options = optionsFn(userOptions)
45+
const options = optionsFn(userOptions as InferIfSchema<O>)
4546

4647
const scriptInput = defu(userOptions.scriptInput, options.scriptInput, { key: registryKey }) as any as UseScriptInput
4748
const scriptOptions = Object.assign(userOptions?.scriptOptions || {}, options.scriptOptions || {})

0 commit comments

Comments
 (0)