Generates json api endpoints from templates
Inspired by mock-json-api and relies heavily on dummy-json
When you want to test your app with a json endpoint that is randomly generated from a template.
What it does is it generates "random" json responses based on a template. This allows you to test your front-end client with different backend data that still coheres to your api.
Each time you restart the server, the endpoint will give you a new generated response.
You create your desired json structure by writing handlebars template files that will generate the json response (using dummy-json)
See example
Multiple endpoints can be defined, just create more template files and put them in your specified template folder.
$ npm install json-api-generator --save-dev
The exported function takes a map with the following keys:
The directory where you keep your template files.
A map of generator helpers. Functions that will be available in your template file. Some helpers are available out of the box, see (helpers)[#helpers]
A boolean specifying whether to log each request in the terminal. Defaults to false.
Which address the server should listen to, default to localhost
Which port the server should be run on, defaults to 1989.
A boolean specifying if the browser should be opened to the first endpoint automatically when running the server.
Helpers are functions that can generate random data that can be used in the template. In addition to the helpers available here I have added a few others
Helper | Description |
---|---|
imageUrl |
Sends a random image url from lorempixel. |
text |
Lorem ipsum text |
randomFloat |
Random decimal number |
In this example we will create a GeoJSON response.
A full repo with this example can be found here: geojson-generator
An example of a resulting json is found here: geojson-example
"use strict";
var mock = require("json-api-generator");
mock({
// We keep our templates in a directory called templates
templateDir: './templates/',
// We create a helper that will return one of the values Project, Office or Trip.
helpers: {
place: function() {
var place = ["Project","Office","Trip"];
return place[Math.floor(Math.random() * place.length)];
}
},
// We wish to see when the resource is requested in the terminal
log: true,
// We also with that the browser should be opened to the correct url when starting the server
open: true,
});
This is a template that generates a GeoJSON with five points on the map.
1.1.0
Added address parameter