Skip to content

Commit

Permalink
fix: support explicit scriptInput.src override
Browse files Browse the repository at this point in the history
Relates to #152
  • Loading branch information
harlan-zw committed Jul 15, 2024
1 parent 29c83fc commit be2389f
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/runtime/registry/crisp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const CrispOptions = object({
cookieExpiry: optional(number()),
})

export type CrispInput = RegistryScriptInput<typeof CrispOptions, false>
export type CrispInput = RegistryScriptInput<typeof CrispOptions, false, false, false>

export interface CrispApi {
push: (...args: any[]) => void
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/registry/fathom-analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const FathomAnalyticsOptions = object({
honorDnt: optional(boolean()),
})

export type FathomAnalyticsInput = RegistryScriptInput<typeof FathomAnalyticsOptions, false>
export type FathomAnalyticsInput = RegistryScriptInput<typeof FathomAnalyticsOptions, false, false, false>

export interface FathomAnalyticsApi {
beacon: (ctx: { url: string, referrer?: string }) => void
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/registry/google-adsense.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const GoogleAdsenseOptions = object({
client: optional(string()),
})

export type GoogleAdsenseInput = RegistryScriptInput<typeof GoogleAdsenseOptions>
export type GoogleAdsenseInput = RegistryScriptInput<typeof GoogleAdsenseOptions, true, false, false>

export interface GoogleAdsenseApi {
/**
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/registry/hotjar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const HotjarOptions = object({
sv: optional(number()),
})

export type HotjarInput = RegistryScriptInput<typeof HotjarOptions>
export type HotjarInput = RegistryScriptInput<typeof HotjarOptions, true, false, false>

export function useScriptHotjar<T extends HotjarApi>(_options?: HotjarInput) {
return useRegistryScript<T, typeof HotjarOptions>(_options?.key || 'hotjar', options => ({
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/registry/intercom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const IntercomOptions = object({
vertical_padding: optional(number()),
})

export type IntercomInput = RegistryScriptInput<typeof IntercomOptions>
export type IntercomInput = RegistryScriptInput<typeof IntercomOptions, true, false, false>

export interface IntercomApi {
Intercom: ((event: 'boot', data?: InferInput<typeof IntercomOptions>) => void)
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/registry/matomo-analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const MatomoAnalyticsOptions = object({
enableLinkTracking: optional(boolean()),
})

export type MatomoAnalyticsInput = RegistryScriptInput<typeof MatomoAnalyticsOptions, false>
export type MatomoAnalyticsInput = RegistryScriptInput<typeof MatomoAnalyticsOptions, false, false, false>

interface MatomoAnalyticsApi {
_paq: unknown[]
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/registry/meta-pixel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ declare global {
export const MetaPixelOptions = object({
id: union([string(), number()]),
})
export type MetaPixelInput = RegistryScriptInput<typeof MetaPixelOptions>
export type MetaPixelInput = RegistryScriptInput<typeof MetaPixelOptions, true, false, false>

export function useScriptMetaPixel<T extends MetaPixelApi>(_options?: MetaPixelInput) {
return useRegistryScript<T, typeof MetaPixelOptions>(_options?.key || 'metaPixel', options => ({
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/registry/npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const NpmOptions = object({
type: optional(string()),
})

export type NpmInput = RegistryScriptInput<typeof NpmOptions, true, true>
export type NpmInput = RegistryScriptInput<typeof NpmOptions, true, true, false>

export function useScriptNpm<T extends Record<string | symbol, any>>(_options: NpmInput) {
// TODO support multiple providers? (e.g. jsdelivr, cdnjs, etc.) Only unpkg for now
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/registry/x-pixel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const XPixelOptions = object({
id: string(),
version: optional(string()),
})
export type XPixelInput = RegistryScriptInput<typeof XPixelOptions>
export type XPixelInput = RegistryScriptInput<typeof XPixelOptions, true, false, false>

export function useScriptXPixel<T extends XPixelApi>(_options?: XPixelInput) {
return useRegistryScript<T, typeof XPixelOptions>(_options?.key || 'xPixel', (options) => {
Expand Down
35 changes: 26 additions & 9 deletions src/runtime/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { UseScriptOptions, DataKeys, SchemaAugmentations, ScriptBase } from '@unhead/schema'
import type { UseScriptInput, VueScriptInstance, MaybeComputedRefEntriesOnly } from '@unhead/vue'
import type { UseScriptInput, VueScriptInstance } from '@unhead/vue'
import type { ComputedRef, Ref } from 'vue'
import type { InferInput, ObjectSchema } from 'valibot'
import type { Import } from 'unimport'
Expand Down Expand Up @@ -120,14 +120,31 @@ const emptyOptions = object({})

export type EmptyOptionsSchema = typeof emptyOptions

export type RegistryScriptInput<T extends ObjectSchema<any, any> = EmptyOptionsSchema, Bundelable extends boolean = true, Usable extends boolean = false> = InferInput<T> & {
/**
* A unique key to use for the script, this can be used to load multiple of the same script with different options.
*/
key?: string
scriptInput?: MaybeComputedRefEntriesOnly<Omit<ScriptBase & DataKeys & SchemaAugmentations['script'], 'src'>>
scriptOptions?: Omit<NuxtUseScriptOptions, Bundelable extends true ? '' : 'bundle' | Usable extends true ? '' : 'use'>
}
type ScriptInput = ScriptBase & DataKeys & SchemaAugmentations['script']

export type RegistryScriptInput<
T extends ObjectSchema<any, any> = EmptyOptionsSchema,
Bundelable extends boolean = true,
Usable extends boolean = false,
CanBypassOptions extends boolean = true,
> =
(InferInput<T>
& {
/**
* A unique key to use for the script, this can be used to load multiple of the same script with different options.
*/
key?: string
scriptInput?: ScriptInput
scriptOptions?: Omit<NuxtUseScriptOptions, Bundelable extends true ? '' : 'bundle' | Usable extends true ? '' : 'use'>
})
| (CanBypassOptions extends true ? {
/**
* A unique key to use for the script, this can be used to load multiple of the same script with different options.
*/
key?: string
scriptInput: Required<Pick<ScriptInput, 'src'>> & ScriptInput
scriptOptions?: Omit<NuxtUseScriptOptions, Bundelable extends true ? '' : 'bundle' | Usable extends true ? '' : 'use'>
} : never)

export interface RegistryScript {
import?: Import // might just be a component
Expand Down
7 changes: 6 additions & 1 deletion src/runtime/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ export function useRegistryScript<T extends Record<string | symbol, any>, O exte
const init = scriptOptions.beforeInit
scriptOptions.beforeInit = () => {
// a manual trigger also means it was disabled by nuxt.config
import.meta.dev && !scriptOptions.skipValidation && options.schema && validateScriptInputSchema(key, options.schema, userOptions)
if (import.meta.dev && !scriptOptions.skipValidation && options.schema) {
// overriding the src will skip validation
if (!userOptions.scriptInput?.src) {
validateScriptInputSchema(key, options.schema, userOptions)
}
}
// avoid clearing the user beforeInit
init?.()
if (import.meta.client) {
Expand Down

0 comments on commit be2389f

Please sign in to comment.