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

daigo

v1.2.0

Published

A WeChat Mini Program Request Library

Downloads

5

Readme

daigo

npm version npm downloads npm license

一个轻量级微信小程序请求库。

安装

通过 npm:

npm install daigo

用例

发起一个 GET 请求:

const daigo = require('daigo');

// 向给定 id 的用户发起请求
daigo
  .get('/user?id=12345')
  .then(function (response) {
    // 处理成功情况
    console.log(response);
  })
  .catch(function (error) {
    // 处理错误情况
    console.log(error);
  })
  .finally(function () {
    // 总是会执行
  });

// 上述请求也可以按以下方式完成(可选)
daigo
  .get('/user', {
    data: {
      id: 12345,
    },
  })
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  })
  .finally(function () {
    // always executed
  });

// 支持 async/await 用法
async function getUser() {
  try {
    const response = await daigo.get('/user?id=12345');
    console.log(response);
  } catch (error) {
    console.error(error);
  }
}

发起一个 POST 请求:

daigo
  .post('/user', {
    data: {
      firstName: 'Fred',
      lastName: 'Flintstone',
    },
  })
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });

API

可以向 daigo 传递相关配置来创建请求:

daigo({
  method: 'POST',
  url: '/user',
  data: {
    firstName: 'Fred',
    lastName: 'Flintstone',
  },
});

请求方式别名

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

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

创建一个实例

您可以使用自定义配置创建一个实例:

const instance = daigo.create({
  baseURL: 'https://api.example.com',
  timeout: 6000,
  header: { 'X-Custom-Header': 'foobar' },
});

请求配置

这些是创建请求时可用的配置项,并且完全兼容小程序请求配置:

{
  // baseURL 将自动加在 url 前面,除非 url 是一个绝对 URL
  baseURL: 'https://api.example.com',

  // HTTP 请求方法,默认值为 GET
  method: 'GET',

  // 开发者服务器接口地址
  url: '/user',

  // 超时时间,单位为毫秒,默认值为 60000
  timeout: 6000,

  // 自定义请求头
  header: { 'X-Custom-Header': 'foobar' },

  // 请求的参数
  data: {
    firstName: 'Fred'
  }
}

默认配置

您可以指定默认配置,它将作用于每个请求:

daigo.defaults.baseURL = 'https://api.example.com';
daigo.defaults.timeout = 10000;

自定义实例默认值:

// 创建实例时配置默认值
const instance = daigo.create({
  baseURL: 'https://api.example.com',
});

// 创建实例后修改默认值
instance.defaults.timeout = 10000;

配置会按照优先级进行合并,请求的配置参数优先级是最高的:

// 使用库提供的默认配置创建实例
const instance = daigo.create();

// 重写库的超时默认值
instance.defaults.timeout = 10000;

// 重写此请求的超时时间
instance.get('/query', {
  timeout: 5000,
});

拦截器

在请求或响应被 then 或 catch 处理前拦截它们:

// 添加请求拦截器
daigo.interceptors.request.use(
  (config) => {
    // 在发送请求之前做些什么
    return config;
  },
  (error) => {
    // 对请求错误做些什么
    return Promise.reject(error);
  }
);

// 添加响应拦截器
daigo.interceptors.response.use(
  (response) => {
    // 在响应数据做些什么
    return response;
  },
  (error) => {
    // 对响应错误做些什么
    return Promise.reject(error);
  }
);

如果你稍后需要移除拦截器,可以这样:

const interceptor = daigo.interceptors.request.use(function () {
  /*...*/
});

daigo.interceptors.request.eject(interceptor);

取消请求

您可以使用 CancelToken 取消一个请求:

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

daigo.get('/query', {
  cancelToken: source.token,
});

// 取消请求 (message 参数是可选的)
source.cancel('Operation canceled by the user.');

上传文件

您可以通过将 uploadFile 属性设置为 true 来上传一个文件。

daigo({
  uploadFile: true,
  url: 'https://example.weixin.qq.com/upload', // 仅为示例,非真实的接口地址
  filePath: file.url,
  name: 'file',
  formData: { user: 'test' },
  onProgressUpdate: (progress) => {
    console.log(progress);
  },
});

下载文件

您可以通过将 downloadFile 属性设置为 true 来下载一个文件。

daigo({
  downloadFile: true,
  url: 'https://example.com/audio/123', // 仅为示例,非真实的接口地址
  onProgressUpdate: (progress) => {
    console.log(progress);
  },
});