Skip to content

Commit

Permalink
chore(scripts): export UseScriptReturn
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Jan 21, 2025
1 parent 927f1b4 commit f17d33e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
2 changes: 2 additions & 0 deletions packages/scripts/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,5 @@ export interface UseScriptOptions<T extends BaseScriptApi = Record<string, any>>
*/
beforeInit?: () => void
}

export type UseScriptReturn<T extends Record<symbol | string, any>> = UseScriptContext<UseFunctionType<UseScriptOptions<T>, T>>
3 changes: 2 additions & 1 deletion packages/scripts/src/useScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {
UseScriptInput,
UseScriptOptions,
UseScriptResolvedInput,
UseScriptReturn,
WarmupStrategy,
} from './types'
import { hashCode, ScriptNetworkEvents } from '@unhead/shared'
Expand All @@ -26,7 +27,7 @@ const PreconnectServerModes = ['preconnect', 'dns-prefetch']
*
* @see https://unhead.unjs.io/usage/composables/use-script
*/
export function useScript<T extends Record<symbol | string, any> = Record<symbol | string, any>>(head: Unhead<any>, _input: UseScriptInput, _options?: UseScriptOptions<T>): UseScriptContext<UseFunctionType<UseScriptOptions<T>, T>> {
export function useScript<T extends Record<symbol | string, any> = Record<symbol | string, any>>(head: Unhead<any>, _input: UseScriptInput, _options?: UseScriptOptions<T>): UseScriptReturn<T> {
const input: UseScriptResolvedInput = typeof _input === 'string' ? { src: _input } : _input
const options = _options || {}
const id = resolveScriptKey(input)
Expand Down
14 changes: 10 additions & 4 deletions packages/scripts/src/vue/useScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type {
SchemaAugmentations,
ScriptBase,
} from '@unhead/schema'
import type { MaybeComputedRefEntriesOnly } from '@unhead/vue'
import type { MaybeComputedRefEntriesOnly, VueHeadClient } from '@unhead/vue'
import type { ComponentInternalInstance, Ref, WatchHandle } from 'vue'
import type { UseScriptOptions as BaseUseScriptOptions, ScriptInstance, UseFunctionType, UseScriptStatus } from '../types'
import { injectHead } from '@unhead/vue'
Expand All @@ -16,7 +16,7 @@ export interface VueScriptInstance<T extends Record<symbol | string, any>> exten
}

export type UseScriptInput = string | (MaybeComputedRefEntriesOnly<Omit<ScriptBase & DataKeys & SchemaAugmentations['script'], 'src'>> & { src: string })
export interface UseScriptOptions<T extends Record<symbol | string, any> = Record<string, any>> extends HeadEntryOptions, Pick<BaseUseScriptOptions<T>, 'use' | 'eventContext' | 'beforeInit'> {
export interface UseScriptOptions<T extends Record<symbol | string, any> = Record<string, any>> extends Omit<HeadEntryOptions, 'head'>, Pick<BaseUseScriptOptions<T>, 'use' | 'eventContext' | 'beforeInit'> {
/**
* The trigger to load the script:
* - `undefined` | `client` - (Default) Load the script on the client when this js is loaded.
Expand All @@ -27,6 +27,10 @@ export interface UseScriptOptions<T extends Record<symbol | string, any> = Recor
* - `ref` - Load the script when the ref is true.
*/
trigger?: BaseUseScriptOptions['trigger'] | Ref<boolean>
/**
* Unhead instance.
*/
head?: VueHeadClient<any>
}

export type UseScriptContext<T extends Record<symbol | string, any>> = Promise<T> & VueScriptInstance<T>
Expand Down Expand Up @@ -60,7 +64,9 @@ function registerVueScopeHandlers<T extends Record<symbol | string, any> = Recor
})
}

export function useScript<T extends Record<symbol | string, any> = Record<symbol | string, any>>(_input: UseScriptInput, _options?: UseScriptOptions<T>): UseScriptContext<UseFunctionType<UseScriptOptions<T>, T>> {
export type UseScriptReturn<T extends Record<symbol | string, any>> = UseScriptContext<UseFunctionType<UseScriptOptions<T>, T>>

export function useScript<T extends Record<symbol | string, any> = Record<symbol | string, any>>(_input: UseScriptInput, _options?: UseScriptOptions<T>): UseScriptReturn<T> {
const input = (typeof _input === 'string' ? { src: _input } : _input) as UseScriptInput
const options = _options || {} as UseScriptOptions<T>
const head = options?.head || injectHead()
Expand Down Expand Up @@ -105,5 +111,5 @@ export function useScript<T extends Record<symbol | string, any> = Record<symbol
// we can't override status as it will break the unhead useScript API
return Reflect.get(_, key === 'status' ? '_statusRef' : key, a)
},
}) as any as UseScriptContext<UseFunctionType<UseScriptOptions<T>, T>>
}) as any as UseScriptReturn<T>
}

0 comments on commit f17d33e

Please sign in to comment.