npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

json-api-fetch

v1.1.0

Published

基于fetch实现的JSON API标准

Downloads

11

Readme

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 定义返回指定的数据格式。

不过,我做了一点小的修改:

  1. Content-Type 字段没有使用规定的 application/vnd.api+json,而是使用了 application/json,因为很多安全策略配置的字段默认只有常用的 application/json,前者并没有完全的优势让我们使用它,至少我目前没发现。

  2. 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。今后必然会全面原生支持,没有必要再引入一个过度复杂的库。