Skip to content

Commit a00f753

Browse files
committed
fix(YouTube): broken api types
1 parent 47e933d commit a00f753

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

src/runtime/components/ScriptYouTubePlayer.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ if (props.trigger === 'mousedown' && trigger instanceof Promise) {
5959
}
6060
onMounted(() => {
6161
onLoaded(async (instance) => {
62-
const YouTube: typeof YT & { ready: (fn: () => void) => void } = await instance.YT
62+
const YouTube = instance.YT instanceof Promise ? await instance.YT : instance.YT
6363
await new Promise<void>((resolve) => {
6464
if (typeof YT.Player === 'undefined')
6565
YouTube.ready(resolve)

src/runtime/registry/youtube-player.ts

+24-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,32 @@
11
/// <reference types="youtube" />
22
import { watch } from 'vue'
3+
import type { UseScriptContext } from '@unhead/vue'
4+
import type { MaybePromise } from '../utils'
35
import { useRegistryScript } from '../utils'
46
import { useHead } from '#imports'
57
import type { RegistryScriptInput } from '#nuxt-scripts'
68

79
export interface YouTubePlayerApi {
8-
YT: typeof YT & { ready: (fn: () => void) => void }
10+
YT: MaybePromise<{
11+
Player: YT.Player
12+
PlayerState: YT.PlayerState
13+
get(k: string): any
14+
loaded: 0 | 1
15+
loading: 0 | 1
16+
ready(f: () => void): void
17+
scan(): void
18+
setConfig(config: YT.PlayerOptions): void
19+
subscribe<EventName extends keyof YT.Events>(
20+
event: EventName,
21+
listener: YT.Events[EventName],
22+
context?: any
23+
): void
24+
unsubscribe<EventName extends keyof YT.Events>(
25+
event: EventName,
26+
listener: YT.Events[EventName],
27+
context?: any
28+
): void
29+
}>
930
}
1031

1132
declare global {
@@ -16,7 +37,7 @@ declare global {
1637

1738
export type YouTubePlayerInput = RegistryScriptInput
1839

19-
export function useScriptYouTubePlayer<T extends YouTubePlayerApi>(_options: YouTubePlayerInput) {
40+
export function useScriptYouTubePlayer<T extends YouTubePlayerApi>(_options: YouTubePlayerInput): UseScriptContext<T> {
2041
let readyPromise: Promise<void> = Promise.resolve()
2142
const instance = useRegistryScript<T>('youtubePlayer', () => ({
2243
scriptInput: {
@@ -26,7 +47,7 @@ export function useScriptYouTubePlayer<T extends YouTubePlayerApi>(_options: You
2647
scriptOptions: {
2748
use() {
2849
return {
29-
YT: readyPromise.then(() => {
50+
YT: window.YT || readyPromise.then(() => {
3051
return window.YT
3152
}),
3253
}

src/runtime/utils.ts

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import type {
1111
ScriptRegistry,
1212
} from '#nuxt-scripts'
1313

14+
export type MaybePromise<T> = Promise<T> | T
15+
1416
function validateScriptInputSchema<T extends GenericSchema>(key: string, schema: T, options?: InferInput<T>) {
1517
if (import.meta.dev) {
1618
try {

0 commit comments

Comments
 (0)