Fluent extensible ajax framework.
GET http://my.api/todos?limit=50
jacks().get("http://my.api/todos")
.query("limit", "50")
.header("Accepts", "application/json")
.send(function(jacksResponse) {
// Callback success
}, function(jacksError) {
// Callback error
});
POST
jacks().post("http://my.api/todos")
.body({"title": "Finish the job",
"date": "2015/12/31"})
.header("Content-Type", "application/json")
.send(function(jacksResponse) {
// Callback success
}, function(jacksError) {
// Callback error
});
- Download the latest release.
- Clone the repo:
git clone https://github.com/jccazeaux/jacks.git
. - Install with npm:
npm install jacks-js
.
This creates a new instance of jacks. This instance will be an empty shell and the base to create requests. Each instance will have its own configuration (see plugins and use). So you can have many instances of jacks, each using different sorts of configuration. In your application you should keep the instances in a context to avoid a recreation each time you need it.
A jacks instance is only the beginning, to start a request use the methods described below.
Creates a GET JacksRequest with the url
Creates a POST JacksRequest with the url
Creates a PUT JacksRequest with the url
Creates a DELETE JacksRequest with the url
Creates a OPTIONS JacksRequest with the url
Creates a HEAD JacksRequest with the url
You can add a plugin to jacks using the plugin() function on jacks (not on request)
jacks.plugin("pluginName", function(jacksRequest) {
// Access to all request methods
});
A plugin is a function wich receives the request as parameter.
Use a plugin. Parameter can be the name of a declared plugin or a function for a live plugin
jacks().use("myPlugin")
.use(function(jacksRequest) {
// Plugin code
})
This plugin will be used for all requests.
Jacks comes with a mock API.
jacks.mock(request, response);
Request defines wich requests are mocked. It contains two attributes
- url : url of mocked resource. Can be a String or a regular expression
- method : mocked method. If not specifed, all methods will be mocked
Mocked response to send
- responseText : Text of the response
- response : function callback to create a dynamic responseText. The function takes as only parameter a "done" callback. Use it if your callback is asynchronous.
- headers : response headers
- status : status code
- statusText : status text
- error : mock an error
- delay : simulates a delayed response. In ms.
jacks.mock({
url: "/myurl"
}, {
responseText: "My mocked response",
headers: {"Content-Type": "plain/text"}
});
jacks.mock({
url: "/myurl"
}, {
response: function() {
this.responseText = "My dynamic mock";
},
headers: {"Content-Type": "plain/text"}
});
jacks.mock({
url: "/myurl"
}, {
response: function(done) {
var that = this;
setTimeout(function() {
that.responseText = "My dynamic mock";
done();
}, 0);
},
headers: {"Content-Type": "plain/text"}
});
Sets the body. Only for POST and PUT
jacks().post("http://myurl")
.body({"age": 35, "city": "Bordeaux"})
.send(function(jacksReponse) {
// Callback success
}, function(jacksError) {
// Callback error
});
Add query parameters. Will concatenate the query parameters to the URL.
jacks().get("http://myurl")
.query("age", "35")
.query({"age": 35, "city": "Bordeaux"})
.send(function(jacksReponse) {
// Callback success
}, function(jacksError) {
// Callback error
});
Add a request header.
jacks().get("http://myurl")
.header("X-MY-HEADER", "foo")
Use a plugin. Parameter can be the name of a declared plugin or a function for a live plugin
jacks().get("http://myurl")
.use("myPlugin")
.use(function(jacksRequest) {
// Plugin code
})
The plugin will be used for this request only.
Register a callback for an event on the request.
Available events
- timeout : when the request has been aborted due to a timeout
- abort : when the request has been aborted
- loadstart : XMLHttpRequest loadstart event
- loadend : XMLHttpRequest loadend event
- progress : XMLHttpRequest progress event
- upload-progress : XMLHttpRequest upload progress event
Aborts the request.
Switches the request to synchronous mode. By default it's asynchronous.
Defines a timeout for the request. After the delay (in ms) the request will be aborted.
Sends the request.
Sends the request as binary.
The callback function takes JacksResponse object as parameter witch contains
{
status : <http status code>,
statusText : <http status text>,
responseText : <raw response body>,
response : <parsed response body. The parser is selected with Content-Type header. If no parser is found, will contain the raw response body>,
headers : <response headers>,
getHeader(name): <function to get one header. Delegates it to xhr>
}
The error function takes a JacksError as parameter with contains
{
url : <url called>
type : <error type>
}
Type will be
- "timeout" if request has been interrupted after a timeout
- "abort" if request has been aborted