ng-apimock-data-generator
THIS MODULE IS WORK IN PROGRESS AND STILL IN BETA. IF YOU LIKE TO CONTRIBUTE PLEASE ADD A PR
Node plugin that provides the ability to easily generate mockdata files for ng-Apimock based on a JSON or YAML template
When ng-Apimock is used multiple mockdata files need to be written to mock API-responses.
This module can generate all those mockfiles out of JSON and or YAML files with the usage of default templates for responses.data
and responses.headers
that can ease the writing and prevent repetitive code.
Some core functionalities:
- Multiple different (to be generated) mockdata files can use the default templates for
responses.data
and responses.headers
when they are generated from 1 single template file - Default properties can easily be overwritten by providing a new value for the key.
- Templates can be extended in a single
responses.data
or responses.headers
object by adding a new property.
Getting started
npm install ng-apimock-data-generator --save-dev
Usage
const mockDataGenerator = require('ng-apimock-data-generator');
mockDataGenerator({
templateDir: 'path/which/holds/files/',
outputFolder: 'path/to/save/single-files-to/'
});
Writing template mock-files
There are 2 ways to write a template for mockfiles, they will both be explained below.
Build 1 template mockdata file for multiple API's
If you want to use the strength of this module you can create 1 JSON or YAML template file that hold mockdata for all API's.
Your file should hold a mockDataFiles
-array with the API's as an object.
{
"dataTemplates": {},
"headersTemplates": {},
"mockDataFiles":[
{
"fileName": "name-of-the-first-file-that-needs-to-be-generated",
},
{
"fileName": "name-of-the-second-file-that-needs-to-be-generated",
},
{
}
]
}
dataTemplates: {}
headersTemplates: {}
mockDataFiles:
- fileName: name-of-the-first-file-that-needs-to-be-generated
- fileName: name-of-the-second-file-that-needs-to-be-generated
All the API files can reuse the dataTemplates
and or headersTemplates
with a single property reference, see data/headers templates
Build a template mockdata file for each single API
When you want to have 1 template per API you can use the below structure
{
"dataTemplates": {},
"headersTemplates": {},
"fileName": "name-of-the-file-that-needs-to-be-generated",
}
dataTemplates: {}
headersTemplates: {}
fileName: name-of-the-file-that-needs-to-be-generated
Each file can have an optional dataTemplates
or headersTemplates
object. These objects can hold multiple predefined data
and headers
. The template can prevent double code.
data/headers
-example
In the JSON that is used by ng-Apimock you can have for example a response header-object that will be used by all API responses (or a subset of it).
In the below example headers
are used but it works the same for data
.
"headers": {
"Access-Control-Allow-Credentials":true,
"Access-Control-Allow-Origin": "https://www.example.com",
"Connection": "keep-alive",
"Content-Type": "application/json",
"Date": "Mon, 14 Aug 2017 08:42:49 GMT"
}
You can easily make a template out of this like for example this
"headersTemplates": {
"default:"{
"Access-Control-Allow-Credentials":true,
"Access-Control-Allow-Origin": "https://www.example.com",
"Connection": "keep-alive",
"Content-Type": "application/json",
"Date": "%%today%%"
}
}
And refer to it in the mockdata with a single property like this
{
responses: {
headers: {
"ng-apimock-headersTemplate": "default"
"Content-Type": "application/pdf",
"Cache-Control": "no-cache, no-store, must-revalidate"
}
}
}
You can refer to a template from within the data
- or header
-key with the following properties
"ng-apimock-dataTemplate": "name-of-the-data-template-key"
"ng-apimock-headersTemplate": "name-of-the-headers-template-key"
dataTemplates
or headersTemplates
can be overwritten or extended.
- You can overwrite them by referring to the property and give it a new value.
- You can extend them by adding a new property to the object
For more info and examples please check the JSON or the YAML docs
ng-Apimock
-file structure
The structure of a ng-Apimock-file can be found on ng-Apimock - How to write a mock file.
The difference with the mockfile from ng-Apimock is that the following property needs to be provided
"fileName": "name-of-the-file"
This property represents the name of the file that needs to be generated. No extension is needed.
If this property is not provided generating mockdata will fail.
Changelog
The Changelog can be found here
Contributing
How to contribute can be found here