mini-mock-api
Advanced tools
Comparing version 0.2.1 to 0.2.2
@@ -1,2 +0,10 @@ | ||
<a name="0.2.1"></a> | ||
<a name="0.2.2"></a> | ||
# 0.2.2 (2015-07-21) | ||
## Bug Fixes | ||
- log exception from decorate function ([8bcadce](https://github.com/pwambach/mini-mock-api/commit/8bcadce7f2ed63c88a27540e8ac34db4cdada9a4)) | ||
# 0.2.1 (2015-06-29) | ||
@@ -3,0 +11,0 @@ |
@@ -25,3 +25,3 @@ 'use strict'; | ||
if(this.options.cors === true){ | ||
this.app.use(require('cors')()); | ||
this.app.use(require('cors')()); | ||
} | ||
@@ -33,60 +33,77 @@ return this; | ||
/* | ||
* Handle all API requests with json files found in file system (./mocks). | ||
* If a file is not found the server assumes that a single item was requested | ||
* and tries to search a specific id in the parent collection | ||
* e.g. for tasks/123 it will search in tasks.json for an item with idAttribute === 123 | ||
* | ||
*/ | ||
* Handle all API requests with json files found in file system (./mocks). | ||
* If a file is not found the server assumes that a single item was requested | ||
* and tries to search a specific id in the parent collection | ||
* e.g. for tasks/123 it will search in tasks.json for an item with idAttribute === 123 | ||
* | ||
*/ | ||
MiniMockAPI.prototype.initMockRoute = function(filePath){ | ||
this.app.get(filePath, function(request, response){ | ||
var strippedRequestPath = request.path.substring(this.options.basePath.length), | ||
fileName = this.getFileName(strippedRequestPath); | ||
var strippedRequestPath = request.path.substring(this.options.basePath.length), | ||
fileName = this.getFileName(strippedRequestPath); | ||
fs_readFile(fileName, 'utf8').then( | ||
fs_readFile(fileName, 'utf8').then( | ||
function handleFileFound(data){ | ||
var responseData = sort(JSON.parse(data), request.query[this.options.sortParameter]); | ||
console.log('Serving: ' + fileName); | ||
function handleFileFound(data){ | ||
var parsedData, | ||
responseData; | ||
try { | ||
parsedData = JSON.parse(data); | ||
} catch (e) { | ||
return console.error(e); | ||
} | ||
responseData = sort(parsedData, request.query[this.options.sortParameter]); | ||
console.log('Serving: ' + fileName) | ||
try { | ||
response.json(this.decorate(responseData, request)); | ||
}.bind(this), | ||
} catch (e) { | ||
console.log(e); | ||
} | ||
}.bind(this), | ||
function handleError(){ | ||
var splitted = splitPathInIdAndPath(strippedRequestPath); | ||
if(splitted){ | ||
fs_readFile(this.getFileName(splitted.path), 'utf8').then( | ||
function handleError(){ | ||
var splitted = splitPathInIdAndPath(strippedRequestPath); | ||
if(splitted){ | ||
fs_readFile(this.getFileName(splitted.path), 'utf8').then( | ||
function handleSingleItem(data){ | ||
var queryObj = {}; | ||
function handleSingleItem(data){ | ||
var parsedData, | ||
queryObj = {}; | ||
queryObj[this.options.idAttribute] = splitted.id; | ||
queryObj[this.options.idAttribute] = splitted.id; | ||
try { | ||
parsedData = JSON.parse(data); | ||
} catch (e) { | ||
return console.error(e); | ||
} | ||
//search single item | ||
var foundItem = _.findWhere(JSON.parse(data), queryObj); | ||
//try again with number instead of string | ||
if(!foundItem){ | ||
queryObj[this.options.idAttribute] = parseInt(splitted.id, 10); | ||
foundItem = _.findWhere(JSON.parse(data), queryObj); | ||
} | ||
if(foundItem){ | ||
console.log('Serving: ' + path.join(this.getFileName(splitted.path), splitted.id)); | ||
response.json(this.decorate(foundItem, request)); | ||
} else { | ||
response.sendStatus(404); | ||
} | ||
}.bind(this), | ||
//search single item | ||
var foundItem = _.findWhere(parsedData, queryObj); | ||
//try again with number instead of string | ||
if(!foundItem){ | ||
queryObj[this.options.idAttribute] = parseInt(splitted.id, 10); | ||
foundItem = _.findWhere(parsedData, queryObj); | ||
} | ||
if(foundItem){ | ||
console.log('Serving: ' + path.join(this.getFileName(splitted.path), splitted.id)); | ||
response.json(this.decorate(foundItem, request)); | ||
} else { | ||
response.sendStatus(404); | ||
} | ||
}.bind(this), | ||
function(err) { | ||
console.log('No mock data found:', strippedRequestPath); | ||
response.sendStatus(404); | ||
}.bind(this) | ||
); | ||
} | ||
}.bind(this) | ||
); | ||
}.bind(this)); | ||
function(err) { | ||
console.log('No mock data found:', strippedRequestPath); | ||
response.sendStatus(404); | ||
}.bind(this) | ||
); | ||
} | ||
}.bind(this) | ||
); | ||
}.bind(this)); | ||
} | ||
/* | ||
* This function can be overridden. Allows to modify responses globally. | ||
*/ | ||
* This function can be overridden. Allows to modify responses globally. | ||
*/ | ||
MiniMockAPI.prototype.decorate = function(data) { | ||
@@ -97,5 +114,5 @@ return data; | ||
/* | ||
* Configure express mock route (has to be set up here because custom routes should be defined earlier) | ||
* and start server | ||
*/ | ||
* Configure express mock route (has to be set up here because custom routes should be defined earlier) | ||
* and start server | ||
*/ | ||
MiniMockAPI.prototype.start = function() { | ||
@@ -116,5 +133,5 @@ this.initMockRoute(path.join(this.options.basePath, '*')); | ||
/* | ||
* adds convenient functions for custom routes | ||
* e.g. server.post('abc', function(req, res)) instead of server.app.post(server.options.basePath ...) | ||
*/ | ||
* adds convenient functions for custom routes | ||
* e.g. server.post('abc', function(req, res)) instead of server.app.post(server.options.basePath ...) | ||
*/ | ||
customMethods.forEach(function(method){ | ||
@@ -150,6 +167,6 @@ MiniMockAPI.prototype[method] = function(relativeRoute, fn) { | ||
/* | ||
* Sort data array by property | ||
* leading + is ASC | ||
* leading - is DESC | ||
*/ | ||
* Sort data array by property | ||
* leading + is ASC | ||
* leading - is DESC | ||
*/ | ||
var sort = function(data, sortOrder) { | ||
@@ -156,0 +173,0 @@ if(sortOrder){ |
{ | ||
"name": "mini-mock-api", | ||
"version": "0.2.1", | ||
"version": "0.2.2", | ||
"description": "Lightweight node.js API mock server, route calls to static JSON files", | ||
@@ -5,0 +5,0 @@ "main": "lib/mini-mock-api.js", |
@@ -7,3 +7,3 @@ # mini-mock-api | ||
Mini-Mock-API was written to mock a simple API in a few minutes. API calls are routed to static JSON files. | ||
As an example if your mock directory looks like this: | ||
As an example if your mock directory looks like this: | ||
``` | ||
@@ -79,3 +79,3 @@ + mock-files | ||
``` | ||
myApi.post('custom/status', function(request, response){ | ||
myApi.get('custom/status', function(request, response){ | ||
response.json({status: 'okay'}); | ||
@@ -87,5 +87,5 @@ }); | ||
Please note that custom routes are relativ to your 'basePath' property. If you need a route outside of your API scope use the '[get|post|put|delete]FromRoot' method: | ||
Please note that custom routes are relativ to your 'basePath' property. If you need a route outside of your API scope use the '[get|post|put|delete]FromRoot' method: | ||
``` | ||
myApi.postFromRoot('/ping', function(request, response){ | ||
myApi.getFromRoot('/ping', function(request, response){ | ||
response.json({hello: 'there'}); | ||
@@ -96,2 +96,3 @@ }); | ||
`-> localhost:8080/ping` | ||
## Decorate | ||
@@ -98,0 +99,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13211
10
263
113