A request tool based on fetch.
English | 简体中文
For more discussion, refer to Traditional Ajax is dead, Fetch eternal life If you have good suggestions and needs, please mention issue
npm install --save umi-request
Performing a GET request
import request from 'umi-request';
request
.get('/api/v1/xxx?id=1')
.then(function(response) {
console.log(response);
})
.catch(function(error) {
console.log(error);
});
// use options.params
request
.get('/api/v1/xxx', {
params: {
id: 1,
},
})
.then(function(response) {
console.log(response);
})
.catch(function(error) {
console.log(error);
});
Performing a POST request
request
.post('/api/v1/user', {
data: {
name: 'Mike',
},
})
.then(function(response) {
console.log(response);
})
.catch(function(error) {
console.log(error);
});
Requests can be made by passing relevant options to umi-request
umi-request(url[, options])
import request from 'umi-request';
request('/api/v1/xxx', {
method: 'get',
params: { id: 1 },
})
.then(function(response) {
console.log(response);
})
.catch(function(error) {
console.log(error);
});
request('/api/v1/user', {
method: 'post',
data: {
name: 'Mike',
},
})
.then(function(response) {
console.log(response);
})
.catch(function(error) {
console.log(error);
});
For convenience umi-request have been provided for all supported methods.
request.get(url[, options])
request.post(url[, options])
request.delete(url[, options])
request.put(url[, options])
request.patch(url[, options])
request.head(url[, options])
request.options(url[, options])
You can use extend({[options]}) to create a new instance of umi-request.
extend([options])
import { extend } from 'umi-request';
const request = extend({
prefix: '/api/v1',
timeout: 1000,
headers: {
'Content-Type': 'multipart/form-data',
},
});
request
.get('/user')
.then(function(response) {
console.log(response);
})
.catch(function(error) {
console.log(error);
});
Create an instance of umi-request in NodeJS enviroment
const umi = require('umi-request');
const extendRequest = umi.extend({ timeout: 10000 });
extendRequest('/api/user')
.then(res => {
console.log(res);
})
.catch(err => {
console.log(err);
});
The available instance methods are list below. The specified options will be merge with the instance options.
request.get(url[, options])
request.post(url[, options])
request.delete(url[, options])
request.put(url[, options])
request.patch(url[, options])
request.head(url[, options])
request.options(url[, options])
More umi-request cases can see antd-pro
timeout first
string
--
--
prefix
prefix, generally used to override the uniform settings prefix
string
--
--
suffix
suffix, such as some scenes api need to be unified .json
string
--
credentials
fetch request with cookies
string
--
credentials: 'same-origin'
useCache
Whether to use caching (only support browser environment)
boolean
--
false
validateCache
cache strategy function
(url, options) => boolean
--
only get request to cache
ttl
Cache duration, 0 is not expired
number
--
60000
maxCache
Maximum number of caches
number
--
0(Infinity)
requestType
post request data type
string
json , form
json
parseResponse
response processing simplification
boolean
--
true
charset
character set
string
utf8 , gbk
utf8
responseType
How to parse the returned data
string
json , text , blob , formData ...
json , text
throwErrIfParseFail
throw error when JSON parse fail and responseType is 'json'
boolean
--
false
getResponse
Whether to get the source response, the result will wrap a layer
boolean
--
fasle
errorHandler
exception handling, or override unified exception handling
function(error)
--
cancelToken
Token to cancel request
CancelToken.token
--
--
The other parameters of fetch are valid. See fetch documentation
…
Sometimes we need to update options after extend a request instance, umi-request provide extendOptions for users to update options:
const request = extend({ timeout: 1000, params: { a: '1' } });
// default options is: { timeout: 1000, params: { a: '1' }}
request.extendOptions({ timeout: 3000, params: { b: '2' } });
// after extendOptions: { timeout: 3000, params: { a: '1', b: '2' }}
The response for a request contains the following information.
{
// 'data' is the response that was provided by the server
data: {},
// 'status' is the HTTP status code from the server response
status: 200,
// 'statusText' is the HTTP status message from the server response
statusText: 'OK',
// 'headers' the headers that the server responded with
// All header names are lower cased
headers: {},
}
When options.getResponse === false, the response schema would be 'data'
request.get('/api/v1/xxx', { getResponse: false }).then(function(data) {
console.log(data);
});
When options.getResponse === true ,the response schema would be { data, response }
request.get('/api/v1/xxx', { getResponse: true }).then(function({ data, response }) {
console.log(data);
console.log(response.status);
console.log(response.statusText);
console.log(response.headers);
});
You can get Response from error object in errorHandler or request.catch.
…
Expressive HTTP middleware framework for node.js. For development to enhance before and after request. Support create instance, global, core middlewares.
Instance Middleware (default) request.use(fn) Different instances's
No open issues yet, or sync has not completed.