Skip to content

Commit e9faf1f

Browse files
ts
1 parent d80e2fc commit e9faf1f

File tree

20 files changed

+1656
-1565
lines changed

20 files changed

+1656
-1565
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import axios, { AxiosError } from '../../src/index'
2+
3+
axios({
4+
method: 'get',
5+
url: '/error/get1'
6+
}).then((res) => {
7+
console.log(res)
8+
}).catch((e) => {
9+
console.log(e)
10+
})
11+
12+
axios({
13+
method: 'get',
14+
url: '/error/get'
15+
}).then((res) => {
16+
console.log(res)
17+
}).catch((e) => {
18+
console.log(e)
19+
})
20+
21+
setTimeout(() => {
22+
axios({
23+
method: 'get',
24+
url: '/error/get'
25+
}).then((res) => {
26+
console.log(res)
27+
}).catch((e) => {
28+
console.log(e)
29+
})
30+
}, 5000)
31+
32+
axios({
33+
method: 'get',
34+
url: '/error/timeout',
35+
timeout: 2000
36+
}).then((res) => {
37+
console.log(res)
38+
}).catch((e) => {
39+
console.log(e.message)
40+
})
41+
42+
axios({
43+
method: 'get',
44+
url: '/error/timeout',
45+
timeout: 2000
46+
}).then((res) => {
47+
console.log(res)
48+
}).catch((e: AxiosError) => {
49+
console.log(e.message)
50+
console.log(e.code)
51+
})
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Error example</title>
6+
</head>
7+
<body>
8+
<script src="/__build__/error.js"></script>
9+
</body>
10+
</html>
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import axios from '../../src/index'
2+
3+
axios({
4+
url: '/extend/post',
5+
method: 'post',
6+
data: {
7+
msg: 'hi'
8+
}
9+
})
10+
11+
axios.request({
12+
url: '/extend/post',
13+
method: 'post',
14+
data: {
15+
msg: 'hello'
16+
}
17+
})
18+
19+
axios.get('/extend/get')
20+
21+
axios.options('/extend/options')
22+
23+
axios.delete('/extend/delete')
24+
25+
axios.head('/extend/head')
26+
27+
axios.post('/extend/post', { msg: 'post' })
28+
29+
axios.put('/extend/put', { msg: 'put' })
30+
31+
axios.patch('/extend/patch', { msg: 'patch' })
32+
33+
axios({
34+
url: '/extend/post',
35+
method: 'post',
36+
data: {
37+
msg: 'hi'
38+
}
39+
})
40+
41+
axios('/extend/post', {
42+
method: 'post',
43+
data: {
44+
msg: 'hello'
45+
}
46+
})
47+
48+
interface ResponseData<T = any> {
49+
code: number
50+
result: T
51+
message: string
52+
}
53+
54+
interface User {
55+
name: string
56+
age: number
57+
}
58+
59+
function getUser<T>() {
60+
return axios<ResponseData<T>>('/extend/user')
61+
.then(res => res.data)
62+
.catch(err => console.error(err))
63+
}
64+
65+
66+
async function test() {
67+
const user = await getUser<User>()
68+
if (user) {
69+
console.log(user.result.name)
70+
}
71+
}
72+
73+
test()
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Extend example</title>
6+
</head>
7+
<body>
8+
<script src="/__build__/extend.js"></script>
9+
</body>
10+
</html>

18-TypeScript/axios/ts-axios/examples/index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ <h1>ts-axios examples</h1>
1010
<ul>
1111
<li><a href="simple">Simple</a></li>
1212
<li><a href="base">Base</a></li>
13+
<li><a href="error">Error</a></li>
14+
<li><a href="extend">Extend</a></li>
1315
</ul>
1416
</body>
1517
</html>

18-TypeScript/axios/ts-axios/examples/server.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,23 @@ router.post('/base/buffer', function(req, res) {
5757
})
5858
})
5959

60+
router.get('/error/get', function(req, res) {
61+
if (Math.random() > 0.5) {
62+
res.json({
63+
msg: `hello world`
64+
})
65+
} else {
66+
res.status(500)
67+
res.end()
68+
}
69+
})
70+
71+
router.get('/error/timeout', function(req, res) {
72+
setTimeout(() => {
73+
res.json({
74+
msg: `hello world`
75+
})
76+
}, 3000)
77+
})
78+
6079
app.use(router)

0 commit comments

Comments
 (0)