@feathersjs/feathers 5.0.0-pre.11 → 5.0.0-pre.17

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.
@@ -6,16 +6,20 @@ import {
6
6
  } from '../declarations';
7
7
  import { defaultServiceArguments, getHookMethods } from '../service';
8
8
  import {
9
- collectLegacyHooks,
10
- enableLegacyHooks,
11
- fromAfterHook,
9
+ collectRegularHooks,
10
+ enableRegularHooks
11
+ } from './regular';
12
+
13
+ export {
12
14
  fromBeforeHook,
15
+ fromBeforeHooks,
16
+ fromAfterHook,
17
+ fromAfterHooks,
18
+ fromErrorHook,
13
19
  fromErrorHooks
14
- } from './legacy';
20
+ } from './regular';
15
21
 
16
- export { fromAfterHook, fromBeforeHook, fromErrorHooks };
17
-
18
- export function createContext (service: Service<any>, method: string, data: HookContextData = {}) {
22
+ export function createContext (service: Service, method: string, data: HookContextData = {}) {
19
23
  const createContext = (service as any)[method].createContext;
20
24
 
21
25
  if (typeof createContext !== 'function') {
@@ -34,11 +38,11 @@ export class FeathersHookManager<A> extends HookManager {
34
38
  collectMiddleware (self: any, args: any[]): Middleware[] {
35
39
  const app = this.app as any as Application;
36
40
  const appHooks = app.appHooks[HOOKS].concat(app.appHooks[this.method] || []);
37
- const legacyAppHooks = collectLegacyHooks(this.app, this.method);
41
+ const regularAppHooks = collectRegularHooks(this.app, this.method);
38
42
  const middleware = super.collectMiddleware(self, args);
39
- const legacyHooks = collectLegacyHooks(self, this.method);
43
+ const regularHooks = collectRegularHooks(self, this.method);
40
44
 
41
- return [...appHooks, ...legacyAppHooks, ...middleware, ...legacyHooks];
45
+ return [...appHooks, ...regularAppHooks, ...middleware, ...regularHooks];
42
46
  }
43
47
 
44
48
  initializeContext (self: any, args: any[], context: HookContext) {
@@ -63,7 +67,9 @@ export function hookMixin<A> (
63
67
  }
64
68
 
65
69
  const app = this;
66
- const serviceMethodHooks = getHookMethods(service, options).reduce((res, method) => {
70
+ const hookMethods = getHookMethods(service, options);
71
+
72
+ const serviceMethodHooks = hookMethods.reduce((res, method) => {
67
73
  const params = (defaultServiceArguments as any)[method] || [ 'data', 'params' ];
68
74
 
69
75
  res[method] = new FeathersHookManager<A>(app, method)
@@ -74,18 +80,25 @@ export function hookMixin<A> (
74
80
  method,
75
81
  service,
76
82
  event: null,
77
- type: null
83
+ type: null,
84
+ get statusCode () {
85
+ return this.http?.statusCode;
86
+ },
87
+ set statusCode (value: number) {
88
+ (this.http ||= {}).statusCode = value;
89
+ }
78
90
  });
79
91
 
80
92
  return res;
81
93
  }, {} as HookMap);
82
- const handleLegacyHooks = enableLegacyHooks(service);
94
+
95
+ const handleRegularHooks = enableRegularHooks(service, hookMethods);
83
96
 
84
97
  hooks(service, serviceMethodHooks);
85
98
 
86
99
  service.hooks = function (this: any, hookOptions: any) {
87
100
  if (hookOptions.before || hookOptions.after || hookOptions.error) {
88
- return handleLegacyHooks.call(this, hookOptions);
101
+ return handleRegularHooks.call(this, hookOptions);
89
102
  }
90
103
 
91
104
  if (Array.isArray(hookOptions)) {
@@ -0,0 +1,207 @@
1
+ import { HookFunction, RegularHookFunction, RegularHookMap } from '../declarations';
2
+ import { defaultServiceMethods } from '../service';
3
+
4
+ const runHook = <A, S> (hook: RegularHookFunction<A, S>, context: any, type?: string) => {
5
+ if (type) context.type = type;
6
+ return Promise.resolve(hook.call(context.self, context))
7
+ .then((res: any) => {
8
+ if (type) context.type = null;
9
+ if (res && res !== context) {
10
+ Object.assign(context, res);
11
+ }
12
+ });
13
+ };
14
+
15
+ export function fromBeforeHook<A, S> (hook: RegularHookFunction<A, S>): HookFunction<A, S> {
16
+ return (context, next) => {
17
+ return runHook(hook, context, 'before').then(next);
18
+ };
19
+ }
20
+
21
+ export function fromAfterHook<A, S> (hook: RegularHookFunction<A, S>): HookFunction<A, S> {
22
+ return (context, next) => {
23
+ return next().then(() => runHook(hook, context, 'after'));
24
+ }
25
+ }
26
+
27
+ export function fromErrorHook<A, S> (hook: RegularHookFunction<A, S>): HookFunction<A, S> {
28
+ return (context, next) => {
29
+ return next().catch((error: any) => {
30
+ if (context.error !== error || context.result !== undefined) {
31
+ (context as any).original = { ...context };
32
+ context.error = error;
33
+ delete context.result;
34
+ }
35
+
36
+ return runHook(hook, context, 'error').then(() => {
37
+ if (context.result === undefined && context.error !== undefined) {
38
+ throw context.error;
39
+ }
40
+ });
41
+ });
42
+ }
43
+ }
44
+
45
+ const RunHooks = <A, S> (hooks: RegularHookFunction<A, S>[]) => (context: any) => {
46
+ return hooks.reduce((promise, hook) => {
47
+ return promise.then(() => runHook(hook, context))
48
+ }, Promise.resolve(undefined));
49
+ };
50
+
51
+ export function fromBeforeHooks<A, S> (hooks: RegularHookFunction<A, S>[]) {
52
+ return fromBeforeHook(RunHooks(hooks));
53
+ }
54
+
55
+ export function fromAfterHooks<A, S> (hooks: RegularHookFunction<A, S>[]) {
56
+ return fromAfterHook(RunHooks(hooks));
57
+ }
58
+
59
+ export function fromErrorHooks<A, S> (hooks: RegularHookFunction<A, S>[]) {
60
+ return fromErrorHook(RunHooks(hooks));
61
+ }
62
+
63
+ export function collectRegularHooks (target: any, method: string) {
64
+ return target.__hooks.hooks[method] || [];
65
+ }
66
+
67
+ // Converts different hook registration formats into the
68
+ // same internal format
69
+ export function convertHookData (input: any) {
70
+ const result: { [ method: string ]: RegularHookFunction[] } = {};
71
+
72
+ if (Array.isArray(input)) {
73
+ result.all = input;
74
+ } else if (typeof input !== 'object') {
75
+ result.all = [ input ];
76
+ } else {
77
+ for (const key of Object.keys(input)) {
78
+ const value = input[key];
79
+ result[key] = Array.isArray(value) ? value : [ value ];
80
+ }
81
+ }
82
+
83
+ return result;
84
+ }
85
+
86
+ type RegularType = 'before' | 'after' | 'error';
87
+
88
+ type RegularMap = { [ type in RegularType ]: ReturnType< typeof convertHookData > };
89
+
90
+ type RegularAdapter = HookFunction & { hooks: RegularHookFunction[] };
91
+
92
+ type RegularStore = {
93
+ before: { [ method: string ]: RegularAdapter },
94
+ after: { [ method: string ]: RegularAdapter },
95
+ error: { [ method: string ]: RegularAdapter },
96
+ hooks: { [ method: string ]: HookFunction[] }
97
+ };
98
+
99
+ const types: RegularType[] = ['before', 'after', 'error'];
100
+
101
+ const isType = (value: any): value is RegularType => types.includes(value);
102
+
103
+ const wrappers = {
104
+ before: fromBeforeHooks,
105
+ after: fromAfterHooks,
106
+ error: fromErrorHooks
107
+ };
108
+
109
+ const createStore = (methods: string[]) => {
110
+ const store: RegularStore = {
111
+ before: {},
112
+ after: {},
113
+ error: {},
114
+ hooks: {}
115
+ };
116
+
117
+ for (const method of methods) {
118
+ store.hooks[method] = [];
119
+ }
120
+
121
+ return store;
122
+ };
123
+
124
+ const setStore = (object: any, store: RegularStore) => {
125
+ Object.defineProperty(object, '__hooks', {
126
+ configurable: true,
127
+ value: store,
128
+ writable: true
129
+ });
130
+ };
131
+
132
+ const getStore = (object: any): RegularStore => object.__hooks;
133
+
134
+ const createMap = (input: RegularHookMap<any, any>, methods: string[]) => {
135
+ const map = {} as RegularMap;
136
+
137
+ Object.keys(input).forEach((type) => {
138
+ if (!isType(type)) {
139
+ throw new Error(`'${type}' is not a valid hook type`);
140
+ }
141
+
142
+ const data = convertHookData(input[type]);
143
+
144
+ Object.keys(data).forEach((method) => {
145
+ if (method !== 'all' && !methods.includes(method)) {
146
+ throw new Error(`'${method}' is not a valid hook method`);
147
+ }
148
+ });
149
+
150
+ map[type] = data;
151
+ });
152
+
153
+ return map;
154
+ };
155
+
156
+ const createAdapter = (type: RegularType) => {
157
+ const hooks: RegularHookFunction[] = [];
158
+ const hook = wrappers[type](hooks);
159
+ const adapter = Object.assign(hook, { hooks });
160
+
161
+ return adapter;
162
+ };
163
+
164
+ const updateStore = (store: RegularStore, map: RegularMap) => {
165
+ Object.keys(store.hooks).forEach((method) => {
166
+ let adapted = false;
167
+
168
+ Object.keys(map).forEach((key) => {
169
+ const type = key as RegularType;
170
+ const allHooks = map[type].all || [];
171
+ const methodHooks = map[type][method] || [];
172
+
173
+ if (allHooks.length || methodHooks.length) {
174
+ const adapter = store[type][method] ||= (adapted = true, createAdapter(type));
175
+
176
+ adapter.hooks.push(...allHooks, ...methodHooks);
177
+ }
178
+ });
179
+
180
+ if (adapted) {
181
+ store.hooks[method] = [
182
+ store.error[method],
183
+ store.before[method],
184
+ store.after[method]
185
+ ].filter(hook => hook);
186
+ }
187
+ });
188
+ };
189
+
190
+ // Add `.hooks` functionality to an object
191
+ export function enableRegularHooks (
192
+ object: any,
193
+ methods: string[] = defaultServiceMethods
194
+ ) {
195
+ const store = createStore(methods);
196
+
197
+ setStore(object, store);
198
+
199
+ return function regularHooks (this: any, input: RegularHookMap<any, any>) {
200
+ const store = getStore(this);
201
+ const map = createMap(input, methods);
202
+
203
+ updateStore(store, map);
204
+
205
+ return this;
206
+ }
207
+ }
package/src/service.ts CHANGED
@@ -24,6 +24,7 @@ export const defaultEventMap = {
24
24
  export const protectedMethods = Object.keys(Object.prototype)
25
25
  .concat(Object.keys(EventEmitter.prototype))
26
26
  .concat([
27
+ 'all',
27
28
  'before',
28
29
  'after',
29
30
  'error',
package/src/version.ts CHANGED
@@ -1 +1 @@
1
- export default 'development';
1
+ export default '5.0.0-pre.17';
@@ -1,7 +0,0 @@
1
- import { LegacyHookFunction } from '../declarations';
2
- export declare function fromBeforeHook(hook: LegacyHookFunction): (context: any, next: any) => Promise<any>;
3
- export declare function fromAfterHook(hook: LegacyHookFunction): (context: any, next: any) => any;
4
- export declare function fromErrorHooks(hooks: LegacyHookFunction[]): (context: any, next: any) => any;
5
- export declare function collectLegacyHooks(target: any, method: string): any[];
6
- export declare function convertHookData(obj: any): any;
7
- export declare function enableLegacyHooks(obj: any, methods?: string[], types?: string[]): (this: any, allHooks: any) => any;
@@ -1,126 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.enableLegacyHooks = exports.convertHookData = exports.collectLegacyHooks = exports.fromErrorHooks = exports.fromAfterHook = exports.fromBeforeHook = void 0;
4
- const dependencies_1 = require("../dependencies");
5
- const { each } = dependencies_1._;
6
- const mergeContext = (context) => (res) => {
7
- if (res && res !== context) {
8
- Object.assign(context, res);
9
- }
10
- return res;
11
- };
12
- function fromBeforeHook(hook) {
13
- return (context, next) => {
14
- context.type = 'before';
15
- return Promise.resolve(hook.call(context.self, context))
16
- .then(mergeContext(context))
17
- .then(() => {
18
- context.type = null;
19
- return next();
20
- });
21
- };
22
- }
23
- exports.fromBeforeHook = fromBeforeHook;
24
- function fromAfterHook(hook) {
25
- return (context, next) => {
26
- return next()
27
- .then(() => {
28
- context.type = 'after';
29
- return hook.call(context.self, context);
30
- })
31
- .then(mergeContext(context))
32
- .then(() => {
33
- context.type = null;
34
- });
35
- };
36
- }
37
- exports.fromAfterHook = fromAfterHook;
38
- function fromErrorHooks(hooks) {
39
- return (context, next) => {
40
- return next().catch((error) => {
41
- let promise = Promise.resolve();
42
- context.original = { ...context };
43
- context.error = error;
44
- context.type = 'error';
45
- delete context.result;
46
- for (const hook of hooks) {
47
- promise = promise.then(() => hook.call(context.self, context))
48
- .then(mergeContext(context));
49
- }
50
- return promise.then(() => {
51
- context.type = null;
52
- if (context.result === undefined) {
53
- throw context.error;
54
- }
55
- });
56
- });
57
- };
58
- }
59
- exports.fromErrorHooks = fromErrorHooks;
60
- function collectLegacyHooks(target, method) {
61
- const { before: { [method]: before = [] }, after: { [method]: after = [] }, error: { [method]: error = [] } } = target.__hooks;
62
- const beforeHooks = before;
63
- const afterHooks = [...after].reverse();
64
- const errorHook = fromErrorHooks(error);
65
- return [errorHook, ...beforeHooks, ...afterHooks];
66
- }
67
- exports.collectLegacyHooks = collectLegacyHooks;
68
- // Converts different hook registration formats into the
69
- // same internal format
70
- function convertHookData(obj) {
71
- let hook = {};
72
- if (Array.isArray(obj)) {
73
- hook = { all: obj };
74
- }
75
- else if (typeof obj !== 'object') {
76
- hook = { all: [obj] };
77
- }
78
- else {
79
- each(obj, function (value, key) {
80
- hook[key] = !Array.isArray(value) ? [value] : value;
81
- });
82
- }
83
- return hook;
84
- }
85
- exports.convertHookData = convertHookData;
86
- // Add `.hooks` functionality to an object
87
- function enableLegacyHooks(obj, methods = ['find', 'get', 'create', 'update', 'patch', 'remove'], types = ['before', 'after', 'error']) {
88
- const hookData = {};
89
- types.forEach(type => {
90
- // Initialize properties where hook functions are stored
91
- hookData[type] = {};
92
- });
93
- // Add non-enumerable `__hooks` property to the object
94
- Object.defineProperty(obj, '__hooks', {
95
- configurable: true,
96
- value: hookData,
97
- writable: true
98
- });
99
- return function legacyHooks(allHooks) {
100
- each(allHooks, (current, type) => {
101
- if (!this.__hooks[type]) {
102
- throw new Error(`'${type}' is not a valid hook type`);
103
- }
104
- const hooks = convertHookData(current);
105
- each(hooks, (_value, method) => {
106
- if (method !== 'all' && methods.indexOf(method) === -1) {
107
- throw new Error(`'${method}' is not a valid hook method`);
108
- }
109
- });
110
- methods.forEach(method => {
111
- let currentHooks = [...(hooks.all || []), ...(hooks[method] || [])];
112
- this.__hooks[type][method] = this.__hooks[type][method] || [];
113
- if (type === 'before') {
114
- currentHooks = currentHooks.map(fromBeforeHook);
115
- }
116
- if (type === 'after') {
117
- currentHooks = currentHooks.map(fromAfterHook);
118
- }
119
- this.__hooks[type][method].push(...currentHooks);
120
- });
121
- });
122
- return this;
123
- };
124
- }
125
- exports.enableLegacyHooks = enableLegacyHooks;
126
- //# sourceMappingURL=legacy.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"legacy.js","sourceRoot":"","sources":["../../src/hooks/legacy.ts"],"names":[],"mappings":";;;AAAA,kDAAoC;AAGpC,MAAM,EAAE,IAAI,EAAE,GAAG,gBAAC,CAAC;AACnB,MAAM,YAAY,GAAG,CAAC,OAAY,EAAE,EAAE,CAAC,CAAC,GAAQ,EAAE,EAAE;IAClD,IAAI,GAAG,IAAI,GAAG,KAAK,OAAO,EAAE;QAC1B,MAAM,CAAC,MAAM,CAAC,OAAO,EAAE,GAAG,CAAC,CAAC;KAC7B;IACD,OAAO,G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4
- const { each } = _;
5
- const mergeContext = (context: any) => (res: any) => {
6
- if (res && res !== context) {
7
- Object.assign(context, res);
8
- }
9
- return res;
10
- }
11
-
12
- export function fromBeforeHook (hook: LegacyHookFunction) {
13
- return (context: any, next: any) => {
14
- context.type = 'before';
15
-
16
- return Promise.resolve(hook.call(context.self, context))
17
- .then(mergeContext(context))
18
- .then(() => {
19
- context.type = null;
20
- return next();
21
- });
22
- };
23
- }
24
-
25
- export function fromAfterHook (hook: LegacyHookFunction) {
26
- return (context: any, next: any) => {
27
- return next()
28
- .then(() => {
29
- context.type = 'after';
30
- return hook.call(context.self, context)
31
- })
32
- .then(mergeContext(context))
33
- .then(() => {
34
- context.type = null;
35
- });
36
- }
37
- }
38
-
39
- export function fromErrorHooks (hooks: LegacyHookFunction[]) {
40
- return (context: any, next: any) => {
41
- return next().catch((error: any) => {
42
- let promise: Promise<any> = Promise.resolve();
43
-
44
- context.original = { ...context };
45
- context.error = error;
46
- context.type = 'error';
47
-
48
- delete context.result;
49
-
50
- for (const hook of hooks) {
51
- promise = promise.then(() => hook.call(context.self, context))
52
- .then(mergeContext(context))
53
- }
54
-
55
- return promise.then(() => {
56
- context.type = null;
57
-
58
- if (context.result === undefined) {
59
- throw context.error;
60
- }
61
- });
62
- });
63
- }
64
- }
65
-
66
- export function collectLegacyHooks (target: any, method: string) {
67
- const {
68
- before: { [method]: before = [] },
69
- after: { [method]: after = [] },
70
- error: { [method]: error = [] }
71
- } = target.__hooks;
72
- const beforeHooks = before;
73
- const afterHooks = [...after].reverse();
74
- const errorHook = fromErrorHooks(error);
75
-
76
- return [errorHook, ...beforeHooks, ...afterHooks];
77
- }
78
-
79
- // Converts different hook registration formats into the
80
- // same internal format
81
- export function convertHookData (obj: any) {
82
- let hook: any = {};
83
-
84
- if (Array.isArray(obj)) {
85
- hook = { all: obj };
86
- } else if (typeof obj !== 'object') {
87
- hook = { all: [ obj ] };
88
- } else {
89
- each(obj, function (value, key) {
90
- hook[key] = !Array.isArray(value) ? [ value ] : value;
91
- });
92
- }
93
-
94
- return hook;
95
- }
96
-
97
- // Add `.hooks` functionality to an object
98
- export function enableLegacyHooks (
99
- obj: any,
100
- methods: string[] = ['find', 'get', 'create', 'update', 'patch', 'remove'],
101
- types: string[] = ['before', 'after', 'error']
102
- ) {
103
- const hookData: any = {};
104
-
105
- types.forEach(type => {
106
- // Initialize properties where hook functions are stored
107
- hookData[type] = {};
108
- });
109
-
110
- // Add non-enumerable `__hooks` property to the object
111
- Object.defineProperty(obj, '__hooks', {
112
- configurable: true,
113
- value: hookData,
114
- writable: true
115
- });
116
-
117
- return function legacyHooks (this: any, allHooks: any) {
118
- each(allHooks, (current: any, type) => {
119
- if (!this.__hooks[type]) {
120
- throw new Error(`'${type}' is not a valid hook type`);
121
- }
122
-
123
- const hooks = convertHookData(current);
124
-
125
- each(hooks, (_value, method) => {
126
- if (method !== 'all' && methods.indexOf(method) === -1) {
127
- throw new Error(`'${method}' is not a valid hook method`);
128
- }
129
- });
130
-
131
- methods.forEach(method => {
132
- let currentHooks = [...(hooks.all || []), ...(hooks[method] || [])];
133
-
134
- this.__hooks[type][method] = this.__hooks[type][method] || [];
135
-
136
- if (type === 'before') {
137
- currentHooks = currentHooks.map(fromBeforeHook);
138
- }
139
-
140
- if (type === 'after') {
141
- currentHooks = currentHooks.map(fromAfterHook);
142
- }
143
-
144
- this.__hooks[type][method].push(...currentHooks);
145
- });
146
- });
147
-
148
- return this;
149
- }
150
- }