From 958d970efabb21dbd4ca315249d549a5c1e949fb Mon Sep 17 00:00:00 2001 From: "Mr.Hope" Date: Fri, 21 Apr 2023 13:59:42 +0800 Subject: [PATCH] feat(client): add support for clientData --- packages/client/src/app.ts | 6 ++++++ packages/client/src/composables/clientData.ts | 15 +++++++++++++++ packages/client/src/composables/index.ts | 1 + packages/client/src/helpers/defineClientData.ts | 13 +++++++++++++ packages/client/src/helpers/index.ts | 1 + 5 files changed, 36 insertions(+) create mode 100644 packages/client/src/composables/clientData.ts create mode 100644 packages/client/src/helpers/defineClientData.ts diff --git a/packages/client/src/app.ts b/packages/client/src/app.ts index 8ea77ffd41..bded528e2a 100644 --- a/packages/client/src/app.ts +++ b/packages/client/src/app.ts @@ -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' @@ -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 }) diff --git a/packages/client/src/composables/clientData.ts b/packages/client/src/composables/clientData.ts new file mode 100644 index 0000000000..0a6fac9a3e --- /dev/null +++ b/packages/client/src/composables/clientData.ts @@ -0,0 +1,15 @@ +import { inject } from 'vue' +import type { InjectionKey } from 'vue' + +export const useClientData = ( + key: InjectionKey, + 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 +} diff --git a/packages/client/src/composables/index.ts b/packages/client/src/composables/index.ts index 838b38b191..4253ac28d4 100644 --- a/packages/client/src/composables/index.ts +++ b/packages/client/src/composables/index.ts @@ -1,3 +1,4 @@ +export * from './clientData.js' export * from './layouts.js' export * from './pageData.js' export * from './pageFrontmatter.js' diff --git a/packages/client/src/helpers/defineClientData.ts b/packages/client/src/helpers/defineClientData.ts new file mode 100644 index 0000000000..9f11a2f2a3 --- /dev/null +++ b/packages/client/src/helpers/defineClientData.ts @@ -0,0 +1,13 @@ +import type { InjectionKey } from 'vue' + +export const clientDataMap = new Map, unknown>() + +/** + * A helper function to help you define vuepress client data + */ +export const defineClientData = ( + key: InjectionKey, + data: T +): void => { + clientDataMap.set(key, data) +} diff --git a/packages/client/src/helpers/index.ts b/packages/client/src/helpers/index.ts index e5726afa68..223b916e0a 100644 --- a/packages/client/src/helpers/index.ts +++ b/packages/client/src/helpers/index.ts @@ -1,2 +1,3 @@ export * from './defineClientConfig.js' +export * from './defineClientData.js' export * from './withBase.js'