Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

我搞了一个类似的请求库 #276

Closed
xdoer opened this issue Jun 20, 2021 · 0 comments
Closed

我搞了一个类似的请求库 #276

xdoer opened this issue Jun 20, 2021 · 0 comments

Comments

@xdoer
Copy link

xdoer commented Jun 20, 2021

PreQuest项目,采用了洋葱模型的中间件设计,设计理念是请求内核与上层相分离,附加功能通过注册中间件来实现,项目中针对接口缓存,错误重试,token 失效刷新等常见的场景提供了开箱即用的解决方案, 使用起来相当灵活。

文档地址在这里: https://pre-quest.vercel.app/

这里有个小程序的 demo:

import { create, Prequest } from '@prequest/miniprogram'

// 全局配置
PreQuest.defaults.baseURL = 'http://localhost:3000'

// 微信小程序,创建实例时,也支持传入参数
const prequest = create(wx.request, { http2: true })

// 传入支付宝小程序请求方法,可以支持支付宝平台
const prequest = create(my.request)

// 百度小程序
const prequest = create(swan.request)

//...其他小程序类似

// 实例中间件
prequest.use(async (ctx, next) => {
    console.log(ctx.request) // 请求的参数
    await next()
    console.log(ctx.response)  // 响应的结果
})

prequest.get('/api', { params: { a: 1 } })

prequest('/api')

不止小程序,像快应用,华为鸿蒙等基本都是支持的。

演示在快应用中使用

import fetch from '@system.fetch'

const prequest = create(fetch)

想知道怎么实现的吗?可以查看源代码

此外,项目中针对 XMLHttpRequest 、Fetch、node 端的 http、类小程序都封装了一套请求库,可以按需使用。

@xdoer xdoer closed this as completed May 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant