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

@shene/wx-request

v0.2.0

Published

## 安装

Downloads

6

Readme

@shene/wx-request

安装

使用 npm:

$ npm install @shene/wx-request

使用 yarn:

$ yarn add @shene/wx-request

须知

  1. 需要开启 使用npm模块
  2. 需要开启 增强编译
  3. 安装后 需要构建 npm
  4. 此包是为了在像 web 中使用 axios 库一样的方式在小程序中发送网络请求,建议二次封装
  5. post 请求并且 config 配置 upload=true 会走 wx.uploadFile 进行上传文件,data 中必须含有 filePath 字段

API

import request from '@shene/wx-request';

request(config)

request({
  method: 'post',
  url: '/api/user/1111',
  data: {},
});

request(url[, config])

request('/api/user/1111');

请求方法别名

为了方便起见,已为所有受支持的请求方法提供了别名。

request.request(config)
request.get(url[, config])
request.delete(url[, config])
request.head(url[, config])
request.options(url[, config])
request.post(url[, data[, config]])
request.put(url[, data[, config]])
request.patch(url[, data[, config]])

注意

使用别名方法时,url、method、data 无需在 config 中配置。

创建一个实例

您可以使用自定义配置创建 request 的新实例。

request.create([config])
const instance = request.create({
  baseURL: 'https://api.example.com',
  timeout: 1000,
  headers: { 'X-Custom-Header': 'custom' },
});

实例

可用的实例方法在下面列出。指定的配置将与实例配置合并。

request#request(config)
request#get(url[, config])
request#delete(url[, config])
request#head(url[, config])
request#options(url[, config])
request#post(url[, data[, config]])
request#put(url[, data[, config]])
request#patch(url[, data[, config]])
request#getUri([config])

请求配置

请求配置除了wx.request的参数(不包括方法),还有其他参数,除了url为必须配置的参数,其他可使用默认参数,method默认为get.

{
  url: '/user',
  method: 'get', // default
  baseURL: 'https://api.example.com',

  transformRequest: [function (data, headers) {
    return data;
  }],

  transformResponse: [function (data) {
    return data;
  }],

  headers: {'X-Requested-With': 'XMLHttpRequest'},

  params: {
    ID: '11111'
  },
  data: {
    firstName: 'Fred'
  },
  timeout: 60000,// default
  adapter: function (config) {
    /* ... */
  },
  responseType: 'json', // default
  cancelToken: new CancelToken(function (cancel) {
  }),
}

响应信息

{
  data: {},
  cookies: [],
  status: 200,
  statusText: 'request.ok',
  headers: {},
  config: {},
  request: {}
}

使用 then 方式

request.get('/api/user/1111').then(function (response) {
  console.log(response.data);
  console.log(response.status);
  console.log(response.statusText);
  console.log(response.headers);
  console.log(response.config);
});

配置默认值

您可以指定将应用于每个请求的配置默认值。

全局 request 默认值

request.defaults.baseURL = 'https://api.example.com';
request.defaults.headers.common['Authorization'] = AUTH_TOKEN;
request.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';

自定义实例默认值

const instance = request.create({
  baseURL: 'https://api.example.com',
});

instance.defaults.headers.common['Authorization'] = AUTH_TOKEN;

配置优先级

const instance = request.create();

instance.defaults.timeout = 2500;

instance.get('/longRequest', {
  timeout: 5000,
});

拦截器

您可以先拦截请求或响应,然后再由 then 或处理 catch。

request.interceptors.request.use(
  function (config) {
    return config;
  },
  function (error) {
    return Promise.reject(error);
  }
);

request.interceptors.response.use(
  function (response) {
    return response;
  },
  function (error) {
    return Promise.reject(error);
  }
);

错误处理

request.get('/api/user/1111').catch(function (error) {
  if (error.response) {
    console.log(error.response.data);
    console.log(error.response.status);
    console.log(error.response.headers);
  } else if (error.request) {
    console.log(error.request);
  } else {
    console.log('Error', error.message);
  }
  console.log(error.config);
});

取消请求

您可以使用取消令牌取消请求。

const CancelToken = request.CancelToken;
const source = CancelToken.source();

request
  .get('/api/user/1111', {
    cancelToken: source.token,
  })
  .catch(function (thrown) {
    if (request.isCancel(thrown)) {
      console.log('Request canceled', thrown.message);
    } else {
    }
  });

request.post(
  '/api/user/1111',
  {
    name: 'new name',
  },
  {
    cancelToken: source.token,
  }
);

source.cancel('message');
const CancelToken = request.CancelToken;
let cancel;

request.get('/api/user/1111', {
  cancelToken: new CancelToken(function executor(c) {
    cancel = c;
  }),
});

cancel();

License

MIT