@feathersjs/feathers 5.0.0-pre.3 → 5.0.0-pre.31

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,109 +0,0 @@
1
- import {
2
- getManager, HookContextData, HookManager, HookMap, HOOKS, hooks, Middleware
3
- } from '../dependencies';
4
- import {
5
- Service, ServiceOptions, HookContext, FeathersService, Application
6
- } from '../declarations';
7
- import { defaultServiceArguments, getHookMethods } from '../service';
8
- import {
9
- collectLegacyHooks,
10
- enableLegacyHooks,
11
- fromAfterHook,
12
- fromBeforeHook,
13
- fromErrorHooks
14
- } from './legacy';
15
-
16
- export { fromAfterHook, fromBeforeHook, fromErrorHooks };
17
-
18
- export function createContext (service: Service<any>, method: string, data: HookContextData = {}) {
19
- const createContext = (service as any)[method].createContext;
20
-
21
- if (typeof createContext !== 'function') {
22
- throw new Error(`Can not create context for method ${method}`);
23
- }
24
-
25
- return createContext(data) as HookContext;
26
- }
27
-
28
- export class FeathersHookManager<A> extends HookManager {
29
- constructor (public app: A, public method: string) {
30
- super();
31
- this._middleware = [];
32
- }
33
-
34
- collectMiddleware (self: any, args: any[]): Middleware[] {
35
- const app = this.app as any as Application;
36
- const appHooks = app.appHooks[HOOKS].concat(app.appHooks[this.method] || []);
37
- const legacyAppHooks = collectLegacyHooks(this.app, this.method);
38
- const middleware = super.collectMiddleware(self, args);
39
- const legacyHooks = collectLegacyHooks(self, this.method);
40
-
41
- return [...appHooks, ...legacyAppHooks, ...middleware, ...legacyHooks];
42
- }
43
-
44
- initializeContext (self: any, args: any[], context: HookContext) {
45
- const ctx = super.initializeContext(self, args, context);
46
-
47
- ctx.params = ctx.params || {};
48
-
49
- return ctx;
50
- }
51
-
52
- middleware (mw: Middleware[]) {
53
- this._middleware.push(...mw);
54
- return this;
55
- }
56
- }
57
-
58
- export function hookMixin<A> (
59
- this: A, service: FeathersService<A>, path: string, options: ServiceOptions
60
- ) {
61
- if (typeof service.hooks === 'function') {
62
- return service;
63
- }
64
-
65
- const app = this;
66
- const serviceMethodHooks = getHookMethods(service, options).reduce((res, method) => {
67
- const params = (defaultServiceArguments as any)[method] || [ 'data', 'params' ];
68
-
69
- res[method] = new FeathersHookManager<A>(app, method)
70
- .params(...params)
71
- .props({
72
- app,
73
- path,
74
- method,
75
- service,
76
- event: null,
77
- type: null
78
- });
79
-
80
- return res;
81
- }, {} as HookMap);
82
- const handleLegacyHooks = enableLegacyHooks(service);
83
-
84
- hooks(service, serviceMethodHooks);
85
-
86
- service.hooks = function (this: any, hookOptions: any) {
87
- if (hookOptions.before || hookOptions.after || hookOptions.error) {
88
- return handleLegacyHooks.call(this, hookOptions);
89
- }
90
-
91
- if (Array.isArray(hookOptions)) {
92
- return hooks(this, hookOptions);
93
- }
94
-
95
- Object.keys(hookOptions).forEach(method => {
96
- const manager = getManager(this[method]);
97
-
98
- if (!(manager instanceof FeathersHookManager)) {
99
- throw new Error(`Method ${method} is not a Feathers hooks enabled service method`);
100
- }
101
-
102
- manager.middleware(hookOptions[method]);
103
- });
104
-
105
- return this;
106
- }
107
-
108
- return service;
109
- }
@@ -1,138 +0,0 @@
1
- import { _ } from '../dependencies';
2
- import { LegacyHookFunction } from '../declarations';
3
-
4
- const { each } = _;
5
-
6
- export function fromBeforeHook (hook: LegacyHookFunction) {
7
- return (context: any, next: any) => {
8
- context.type = 'before';
9
-
10
- return Promise.resolve(hook.call(context.self, context)).then(() => {
11
- context.type = null;
12
- return next();
13
- });
14
- };
15
- }
16
-
17
- export function fromAfterHook (hook: LegacyHookFunction) {
18
- return (context: any, next: any) => {
19
- return next().then(() => {
20
- context.type = 'after';
21
- return hook.call(context.self, context)
22
- }).then(() => {
23
- context.type = null;
24
- });
25
- }
26
- }
27
-
28
- export function fromErrorHooks (hooks: LegacyHookFunction[]) {
29
- return (context: any, next: any) => {
30
- return next().catch((error: any) => {
31
- let promise: Promise<any> = Promise.resolve();
32
-
33
- context.original = { ...context };
34
- context.error = error;
35
- context.type = 'error';
36
-
37
- delete context.result;
38
-
39
- for (const hook of hooks) {
40
- promise = promise.then(() => hook.call(context.self, context))
41
- }
42
-
43
- return promise.then(() => {
44
- context.type = null;
45
-
46
- if (context.result === undefined) {
47
- throw context.error;
48
- }
49
- });
50
- });
51
- }
52
- }
53
-
54
- export function collectLegacyHooks (target: any, method: string) {
55
- const {
56
- before: { [method]: before = [] },
57
- after: { [method]: after = [] },
58
- error: { [method]: error = [] }
59
- } = target.__hooks;
60
- const beforeHooks = before;
61
- const afterHooks = [...after].reverse();
62
- const errorHook = fromErrorHooks(error);
63
-
64
- return [errorHook, ...beforeHooks, ...afterHooks];
65
- }
66
-
67
- // Converts different hook registration formats into the
68
- // same internal format
69
- export function convertHookData (obj: any) {
70
- let hook: any = {};
71
-
72
- if (Array.isArray(obj)) {
73
- hook = { all: obj };
74
- } else if (typeof obj !== 'object') {
75
- hook = { all: [ obj ] };
76
- } else {
77
- each(obj, function (value, key) {
78
- hook[key] = !Array.isArray(value) ? [ value ] : value;
79
- });
80
- }
81
-
82
- return hook;
83
- }
84
-
85
- // Add `.hooks` functionality to an object
86
- export function enableLegacyHooks (
87
- obj: any,
88
- methods: string[] = ['find', 'get', 'create', 'update', 'patch', 'remove'],
89
- types: string[] = ['before', 'after', 'error']
90
- ) {
91
- const hookData: any = {};
92
-
93
- types.forEach(type => {
94
- // Initialize properties where hook functions are stored
95
- hookData[type] = {};
96
- });
97
-
98
- // Add non-enumerable `__hooks` property to the object
99
- Object.defineProperty(obj, '__hooks', {
100
- configurable: true,
101
- value: hookData,
102
- writable: true
103
- });
104
-
105
- return function legacyHooks (this: any, allHooks: any) {
106
- each(allHooks, (current: any, type) => {
107
- if (!this.__hooks[type]) {
108
- throw new Error(`'${type}' is not a valid hook type`);
109
- }
110
-
111
- const hooks = convertHookData(current);
112
-
113
- each(hooks, (_value, method) => {
114
- if (method !== 'all' && methods.indexOf(method) === -1) {
115
- throw new Error(`'${method}' is not a valid hook method`);
116
- }
117
- });
118
-
119
- methods.forEach(method => {
120
- let currentHooks = [...(hooks.all || []), ...(hooks[method] || [])];
121
-
122
- this.__hooks[type][method] = this.__hooks[type][method] || [];
123
-
124
- if (type === 'before') {
125
- currentHooks = currentHooks.map(fromBeforeHook);
126
- }
127
-
128
- if (type === 'after') {
129
- currentHooks = currentHooks.map(fromAfterHook);
130
- }
131
-
132
- this.__hooks[type][method].push(...currentHooks);
133
- });
134
- });
135
-
136
- return this;
137
- }
138
- }