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

v0.1.18

Published

基于`umi-request`的网络请求库,返回格式默认是`{code, data, msg}` <br/> 常用方法有:`get` `post` `put` `delete` 等等,并添加了 `form` `update` `download` 三个方法

Downloads

19

Readme

gdin-request

基于umi-request的网络请求库,返回格式默认是{code, data, msg} 常用方法有:get post put delete 等等,并添加了 form update download 三个方法

安装

npm install --save gdin-request
or
yarn add gdin-request

引入

import request from 'gdin-request'

初始化配置项

// 接口前缀, 默认是空
request.base.prefix = '/api';

// 超时设置
request.base.timeout = 12000;

// 请求数据返回格式,default是{code, data, msg}; simple是直接返回data,异常情况在catch中捕获
request.base.dataMode = 'default';

// headers中的鉴权值authorization
request.base.getAuthorization = function() {
  return '';
};

// 拦截错误情况
request.base.interceptError = function(error) {
  return true;
};

或者调用初始化方法初始配置项

request.init({
  prefix: '/api',
  timeout: 12000,
  dataMode: 'simple',
  getAuthorization: () => {
    return 'Basic dGVzdDp0ZXN0';
  },
  interceptError: error => {
    return true;
  },
});

Request 配置项说明:

| 字段名 | 值类型 | 说明 | 默认值 | | ---------------- | -------- | ----------------------------------------------------------------------------------------------- | ---------- | | prefix | string | 接口前缀 | | | timeout | number | 超时设置 | 12000 | | dataMode | string | 请求数据返回格式,default 是{code, data, msg}; simple 是直接返回 data,异常情况在 catch 中捕获 | default | | getAuthorization | function | headers 中的鉴权值 authorization | | | interceptError | function | 拦截错误情况 | () => true |

一般用法

  • content-type 默认是 application/json
  • method:GET、POST、PUT、DELETE、HEAD、OPTION、UPDATE、DOWNLOAD 等等
  • params:会拼接在 url 上面,是 url 参数
  • data:是请求体 body 内容
request('/test/save', { method: 'POST', data: { name: '123' } });
request('/test/list', { params: { area: 1 } });

内置封装方法

  • request.get
  • request.post
  • request.put
  • request.delete
  • request.form
  • request.upload
  • request.download

form 表单提交

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

upload 上传

POST请求

request.upload(url, options);

options 参数说明:

| 字段名 | 类型 | 说明 | | ----------- | -------- | ---------------------------------- | | headers | object | 请求头信息 | | params | object | url 参数 | | data | object | 请求体内容 | | onLoadStart | Function | 请求开始 | | onProgress | Function | 上传进度条监听函数,返回值是 0-100 |

download 下载

默认是GET请求,可在options中添加method指定其他请求类型,其他同 upload