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

minipro-request

v2.1.2

Published

一个小程序的请求库

Downloads

9

Readme

基于微信小程序封装的请求库

正常场景下, 我们的请求都需要统一配置token以及对请求和响应进行拦截,于是对小程序的请求封装了一下

1.安装下载

npm i minipro-request or yarn add minipro-request

2.引入以及使用

// app.js
import Request, { mixedServe } from 'minipro-request';
// Request可接受默认的参数
const request = new Request({
  timeout: 10000, // 默认配置
});
App({
  onLaunch() {},
  request,
});
// pages/index/index.js
const app = getApp()
Page({
  onLoad() {
    this.getData()
  },
  async getData() {
    const res = await app.request.post({
      url: "/login",
      data: {},
      ...
    })
  }
})

响应和请求拦截

const req = new Request({
  timeout: 10000,
});
req.useRequestInterceptor((config) => {
  config.url = 'http://localhost:8080/' + (config.url || '');
  config.header.Authorization = `Bearer`;
  return config;
});
req.useResponseInterceptor(
  (res) => {
    console.log('响应成功', res);
  },
  (err) => {
    console.log('响应失败', err);
  },
);

3.混入请求

有时候,我们不同的模块,会生成不同的请求文件 | servers --| user.js --| list.js
... 提供了对请求的混入

// services/user.js
class User {
  getUser(data) {
    return this.request.post({
      url: "/login",
      data
    })
  }
}

// app.js
import Request,{ mixedServe } from "minipro-request";
// Request可接受默认的参数
const request = new Request({
  timeout: 10000, // 默认配置
});
const list = ['user','list']
const services = list.map((service) => require(`./services/${service}.js`));
const fetch = mixedServe({
  services,
  request
})
App({
  onLaunch(){},
  request:fetch
})

//pages/index/index.js
const app = getApp()
Page({
  onLoad() {
    this.getData()
  }
  async getData() {
    const res = await app.request.getUser({
      name: '123'
    })
  }
})