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

@gdin/request

v1.0.6

Published

基于axios的网络请求库

Downloads

16

Readme

@gdin/request 网络请求库

基于axios的网络请求库,返回格式默认是{code, data, msg}

常用方法有:get post put delete 等等,并添加了 form update download 三个方法

安装

yarn add @gdin/request

使用

用法同axios,只是对post put patch 有所修改

import request from '@gdin/request';

# 默认
request.defaults.timeout = 12000;
request.defaults.withCredentials = true;

# 业务方设置全局配置
request.defaults.baseURL = '/api';
request.defaults.dataMode = 'simple';
request.defaults.headers.common.Authorization = 'Basic c3RhcnRlcjpzdGFydGVyLXNlY3JldA==';

#......

axiosdefaults以及config扩展了一个属性dataMode,用来设置返回值格式:

| 值 | 说明 | | ------- | ------------------------------------------------------------------------- | | default | 默认,会把数据封装成 { code: number, data: Data, msg: string } 格式 | | simple | 只返回 data 数据 | | all | 后端接口返回什么就是什么 |

其他配置项请前往 [ Axios Config ] 查看

覆盖修改

针对 post put patch 这三个方法的参数,做了些微调整,例如:

原 post 方法是:axios.post(url[, data[, config]]),请求体参数data是作为第二个参数放置的, 现将data合并进config里面,改为两个参数即可。

即:axios.post(url[, data[, config]]) => request.post(url[, config])

  • 例子:
request.post('/user/save', {
  data: {
    username: 'test',
    name: '测试'
  }
})

扩展方法

  • request.form(url[, config])
  • request.upload(url[, config])
  • request.download(url[, config])

form 表单提交

是 POST 请求,并自动将请求头的中 content-type 改为application/x-www-form-urlencoded

upload 上传

POST请求,默认不设置请求超时时间,即timeout = 0,可配置onUploadProgress监听上传进度

download 下载

默认是GET请求,可在config中添加method指定其他请求类型,默认不设置请求超时时间,即timeout = 0,可配置onDownloadProgress监听下载进度

其他说明

关于在请求头设置或者更新登录凭证Authorization的值,可在登录成功后将数据存储到localStorage,存储的key要求是:

  • SYSTEM_TOKEN: Authorization 的值
  • SYSTEM_TOKEN_EXPIRE: 过期时间(毫秒)

每个请求再发起时都会在拦截器 axios.interceptors.request 里从localStorage提取数据设置Authorization的值。

例子:

request.form('/login', {data}).then((res) => {
  const { access_token, token_type, expires_in } = res;
  # 保存token值
  window.localStorage.setItem('SYSTEM_TOKEN',`${token_type} ${access_token}`);
  # 保存token过期时间
  window.localStorage.setItem('SYSTEM_TOKEN_EXPIRE',`${Date.now() + expires_in * 1000}`);
});