Skip to content

Commit be2389f

Browse files
committed
fix: support explicit scriptInput.src override
Relates to #152
1 parent 29c83fc commit be2389f

File tree

11 files changed

+41
-19
lines changed

11 files changed

+41
-19
lines changed

src/runtime/registry/crisp.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export const CrispOptions = object({
3535
cookieExpiry: optional(number()),
3636
})
3737

38-
export type CrispInput = RegistryScriptInput<typeof CrispOptions, false>
38+
export type CrispInput = RegistryScriptInput<typeof CrispOptions, false, false, false>
3939

4040
export interface CrispApi {
4141
push: (...args: any[]) => void

src/runtime/registry/fathom-analytics.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export const FathomAnalyticsOptions = object({
2525
honorDnt: optional(boolean()),
2626
})
2727

28-
export type FathomAnalyticsInput = RegistryScriptInput<typeof FathomAnalyticsOptions, false>
28+
export type FathomAnalyticsInput = RegistryScriptInput<typeof FathomAnalyticsOptions, false, false, false>
2929

3030
export interface FathomAnalyticsApi {
3131
beacon: (ctx: { url: string, referrer?: string }) => void

src/runtime/registry/google-adsense.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export const GoogleAdsenseOptions = object({
1111
client: optional(string()),
1212
})
1313

14-
export type GoogleAdsenseInput = RegistryScriptInput<typeof GoogleAdsenseOptions>
14+
export type GoogleAdsenseInput = RegistryScriptInput<typeof GoogleAdsenseOptions, true, false, false>
1515

1616
export interface GoogleAdsenseApi {
1717
/**

src/runtime/registry/hotjar.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export const HotjarOptions = object({
1919
sv: optional(number()),
2020
})
2121

22-
export type HotjarInput = RegistryScriptInput<typeof HotjarOptions>
22+
export type HotjarInput = RegistryScriptInput<typeof HotjarOptions, true, false, false>
2323

2424
export function useScriptHotjar<T extends HotjarApi>(_options?: HotjarInput) {
2525
return useRegistryScript<T, typeof HotjarOptions>(_options?.key || 'hotjar', options => ({

src/runtime/registry/intercom.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export const IntercomOptions = object({
1616
vertical_padding: optional(number()),
1717
})
1818

19-
export type IntercomInput = RegistryScriptInput<typeof IntercomOptions>
19+
export type IntercomInput = RegistryScriptInput<typeof IntercomOptions, true, false, false>
2020

2121
export interface IntercomApi {
2222
Intercom: ((event: 'boot', data?: InferInput<typeof IntercomOptions>) => void)

src/runtime/registry/matomo-analytics.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export const MatomoAnalyticsOptions = object({
1010
enableLinkTracking: optional(boolean()),
1111
})
1212

13-
export type MatomoAnalyticsInput = RegistryScriptInput<typeof MatomoAnalyticsOptions, false>
13+
export type MatomoAnalyticsInput = RegistryScriptInput<typeof MatomoAnalyticsOptions, false, false, false>
1414

1515
interface MatomoAnalyticsApi {
1616
_paq: unknown[]

src/runtime/registry/meta-pixel.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ declare global {
3939
export const MetaPixelOptions = object({
4040
id: union([string(), number()]),
4141
})
42-
export type MetaPixelInput = RegistryScriptInput<typeof MetaPixelOptions>
42+
export type MetaPixelInput = RegistryScriptInput<typeof MetaPixelOptions, true, false, false>
4343

4444
export function useScriptMetaPixel<T extends MetaPixelApi>(_options?: MetaPixelInput) {
4545
return useRegistryScript<T, typeof MetaPixelOptions>(_options?.key || 'metaPixel', options => ({

src/runtime/registry/npm.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export const NpmOptions = object({
1010
type: optional(string()),
1111
})
1212

13-
export type NpmInput = RegistryScriptInput<typeof NpmOptions, true, true>
13+
export type NpmInput = RegistryScriptInput<typeof NpmOptions, true, true, false>
1414

1515
export function useScriptNpm<T extends Record<string | symbol, any>>(_options: NpmInput) {
1616
// TODO support multiple providers? (e.g. jsdelivr, cdnjs, etc.) Only unpkg for now

src/runtime/registry/x-pixel.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export const XPixelOptions = object({
4444
id: string(),
4545
version: optional(string()),
4646
})
47-
export type XPixelInput = RegistryScriptInput<typeof XPixelOptions>
47+
export type XPixelInput = RegistryScriptInput<typeof XPixelOptions, true, false, false>
4848

4949
export function useScriptXPixel<T extends XPixelApi>(_options?: XPixelInput) {
5050
return useRegistryScript<T, typeof XPixelOptions>(_options?.key || 'xPixel', (options) => {

src/runtime/types.ts

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { UseScriptOptions, DataKeys, SchemaAugmentations, ScriptBase } from '@unhead/schema'
2-
import type { UseScriptInput, VueScriptInstance, MaybeComputedRefEntriesOnly } from '@unhead/vue'
2+
import type { UseScriptInput, VueScriptInstance } from '@unhead/vue'
33
import type { ComputedRef, Ref } from 'vue'
44
import type { InferInput, ObjectSchema } from 'valibot'
55
import type { Import } from 'unimport'
@@ -120,14 +120,31 @@ const emptyOptions = object({})
120120

121121
export type EmptyOptionsSchema = typeof emptyOptions
122122

123-
export type RegistryScriptInput<T extends ObjectSchema<any, any> = EmptyOptionsSchema, Bundelable extends boolean = true, Usable extends boolean = false> = InferInput<T> & {
124-
/**
125-
* A unique key to use for the script, this can be used to load multiple of the same script with different options.
126-
*/
127-
key?: string
128-
scriptInput?: MaybeComputedRefEntriesOnly<Omit<ScriptBase & DataKeys & SchemaAugmentations['script'], 'src'>>
129-
scriptOptions?: Omit<NuxtUseScriptOptions, Bundelable extends true ? '' : 'bundle' | Usable extends true ? '' : 'use'>
130-
}
123+
type ScriptInput = ScriptBase & DataKeys & SchemaAugmentations['script']
124+
125+
export type RegistryScriptInput<
126+
T extends ObjectSchema<any, any> = EmptyOptionsSchema,
127+
Bundelable extends boolean = true,
128+
Usable extends boolean = false,
129+
CanBypassOptions extends boolean = true,
130+
> =
131+
(InferInput<T>
132+
& {
133+
/**
134+
* A unique key to use for the script, this can be used to load multiple of the same script with different options.
135+
*/
136+
key?: string
137+
scriptInput?: ScriptInput
138+
scriptOptions?: Omit<NuxtUseScriptOptions, Bundelable extends true ? '' : 'bundle' | Usable extends true ? '' : 'use'>
139+
})
140+
| (CanBypassOptions extends true ? {
141+
/**
142+
* A unique key to use for the script, this can be used to load multiple of the same script with different options.
143+
*/
144+
key?: string
145+
scriptInput: Required<Pick<ScriptInput, 'src'>> & ScriptInput
146+
scriptOptions?: Omit<NuxtUseScriptOptions, Bundelable extends true ? '' : 'bundle' | Usable extends true ? '' : 'use'>
147+
} : never)
131148

132149
export interface RegistryScript {
133150
import?: Import // might just be a component

0 commit comments

Comments
 (0)