Skip to content

Commit 0bdd1e9

Browse files
author
Simon he
committed
chore: update vFetch
1 parent bb98e4d commit 0bdd1e9

File tree

10 files changed

+270
-102
lines changed

10 files changed

+270
-102
lines changed

README.md

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,39 @@
3131
- isSymbol // 判断是否是Symbol
3232
- isNaN // 判断是否是NaN
3333
- isReg // 判断是否是正则表达式
34-
- VFetch // fetch请求封装 export interface IFetch {
35-
url: string
34+
- vFetch // 基于fetch的axios api式请求封装 export interface VFetch {
35+
url?: string
36+
baseURL?: string
37+
body?: any
3638
method?: Method
3739
headers?: Record<string, string>
3840
credentials?: Credentials
3941
params?: Record<string, string>
4042
timeout?: number
41-
returnType?: ReturnType
43+
responseType?: ResponseType
4244
bodyType?: BodyType
4345
cache?: Cache
4446
redirect?: Redirect
4547
mode?: Mode
46-
firstThen?: (response: Response) => Response
48+
result?: Promise<any>
49+
transformResponse?: (response: Response) => Response
50+
set?: (target: keyof VFetch, value: Record<string, string>) => void
51+
bodyToString?: () => string
52+
request?: () => Promise<Response>
53+
then?: (resolve: (value: any) => void, reject: (reason: any) => void) => Promise<void>
54+
create?: (options: IFetchOptions) => (options: VFetch) => VFetch
55+
interceptors?: {
56+
request: {
57+
use: (successCallback?: ((response: Response) => Response), errorCallback?: ((error: any) => Promise<never>)) => void
58+
success: (response: Response) => Response
59+
error: (error: any) => Promise<any>
60+
},
61+
response: {
62+
use: (successCallback?: ((response: Response) => Response), errorCallback?: ((error: any) => Promise<never>)) => void
63+
success: (response: Response) => Response
64+
error: (error: any) => Promise<any>
65+
}
66+
}
4767
}
4868
- interceptError // 自动捕获传入函数执行的异常
4969

@@ -73,4 +93,6 @@
7393
![traverse](assets/traverse.png)
7494
### transformKey
7595
![transformKey](assets/transformKey.png)
96+
### vFetch
97+
![vFetch](assets/vFetch.png)
7698

assets/vFetch.png

30.6 KB
Loading

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"dev": "pkgroll --watch",
3838
"play": "pnpm run -C playground dev",
3939
"play:build": "pnpm run -C playground build",
40+
"serve": "pnpm run -C playground serve",
4041
"lint": "eslint .",
4142
"lint:fix": "eslint . --fix",
4243
"typecheck": "vue-tsc --noEmit",
@@ -61,5 +62,8 @@
6162
"simon-js-tool": "workspace:^1.0.8",
6263
"typescript": "^4.7.2",
6364
"vitest": "^0.14.2"
65+
},
66+
"eslintConfig": {
67+
"extends": "@antfu"
6468
}
6569
}

playground/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"lint": "eslint .",
99
"typecheck": "vue-tsc --noEmit",
1010
"preview": "vite preview",
11-
"test": "vitest"
11+
"test": "vitest",
12+
"serve": "node ./server/index.js"
1213
},
1314
"dependencies": {
1415
"@vueuse/core": "^8.1.1",

playground/server/index.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const express = require('express')
2+
const app = express()
3+
app.all('*', (req, res, next) => {
4+
// 设置允许跨域的域名,*代表允许任意域名跨域
5+
res.header('Access-Control-Allow-Origin', '*')
6+
// 允许的header类型
7+
res.header('Access-Control-Allow-Headers', 'content-type')
8+
// 跨域允许的请求方式
9+
res.header('Access-Control-Allow-Methods', 'DELETE,PUT,POST,GET,OPTIONS')
10+
if (req.method.toLowerCase() === 'options')
11+
res.send(200) // 让options尝试请求快速结束
12+
else
13+
next()
14+
})
15+
16+
app.get('/test', (req, res) => {
17+
res.json({
18+
code: 300,
19+
body: {
20+
name: 'simon',
21+
},
22+
})
23+
})
24+
25+
app.listen(5001, () => {
26+
// console.log('服务器启动5001')
27+
})

playground/src/main.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { createApp } from 'vue'
22
import { createRouter, createWebHistory } from 'vue-router'
33
import routes from 'virtual:generated-pages'
4+
import { vFetch } from '../../src'
45
import App from './App.vue'
56

67
import '@unocss/reset/tailwind.css'
@@ -14,3 +15,26 @@ const router = createRouter({
1415
})
1516
app.use(router)
1617
app.mount('#app')
18+
19+
vFetch.interceptors.request.use((response) => {
20+
// console.log('request success', response)
21+
return response
22+
})
23+
const instance = vFetch.create({
24+
baseURL: 'http://localhost:5001/',
25+
})
26+
27+
instance({
28+
url: 'test',
29+
}).then((res: any) => {
30+
// console.log(res)
31+
return res
32+
})
33+
34+
// vFetch.get({
35+
// url: "http://localhost:5001/t"
36+
// }).then(res => {
37+
// console.log(res)
38+
// }, err => {
39+
// console.log(err)
40+
// })

pnpm-lock.yaml

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)