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

m-http

v2.0.0

Published

A library for HTTP request helper on node.

Downloads

7

Readme

http-helper

A library for HTTP request helper on node.

import MHttp from 'm-http'
const http = new MHttp()

http.get('https://api.github.com', {
  headers: {
    Accept: 'application/vnd.github.v3+json',
    'User-Agent': 'Test-App'
  }
}).then(res => {
  console.log(res.data)
})

API

new MHttp(options) 创建一个请求对象http

  • options object 可选 配置对象
  • options.urlEncode boolean 是否编码url,默认true

http

  • http.fetch(options) 创建一个请求,返回 Promise 对象;成功会resolve IncomingMessage 对象,IncomingMessage.body 返回响应 Buffer 数据。

    • options object 必须,请求参数配置
    • options.url string 必须,请求url
    • options.method string 可选,请求类型,默认 GET
    • options.timeout string
    • options.headers object 可选,请求头设置,key/val
    • options.auth string
    • options.agent string
    • options.body object|string|FormData 可选,含有body(POST/PUT/PATCH/DELETE)的提交,key/val
    • options.params object 可选,构造查询参数,key/val
  • http.get(url, options) 创建GET请求

  • http.delete(url, options) 创建DELETE请求

  • http.head(url, options) 创建HEAD请求

  • http.post(url, options) 创建POST请求

  • http.put(url, options) 创建PUT请求

  • http.patch(url, options) 创建PATCH请求

Events

start

开始发起请求,参数: options

end

响应结束,参数:'IncomingMessage'

error

请求过程存在错误

upload

multipart/form-data 请求时触发(每项数据上传会触发一次)

interceptors

可以增加中间件处理一些请求前后的操作。

中间件必须调用 next() 才可继续下一步。

请求中间件

http.request.use(function(options, next) {
  // do
  next()
})

响应中间件

http.response.use(function(res, next) {
  // do
  // res.data = JSON.parse(res.body.toString())
  next()
})

application/x-www-form-urlencoded

具有body体的请求, 默认 Content-Typeapplication/x-www-form-urlencoded

http.post('http://localhost:3000/api/all', {
  params: {
    page: 1,
    pageSize: 12,
    zh: '中文'
  },
  body: {
    username: 'admin',
    password: '123456'
  }
}).then(res => {
  console.log('response:', res.body.toString())
}).catch(err => {
  console.log('response err:', err)
})

application/json

如果想要提交json格式数据,需要指定 Content-Typeapplication/json

http.post('http://localhost:3000/api/all', {
  headers: {
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({...data})
}).then(res => {
  console.log('response:', res.body.toString())
}).catch(err => {
  console.log('response err:', err)
})

multipart/form-data

上传文件时,并不需要指定 Content-Typemultipart/form-data,默认会判断 options.body 对象类型是否为 FormData 来设置。

import MHttp from 'm-http'
import FormData from 'm-http/form-data'
import File from 'm-http/file'

const http = new MHttp()
const form = new FormData()

form.append('key', 'value')
form.append('username', 'admin')
form.append('password', '123456')
form.append('userId', 1001)
form.append('zh', '中文')
form.append('data', JSON.stringify({name: 'admin', info: 'this is a test.', zh: '中文'}))
form.append('text', new File({name: 'text.txt', buffer: Buffer.from('this is a test\r\n这是一个测试。')}))
form.append('zip', new File({filePath: './test.zip'}))

console.log(form)

http.post('http://localhost:3000/api/file', {
  body: form
}).then(res => {
  console.log('response:', res.body.toString())
}).catch(err => {
  console.log('response err:', err)
})

Other

FormData

import FormData from 'm-http/form-data'

用于构造 multipart/form-data 数据。

const form = new FormData()
form.append('key', 'val')
// ...

File

import File from 'm-http/file'

用于定义文件的对象。

new File({name: 'text.txt', buffer: Buffer.from('this is a test\r\n这是一个测试。')}