Play as much as you want with me and get a nice server.
Plasticine is designed by a font-end developper for front-end developpers. It supplies a simple API to fake some requests. It can be used when dvelopping a new feature without a ready back-end. Just define the API the server will use, create your mock and start to develop!
The library is define as UMD module (see amdWeb). It can be injected as an AMD module or added in the JavascriptCode and define the global variable Plasticine.
Here an example to play with Plasticine:
Plasticine.addMock({
route: '/info.json',
get: function() {
return {status: 200, body: {message: 'Hello world!'}};
}
});With this sample, any request GET on route info.json is faked (no request is send). The response of the request is defined by the get callback.
params is an object with those keys:
route: define the request route to catch. It can have parameters, syntax define by crossroads.js. This parameter is mandatory.get: callback to determine a GET request fake response. Callback get route variables as parameters and return an object with keysstatusandbody:
Plasticine.addMock({
route: '/messages/{id}.json',
get: function(route_params) {
if (route_params.id === '1')
return {status: 200, body: {message: 'Hello world!'}};
else
return {status: 404, body: {error: 'unknown message'}};
}
});delete: same asgetbut on a DELETE request.post: same asgetbut on a POST request and callback second parameter has request payload:
Plasticine.addMock({
route: '/messages/{id}.json',
post: function(route_params, data) {
data.id = Math.round(Math.random()*1000000000)
return {status: 200, body: data};
}
});put: same aspostbut on a PUT request.patch: same aspostbut on a PATCH request.afterGet: callback to modify a request before notifying it's loaded. The callback get an object whith keysstatus,headersandbodywhich it can change to affect the request response:
Plasticine.addMock({
route: '/messages/{id}.json',
afterGet: function (request) {
// add key starred to introduce a new feature which is under development by backend
request.body.starred = Math.random() < 0.5 ? true : false;
});afterDelete: same asafterGetbut on a DELETE request.afterPost: same asafterGetbut on a POST request.afterPut: same asafterGetbut on a PUT request.afterPatch: same asafterGetbut on a PATCH request.
Calling this method makes the Mock return by Plasticine.addMock() not to intercept requests anymore.
All dependencies are include in the library:
- Library dependencies
- Almond: to load library modules
- Crossroads.js: to define route and parse request url
- Lodash: AMD utilities
- Sinon.js: to intercept requests and to test the library
- Development dependencies
- Configure custom delay
- Chrome extention to mixed up faked and not faked requests