1.1.11 • Published 1 year ago

requex v1.1.11

Weekly downloads
3
License
MIT
Repository
github
Last release
1 year ago

简体中文 | English

Requex

A HTTP component based on axios, with loading spinner based on NProgress. Could be used to customize error on business level and simplify the HTTP request flow in your project.

Getting Started

# Install
$ yarn add requex
// Examples
import createInstance from 'requex';

// [createInstance] used to create a request instance.
// The parameters are separated into [axios configs] and [request options]
// The first parameter is the [axios config] which is  used to configure the global axios features same as axios.create()
const request = createInstance({
  withCredentials: true,
  headers: {
    Accept: 'application/json',
    'X-Requested-With': 'XMLHttpRequest',
  },
});

// The second parameter is the [request options] which is used to configure the global request features.
const request = createInstance(
  {
    // Some kinds of the response should be defined as error even it's network response code is between 200 and 300. [isSuccess] is the certain parameter to defined the response pattern.
    // e.g. Only response which data.code equal to '200000' or '200100' could be treated as successful.
    isSuccess: data => {
      const { code } = data;
      return ['200000', '200100'].includes(code);
    },
  },
  {
    // The callback function on a success response.
    // Usually used to display a success message with a UI framework.
    onSuccess: data => {
      const { message } = data;
      Message.success(message);
    },
    // The callback function on a error response.
    // Status between 200 and 300 will also involved if this response is defined as error by [isSuccess].
    onFail: (data, status, config) => {
      if (status === 401) {
        // handle different kinds of network return code.
      } else if (status >= 200 && status < 300) {
      } else {
      }
    },
  },
);

// The [axios configs] and [request options] both could be overrided in request instance
const response = await request(
    {
    url: '/api/v1',
    method: 'POST',
    data: { a: 1, b: 2 },
    }, 
    {
        onSuccess: data => {
            const { message } = data;
            AnotherMessage.success(message);
        }
    },
);

// extraData is same as axios.data
const request = createInstance({});
const request = await request(
  {
    url: '/api/v1',
    method: 'POST',
  },
  {
    extraData: { c: 1, d: 2 },
  },
);

// axios.data has higher priority than extraData.
// { a: 1, b: 2 } will be sended.
const request = createInstance({});
const request = await request(
  {
    url: '/api/v1',
    method: 'POST',
    data: { a: 1, b: 2 },
  },
  {
    extraData: { c: 1, d: 2 },
  },
);

// axios.data and extraData will be used as url parameters when match the pattern ':param'.
// url will be '/api/v1/1/2' and data will be { e: 3 }
const request = createInstance({});
const request = await request(
  {
    url: '/api/v1/:c/:d',
    method: 'POST',
  },
  {
    extraData: { c: 1, d: 2, e: 3 },
  },
);

Request Options

ParameterTypeDescriptionDefault
isSuccess(data:any): booleana condition used to define a response as successful response.() => true
confirmTextstringthe confirm text when return a new page.'Jump to the target page?'
showSpinbooleanwhether show the loading spinner.true
getContainer(): HTMLElementParent node which the loading spinner should be rendered to.() => document.getElementById('root')

Extra Request Options

ParameterTypeDescriptionDefault
beforeRequest(requestId:number): voida callback function executed before a request. Usually used to open a loading spinner with an UI framework.() => {}
afterRequest(requestId:number): voida callback function executed after a request. Usually used to close a loading spinner with an UI framework.() => {}
onSuccess(data: any, status: number, config: AxiosRequestConfig): voida callback function executed on a successful response. Usually used to save response data or display a message with an UI framework.() => {}
onFail(data: any, status: number, config: AxiosRequestConfig): voida callback function executed on a error response. Usually used to do error handling or display message with an UI framework.() => {}
extraDataobjectsame as axios.data with lower priority.{}
1.1.11

1 year ago

1.1.9

2 years ago

2.0.3

2 years ago

2.0.2

2 years ago

2.0.4

2 years ago

1.1.10

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago

1.1.8

3 years ago

1.1.7

4 years ago

1.1.6

4 years ago

1.1.5

4 years ago

1.1.4

4 years ago

1.1.3

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago