Git Product home page Git Product logo

prequest's Introduction

PreQuest

一套模块化,可插拔的 JS 运行时 HTTP 请求解决方案。

npm Minzipped size NPM Downloads code style: prettier Codacy Badge GitHub license

简介

PreQuest 是一套 JS 运行时的 HTTP 解决方案。

它包含了一些针对不同 JS 运行平台的封装的请求库,并为这些请求库提供了一致的中间件、拦截器、全局配置等功能的体验

还针对诸如 Token 的添加,失效处理,无感知更新、接口缓存、错误重试等常见业务场景,提供了解决方案。

当这些功能不满足你的需求、或者你想定制化时,也可以基于 @prequest/core 快速封装一个实现你需求的请求库。

支持平台

Web、小程序、ReactNative、快应用等

使用示例

下面以 @prequest/xhr 为例,介绍使用方法

import { PreQuest, create } from '@prequest/xhr'
import timeoutMiddleware from '@prequest/timeout'

// 全局配置
PreQuest.defaults.baseURL = 'http://locahost:8080'
// 项目中如果有设置 header 的需求,建议先初始化有个空对象
PreQuest.defaults.headers = {}

// 全局中间件
PreQuest.use((ctx, next) => {
  // 在这里,你可以全局修改请求参数
  console.log(ctx.request)

  await next()

  // 这里,你可以全局修改响应数据
  console.log(ctx.response)
})

// 创建实例
const prequest = create({
  headers: {
    'Content-Type': 'application/json; charset=utf-8',
  },
})

// 实例中间件
prequest.use(timeoutMiddleware)

// 请求调用
prequest('/user')
prequest('/user', { method: 'POST' })
prequest.get('/user', { params: { a: 1 } })
prequest.post('/user', { data: { a: 1 } })

// 响应类型注入
interface User {}
const user = await prequest<User>('/user', { params: { id: 1 } })
const user2 = await prequest.get<User>('/user', { params: { id: 1 } })

文档

website: https://pre-quest.vercel.app

贡献

Contributing Guide

License

MIT License

prequest's People

Contributors

xdoer avatar bingtsingw avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.