Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for clientData #1286

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/client/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { clientConfigs } from '@internal/clientConfigs'
import { createApp, createSSRApp, h } from 'vue'
import { RouterView } from 'vue-router'
import { siteData } from './composables/index.js'
import { clientDataMap } from './helpers/index.js'
import { createVueRouter } from './router.js'
import { setupGlobalComponents } from './setupGlobalComponents.js'
import { setupGlobalComputed } from './setupGlobalComputed.js'
Expand Down Expand Up @@ -50,6 +51,11 @@ export const createVueApp: CreateVueAppFunction = async () => {
setupDevtools(app, globalComputed)
}

// provide client data
for (const [key, value] of clientDataMap) {
app.provide(key, value)
}

// invoke all client enhance
for (const clientConfig of clientConfigs) {
await clientConfig.enhance?.({ app, router, siteData })
Expand Down
15 changes: 15 additions & 0 deletions packages/client/src/composables/clientData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { inject } from 'vue'
import type { InjectionKey } from 'vue'

export const useClientData = <T = unknown, U extends boolean = false>(
key: InjectionKey<T>,
required?: U,
): U extends true ? T : T | undefined => {
const result = inject(key)

if (required && !result) {
throw new Error(`Can not found ${key} in clientData()`)
}

return result as U extends true ? T : T | undefined
}
1 change: 1 addition & 0 deletions packages/client/src/composables/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './clientData.js'
export * from './layouts.js'
export * from './pageData.js'
export * from './pageFrontmatter.js'
Expand Down
14 changes: 14 additions & 0 deletions packages/client/src/helpers/defineClientData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { InjectionKey } from 'vue'

export const clientDataMap = new Map<InjectionKey<unknown>, unknown>()

/**
* A helper function to help you define vuepress client data
*/
export const defineClientData = <T = unknown>(
key: InjectionKey<T>,
data: T,
overrideExisting = true,
): void => {
if (overrideExisting || !clientDataMap.has(key)) clientDataMap.set(key, data)
}
1 change: 1 addition & 0 deletions packages/client/src/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './defineClientConfig.js'
export * from './defineClientData.js'
export * from './withBase.js'