Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add axios request test #14

Merged
merged 10 commits into from
Jan 15, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/website/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
NODE_ENV=production
VITE_APP_ROOT=https://api.bgm.tv
1 change: 1 addition & 0 deletions packages/website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"preview": "vite preview"
},
"dependencies": {
"axios": "^0.24.0",
"react": "^17.0.2",
"react-dom": "^17.0.2"
},
Expand Down
29 changes: 27 additions & 2 deletions packages/website/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import React, { useState, FC } from 'react'
import React, { useState, FC, useEffect } from 'react'
import { Button } from '@bangumi/design'
import style from './App.module.css'

import { getCharacterDetail } from './api/character'
import { CharacterDetail } from './types/common'
const App: FC = () => {
const [count, setCount] = useState(0)
const [detail, setDetail] = useState<CharacterDetail|null>(null)
useEffect(() => {
getCharacterDetail(39115).then(res => {
shadowdreamer marked this conversation as resolved.
Show resolved Hide resolved
setDetail(res.data)
})
}, [])
return (
<div className={style.main}>
<div className={style.count}>
Expand All @@ -16,6 +23,24 @@ const App: FC = () => {
Increase
</Button>
<Button disabled>Disabled</Button>
{ detail
? <div>
<h1>{detail.name}</h1>
{
detail.infobox.map(el =>
<tr key={el.key}>
<td>{el.key}</td>
<tr>
{ Array.isArray(el.value)
? el.value.map(val => <p key={val.k}>{`${val.k}:${val.v}`}</p>)
: el.value }
</tr>
</tr>
)
}
<p>{detail.summary}</p>
</div>
: <div>loading</div>}
</div>
)
}
Expand Down
13 changes: 13 additions & 0 deletions packages/website/src/api/character.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { request } from './request'
import { CharacterDetail } from '../types/common'
import { AxiosPromise } from 'axios'

const api = {
getCharacterDetail: '/v0/characters/'
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

个人觉得可以不用这个 api,直接写到方法上就好。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

有的时候可以复用,路径多了也方便管理

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

又发邮件警报了 我删掉了🤣


type CharacterId = string|number;
shadowdreamer marked this conversation as resolved.
Show resolved Hide resolved

export const getCharacterDetail = (characterId:CharacterId):AxiosPromise<CharacterDetail> => {
return request(api.getCharacterDetail + characterId)
shadowdreamer marked this conversation as resolved.
Show resolved Hide resolved
}
24 changes: 24 additions & 0 deletions packages/website/src/api/request.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import axios from 'axios'

const baseURL = 'https://api.bgm.tv'
export const request = axios.create({
// baseURL: import.meta.env.VITE_APP_ROOT as string,
// import.meta无法通过jest https://github.com/facebook/jest/issues/4842 可能需要改一下jest配置
baseURL,
timeout: 6000 // 请求超时时间
})

// 异常拦截处理器
const errorHandler = (error:any) => {
console.log(error)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
console.log(error)

console.log 好像不太必要

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

请求异常先log一下,以后再补异常处理


return Promise.reject(error)
}

request.interceptors.request.use(config => {
return config
}, errorHandler)

request.interceptors.response.use((response) => {
return response
}, errorHandler)
31 changes: 31 additions & 0 deletions packages/website/src/types/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
type infoBox = {
key:string,
value:string | {
k:string
v:string
}[]
}[]

export type CharacterDetail={
id: number
name: string
type: number
images: {
large: string
medium: string
small: string
grid: string
},
summary: string
locked: boolean
infobox: infoBox
gender: string
blooType: number
birthYear: number
birthMon: number
birthDay: number
stat: {
comments: number
collects: number
}
}
20 changes: 20 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.