# json-api-generator

> A simple module that creates a json api from handlebar templates

Latest version **1.1.0** (published 2015-10-20) · ISC license · 0 weekly downloads

## Install

```sh
npm install json-api-generator
pnpm add json-api-generator
yarn add json-api-generator
bun add json-api-generator
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2015-10-20 |
| First published | 2015-10-15 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 6 |
| Known vulnerabilities | 0 (+9 in 3 direct dependencies) |
| Install scripts | no |
| GitHub stars | 13 |
| Author | Viktor Eriksson |
| Maintainers | vikeri |

## Links

- npm: https://www.npmjs.com/package/json-api-generator
- Repository: https://github.com/vikeri/json-api-generator
- Homepage: https://github.com/vikeri/json-api-generator#readme
- Issues: https://github.com/vikeri/json-api-generator/issues
- npm.io page: https://npm.io/package/json-api-generator

## Dependencies (6)

- [open](https://npm.io/package/open.md) 0.0.5
- [lodash](https://npm.io/package/lodash.md) ^3.9.3
- [express](https://npm.io/package/express.md) ~4.13.0
- [dummy-json](https://npm.io/package/dummy-json.md) 0.0.3
- [lorem-ipsum](https://npm.io/package/lorem-ipsum.md) ^1.0.3
- [console.table](https://npm.io/package/console.table.md) ^0.4.0

## Recent versions

- 1.1.0 (latest) — 2015-10-20
- 1.0.1 — 2015-10-15
- 1.0.0 — 2015-10-15

## README

# json-api-generator
> Generates json api endpoints from templates

Inspired by [mock-json-api](https://www.npmjs.com/package/mock-json-api) and relies heavily on [dummy-json](https://github.com/webroo/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](https://github.com/webroo/dummy-json))

See [example](#example)

## Anything fancy?

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

## Install
```sh
$ 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](https://github.com/webroo/dummy-json#available-helpers)
 I have added a few others
 
| Helper | Description|
| ----- | ----- |
| `imageUrl` | Sends a random image url from [lorempixel](http://lorempixel.com/). |
| `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](https://github.com/vikeri/geojson-generator)

An example of a resulting json is found here: [geojson-example](https://github.com/vikeri/geojson-generator/blob/master/example-output.json?short_path=b2147ad)

### `index.js`

```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.

```hbs
{
"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}}
	]
}

```

---
_Source: https://npm.io/package/json-api-generator · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
