Axios + 标准化错误 + 请求/响应转换。
[!重要]
仅提供社区支持:Apisauce 已不再积极维护,尽管 Infinite Red 会发布社区提交的安全补丁。我们不建议在新项目中使用它。请直接使用 Axios,并自行构建封装,或寻找其他维护更好的封装。
(Ring ring ring)
< Hello?
> Hi, can I speak to JSON API.
< Speaking.
> Hi, it's me JavaScript. Look, we need to talk.
< Now is not a good time...
> Wait, I just wanted to say, sorry.
< ...与 API 交流不再尴尬。
axios HTTP 客户端库的轻量级封装problem 属性,有助于引导异常流程npm i apisauce --save 或 yarn add apisauce
axios。// showLastCommitMessageForThisLibrary.js
import { create } from 'apisauce'
// define the api
const api = create({
baseURL: 'https://api.github.com',
headers: { Accept: 'application/vnd.github.v3+json' },
})
// start making calls
api
.get('/repos/skellock/apisauce/commits')
.then(response => response.data[0].commit.message)
.then(console.log)
// customizing headers per-request
api.post('/users', { name: 'steve' }, { headers: { 'x-gigawatts': '1.21' } })更多代码示例请参阅 examples 文件夹。
你通过调用 .create() 并传入一个配置对象来创建 API。
const api = create({ baseURL: 'https://api.github.com' })唯一必需的属性是 baseURL,它应该是你 API 的起始点。它也可以包含子路径和端口。
const api = create({ baseURL: 'https://example.com/api/v3' })所有请求的 HTTP 请求头也可以包含在内。
const api = create({
baseURL: '...',
headers: {
'X-API-KEY':暂无开放 Issues,或尚未同步最近议题。