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

@doddle/http

v3.0.15

Published

`Http`是请求类`class`, 实际使用的时候,需要创建一个实例化的对象, 可通过以下两种方式创建新的实例

Downloads

13

Readme

如何创建 http 实例

Http是请求类class, 实际使用的时候,需要创建一个实例化的对象, 可通过以下两种方式创建新的实例

  • Http.create(config)
  • new Http(config)

两种创建都默认会添加 defaults middlewares`配置.

//在应用系统先创建一个基础的http实例,其他的http再通过实例的create方法来扩展
http.create('domain');

config 参数

| 参数 | 说明 | 类型 | 默认值 | | -------------- | ---------------------------------------------- | -------- | ------ | | servers | 支持的服务域名对象 | object | {} | | query | 统一查询字符串 | function | -- | | contentKey | 响应数据中业务数据对应的key | string | '' | | bodyParam | fetch init 参数设置(like: mode,credentials...) | object | {} | | beforeRequest | 请求发起前自定义中间件集合 | array | [] | | beforeResponse | 请求响应前自定义中间件集合 | array | [] |

如何发送ajax请求

通过上述方法创建实例后,可通过实例上的get以及post等方法发送ajax请求

// url请求的路径,data请求的数据,options参数
get(url, data, options);
post(url, data, options);

// example

// util/http.js
import Http from '@doddle/http';

// 创建base http实例
export default Http.create({
  servers: getEnvServers(), // 必传
  query() {
    return {
      token: cookie.get('token'),
    };
  },
});

// servers/admin.js
import http from 'utils/http';

const { get, post } = http.create('admin');

export function getUserList(params) {
  return get('/get/user/list', params);
}

export function saveUser(user) {
  return post('/save/user', user);
}

export function deleteUser(id) {
  return get('/save/user', { id }, { ignoreErrorModal: true });
}

options 参数

| 参数 | 说明 | 类型 | 默认值 | | ----------- | -------------------------------------------------- | ------- | -------------- | | ignoreQuery | 是否忽略携带 query 参数 | boolean | false | | type | contentType 参数设置, 支持 form, formData, json | string | form | | headers | fetch init 参数的 headers 设置 | {} | 继承构造函数的 | | [others] | fetch init 参数其他设置(like: mode,credentials...) | -- | 继承构造函数的 |

HTTP 中间件

http 的核心是通过中间件以及配置对象实现,可通过在 http 实例构造时,在 beforeQuest 或 beforeResponse 中设置,也可通过 http.use(middleware, order, replaceCount)中设置;

// 默认中间件, 也是其依次处理的顺序
  addRequestDomain,
  addRequestQuery,
  fetchRequest,
  responseStatusHandle,
  responseContentHandle,
  • requestDomain
    domain 中间件,发送请求后,自动给url加上对应的server地址, 需要配合servers使用

  • requestQuery
    查询字符串中间件,主要用于添加统一的权限字符串,需要配合query参数使用

  • fetchRequest
    fetch 发起,核心

  • responseStatusHandle
    响应状态码中间件,如果状态码status >= 200 && status < 300 则代表请求成功,否则将请求结果设置为失败

  • responseContentHandle 依据 contentKey,自动将响应中数据对应的 key,作为最后的响应结果

设计思路

边看边写:基于 Fetch 仿洋葱模型写一个 Http 构造类

项目 demo