Roughing out the demo app
This commit is contained in:
29
node_modules/json-server/lib/server/router/nested.js
generated
vendored
Normal file
29
node_modules/json-server/lib/server/router/nested.js
generated
vendored
Normal file
@@ -0,0 +1,29 @@
|
||||
"use strict";
|
||||
|
||||
const express = require('express');
|
||||
|
||||
const pluralize = require('pluralize');
|
||||
|
||||
const delay = require('./delay');
|
||||
|
||||
module.exports = opts => {
|
||||
const router = express.Router();
|
||||
router.use(delay); // Rewrite URL (/:resource/:id/:nested -> /:nested) and request query
|
||||
|
||||
function get(req, res, next) {
|
||||
const prop = pluralize.singular(req.params.resource);
|
||||
req.query[`${prop}${opts.foreignKeySuffix}`] = req.params.id;
|
||||
req.url = `/${req.params.nested}`;
|
||||
next();
|
||||
} // Rewrite URL (/:resource/:id/:nested -> /:nested) and request body
|
||||
|
||||
|
||||
function post(req, res, next) {
|
||||
const prop = pluralize.singular(req.params.resource);
|
||||
req.body[`${prop}${opts.foreignKeySuffix}`] = req.params.id;
|
||||
req.url = `/${req.params.nested}`;
|
||||
next();
|
||||
}
|
||||
|
||||
return router.get('/:resource/:id/:nested', get).post('/:resource/:id/:nested', post);
|
||||
};
|
||||
Reference in New Issue
Block a user