|
| 1 | +import Proto from 'uberproto'; |
| 2 | +import { hooks as utils } from 'feathers-commons'; |
| 3 | +import { addHookTypes, processHooks, baseMixin, getHooks } from './commons'; |
| 4 | + |
| 5 | +function isPromise (result) { |
| 6 | + return typeof result !== 'undefined' && |
| 7 | + typeof result.then === 'function'; |
| 8 | +} |
| 9 | + |
| 10 | +function hookMixin (service) { |
| 11 | + if (typeof service.hooks === 'function') { |
| 12 | + return; |
| 13 | + } |
| 14 | + |
| 15 | + const app = this; |
| 16 | + const methods = app.methods; |
| 17 | + const old = { |
| 18 | + before: service.before, |
| 19 | + after: service.after |
| 20 | + }; |
| 21 | + const mixin = baseMixin(methods, { |
| 22 | + before (before) { |
| 23 | + return this.hooks({ before }); |
| 24 | + }, |
| 25 | + |
| 26 | + after (after) { |
| 27 | + return this.hooks({ after }); |
| 28 | + } |
| 29 | + }); |
| 30 | + |
| 31 | + addHookTypes(service); |
| 32 | + |
| 33 | + methods.forEach(method => { |
| 34 | + if (typeof service[method] !== 'function') { |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + mixin[method] = function () { |
| 39 | + const service = this; |
| 40 | + // A reference to the original method |
| 41 | + const _super = this._super.bind(this); |
| 42 | + // Additional data to add to the hook object |
| 43 | + const hookData = { |
| 44 | + app, |
| 45 | + service, |
| 46 | + get path () { |
| 47 | + return Object.keys(app.services) |
| 48 | + .find(path => app.services[path] === service); |
| 49 | + } |
| 50 | + }; |
| 51 | + // Create the hook object that gets passed through |
| 52 | + const hookObject = utils.hookObject(method, 'before', arguments, hookData); |
| 53 | + // Get all hooks |
| 54 | + const hooks = { |
| 55 | + // For before hooks the app hooks will run first |
| 56 | + before: getHooks(app, this, 'before', method), |
| 57 | + // For after and error hooks the app hooks will run last |
| 58 | + after: getHooks(app, this, 'after', method, true), |
| 59 | + error: getHooks(app, this, 'error', method, true) |
| 60 | + }; |
| 61 | + |
| 62 | + // Process all before hooks |
| 63 | + return processHooks.call(this, hooks.before, hookObject) |
| 64 | + // Use the hook object to call the original method |
| 65 | + .then(hookObject => { |
| 66 | + if (typeof hookObject.result !== 'undefined') { |
| 67 | + return Promise.resolve(hookObject); |
| 68 | + } |
| 69 | + |
| 70 | + return new Promise((resolve, reject) => { |
| 71 | + const args = utils.makeArguments(hookObject); |
| 72 | + // The method may not be normalized yet so we have to handle both |
| 73 | + // ways, either by callback or by Promise |
| 74 | + const callback = function (error, result) { |
| 75 | + if (error) { |
| 76 | + reject(error); |
| 77 | + } else { |
| 78 | + hookObject.result = result; |
| 79 | + resolve(hookObject); |
| 80 | + } |
| 81 | + }; |
| 82 | + |
| 83 | + // We replace the callback with resolving the promise |
| 84 | + args.splice(args.length - 1, 1, callback); |
| 85 | + |
| 86 | + const result = _super(...args); |
| 87 | + |
| 88 | + if (isPromise(result)) { |
| 89 | + result.then(data => callback(null, data), callback); |
| 90 | + } |
| 91 | + }); |
| 92 | + }) |
| 93 | + // Make a copy of hookObject from `before` hooks and update type |
| 94 | + .then(hookObject => Object.assign({}, hookObject, { type: 'after' })) |
| 95 | + // Run through all `after` hooks |
| 96 | + .then(processHooks.bind(this, hooks.after)) |
| 97 | + // Finally, return the result |
| 98 | + .then(hookObject => hookObject.result) |
| 99 | + // Handle errors |
| 100 | + .catch(error => { |
| 101 | + const errorHook = Object.assign({}, error.hook || hookObject, { |
| 102 | + type: 'error', |
| 103 | + original: error.hook, |
| 104 | + error |
| 105 | + }); |
| 106 | + |
| 107 | + return processHooks |
| 108 | + .call(this, hooks.error, errorHook) |
| 109 | + .then(hook => Promise.reject(hook.error)); |
| 110 | + }); |
| 111 | + }; |
| 112 | + }); |
| 113 | + |
| 114 | + service.mixin(mixin); |
| 115 | + |
| 116 | + // Before hooks that were registered in the service |
| 117 | + if (old.before) { |
| 118 | + service.before(old.before); |
| 119 | + } |
| 120 | + |
| 121 | + // After hooks that were registered in the service |
| 122 | + if (old.after) { |
| 123 | + service.after(old.after); |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +function configure () { |
| 128 | + return function () { |
| 129 | + const app = this; |
| 130 | + |
| 131 | + addHookTypes(app); |
| 132 | + |
| 133 | + Proto.mixin(baseMixin(app.methods), app); |
| 134 | + |
| 135 | + this.mixins.unshift(hookMixin); |
| 136 | + }; |
| 137 | +} |
| 138 | + |
| 139 | +export default configure; |
0 commit comments