1.1.0 • Published 26 days ago
json-api-fetch v1.1.0
json-api-fetch
install
npm install json-api-fetch
Getting started
import { createInstance } from 'json-api-fetch'
const request = createInstance({
baseUrl: 'http://example.com',
errorInterceptor: (jsonResponse) => {
if (jsonResponse.error.status === 401) {
// login
}
}
})
request
.get('/something')
.then((res) => {
console.log(res.data);
})
.catch((errRes) => {
console.log(errRes.error);
})
说明
该库使用的是fetch,所以如果要在低版本浏览器运行,你可能仍然需要引入 whatwg-fetch。
作用
主要是为了统一前后端 restful api 的返回格式标准,所以后端需要按照 https://jsonapi.org 定义返回指定的数据格式。
不过,我做了一点小的修改:
Content-Type 字段没有使用规定的 application/vnd.api+json,而是使用了 application/json,因为很多安全策略配置的字段默认只有常用的 application/json,前者并没有完全的优势让我们使用它,至少我目前没发现。
errors 返回是一个对象而非数组。往往接口出现一个错误,后端没必要往下走,前端也应该及时给出提示。而非文档中的例子,这种场景基本不会出现:
{
"errors": [
{
"status": "403",
"source": { "pointer": "/data/attributes/secretPowers" },
"detail": "Editing secret powers is not authorized on Sundays."
},
{
"status": "422",
"source": { "pointer": "/data/attributes/volume" },
"detail": "Volume does not, in fact, go to 11."
},
{
"status": "500",
"source": { "pointer": "/data/attributes/reputation" },
"title": "The backend responded with an error",
"detail": "Reputation service not responding after three requests."
}
]
}
为什么不用axios?
因为 fetch 已经足够好,而且是 es 标准里的api。今后必然会全面原生支持,没有必要再引入一个过度复杂的库。
1.1.0
26 days ago
1.0.5
26 days ago
1.0.4
26 days ago
1.0.3
3 months ago
1.0.2
4 months ago
1.0.1
5 months ago
1.0.0
5 months ago
0.7.0
11 months ago
0.6.0
11 months ago
0.5.0
2 years ago
0.5.2
2 years ago
0.5.1
2 years ago
0.4.1
2 years ago
0.4.0
4 years ago
0.3.8
4 years ago
0.3.7
4 years ago
0.3.6
4 years ago
0.3.5
4 years ago
0.3.4
4 years ago
0.3.2
4 years ago
0.3.3
4 years ago
0.3.1
4 years ago
0.3.0
4 years ago
0.2.1
4 years ago
0.1.1
4 years ago
0.1.0
4 years ago
0.0.2
4 years ago
0.0.1
4 years ago