Skip to content

Commit

Permalink
added opts.naming
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukas Herzberger committed May 17, 2019
1 parent 61f32c5 commit a7c9135
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
11 changes: 9 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,13 @@ module.exports = (models, opts) => {
opts = opts || {};
opts.middleware = opts.middleware || {};
opts.openapi = opts.openapi || {};
const namingScheme = opts.naming || function (value) { return value; };
if (!typeof namingScheme === 'function') {
throw new Error('naming scheme must be a function');
}
if (!typeof namingScheme('foo') === 'string') {
throw new Error('naming scheme must return a string');
}

if (!models) throw new Error('models must be set!');
if (!(models instanceof Array)) throw new Error('models must be an array');
Expand All @@ -276,7 +283,7 @@ module.exports = (models, opts) => {
}))
throw new Error(`model ${model.model.name} already registered`);
const router = express.Router();
const route = model.opts.route || model.model.name;
const route = model.opts.route || namingScheme(model.model.name);
routingInformation.push({
model,
route,
Expand Down Expand Up @@ -441,7 +448,7 @@ module.exports = (models, opts) => {
const association = model.getAssociationByName(associationName);
const target = association.target;
const source = association.source;
const targetRoute = association.options.name.singular;
const targetRoute = namingScheme(association.options.name.singular);
const auth = _getAuthorizationMiddleWare.bind(null, models, target, source);

const unlinkRelations = (req, res, next, setterFunctionName) => {
Expand Down
22 changes: 22 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,28 @@ describe('index.js', () => {
});
});
});
describe('opts.route', () => {
it('should make the camel cased route name a dashed string.', () => {
expect(exseq([
{
model: TestModel,
opts: {}
}
], {
naming: function (v) { return v.replace(/([a-zA-Z])(?=[A-Z])/g, '$1-').toLowerCase(); }
}).routingInformation[0].route).to.equal('/test-model');
});
it('should not change a custom route name.', () => {
expect(exseq([
{
model: TestModel,
opts: {route: 'UseThis'}
}
], {
naming: function (v) { return v.replace(/([a-zA-Z])(?=[A-Z])/g, '$1-').toLowerCase(); }
}).routingInformation[0].route).to.equal('/UseThis');
});
});
});
describe('model.opts', () => {
describe('opts.route', () => {
Expand Down

0 comments on commit a7c9135

Please sign in to comment.