@mosuzi/tiny-ajax

1.0.0 • Public • Published

Tiny Ajax

A very simple ajax.

Install

npm install --save @mosuzi/tiny-ajax

Usage

import TinyAjax from '@mosuzi/tiny-ajax'

const data = { id: 'fakeId' }

// async
const result = TinyAjax.ajax('get', '<api address>', data)
result.then((resp) => {
  // do business actions
})

// sync
const result = TinyAjax.ajax('get', '<api address>', data, { async: false })
// do business actions

resp.responseData or result.responseData is parsed data

Api

ajax()

ajax: (method: string, url: string, params: any, { timeout, async, ignoreRequestInterceptors, ignoreResponseInterceptors }: {
  timeout: number;
  async: boolean;
  ignoreRequestInterceptors: boolean;
  ignoreResponseInterceptors: boolean;
}) => any;
  • method: Request method, such as get
  • url: Request url
  • params: Request data
  • options
    • timeout: Request timeout. Ignored when async sets false
    • async: Indicates if the request is asynchronous. Defaults to true. When set to false, ajax() return response, otherwise return a promise
    • ignoreRequestInterceptors: Indicates if ignore request interceptors
    • ignoreResponseInterceptors: Indicates if ignore response interceptors

addInterceptorsRequest()

addInterceptorsRequest: (name: string, action: (e: any) => any) => void;
  • name: Interceptor name
  • action: Interceptor

addInterceptorsResponse()

addInterceptorsResponse: (name: string, action: (e: any) => any) => void;
  • name: Interceptor name
  • action: Interceptor

setAjaxConfig()

setAjaxConfig: ({ baseUrl, timeout }?: AjaxConfig) => void;
  • Params: See Type/AjaxConfig

getAjaxConfig()

getAjaxConfig: () => AjaxConfig;
  • Return value: See Type/AjaxConfig

Type

AjaxConfig

declare type AjaxConfig = {
  baseUrl?: string
  timeout?: number
}
  • baseUrl: Prefix of request url. When sending request, final url equals to baseUrl splices url which is specified by ajax()
  • timeout: Default value of request timeout

Readme

Keywords

none

Package Sidebar

Install

npm i @mosuzi/tiny-ajax

Weekly Downloads

0

Version

1.0.0

License

MIT

Unpacked Size

9.13 kB

Total Files

5

Last publish

Collaborators

  • mosuzi