fixture-middleware
Advanced tools
Comparing version 1.0.0 to 1.0.1
25
index.js
@@ -25,2 +25,8 @@ /*! | ||
/** | ||
* Create a fixture middleware. | ||
* @param {String} directory The directory containing fixtures. | ||
* @return {Function} | ||
*/ | ||
function fixtureMiddleware(directory) { | ||
@@ -38,13 +44,23 @@ | ||
/** | ||
* Fixture Middleware. | ||
* @param {Object} req The request. | ||
* @param {Object} res The response. | ||
* @param {Function} next The next function. | ||
*/ | ||
return async function fixtureMiddleware(req, res, next) { | ||
debug('request url:', req.url) | ||
// Ignore any non-get request. | ||
if (req.method !== 'GET') { | ||
debug('ignoring non-GET request') | ||
debug('ignoring non-GET request:', req.method) | ||
return next() | ||
} | ||
// Determine the name of the fixture file. | ||
// Example: /user/all/active > user--all--active | ||
const fixtureName = req.url.substr(1).replace(/\//g, '--') | ||
debug('fixture name:', fixtureName) | ||
debug('requested fixture name:', fixtureName) | ||
@@ -55,5 +71,8 @@ const fixtureBasePath = path.join(directory, fixtureName) | ||
let [fixturePath] = fixturePaths | ||
// If request accept header is present, then we use it to determine the file type. | ||
if (req.headers.accept) { | ||
debug('using request accept headers:', req.headers.accept) | ||
const extension = mime.extension(req.headers.accept) | ||
debug('extension from request accept headers:', extension) | ||
@@ -63,2 +82,3 @@ fixturePath = fixturePaths.find(fp => fp.endsWith(`.${extension}`)) | ||
// When no fixture file is found, then delegate request to next middleware in chain. | ||
if (!fixturePath) { | ||
@@ -71,2 +91,3 @@ debug('no fixture found') | ||
// Read the fixture file and return it. | ||
try { | ||
@@ -73,0 +94,0 @@ await fs.access(fixturePath) |
{ | ||
"name": "fixture-middleware", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Use fixture data in your express app!", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
107386
139