Skip to content
This repository has been archived by the owner on Aug 19, 2022. It is now read-only.

Hooks #503

Merged
merged 3 commits into from
Mar 30, 2018
Merged

Hooks #503

merged 3 commits into from
Mar 30, 2018

Conversation

ShogunPanda
Copy link
Contributor

Implements #502

@coveralls
Copy link

coveralls commented Mar 28, 2018

Coverage Status

Coverage increased (+0.1%) to 94.221% when pulling 27d39db on hooks into a9f394e on master.

Copy link
Contributor

@dberesford dberesford left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great - minor comments but overall fantastic job @ShogunPanda

@@ -36,6 +36,35 @@ udaru.organizations.list({}, (err, orgs) => {

```

## Hooks

Hooks are registered using the `udaru.addHook` method and allow you to listen to specific events in udaru life.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would remove life


Each udaru method exposes a namespaced hook (e.g.: the `udaru.authorize.isUserAuthorized` method exposes the `authorize:isUserAuthorized` hook).

The hook is a node-style callback with three arguments: the method arguments, the method result values and a callback to invoke once done
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

small nit, missing full stop


udaru.addHook('authorize:isUserAuthorized', function (error, args, result, done) {
if (error) {
console.error(`Authorization errored: ${error}`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so this is the contentious bit, IMO this should be:

if (error) {
  return done(error)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. This was a legacy of first draft when the errors were swallowed. Updating it.


udaru.addHook('authorize:isUserAuthorized', function (error, args, result, done) {
if (error) {
console.error(`Authorization errored: ${error}`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comment as above

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same for me :)

addPolicies: hooks.wrap('organization:addPolicies', organizationOps.addOrganizationPolicies),
replacePolicies: hooks.wrap('organization:replacePolicies', organizationOps.replaceOrganizationPolicies),
deletePolicies: hooks.wrap('organization:deletePolicies', organizationOps.deleteOrganizationAttachedPolicies),
deletePolicy: hooks.wrap('organization:deletePolicy', organizationOps.deleteOrganizationAttachedPolicy)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so does this mean that we can have hooks on all these functions? I wouldn't of seen the need to wrap read and list commands for example, but I guess if we get them for free, why not..

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see many use cases as well, but who knows. Might be useful for security monitoring.
Anyway each wrapping has small cost on bootstraping.
When the handlers list is empty each call will result in additional function invoked with iteration on a empty array. So it shouldn't impact performances at all. Especially when these methods are not really used. :)

@@ -27,6 +28,36 @@ const server = new Hapi.server()
server.register({register: UdaruPlugin})
```

In order to register udaru hooks, just provide a `hooks` key in the plugin options where keys are the names and values are handler functions (or array of functions).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add ## Hooks heading here

@dberesford dberesford merged commit f075022 into master Mar 30, 2018
@dberesford dberesford deleted the hooks branch March 30, 2018 09:30
ShogunPanda added a commit that referenced this pull request Mar 30, 2018
* Added hooks. [#502]

* Fixed remark warnings.

* PR requested changes.
ShogunPanda added a commit that referenced this pull request Mar 30, 2018
* Added hooks. [#502]

* Fixed remark warnings.

* PR requested changes.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants