Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I already had lodash in here, with with node:assert now available, I didn't really need Hoek anymore #34

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ These options allow you to act on behalf of the authenticated user. Typically t

- `userIdProperty` (string, defaults `'id'`). Applies to `findOne`, `find`, `create`, `update`, `destroy`, `add`, `remove`, and `populate`.

When `actAsUser` is `true` this option takes effect. It defines a path into `Request.auth.credentials` to determine the acting user's id. For example, if the credentials object equals `{user: {info: {id: 17}}}` then `'user.info.id'` would grab user id `17`. See [`Hoek.reach`](https://github.com/hapijs/hoek#reachobj-chain-options), which is used to convert the string to a deep property in the hapi credentials object.
When `actAsUser` is `true` this option takes effect. It defines a path into `Request.auth.credentials` to determine the acting user's id. For example, if the credentials object equals `{user: {info: {id: 17}}}` then `'user.info.id'` would grab user id `17`.

- `userUrlPrefix` (string, defaults `'/user'`). Applies to `findOne`, `update`, `destroy`, `add`, `remove`, and `populate`.

Expand Down
11 changes: 6 additions & 5 deletions lib/action-util.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict';

const _ = require('lodash');
const Assert = require('node:assert');
const Boom = require('@hapi/boom');
const Hoek = require('@hapi/hoek');

const internals = {};

Expand Down Expand Up @@ -187,11 +188,11 @@ module.exports = (request, options) => {

getUserId: () => {

Hoek.assert(options.actAsUser, 'Not currently acting as user, per `options.actAsUser`.');
Hoek.assert(typeof options.userIdProperty === 'string', '`options.userIdProperty` must be a string.');
Hoek.assert(request.auth.credentials, 'Unable to get user ID from credentials');
Assert.ok(options.actAsUser, 'Not currently acting as user, per `options.actAsUser`.');
Assert.ok(typeof options.userIdProperty === 'string', '`options.userIdProperty` must be a string.');
Assert.ok(request.auth.credentials, 'Unable to get user ID from credentials');

const userId = Hoek.reach(request.auth.credentials, options.userIdProperty);
const userId = _.get(request.auth.credentials, options.userIdProperty);

return userId;
}
Expand Down
29 changes: 14 additions & 15 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

const Call = require('@hapi/call');
const _ = require('lodash');
const Hoek = require('@hapi/hoek');
const Assert = require('node:assert');
const Call = require('@hapi/call');
const Package = require('../package.json');

const internals = {};
Expand All @@ -21,18 +21,17 @@ exports.plugin = {

// handlerOptions come user-defined in route definition
// nothing should override these!
const thisRouteOpts = Hoek.clone(internals.defaults);

const thisRouteOpts = _.cloneDeep(internals.defaults);
// Plugin-level user-defined options
Hoek.merge(thisRouteOpts, options);
_.merge(thisRouteOpts, options);

// Route-level user-defined options
Hoek.merge(thisRouteOpts, handlerOptions);
_.merge(thisRouteOpts, handlerOptions);

// Route-level info (should not override plugin options & handler options)
internals.setOptionsFromRouteInfo(route, thisRouteOpts);

Hoek.assert(thisRouteOpts.model, 'Unable to determine model for route ' + route.path);
Assert.ok(thisRouteOpts.model, `Unable to determine model for route ${route.path}`);

//We want to allow for Model || model
let Model = {};
Expand All @@ -43,7 +42,7 @@ exports.plugin = {
Model = server.models(true)[capitalizedModel];
}

Hoek.assert(Model, 'Model `' + thisRouteOpts.model + '` must exist to build route.');
Assert.ok(Model, 'Model `' + thisRouteOpts.model + '` must exist to build route.');

// Set associations now that the model is locked-down
_.defaults(thisRouteOpts, { associations: Model.relationMappings });
Expand Down Expand Up @@ -224,7 +223,7 @@ internals.setOptionsFromRouteInfo = (route, thisRouteOpts) => {
const pathInfo = internals.Router.analyze(path);
const pathSegments = pathInfo.segments.length;

Hoek.assert(pathSegments <= 4, 'Number of path segments should be between 1 and 4.');
Assert.ok(pathSegments <= 4, 'Number of path segments should be between 1 and 4.');

switch (pathSegments) {
case 4:
Expand All @@ -242,21 +241,21 @@ internals.setOptionsFromRouteInfo = (route, thisRouteOpts) => {

internals.normalizePath = (path, thisRouteOpts) => {

Hoek.assert(typeof thisRouteOpts.userUrlPrefix === 'string' || !thisRouteOpts.userUrlPrefix, 'Option userUrlPrefix should only have a string or a falsy value.');
Hoek.assert(typeof thisRouteOpts.userModel === 'string' || !thisRouteOpts.userModel, 'Option userModel should only have a string or a falsy value.');
Assert.ok(typeof thisRouteOpts.userUrlPrefix === 'string' || !thisRouteOpts.userUrlPrefix, 'Option userUrlPrefix should only have a string or a falsy value.');
Assert.ok(typeof thisRouteOpts.userModel === 'string' || !thisRouteOpts.userModel, 'Option userModel should only have a string or a falsy value.');

if (internals.pathEndsWith(path, '/count')) {
thisRouteOpts._private.count = true;
path = internals.removeSuffixFromPath(path, '/count');
}

//use search instead of indexof so that we're only doing whole word matching
Hoek.assert(path.search(/\bcount\b/) === -1, 'Count can only appear at the end of a route path');
Assert.ok(path.search(/\bcount\b/) === -1, 'Count can only appear at the end of a route path');

// Deal with prefix option
if (thisRouteOpts.prefix) {
// Prefix pattern copied from hapi's prefix validation
Hoek.assert(typeof thisRouteOpts.prefix === 'string' && thisRouteOpts.prefix.match(/^\/.+/), 'Prefix parameter should be a string following the pattern: /^\\/.+/');
Assert.ok(typeof thisRouteOpts.prefix === 'string' && thisRouteOpts.prefix.match(/^\/.+/), 'Prefix parameter should be a string following the pattern: /^\\/.+/');
path = internals.removePrefixFromPath(path, thisRouteOpts.prefix);
}

Expand Down Expand Up @@ -326,8 +325,8 @@ internals.pathBeginsWith = (path, needle) => {

internals.removePrefixFromPath = (path, prefix) => {

Hoek.assert(typeof path === 'string', 'Path parameter should be a string');
Hoek.assert(typeof prefix === 'string', 'Prefix parameter should be a string');
Assert.ok(typeof path === 'string', 'Path parameter should be a string');
Assert.ok(typeof prefix === 'string', 'Prefix parameter should be a string');
// Remove trailing slashes from prefix
prefix = prefix.replace(/\/+$/, '');

Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,14 @@
"rest",
"crud"
],
"author": "Matt Boutet <matt@bigroomstudios.com>",
"author": "Matt Boutet <mattboutet@gmail.com> and contributors",
"license": "MIT",
"bugs": {
"url": "https://github.com/hapipal/tandy/issues"
},
"dependencies": {
"@hapi/boom": "^10.0.1",
"@hapi/call": "^9.0.1",
"@hapi/hoek": "^11.0.2",
"joi": "^17.7.1",
"lodash": "^4.17.21"
},
Expand Down
9 changes: 5 additions & 4 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
'use strict';

const Lab = require('@hapi/lab');
const _ = require('lodash');
const Boom = require('@hapi/boom');
const Hapi = require('@hapi/hapi');
const Joi = require('joi');
const Hoek = require('@hapi/hoek');
const Boom = require('@hapi/boom');
const Lab = require('@hapi/lab');
const Schwifty = require('@hapipal/schwifty');

const Tandy = require('..');
const TestModels = require('./models');

Expand Down Expand Up @@ -36,7 +37,7 @@ describe('Tandy', () => {
}
};

return Hoek.applyToDefaults(options, extras || {});
return _.defaultsDeep({}, extras, options);
};

const scheme = (server, options) => {
Expand Down