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

@prequest/wrapper

v1.5.2

Published

一个可以赋予你定义的请求方法 PreQuest 能力的包装器。

Downloads

45

Readme

包装器

一个可以赋予你定义的请求方法 PreQuest 能力的包装器。

简介

当我们基于 Fetch、XMLHttpRequest 和其他一些原生请求 API 定义一个请求方法,并在项目中大量使用。那么将其切换到 PreQuest 的代价是相对高昂的,这个项目可以很方便的、不侵入的将 PreQuest 的能力赋予到你的请求方法上。

安装

npm install @prequest/wrapper

使用

定义请求方法

function ajax(params) {
  const { path, method, baseURL, ...rest } = params

  // ...some code
}

包装器包装

import { wrapper } from '@prequest/wrapper'

const ajax = wrapper(function(params) {
  const { path, method, baseURL, ...rest } = params

  // some code
})

Enjoy it

你可以像往常一样使用你的方法

ajax({ path: 'http://localhost:1000/api', method: 'GET', params: { a: 'aaa' } })

使用 PreQuest 能力

ajax('http://localhost:1000/api', { method: 'GET', params: { a: 'aaa' } })

ajax.get('http://localhost:1000/api', { params: { a: 'aaa' } })

ajax.use(async (ctx, next) => {
  console.log(ctx.request)
  await next()
  console.log(ctx.response)
})

创建实例

const instance = ajax.create({ baseURL: 'http://localhost:1000' })

instance.get('/api', { params: { a: 'aaa' } })

instance.use(async (ctx, next) => {
  console.log(ctx.request)
  await next()
  console.log(ctx.response)
})

全局配置

import { PreQuest } from '@prequest/wrapper'

PreQuest.defaults.baseURL = 'http://localhost:1000'

PreQuest.use(async (ctx, next) => {
  console.log(ctx.request)
  await next()
  console.log(ctx.response)
})

注意

你定义的方法必须满足几个条件。

只有一个参数

wrapper 的回调函数只支持一个参数,这意味着你只能传入一个对象。

// 不支持
function ajax(path, method, opt)

// 支持
function ajax({ path, method, opt })

混入参数

如果你通过别名的方式调用请求,PreQuest 会向你的参数中混入 pathmethod 参数。

// 混入 `{ path : http://localhost:1000/api }`
ajax('http://localhost:1000/api')

// 混入 `{ path : http://localhost:1000/api, method: post }`
ajax.post('http://localhost:1000/api')

// 混入 `{ path : /api, method: get }`
ajax.get('/api')