Skip to content

vikeri/json-api-generator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

json-api-generator

Generates json api endpoints from templates

Inspired by mock-json-api and relies heavily on dummy-json

When?

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.

How?

You create your desired json structure by writing handlebars template files that will generate the json response (using dummy-json)

See example

Anything fancy?

Multiple endpoints can be defined, just create more template files and put them in your specified template folder.

Install

$ npm install json-api-generator --save-dev

Parameters

The exported function takes a map with the following keys:

templateDir

The directory where you keep your template files.

helpers (optional)

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]

log (optional)

A boolean specifying whether to log each request in the terminal. Defaults to false.

address (optional)

Which address the server should listen to, default to localhost

port (optional)

Which port the server should be run on, defaults to 1989.

open (optional)

A boolean specifying if the browser should be opened to the first endpoint automatically when running the server.

Helpers

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 

Example

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

index.js

"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,
});

templates/map.hbs

This is a template that generates a GeoJSON with five points on the map.

{
"type": "FeatureCollection",
"features":
	[
	{{#repeat 5 }}
		{
		"type": "Feature",
		"properties": {
			"id": "{{index}}",
			"title": "{{place}}", {{! Here we use our handler defined in index.js }}
			"description": "{{text}}",
			"image": "{{imageUrl}}"
		},
		"geometry": {
			"coordinates": [
			{{randomFloat -90 90}},
			{{randomFloat -30 60}}
			],
			"type": "Point"
		},
		"id": "{{index}}"
		}
	{{/repeat}}
	]
}

Changes

  • 1.1.0 Added address parameter

About

Generates json api endpoints from templates

Resources

Stars

Watchers

Forks

Packages

No packages published