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

react-axios-http

v1.4.4

Published

这是一款结合axios和react的封装请求库

Downloads

43

Readme

react-axios-http

这是一款结合axios和react的封装请求库, 主要解决了请求调用处理的统一和接口的统一管理, 以及在项目中的接口便捷调用

使用方式

初始化

// ./http
import http, { combineApi } from "react-axios-http"

const apis = {
  // demo
  get: {
    // get 默认
    url: `/projectManage/show`,
    //余下参数配置支持axios中的参数
  }
  post: {
    // post
    url: `/projectManage/show`,
    method: 'post',
    // isFormData: true 默认false
    // 是否需要以formData形式传参
    // isQuery: true 默认false
    // post请求下,是否以url Query方式 ?pageNum=1&pageSize=10,

    //余下参数配置支持axios中的参数
  },
  put: {
    // put
    url: `/projectManage/show`,
    method: 'put',
    // isFormData: true 默认false
    // 是否需要以formData形式传参
    // isQuery: true 默认false
    // put请求下,是否以url Query方式 ?pageNum=1&pageSize=10

    //余下参数配置支持axios中的参数
  },
  delete: {
    // delete
    url: `/projectManage/show`,
    method: 'delete',

    //余下参数配置支持axios中的参数
  },
  getJsonp: {
    // jsonp
    url: `/projectManage/show`,
    method: 'getJsonp',
  },
};

/* combineApi合并apis,作用域。
const IO = http(combineApi({
  apis_1: apis,
  apis_2: apis,
  apis_3: apis,
},
true/false 默认true,作用域开启
)); */

const IO = http(apis);

// const IO = http(apis, {
//   /* {opt} 默认可不传 */
//   // 高级模式下的数据返回结果处理方式
//   resultMode: 'native',// 可选array, 默认已设置native
//   prefix: '', // 接口统一前缀
//   余下参数配置支持axios中的参数
// });

IO.interceptors.request.use(
  config => {
    console.log(config, 'interceptors request');
    return config;
  },
  error => Promise.reject(error),
);

IO.interceptors.response.use(
  response => {
    console.log(response, 'interceptors response');
    return response;
  },
  error => Promise.reject(error),
);

export default IO;

项目中基本使用方式

// 上面文件
import IO from './http'

const opt = {
  key1: 'value1',
  key2: 'value2',
}

const handler = (res) => {
  // res为返回的结果。
  return res;
}

// IO.getxxx(opt, handler)
// opt是传的参数,不需要传的可不写;
// handler 是对返回结果的二次加工,可不写;

IO.getxxx(opt, handler)
  .then(res => {
    // do something
  });

// 如无参数,直接对返回结果的二次加工
IO.getxxx(handler)
  .then(res => {
    // do something
  });

IO.getxxx().then(res => {
  // do something
});

基本使用说明

  • 默认get请求
  • 导出文件 import IO from './http'
  • 接口请求使用,直接IO.getxxx(),
  • getxxx为apis中一接口名
  • IO.getxxx(opt, handler),()中get和post请求无参数可为(空)
  • handler 是对返回结果的二次加工,可不写;
  • opt 此参数为post和get请求参数,类型皆为对象,如
  • IO.getxxx({
  • key: value,
  • key: value,
  • }, handler)

具体使用请参考 example目录;

高级使用方式

模仿react-redux方式

// 父组件或者根组件
import { ProviderApi } from "react-axios-http";
import apis from './http'
import Child from './Child'

ReactDOM.render(
    <ProviderApi apis={apis}>
        <Child />
    </ProviderApi>)
  document.getElementById("container")
);

//子组件

import React, { Component } from 'react'
import { connectApi } from "react-axios-http";

// 高级处理模式1 resultMode = 'native';默认

// 支持装饰器写法
// 1、@connectApi
// 2、@connectApi('apis_1')
// 3、@connectApi(Child,'apis_1')
class Child extends Component {

  // 使用方式1
  getFetch = async () => {
    const { postxxx } = this.props;
    const res = await postxxx(/*对象参数*/, /* handler 二次加工函数,可不写 */);
    // 如无参数,直接对返回结果的二次加工
    // const res = await postxxx(/* handler 二次加工函数,可不写 */);
    //to do something
  }
  // 使用方式2
  getFetch = () => {
    const { postxxx } = this.props;
    postxxx(/*对象参数*/, /* handler 二次加工函数,可不写 */)
    // 如无参数,直接对返回结果的二次加工
    // postxxx(/* handler 二次加工函数,可不写 */)
    .then(res => {
      //to do something
    });
  }

  render() {
    return (
      <div onClick={this.getFetch}>
      </div>
    )
  }
}

export default connectApi(Child);

// 高级处理模式2 resultMode = 'array,
// 返回一个数组,错误优先处理方式
class Child extends Component {

  // 使用方式1
  getFetch = async () => {
    const { postxxx } = this.props;
    const [err, res] = await postxxx(/*对象参数*/, /* handler 二次加工函数,可不写 */);
    // 如无参数,直接对返回结果的二次加工
    // const [err, res] = await postxxx(/* handler 二次加工函数,可不写 */);
    // err参数不为空则说明请求出现请求异常或者错误
    if (!err) {
      //to do something
    }
  }
  // 使用方式2
  getFetch = () => {
    const { postxxx } = this.props;
    postxxx(/*对象参数*/, /* handler 二次加工函数,可不写 */)
    .then(([err, res]) => {
      if (!err) {
        //to do something
      }
    });
  }

  render() {
    return (
      <div onClick={this.getFetch}>
      </div>
    )
  }
}

export default connectApi(Child);