Skip to content

Commit c9a35d6

Browse files
committed
feat: add fetch example
1 parent a4f3e18 commit c9a35d6

File tree

11 files changed

+139
-9
lines changed

11 files changed

+139
-9
lines changed

.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
NUXT_PUBLIC_API_BASE=https://easyapi.devv.zone

.gitignore

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,3 @@ logs
1717
.DS_Store
1818
.fleet
1919
.idea
20-
21-
# Local env files
22-
.env
23-
.env.*
24-
!.env.example

app/api/http.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import type { $Fetch } from 'ofetch'
2+
3+
import { useRuntimeConfig } from '#app'
4+
import { ofetch } from 'ofetch'
5+
6+
type HttpStatusErrorHandler = (message: string, statusCode: number) => void
7+
let httpStatusErrorHandler: HttpStatusErrorHandler
8+
9+
let http: $Fetch
10+
11+
export function setupHttp() {
12+
if (http)
13+
return http
14+
15+
const config = useRuntimeConfig()
16+
const baseURL = config.public.apiBase as string
17+
18+
http = ofetch.create({
19+
baseURL,
20+
headers: { 'Content-Type': 'application/json' },
21+
async onRequest({ options }) {
22+
const token = localStorage.getItem('token')
23+
24+
options.headers = {
25+
...options.headers,
26+
...(token && { Authorization: `Bearer ${token}` }),
27+
}
28+
},
29+
async onResponseError({ response }) {
30+
const { message } = response._data
31+
if (Array.isArray(message)) {
32+
message.forEach((item) => {
33+
httpStatusErrorHandler?.(item, response.status)
34+
})
35+
}
36+
else {
37+
httpStatusErrorHandler?.(message, response.status)
38+
}
39+
return Promise.reject(response._data)
40+
},
41+
retry: 3,
42+
retryDelay: 1000,
43+
})
44+
}
45+
46+
export function injectHttpStatusErrorHandler(handler: HttpStatusErrorHandler) {
47+
httpStatusErrorHandler = handler
48+
}
49+
50+
export function getHttp() {
51+
if (!http) {
52+
throw new Error('HTTP client not initialized. Call setupHttp first.')
53+
}
54+
return http
55+
}

app/api/prose.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { getHttp } from './http'
2+
3+
export async function getProse() {
4+
const http = getHttp()
5+
return await http('/api/prose', {
6+
method: 'GET',
7+
})
8+
}

app/pages/index.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ const menus = computed(() => [
4040
{ title: t('menu.unocssExample'), route: 'unocss' },
4141
{ title: t('menu.keepAlive'), route: 'keepalive' },
4242
{ title: t('menu.persistPiniaState'), route: 'counter' },
43+
{ title: t('menu.fetch'), route: 'prose' },
4344
{ title: t('menu.404Demo'), route: 'unknown' },
4445
])
4546

app/pages/prose/index.vue

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<script lang="ts" setup>
2+
import { useProseStore } from '~/stores/prose'
3+
4+
definePageMeta({
5+
layout: 'default',
6+
title: '随笔',
7+
i18n: 'menu.fetch',
8+
})
9+
10+
const proseStore = useProseStore()
11+
12+
async function fetchData() {
13+
await proseStore.fetchProse()
14+
}
15+
16+
fetchData()
17+
</script>
18+
19+
<template>
20+
<div>
21+
{{ proseStore.prose }}
22+
</div>
23+
</template>

app/plugins/http.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { setupHttp } from '~/api/http'
2+
3+
export default defineNuxtPlugin(() => {
4+
setupHttp()
5+
})

app/stores/prose.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { defineStore } from 'pinia'
2+
import { getProse } from '~/api/prose'
3+
4+
export const useProseStore = defineStore(
5+
'prose',
6+
() => {
7+
const prose = ref<string>('')
8+
9+
function initProse(val: string) {
10+
if (!prose.value) {
11+
prose.value = ''
12+
}
13+
14+
prose.value = val
15+
}
16+
17+
function clearProse() {
18+
prose.value = ''
19+
}
20+
21+
async function fetchProse() {
22+
const res = await getProse()
23+
initProse(res.result)
24+
}
25+
26+
return {
27+
prose,
28+
initProse,
29+
clearProse,
30+
fetchProse,
31+
}
32+
},
33+
)

i18n/locales/en-US.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"404Demo": "🙅 Page 404 Demo",
88
"unocssExample": "🎨 Unocss example",
99
"keepAlive": "🧡 KeepAlive Demo",
10-
"persistPiniaState": "💾 Persist Pinia State"
10+
"persistPiniaState": "💾 Persist Pinia State",
11+
"fetch": "🌐 Network Request"
1112
},
1213
"tabbar": {
1314
"home": "Home",

i18n/locales/zh-CN.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"404Demo": "🙅 404页 演示",
88
"unocssExample": "🎨 Unocss 示例",
99
"keepAlive": "🧡 KeepAlive 演示",
10-
"persistPiniaState": "💾 持久化 Pinia 状态"
10+
"persistPiniaState": "💾 持久化 Pinia 状态",
11+
"fetch": "🌐 网络请求"
1112
},
1213
"tabbar": {
1314
"home": "主页",

0 commit comments

Comments
 (0)