Git Product home page Git Product logo

chili-request's Introduction

Welcome to chili-request 👋

Version License: MIT

简介

chili-request 使用 axois 来发送 http 请求,但尝试解决了 axios 里使用存在的几个缺陷:

  • axios 的 get/post 请求传参方式不一致
  • axios 没有明确的针对于每一个接口的响应数据拦截器

同时,chili-request 专注于解决 http 请求配置分散到项目各个角落的问题,实现在一个目录下管理所有 http 请求配置。

chili-request 专注于解决的另一个问题是:无法定义请求传参类型和响应数据类型。

安装

npm install chili-request

or

yarn chili-request

案例

  1. 全局配置(与 axios 配置基本一致)
import chiliReqBase from 'chili-request';
export default chiliReqBase({
  baseURL: config.apiUrl,
  interceptorReq: (request: any) => {
    ...
    // 请求发送前拦截器,可以对请求头进行统一处理。比如 携带自定义的 accessToken
    // 将处理后的请求头返回
    return request;
  },
  interceptorRes: (response: any) => {
    ...
    // 对获取到的响应数据进行统一处理
    // 比如对登录错误或其他类型错误进行统一处理
    return res
  }
});
  1. 书写 http 请求配置
// 响应数据类型/接口描述
export interface Res {
  [key:string]:any
}

// login 函数参数是接口请求调用传参的类型/接口描述
export default function login(data: { name: string; pwd: string }) {
  return {
    option: { 
      method: "GET", // http method
      url: "/login", // relative url
      data // params(get/post/delete/ 等一致)
    }
  };
}
  1. 使用 chiliReqlogin 发送请求
chiliReq<Res>(login(name: "", pwd: "")).then(res => {
  // res 的类型即 Res
}).catch() => {

}

基于 chili-request 实现的 restful-api 前端接口管理实现的目录一般如下图: 接口目录

支持

Give a ⭐️ if this project helped you!


This README was generated with ❤️ by readme-md-generator

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.