API Stub
Simple, no database, temporary API for your prototype.
What is it ?
API Stub is a node.js based API server for prototyping. Idea was simple, sometimes I wanted to work on frontend app before I finish my backend API, or I often needed dummy data when testing out new data visualization designs. I've been using large JSON document as data stub, but I wanted something more quick to modify both schema and size of dataset.Getting started
You will need node.js installed on your machine.npm install api-stub --save-dev
// api.js
var API = require('api-stub');
var config = [{
path:'/status',
data: {status: true}
}]
var server = new API(config);
server.start(3000);
//then run the script `node api.js`
After you run the script - node api.js
, http://localhost:3000/status will return following.
{ "status": true }
You can put as many endpoints as you like in config array.Repeated dataset
If you need repeated dataset, you can specifyingtemplate
field, and use placeholder text in data
to set repeated amount.
var config = [{
path:'/random',
data: [/// info * 3 ///] // [templatename] * [how many to repeat]
templates:{
info:{
message:'Hi there!'
}
}
}]
[{ "message": "Hi there!" },{ "message": "Hi there!" },{ "message": "Hi there!" }]
Dynamically generate data
Built in templates
There are few built in templates to generate data dynamically. (Check Templates section for more)var config = [{
path:'/id',
data: [/// users * 3 ///]
templates:{
users:{
id:{
stub_type:'number',
starting_num:1000,
increments:1,
type:'incremental'
},
}
}
}]
[{ "id": 1000 },{ "id": 1001 },{ "id": 1002 }]
Custom functions
You can also assign your custom function to any key. When initializing API server, API Stub will creates deep copy ofdata
& invoke any function assigned.
var config = [{
path:'/random',
data: {value: function(){Math.random();}}
}]
{ "value": 0.05823205946944654 } // random value everytime you start the serer
API
server.start(port, callback)
Start API server on specified port numberserver.stop(callback)
Stop API serverserver.profile()
Returns status of the API server- status: True if API server is running
- port: port number that API sever is using,
- runningsince: timestamp of when the API server started,
- path : list of paths this API server is serving
{
status: true,
port: 3000,
running_since: Fri May 15 2015 08:29:21 GMT-0400 (EDT),
path: [ '/data', '/status' ]
}
Templates
There are 4stub_type
.select
Select values from given choice.property | type | value --- | --- | --- type |
string
| 'liner', 'random', or 'randomunique'
choice | array
| array of choices
'liner' will select values from top the the array. 'random' will pick values randomly. 'randomunique' will pick values randomly but will not draw same value twice (must provide enough choices if you are creating repeated dataset).
data:{
stub_type:'select',
type:'liner',
choice:[100,200,300,400]
}
number
Numbers in set increments or randomly within specified rangeproperty | type | value --- | --- | --- type |
string
| 'incremental' or 'random'
startingnum | number
| starting value for 'incremental' type
increments | number
| increment step for 'incremental' type
minnum | number
| minimum value for 'random' type
maxnum | number
| maximum value for 'random' type
'incremental' will inclement values specified in increments
. 'random' will pick random values between minnum and maxnum.
id:{
stub_type:'number',
type:'incremental',
starting_num:1000,
increments:1
}
value:{
stub_type:'number',
type:'random',
min_num:1000,
max_num:5000
}
lipsum
Lorem Ipsum textproperty | type | value --- | --- | --- mincharlength |
number
| minimum character count
maxcharlength | number
| maximum character counttext:{
stub_type:'lipsum',
min_char_length:200,
max_char_length:500,
}
datetime
parse & create datetime related textproperty | type | value --- | --- | --- type |
string
| 'incremental' or 'random'
startingdatetime | string
| starting value for 'incremental' type. Pass any string that Date.parse(str)
can process
increments| string
| increments for 'incremental' type in milliseconds (86400000
would be one day)
mindatetime | string
| minimum date for 'random'. Pass any string that Date.parse(str)
can process
maxdatetime | string
| maximum date for 'random'. Pass any string that Date.parse(str)
can process
outputformat | string
| parsing method for Date object (see outputformat table below)timestamp:{
stub_type:'datetime',
type:'random',
min_datetime:'2014-01-01',
max_datetime:'2014-01-31',
output_format:'toYMDString',
}
timestamp:{
stub_type:'date',
type:'incremental',
starting_datetime:'2014-01-01',
increments:86400000,
output_format:'toTimeString',
}
outputformat| return --- | --- toString |
Fri May 15 2015 09:30:16 GMT-0400 (EDT)
toDateString | Fri May 15 2015'
toISOString | 2015-05-15T13:30:16.411Z
toJSON | 2015-05-15T13:30:16.411Z
toGMTString | Fri, 15 May 2015 13:30:16 GMT
toLocaleDateString | 5/15/2015
toLocaleString | 5/15/2015, 9:30:16 AM
toLocaleTimeString | 9:30:16 AM'
toTimeString | 09:30:16 GMT-0400 (EDT)'
toUTCString | Fri, 15 May 2015 13:30:16 GMT
toYMDString | 2015-05-15