Browse Source

feat: Integrate ofetch for improved data fetching

- Replaced native fetch with $fetch from ofetch in useFetch composable for enhanced performance.
- Updated auto-imports to include $fetch globally.
- Added ofetch as a dependency in package.json.
- Adjusted Vite configuration to support automatic imports of $fetch.
mono
谢亚昕 2 months ago
parent
commit
844aa7f0b6
  1. BIN
      bun.lockb
  2. 3
      internal/x/composables/useFetch.ts
  3. 2
      packages/client/auto-imports.d.ts
  4. 1
      packages/client/components.d.ts
  5. 1
      packages/client/package.json
  6. 8
      packages/client/vite.config.ts

BIN
bun.lockb

Binary file not shown.

3
internal/x/composables/useFetch.ts

@ -2,6 +2,7 @@ import { ref, onMounted, onServerPrefetch, Ref } from 'vue'
import { getCurrentInstance } from 'vue' import { getCurrentInstance } from 'vue'
import type { SSRContext } from './ssrContext' import type { SSRContext } from './ssrContext'
import { resolveSSRContext } from './ssrContext' import { resolveSSRContext } from './ssrContext'
import { $fetch } from 'ofetch'
// 全局数据缓存,用于 SSR 数据共享 // 全局数据缓存,用于 SSR 数据共享
const globalCache = new Map<string, any>() const globalCache = new Map<string, any>()
@ -99,7 +100,7 @@ export function useFetch<T = any>(
} }
// 执行请求 // 执行请求
const response = await fetch(fetchUrl, requestInit) const response = await $fetch(fetchUrl, requestInit)
if (!response.ok) { if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`) throw new Error(`HTTP ${response.status}: ${response.statusText}`)

2
packages/client/auto-imports.d.ts

@ -6,6 +6,7 @@
// biome-ignore lint: disable // biome-ignore lint: disable
export {} export {}
declare global { declare global {
const $fetch: typeof import('ofetch')['$fetch']
const EffectScope: typeof import('vue')['EffectScope'] const EffectScope: typeof import('vue')['EffectScope']
const acceptHMRUpdate: typeof import('pinia')['acceptHMRUpdate'] const acceptHMRUpdate: typeof import('pinia')['acceptHMRUpdate']
const clearSSRContext: typeof import('../../internal/x/composables/ssrContext')['clearSSRContext'] const clearSSRContext: typeof import('../../internal/x/composables/ssrContext')['clearSSRContext']
@ -120,6 +121,7 @@ import { UnwrapRef } from 'vue'
declare module 'vue' { declare module 'vue' {
interface GlobalComponents {} interface GlobalComponents {}
interface ComponentCustomProperties { interface ComponentCustomProperties {
readonly $fetch: UnwrapRef<typeof import('ofetch')['$fetch']>
readonly EffectScope: UnwrapRef<typeof import('vue')['EffectScope']> readonly EffectScope: UnwrapRef<typeof import('vue')['EffectScope']>
readonly acceptHMRUpdate: UnwrapRef<typeof import('pinia')['acceptHMRUpdate']> readonly acceptHMRUpdate: UnwrapRef<typeof import('pinia')['acceptHMRUpdate']>
readonly clearSSRContext: UnwrapRef<typeof import('../../internal/x/composables/ssrContext')['clearSSRContext']> readonly clearSSRContext: UnwrapRef<typeof import('../../internal/x/composables/ssrContext')['clearSSRContext']>

1
packages/client/components.d.ts

@ -9,7 +9,6 @@ export {}
declare module 'vue' { declare module 'vue' {
export interface GlobalComponents { export interface GlobalComponents {
AiDemo: typeof import('./src/components/AiDemo/index.vue')['default'] AiDemo: typeof import('./src/components/AiDemo/index.vue')['default']
AXBubble: typeof import('ant-design-x-vue')['Bubble']
ClientOnly: typeof import('./../../internal/x/components/ClientOnly.vue')['default'] ClientOnly: typeof import('./../../internal/x/components/ClientOnly.vue')['default']
CookieDemo: typeof import('./src/components/CookieDemo.vue')['default'] CookieDemo: typeof import('./src/components/CookieDemo.vue')['default']
DataFetch: typeof import('./src/components/DataFetch.vue')['default'] DataFetch: typeof import('./src/components/DataFetch.vue')['default']

1
packages/client/package.json

@ -21,6 +21,7 @@
"htmlparser2": "^10.0.0", "htmlparser2": "^10.0.0",
"marked": "^16.3.0", "marked": "^16.3.0",
"maz-ui": "^4.1.6", "maz-ui": "^4.1.6",
"ofetch": "^1.4.1",
"quill": "^2.0.3", "quill": "^2.0.3",
"unplugin-auto-import": "^20.2.0", "unplugin-auto-import": "^20.2.0",
"unplugin-vue-router": "^0.15.0", "unplugin-vue-router": "^0.15.0",

8
packages/client/vite.config.ts

@ -57,7 +57,13 @@ export default defineConfig({
dtsMode: "overwrite", dtsMode: "overwrite",
resolvers: [MazModulesResolver()], resolvers: [MazModulesResolver()],
ignore: ["**/_*/**/*"], ignore: ["**/_*/**/*"],
imports: ['vue', 'vue-router', 'pinia', VueRouterAutoImports, unheadVueComposablesImports], imports: ['vue', 'vue-router', 'pinia', VueRouterAutoImports, unheadVueComposablesImports,
{
'ofetch': [
['$fetch']
]
}
],
dirs: ['./src/composables/**/*', '../../internal/x/composables/**', "./src/store/**/*"], dirs: ['./src/composables/**/*', '../../internal/x/composables/**', "./src/store/**/*"],
vueTemplate: true, vueTemplate: true,
}), }),

Loading…
Cancel
Save