Skip to content

Commit

Permalink
feat(client): improve client data types
Browse files Browse the repository at this point in the history
  • Loading branch information
Mister-Hope committed Jan 17, 2025
1 parent a5ce1e7 commit 40ac59e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
7 changes: 5 additions & 2 deletions packages/client/src/composables/clientData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ export const clientDataSymbol: InjectionKey<ClientData> = Symbol(
/**
* Returns client data
*/
export const useClientData = (): ClientData => {
const clientData = inject(clientDataSymbol)
export const useClientData = <
Frontmatter extends Record<string, unknown> = Record<string, unknown>,
Data extends Record<string, unknown> = Record<string, unknown>,
>(): ClientData<Frontmatter, Data> => {
const clientData = inject<ClientData<Frontmatter, Data>>(clientDataSymbol)
if (!clientData) {
throw new Error('useClientData() is called without provider.')
}
Expand Down
5 changes: 2 additions & 3 deletions packages/client/src/composables/clientDataUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@ export const usePageComponent = (): PageComponentRef =>

export const usePageData = <
T extends Record<string, unknown> = Record<string, unknown>,
>(): PageDataRef<T> => useClientData().pageData as PageDataRef<T>
>(): PageDataRef<T> => useClientData<Record<string, unknown>, T>().pageData

export const usePageFrontmatter = <
T extends Record<string, unknown> = Record<string, unknown>,
>(): PageFrontmatterRef<T> =>
useClientData().pageFrontmatter as PageFrontmatterRef<T>
>(): PageFrontmatterRef<T> => useClientData<T>().pageFrontmatter

export const usePageHead = (): PageHeadRef => useClientData().pageHead

Expand Down
9 changes: 6 additions & 3 deletions packages/client/src/types/clientData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,14 @@ export type RoutesRef = Ref<Routes>
export type SiteDataRef = Ref<SiteData>
export type SiteLocaleDataRef = ComputedRef<SiteLocaleData>

export interface ClientData {
export interface ClientData<
Frontmatter extends Record<string, unknown> = Record<string, unknown>,
Data extends Record<string, unknown> = Record<string, unknown>,
> {
layouts: LayoutsRef
pageComponent: PageComponentRef
pageData: PageDataRef
pageFrontmatter: PageFrontmatterRef
pageData: PageDataRef<Data>
pageFrontmatter: PageFrontmatterRef<Frontmatter>
pageHead: PageHeadRef
pageHeadTitle: PageHeadTitleRef
pageLang: PageLangRef
Expand Down

0 comments on commit 40ac59e

Please sign in to comment.