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

liyinggan-http

v1.0.0

Published

微信小程序 [wx.request](https://developers.weixin.qq.com/miniprogram/dev/api/network/request/wx.request.html) 和 [wx.uploadFile](https://developers.weixin.qq.com/miniprogram/dev/api/network/upload/wx.uploadFile.html) 请求的扩展,支持 Promise、拦截器和 TypeScript。

Downloads

3

Readme

liyingan-http v1.0.2

微信小程序 wx.requestwx.uploadFile 请求的扩展,支持 Promise、拦截器和 TypeScript。

安装

npm install liyingan-http

使用说明

import http from 'liyingan-http'

// 接口基础地址
http.baseURL = 'https://your.host.com'

// 请求拦截器
http.intercept.request = (options) => {
  // 合并头信息
  options.header = {
    // 权限认证
    Authorization: 'Bearer token'
    // 默认头信息
    ...options.header
  }
  // 拦截器处理后的请求参数
  return options
}

// 响应拦截器
http.intercept.response = (result) => {
  console.log(result.statusCode) // http 响应状态码
  console.log(result.config) // 发起请求时的参数
  // 拦截器处理后的响应结果
  return result.data
}

// 作为模块导出
export default http

// 也可挂载到 wx 全局命名空间
wx.http = http

快捷方法

// GET 方法请求
http.get(url, data?, config?)

// POST 方法请求
http.post(url, data?, config?)

// PUT 方法请求
http.put(url, data?, config?)

// DELETE 方法请求
http.delete(url, data?, config?)

// 调用 wx.uploadFile 上传文件
http.upload(url, data?, config?)

示例

Page({
  async onLoad() {
    // request 网络请求 - 普通用法
    await wx.http({ url: '/path', method: 'POST' })
    // 或快捷方法
    await wx.http.get('/path')


    // 选择文件
    const { tempFiles } = await wx.chooseMedia({ count: 1 })
    // uploadFile 上传文件 - 普通用法
    await wx.http({
      url: '/path',
      method: 'UPLOAD', // 调用 wx.uploadFile
      name: 'file', // 文件的 key , 服务器端通过 key 获取文件二进制内容
      filePath: tempFiles[0].tempFilePath, // 文件的 value , 要上传文件资源的路径(本地路径)
      formData: {}, // HTTP 请求中其他额外的 form data
    })
    // 或快捷方法
    await wx.http.upload('/path', {
      name: 'file', // 文件的 key
      filePath: tempFiles[0]tempFilePath, // 文件的 value
      formData: {}, // // HTTP 请求中其他额外的 form data
    })
  },
})

TypeScript 支持

// global.d.ts
import type { Http } from 'wechat-http'

// 扩展到全局对象 wx 中调用
declare global {
  namespace WechatMiniprogram {
    interface Wx {
      http: Http
    }
  }
}

// 返回数据的类型 - <用户自定义业务接口类型>
declare module 'wechat-http' {
  export interface ResponseResultData<T = any> {
    code: number
    message: string
    data: T
  }
}