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 tinyjx
Usage
;
API
ajax(options: AjaxOptions): Abortable
Returns a Abortable
object which implemented a abort()
method like xhr.abort()
.
AjaxOptions: Object
-
url
:<string>
, URL to request -
method
:<string>
, HTTP method, case insensitive, default"GET"
, onlyGET
,HEAD
,POST
,PUT
,PATCH
,DELETE
,OPTIONS
supported. -
data
:<any>
, request data, used byPOST
,PUT
,PATCH
-
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
, defaultjson
-
dataType
:<string>
, MIME type expect from the remote server, will be treat asAccept
, defaultjson
-
processData
:<boolean>
, like jQuery, default istrue
-
headers
: request headers, which is a key-value object. eg.{'Content-Type': 'text/plain'}
-
mimeType
: specifies the parameters ofxhr.overrideMimeType()
-
username
: user name to use for authentication -
password
: password to use for authentication -
events
: events of XMLHttpRequest, except ofonerror
andonreadystatechange
, which will be overriden by tinyjx. eg.{}{} -
uploadEvents
: events of XMLHttpRequestUpload -
cache
: allow browser to cache responses, defaulttrue
-
responseType
: specifies thexhr.responseType
-
timeout
: specifies thexhr.timeout
-
withCredentials
: specifies thexhr.withCredentials
-
beforeSend(xhr, options)
:<Function>
, before the request is sent.options
isAjaxOptions
. Returnfalse
will cancel the request -
success(respData, xhr, event)
:<Function>
, when request succeeds -
recoverableError(err, respData, xhr, event)
:<Function>
, when request error occurred, such as response status is 404, 500, which means browser received the response, but an error occurred from remote server -
unrecoverableError(err, xhr, event)
:<Function>
, different fromrecoverableError
, which means request is failed or browser didn't receive any response, such as cross origin error or network error -
complete(xhr, status)
:<Function>
, aftersuccess()
orerror()
, note ifsuccess()
orerror()
throws an error,complete()
will not be called -
ontimeout(event)
:<Function>
, similar withxhr.ontimeout
, butthis
is null -
serialize
:<Function>
, specifies a serialize method for this request, returns an object which containsurl
anddata
.options
contains:data
: raw data(data
) inAjaxOptions
url
:<string>
, request URLmethod
:<string>
, HTTP method of requestcontentType
:<string>
, MIME type ofdata
cache
:<boolean>
,cache
inAjaxOptions
, allow browser to cache response
;// 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 thedata
ofsuccess
callback.options
contains:data
: raw data in response, may bexhr.responseXML
,xhr.response
orxhr.responseText
contentType
:Content-Type
header of responseacceptType
:Accept
of request
;
jsonp(options: JsonpOptions): void
Returns undefined
.
JsonpOptions: Object
url
: URL to requestcache
:<boolean>
, allow browser to cache responses, defaultfalse
crossorigin
:crossorigin
attribute of<script>
, default isn't setcallbackName
:<string>
, custom callback name, defaultjsonp${randomId}
beforeSend(url, options)
:<Function>
, before the request is sent.options
isJsonpOptions
. Returnfalse
will 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 containsurl
anddata
.options
contains:data
: raw data inAjaxOptions
url
:<string>
, request URLmethod
:<string>
, HTTP method of requestcontentType
:<string>
, MIME type ofdata
cache
:<boolean>
,cache
inAjaxOptions
, allow browser to cache responses
deserialize(options)
:<Function>
, specifies a deserialize method for all responses, returns anything what you want which will be thedata
ofsuccess
callback.options
contains:data
: raw data in response, may bexhr.responseXML
,xhr.response
orxhr.responseText
contentType
:Content-Type
header of responseacceptType
:Accept
of request
get(url: string, options: AjaxOptions): Abortable
A GET
request wrapper of ajax()
.
head(url: string, options: AjaxOptions): Abortable
A HEAD
request wrapper of ajax()
.
post(url: string, data: any, options: AjaxOptions): Abortable
A POST
request wrapper of ajax()
.
put(url: string, data: any, options: AjaxOptions): Abortable
A PUT
request wrapper of ajax()
.
patch(url: string, data: any, options: AjaxOptions): Abortable
A PATCH
request wrapper of ajax()
.
del(url: string, data: any, options: AjaxOptions): Abortable
A DELETE
request wrapper of ajax()
.
options(url: string, data: any, options: AjaxOptions): Abortable
A OPTIONS
request wrapper of ajax()
.
TODO
- More test
License
Licensed under the MIT License.