From 40ac59e5160d0deecbe5dae394e94f75be44b803 Mon Sep 17 00:00:00 2001 From: Mister-Hope Date: Fri, 17 Jan 2025 15:00:56 +0800 Subject: [PATCH] feat(client): improve client data types --- packages/client/src/composables/clientData.ts | 7 +++++-- packages/client/src/composables/clientDataUtils.ts | 5 ++--- packages/client/src/types/clientData.ts | 9 ++++++--- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/packages/client/src/composables/clientData.ts b/packages/client/src/composables/clientData.ts index 52d21ea2c1..b5db09a92e 100644 --- a/packages/client/src/composables/clientData.ts +++ b/packages/client/src/composables/clientData.ts @@ -12,8 +12,11 @@ export const clientDataSymbol: InjectionKey = Symbol( /** * Returns client data */ -export const useClientData = (): ClientData => { - const clientData = inject(clientDataSymbol) +export const useClientData = < + Frontmatter extends Record = Record, + Data extends Record = Record, +>(): ClientData => { + const clientData = inject>(clientDataSymbol) if (!clientData) { throw new Error('useClientData() is called without provider.') } diff --git a/packages/client/src/composables/clientDataUtils.ts b/packages/client/src/composables/clientDataUtils.ts index 3dc3109670..259abd464c 100644 --- a/packages/client/src/composables/clientDataUtils.ts +++ b/packages/client/src/composables/clientDataUtils.ts @@ -22,12 +22,11 @@ export const usePageComponent = (): PageComponentRef => export const usePageData = < T extends Record = Record, ->(): PageDataRef => useClientData().pageData as PageDataRef +>(): PageDataRef => useClientData, T>().pageData export const usePageFrontmatter = < T extends Record = Record, ->(): PageFrontmatterRef => - useClientData().pageFrontmatter as PageFrontmatterRef +>(): PageFrontmatterRef => useClientData().pageFrontmatter export const usePageHead = (): PageHeadRef => useClientData().pageHead diff --git a/packages/client/src/types/clientData.ts b/packages/client/src/types/clientData.ts index 5107ac3067..98dd20c7c8 100644 --- a/packages/client/src/types/clientData.ts +++ b/packages/client/src/types/clientData.ts @@ -54,11 +54,14 @@ export type RoutesRef = Ref export type SiteDataRef = Ref export type SiteLocaleDataRef = ComputedRef -export interface ClientData { +export interface ClientData< + Frontmatter extends Record = Record, + Data extends Record = Record, +> { layouts: LayoutsRef pageComponent: PageComponentRef - pageData: PageDataRef - pageFrontmatter: PageFrontmatterRef + pageData: PageDataRef + pageFrontmatter: PageFrontmatterRef pageHead: PageHeadRef pageHeadTitle: PageHeadTitleRef pageLang: PageLangRef