npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

json-api-generator

v1.1.0

Published

A simple module that creates a json api from handlebar templates

Downloads

9

Readme

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}}
	]
}