A very simple ajax.
npm install --save @mosuzi/tiny-ajaximport 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 actionsresp.responseData or result.responseData is parsed data
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
asyncsets 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
- timeout: Request timeout. Ignored when
addInterceptorsRequest: (name: string, action: (e: any) => any) => void;- name: Interceptor name
- action: Interceptor
addInterceptorsResponse: (name: string, action: (e: any) => any) => void;- name: Interceptor name
- action: Interceptor
setAjaxConfig: ({ baseUrl, timeout }?: AjaxConfig) => void;- Params: See
Type/AjaxConfig
getAjaxConfig: () => AjaxConfig;- Return value: See
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