tinyjx v3.1.2
tinyjx
tinyjx is a tiny http client for browser.
Summary
- Tree shaking friendly
- Error tracking friendly
- Small file size (with tree shaking only 5kb minified and 2kb gzip)
- Xhr pool
- Types supported
- IE 10+
Install
$ npm i -P tinyjxUsage
import { ajax } from 'tinyjx';
ajax({
url: 'http://127.0.0.1/test',
method: 'post',
data: {
hello: world
},
success(data, xhr, event) {
console.log(data);
},
complete(xhr, status) {
console.log(status);
}
})API
ajax(options: AsyncOptions): Abortable
Returns a Abortable object which implemented a abort() method like xhr.abort().
AsyncOptions: Object
url:<string>, URL to requestmethod:<string>, HTTP method, case insensitive, default"GET", onlyGET,HEAD,POST,PUT,PATCH,DELETE,OPTIONSsupported.contentType:<string>, MIME type for request body, also support predefined valuejson,form,html,xml,text, which areapplication/json,application/x-www-form-urlencoded,text/html,application/xml,text/plain, defaultjsondataType:<string>, MIME type expect from ther server, will be treat asAccept, defaultjsonheaders: request headers, which is a key-value object. eg.{'Content-Type': 'text/plain'}cache: allow browser to cache responses, defaulttrueresponseType: specifies thexhr.responseTypemimeType: specifies the parameters ofxhr.overrideMimeType()withCredentials: specifies thexhr.withCredentialstimeout: specifies thexhr.timeoutusername: user name to use for authenticationpassword: password to use for authenticationevents: events of XMLHttpRequest, except ofonerrorandonreadystatechange, which will be overriden by tinyjx. eg.{ onprogress(e) {}, onload(e) {} }uploadEvents: events of XMLHttpRequestUploadbeforeSend(xhr, options):<Function>, before the request is sent.optionsisAsyncOptions. Returnfalsewill cancel the requestsuccess(data, xhr, event):<Function>, when request succeedserror(err, xhr, event):<Function>, when error occurredcomplete(xhr, status):<Function>, aftersuccess()orerror(), note ifsuccess()orerror()throws an error,complete()will not be calledontimeout(event):<Function>, similar withxhr.ontimeout, butthisis nullserialize:<Function>, specifies a serialize method for this request, returns an object which containsurlanddata.optionscontains:data: raw data inAsyncOptions/SyncOptionsurl:<string>, request URLmethod:<string>, HTTP method of requestcontentType:<string>, MIME type ofdatacache:<boolean>,cacheinAsyncOptions/SyncOptions, allow browser to cache responses
ajax({
url: 'http://127.0.0.1:8080/ajax',
data: {
a: 1,
b: 2
},
serialize({data, method, contentType, url, cache}) {
url += '?' + Object.keys(data).map(k => `${encodeURIComponent(k)}=${encodeURIComponent(data[k])}`).join('&');
return {
url,
data: {
hello: 'world'
}
};
}
});
// The request URL will be http://127.0.0.1:8080/ajax?a=1&b=2
// with body {"hello": "world"}deserialize:<Function>, specifies a deserialize method for all responses, returns anything what you want which will be thedataofsuccesscallback.optionscontains:data: raw data in response, may bexhr.responseXML,xhr.responseorxhr.responseTextcontentType:Content-Typeheader of responseacceptType:Acceptof request
ajax({
url: 'http://127.0.0.1:8080/ajax',
deserialize({data, contentType, acceptType}) {
console.log(data); // {"hello": "world"}
console.log(contentType);
console.log(acceptType);
return 'data changed';
},
success(data, xhr, e) {
console.log(data); // data changed
}
});jsonp(options: JsonpOptions): void
Returns undefined.
JsonpOptions: Object
url: URL to requestcache:<boolean>, allow browser to cache responses, defaultfalsecrossorigin:crossoriginattribute of<script>, default isn't setcallbackName:<string>, custom callback name, defaultjsonp${randomId}beforeSend(url, options):<Function>, before the request is sent.optionsisJsonpOptions. Returnfalsewill cancel the requestsuccess(args):<Function>, when request succeedserror(err, event):<Function>, when error occurredcomplete(status):<Function>, aftersuccess()orerror(), note ifsuccess()orerror()throws an error,complete()will not be called
config(options: ConfigOptions): void
Returns undefined.
ConfigOptions: Object
pool:<number> | <boolean>, specifies the size of xhr pool, defaultfalse, will not use xhr pool. Iftrue, default size is 5serialize(options):<Function>, specifies a serialize method for all requests, returns an object which containsurlanddata.optionscontains:data: raw data inAsyncOptions/SyncOptionsurl:<string>, request URLmethod:<string>, HTTP method of requestcontentType:<string>, MIME type ofdatacache:<boolean>,cacheinAsyncOptions/SyncOptions, allow browser to cache responses
deserialize(options):<Function>, specifies a deserialize method for all responses, returns anything what you want which will be thedataofsuccesscallback.optionscontains:data: raw data in response, may bexhr.responseXML,xhr.responseorxhr.responseTextcontentType:Content-Typeheader of responseacceptType:Acceptof request
get(url: string, options: AsyncOptions): Abortable
A GET request wrapper of ajax().
head(url: string, options: AsyncOptions): Abortable
A HEAD request wrapper of ajax().
post(url: string, data: any, options: AsyncOptions): Abortable
A POST request wrapper of ajax().
put(url: string, data: any, options: AsyncOptions): Abortable
A PUT request wrapper of ajax().
patch(url: string, data: any, options: AsyncOptions): Abortable
A PATCH request wrapper of ajax().
del(url: string, data: any, options: AsyncOptions): Abortable
A DELETE request wrapper of ajax().
options(url: string, data: any, options: AsyncOptions): Abortable
A OPTIONS request wrapper of ajax().
TODO
- More test
License
Licensed under the MIT License.
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago