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

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.
package/src/hooks.ts ADDED
@@ -0,0 +1,215 @@
1
+ import {
2
+ getManager,
3
+ HookContextData,
4
+ HookManager,
5
+ HookMap as BaseHookMap,
6
+ hooks,
7
+ Middleware,
8
+ collect
9
+ } from '@feathersjs/hooks'
10
+ import {
11
+ Service,
12
+ ServiceOptions,
13
+ HookContext,
14
+ FeathersService,
15
+ HookMap,
16
+ AroundHookFunction,
17
+ HookFunction
18
+ } from './declarations'
19
+ import { defaultServiceArguments, getHookMethods } from './service'
20
+
21
+ type HookType = 'before' | 'after' | 'error' | 'around'
22
+
23
+ type ConvertedMap = { [type in HookType]: ReturnType<typeof convertHookData> }
24
+
25
+ type HookStore = {
26
+ around: { [method: string]: AroundHookFunction[] }
27
+ before: { [method: string]: HookFunction[] }
28
+ after: { [method: string]: HookFunction[] }
29
+ error: { [method: string]: HookFunction[] }
30
+ collected: { [method: string]: AroundHookFunction[] }
31
+ }
32
+
33
+ type HookEnabled = { __hooks: HookStore }
34
+
35
+ const types: HookType[] = ['before', 'after', 'error', 'around']
36
+
37
+ const isType = (value: any): value is HookType => types.includes(value)
38
+
39
+ // Converts different hook registration formats into the
40
+ // same internal format
41
+ export function convertHookData(input: any) {
42
+ const result: { [method: string]: HookFunction[] | AroundHookFunction[] } = {}
43
+
44
+ if (Array.isArray(input)) {
45
+ result.all = input
46
+ } else if (typeof input !== 'object') {
47
+ result.all = [input]
48
+ } else {
49
+ for (const key of Object.keys(input)) {
50
+ const value = input[key]
51
+ result[key] = Array.isArray(value) ? value : [value]
52
+ }
53
+ }
54
+
55
+ return result
56
+ }
57
+
58
+ export function collectHooks(target: HookEnabled, method: string) {
59
+ const { collected, around } = target.__hooks
60
+
61
+ return [
62
+ ...(around.all || []),
63
+ ...(around[method] || []),
64
+ ...(collected.all || []),
65
+ ...(collected[method] || [])
66
+ ] as AroundHookFunction[]
67
+ }
68
+
69
+ // Add `.hooks` functionality to an object
70
+ export function enableHooks(object: any) {
71
+ const store: HookStore = {
72
+ around: {},
73
+ before: {},
74
+ after: {},
75
+ error: {},
76
+ collected: {}
77
+ }
78
+
79
+ Object.defineProperty(object, '__hooks', {
80
+ configurable: true,
81
+ value: store,
82
+ writable: true
83
+ })
84
+
85
+ return function registerHooks(this: HookEnabled, input: HookMap<any, any>) {
86
+ const store = this.__hooks
87
+ const map = Object.keys(input).reduce((map, type) => {
88
+ if (!isType(type)) {
89
+ throw new Error(`'${type}' is not a valid hook type`)
90
+ }
91
+
92
+ map[type] = convertHookData(input[type])
93
+
94
+ return map
95
+ }, {} as ConvertedMap)
96
+ const types = Object.keys(map) as HookType[]
97
+
98
+ types.forEach((type) =>
99
+ Object.keys(map[type]).forEach((method) => {
100
+ const mapHooks = map[type][method]
101
+ const storeHooks: any[] = (store[type][method] ||= [])
102
+
103
+ storeHooks.push(...mapHooks)
104
+
105
+ if (store.before[method] || store.after[method] || store.error[method]) {
106
+ const collected = collect({
107
+ before: store.before[method] || [],
108
+ after: store.after[method] || [],
109
+ error: store.error[method] || []
110
+ })
111
+
112
+ store.collected[method] = [collected]
113
+ }
114
+ })
115
+ )
116
+
117
+ return this
118
+ }
119
+ }
120
+
121
+ export function createContext(service: Service, method: string, data: HookContextData = {}) {
122
+ const createContext = (service as any)[method].createContext
123
+
124
+ if (typeof createContext !== 'function') {
125
+ throw new Error(`Can not create context for method ${method}`)
126
+ }
127
+
128
+ return createContext(data) as HookContext
129
+ }
130
+
131
+ export class FeathersHookManager<A> extends HookManager {
132
+ constructor(public app: A, public method: string) {
133
+ super()
134
+ this._middleware = []
135
+ }
136
+
137
+ collectMiddleware(self: any, args: any[]): Middleware[] {
138
+ const appHooks = collectHooks(this.app as any as HookEnabled, this.method)
139
+ const middleware = super.collectMiddleware(self, args)
140
+ const methodHooks = collectHooks(self, this.method)
141
+
142
+ return [...appHooks, ...middleware, ...methodHooks]
143
+ }
144
+
145
+ initializeContext(self: any, args: any[], context: HookContext) {
146
+ const ctx = super.initializeContext(self, args, context)
147
+
148
+ ctx.params = ctx.params || {}
149
+
150
+ return ctx
151
+ }
152
+
153
+ middleware(mw: Middleware[]) {
154
+ this._middleware.push(...mw)
155
+ return this
156
+ }
157
+ }
158
+
159
+ export function hookMixin<A>(this: A, service: FeathersService<A>, path: string, options: ServiceOptions) {
160
+ if (typeof service.hooks === 'function') {
161
+ return service
162
+ }
163
+
164
+ const hookMethods = getHookMethods(service, options)
165
+
166
+ const serviceMethodHooks = hookMethods.reduce((res, method) => {
167
+ const params = (defaultServiceArguments as any)[method] || ['data', 'params']
168
+
169
+ res[method] = new FeathersHookManager<A>(this, method).params(...params).props({
170
+ app: this,
171
+ path,
172
+ method,
173
+ service,
174
+ event: null,
175
+ type: null,
176
+ get statusCode() {
177
+ return this.http?.status
178
+ },
179
+ set statusCode(value: number) {
180
+ this.http = this.http || {}
181
+ this.http.status = value
182
+ }
183
+ })
184
+
185
+ return res
186
+ }, {} as BaseHookMap)
187
+
188
+ const registerHooks = enableHooks(service)
189
+
190
+ hooks(service, serviceMethodHooks)
191
+
192
+ service.hooks = function (this: any, hookOptions: any) {
193
+ if (hookOptions.before || hookOptions.after || hookOptions.error || hookOptions.around) {
194
+ return registerHooks.call(this, hookOptions)
195
+ }
196
+
197
+ if (Array.isArray(hookOptions)) {
198
+ return hooks(this, hookOptions)
199
+ }
200
+
201
+ Object.keys(hookOptions).forEach((method) => {
202
+ const manager = getManager(this[method])
203
+
204
+ if (!(manager instanceof FeathersHookManager)) {
205
+ throw new Error(`Method ${method} is not a Feathers hooks enabled service method`)
206
+ }
207
+
208
+ manager.middleware(hookOptions[method])
209
+ })
210
+
211
+ return this
212
+ }
213
+
214
+ return service
215
+ }
package/src/index.ts CHANGED
@@ -1,19 +1,20 @@
1
- import { setDebug } from './dependencies';
2
- import version from './version';
3
- import { Feathers } from './application';
4
- import { Application } from './declarations';
1
+ import { setDebug } from '@feathersjs/commons'
5
2
 
6
- export function feathers<T = any, S = any> () {
7
- return new Feathers<T, S>() as Application<T, S>;
3
+ import version from './version'
4
+ import { Feathers } from './application'
5
+ import { Application } from './declarations'
6
+
7
+ export function feathers<T = any, S = any>() {
8
+ return new Feathers<T, S>() as Application<T, S>
8
9
  }
9
10
 
10
- feathers.setDebug = setDebug;
11
+ feathers.setDebug = setDebug
11
12
 
12
- export { version, Feathers };
13
- export * from './hooks/index';
14
- export * from './declarations';
15
- export * from './service';
13
+ export { version, Feathers }
14
+ export * from './hooks'
15
+ export * from './declarations'
16
+ export * from './service'
16
17
 
17
18
  if (typeof module !== 'undefined') {
18
- module.exports = Object.assign(feathers, module.exports);
19
+ module.exports = Object.assign(feathers, module.exports)
19
20
  }
package/src/service.ts CHANGED
@@ -1,18 +1,19 @@
1
- import { createSymbol } from './dependencies';
2
- import { ServiceOptions } from './declarations';
1
+ import { EventEmitter } from 'events'
2
+ import { createSymbol } from '@feathersjs/commons'
3
+ import { ServiceOptions } from './declarations'
3
4
 
4
- export const SERVICE = createSymbol('@feathersjs/service');
5
+ export const SERVICE = createSymbol('@feathersjs/service')
5
6
 
6
7
  export const defaultServiceArguments = {
7
- find: [ 'params' ],
8
- get: [ 'id', 'params' ],
9
- create: [ 'data', 'params' ],
10
- update: [ 'id', 'data', 'params' ],
11
- patch: [ 'id', 'data', 'params' ],
12
- remove: [ 'id', 'params' ]
8
+ find: ['params'],
9
+ get: ['id', 'params'],
10
+ create: ['data', 'params'],
11
+ update: ['id', 'data', 'params'],
12
+ patch: ['id', 'data', 'params'],
13
+ remove: ['id', 'params']
13
14
  }
14
15
 
15
- export const defaultServiceMethods = Object.keys(defaultServiceArguments);
16
+ export const defaultServiceMethods = Object.keys(defaultServiceArguments)
16
17
 
17
18
  export const defaultEventMap = {
18
19
  create: 'created',
@@ -21,59 +22,55 @@ export const defaultEventMap = {
21
22
  remove: 'removed'
22
23
  }
23
24
 
24
- export function getHookMethods (service: any, options: ServiceOptions) {
25
- const { methods } = options;
25
+ export const protectedMethods = Object.keys(Object.prototype)
26
+ .concat(Object.keys(EventEmitter.prototype))
27
+ .concat(['all', 'around', 'before', 'after', 'error', 'hooks', 'setup', 'teardown', 'publish'])
26
28
 
27
- return defaultServiceMethods.filter(m =>
28
- typeof service[m] === 'function' && !methods.includes(m)
29
- ).concat(methods);
29
+ export function getHookMethods(service: any, options: ServiceOptions) {
30
+ const { methods } = options
31
+
32
+ return defaultServiceMethods
33
+ .filter((m) => typeof service[m] === 'function' && !methods.includes(m))
34
+ .concat(methods)
30
35
  }
31
36
 
32
- export function getServiceOptions (
33
- service: any, options: ServiceOptions = {}
34
- ): ServiceOptions {
35
- const existingOptions = service[SERVICE];
37
+ export function getServiceOptions(service: any, options: ServiceOptions = {}): ServiceOptions {
38
+ const existingOptions = service[SERVICE]
36
39
 
37
40
  if (existingOptions) {
38
- return existingOptions;
41
+ return existingOptions
39
42
  }
40
43
 
41
44
  const {
42
- methods = defaultServiceMethods.filter(method =>
43
- typeof service[method] === 'function'
44
- ),
45
+ methods = defaultServiceMethods.filter((method) => typeof service[method] === 'function'),
45
46
  events = service.events || []
46
- } = options;
47
- const {
48
- serviceEvents = Object.values(defaultEventMap).concat(events)
49
- } = options;
47
+ } = options
48
+ const { serviceEvents = Object.values(defaultEventMap).concat(events) } = options
50
49
 
51
50
  return {
52
51
  ...options,
53
52
  events,
54
53
  methods,
55
54
  serviceEvents
56
- };
55
+ }
57
56
  }
58
57
 
59
- export function wrapService (
60
- location: string, service: any, options: ServiceOptions
61
- ) {
58
+ export function wrapService(location: string, service: any, options: ServiceOptions) {
62
59
  // Do nothing if this is already an initialized
63
60
  if (service[SERVICE]) {
64
- return service;
61
+ return service
65
62
  }
66
63
 
67
- const protoService = Object.create(service);
68
- const serviceOptions = getServiceOptions(service, options);
64
+ const protoService = Object.create(service)
65
+ const serviceOptions = getServiceOptions(service, options)
69
66
 
70
67
  if (Object.keys(serviceOptions.methods).length === 0 && typeof service.setup !== 'function') {
71
- throw new Error(`Invalid service object passed for path \`${location}\``);
68
+ throw new Error(`Invalid service object passed for path \`${location}\``)
72
69
  }
73
70
 
74
71
  Object.defineProperty(protoService, SERVICE, {
75
72
  value: serviceOptions
76
- });
73
+ })
77
74
 
78
- return protoService;
75
+ return protoService
79
76
  }
package/src/version.ts CHANGED
@@ -1 +1 @@
1
- export default '5.0.0-pre.3';
1
+ export default '5.0.0-pre.30'
@@ -1,4 +0,0 @@
1
- import { EventEmitter } from 'events';
2
- export * from '@feathersjs/commons';
3
- export * from '@feathersjs/hooks';
4
- export { EventEmitter };
@@ -1,18 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
- }) : (function(o, m, k, k2) {
6
- if (k2 === undefined) k2 = k;
7
- o[k2] = m[k];
8
- }));
9
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
- };
12
- Object.defineProperty(exports, "__esModule", { value: true });
13
- exports.EventEmitter = void 0;
14
- const events_1 = require("events");
15
- Object.defineProperty(exports, "EventEmitter", { enumerable: true, get: function () { return events_1.EventEmitter; } });
16
- __exportStar(require("@feathersjs/commons"), exports);
17
- __exportStar(require("@feathersjs/hooks"), exports);
18
- //# sourceMappingURL=dependencies.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"dependencies.js","sourceRoot":"","sources":["../src/dependencies.ts"],"names":[],"mappings":";;;;;;;;;;;;;AAAA,mCAAsC;AAI7B,6FAJA,qBAAY,OAIA;AAHrB,sDAAoC;AACpC,oDAAkC"}
@@ -1,14 +0,0 @@
1
- import { HookContextData, HookManager, Middleware } from '../dependencies';
2
- import { Service, ServiceOptions, HookContext, FeathersService, Application } from '../declarations';
3
- import { fromAfterHook, fromBeforeHook, fromErrorHooks } from './legacy';
4
- export { fromAfterHook, fromBeforeHook, fromErrorHooks };
5
- export declare function createContext(service: Service<any>, method: string, data?: HookContextData): HookContext<Application<any, any>, any>;
6
- export declare class FeathersHookManager<A> extends HookManager {
7
- app: A;
8
- method: string;
9
- constructor(app: A, method: string);
10
- collectMiddleware(self: any, args: any[]): Middleware[];
11
- initializeContext(self: any, args: any[], context: HookContext): import("@feathersjs/hooks/lib/base").HookContext<any, any>;
12
- middleware(mw: Middleware[]): this;
13
- }
14
- export declare function hookMixin<A>(this: A, service: FeathersService<A>, path: string, options: ServiceOptions): FeathersService<A, Service<any, Partial<any>>>;
@@ -1,84 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.hookMixin = exports.FeathersHookManager = exports.createContext = exports.fromErrorHooks = exports.fromBeforeHook = exports.fromAfterHook = void 0;
4
- const dependencies_1 = require("../dependencies");
5
- const service_1 = require("../service");
6
- const legacy_1 = require("./legacy");
7
- Object.defineProperty(exports, "fromAfterHook", { enumerable: true, get: function () { return legacy_1.fromAfterHook; } });
8
- Object.defineProperty(exports, "fromBeforeHook", { enumerable: true, get: function () { return legacy_1.fromBeforeHook; } });
9
- Object.defineProperty(exports, "fromErrorHooks", { enumerable: true, get: function () { return legacy_1.fromErrorHooks; } });
10
- function createContext(service, method, data = {}) {
11
- const createContext = service[method].createContext;
12
- if (typeof createContext !== 'function') {
13
- throw new Error(`Can not create context for method ${method}`);
14
- }
15
- return createContext(data);
16
- }
17
- exports.createContext = createContext;
18
- class FeathersHookManager extends dependencies_1.HookManager {
19
- constructor(app, method) {
20
- super();
21
- this.app = app;
22
- this.method = method;
23
- this._middleware = [];
24
- }
25
- collectMiddleware(self, args) {
26
- const app = this.app;
27
- const appHooks = app.appHooks[dependencies_1.HOOKS].concat(app.appHooks[this.method] || []);
28
- const legacyAppHooks = legacy_1.collectLegacyHooks(this.app, this.method);
29
- const middleware = super.collectMiddleware(self, args);
30
- const legacyHooks = legacy_1.collectLegacyHooks(self, this.method);
31
- return [...appHooks, ...legacyAppHooks, ...middleware, ...legacyHooks];
32
- }
33
- initializeContext(self, args, context) {
34
- const ctx = super.initializeContext(self, args, context);
35
- ctx.params = ctx.params || {};
36
- return ctx;
37
- }
38
- middleware(mw) {
39
- this._middleware.push(...mw);
40
- return this;
41
- }
42
- }
43
- exports.FeathersHookManager = FeathersHookManager;
44
- function hookMixin(service, path, options) {
45
- if (typeof service.hooks === 'function') {
46
- return service;
47
- }
48
- const app = this;
49
- const serviceMethodHooks = service_1.getHookMethods(service, options).reduce((res, method) => {
50
- const params = service_1.defaultServiceArguments[method] || ['data', 'params'];
51
- res[method] = new FeathersHookManager(app, method)
52
- .params(...params)
53
- .props({
54
- app,
55
- path,
56
- method,
57
- service,
58
- event: null,
59
- type: null
60
- });
61
- return res;
62
- }, {});
63
- const handleLegacyHooks = legacy_1.enableLegacyHooks(service);
64
- dependencies_1.hooks(service, serviceMethodHooks);
65
- service.hooks = function (hookOptions) {
66
- if (hookOptions.before || hookOptions.after || hookOptions.error) {
67
- return handleLegacyHooks.call(this, hookOptions);
68
- }
69
- if (Array.isArray(hookOptions)) {
70
- return dependencies_1.hooks(this, hookOptions);
71
- }
72
- Object.keys(hookOptions).forEach(method => {
73
- const manager = dependencies_1.getManager(this[method]);
74
- if (!(manager instanceof FeathersHookManager)) {
75
- throw new Error(`Method ${method} is not a Feathers hooks enabled service method`);
76
- }
77
- manager.middleware(hookOptions[method]);
78
- });
79
- return this;
80
- };
81
- return service;
82
- }
83
- exports.hookMixin = hookMixin;
84
- //# sourceMappingURL=index.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/hooks/index.ts"],"names":[],"mappings":";;;AAAA,kDAEyB;AAIzB,wCAAqE;AACrE,qCAMkB;AAET,8FALP,sBAAa,OAKO;AAAE,+FAJtB,uBAAc,OAIsB;AAAE,+FAHtC,uBAAc,OAGsC;AAEtD,SAAgB,aAA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I,CAAC,IAAI,EAAE,WAAW,CAAC,CAAC;SAClD;QAED,IAAI,KAAK,CAAC,OAAO,CAAC,WAAW,CAAC,EAAE;YAC9B,OAAO,oBAAK,CAAC,IAAI,EAAE,WAAW,CAAC,CAAC;SACjC;QAED,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE;YACxC,MAAM,OAAO,GAAG,yBAAU,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC;YAEzC,IAAI,CAAC,CAAC,OAAO,YAAY,mBAAmB,CAAC,EAAE;gBAC7C,MAAM,IAAI,KAAK,CAAC,UAAU,MAAM,iDAAiD,CAAC,CAAC;aACpF;YAED,OAAO,CAAC,UAAU,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,CAAC;QAC1C,CAAC,CAAC,CAAC;QAEH,OAAO,IAAI,CAAC;IACd,CAAC,CAAA;IAED,OAAO,OAAO,CAAC;AACjB,CAAC;AAnDD,8BAmDC"}
@@ -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,114 +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
- function fromBeforeHook(hook) {
7
- return (context, next) => {
8
- context.type = 'before';
9
- return Promise.resolve(hook.call(context.self, context)).then(() => {
10
- context.type = null;
11
- return next();
12
- });
13
- };
14
- }
15
- exports.fromBeforeHook = fromBeforeHook;
16
- function fromAfterHook(hook) {
17
- return (context, next) => {
18
- return next().then(() => {
19
- context.type = 'after';
20
- return hook.call(context.self, context);
21
- }).then(() => {
22
- context.type = null;
23
- });
24
- };
25
- }
26
- exports.fromAfterHook = fromAfterHook;
27
- function fromErrorHooks(hooks) {
28
- return (context, next) => {
29
- return next().catch((error) => {
30
- let promise = Promise.resolve();
31
- context.original = Object.assign({}, context);
32
- context.error = error;
33
- context.type = 'error';
34
- delete context.result;
35
- for (const hook of hooks) {
36
- promise = promise.then(() => hook.call(context.self, context));
37
- }
38
- return promise.then(() => {
39
- context.type = null;
40
- if (context.result === undefined) {
41
- throw context.error;
42
- }
43
- });
44
- });
45
- };
46
- }
47
- exports.fromErrorHooks = fromErrorHooks;
48
- function collectLegacyHooks(target, method) {
49
- const { before: { [method]: before = [] }, after: { [method]: after = [] }, error: { [method]: error = [] } } = target.__hooks;
50
- const beforeHooks = before;
51
- const afterHooks = [...after].reverse();
52
- const errorHook = fromErrorHooks(error);
53
- return [errorHook, ...beforeHooks, ...afterHooks];
54
- }
55
- exports.collectLegacyHooks = collectLegacyHooks;
56
- // Converts different hook registration formats into the
57
- // same internal format
58
- function convertHookData(obj) {
59
- let hook = {};
60
- if (Array.isArray(obj)) {
61
- hook = { all: obj };
62
- }
63
- else if (typeof obj !== 'object') {
64
- hook = { all: [obj] };
65
- }
66
- else {
67
- each(obj, function (value, key) {
68
- hook[key] = !Array.isArray(value) ? [value] : value;
69
- });
70
- }
71
- return hook;
72
- }
73
- exports.convertHookData = convertHookData;
74
- // Add `.hooks` functionality to an object
75
- function enableLegacyHooks(obj, methods = ['find', 'get', 'create', 'update', 'patch', 'remove'], types = ['before', 'after', 'error']) {
76
- const hookData = {};
77
- types.forEach(type => {
78
- // Initialize properties where hook functions are stored
79
- hookData[type] = {};
80
- });
81
- // Add non-enumerable `__hooks` property to the object
82
- Object.defineProperty(obj, '__hooks', {
83
- configurable: true,
84
- value: hookData,
85
- writable: true
86
- });
87
- return function legacyHooks(allHooks) {
88
- each(allHooks, (current, type) => {
89
- if (!this.__hooks[type]) {
90
- throw new Error(`'${type}' is not a valid hook type`);
91
- }
92
- const hooks = convertHookData(current);
93
- each(hooks, (_value, method) => {
94
- if (method !== 'all' && methods.indexOf(method) === -1) {
95
- throw new Error(`'${method}' is not a valid hook method`);
96
- }
97
- });
98
- methods.forEach(method => {
99
- let currentHooks = [...(hooks.all || []), ...(hooks[method] || [])];
100
- this.__hooks[type][method] = this.__hooks[type][method] || [];
101
- if (type === 'before') {
102
- currentHooks = currentHooks.map(fromBeforeHook);
103
- }
104
- if (type === 'after') {
105
- currentHooks = currentHooks.map(fromAfterHook);
106
- }
107
- this.__hooks[type][method].push(...currentHooks);
108
- });
109
- });
110
- return this;
111
- };
112
- }
113
- exports.enableLegacyHooks = enableLegacyHooks;
114
- //# 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;AAEnB,SAAgB,cAAc,CAAE,IAAwB;IACtD,OAAO,CAAC,OAAY,EAAE,IAAS,EAAE,EAAE;QACjC,OAAO,CAAC,IAAI,GAAG,QAAQ,CAAC;QAExB,OAAO,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE;YACjE,OAAO,CAAC,IAAI,GAAG,IAAI,CAAC;YACpB,OAAO,IAAI,EAAE,CAAC;QAChB,CAAC,CAAC,CAAC;IACL,CAAC,CAAC;AACJ,CAAC;AATD,wCASC;AAED,SAAgB,aAAa,CAAE,IAAwB;IACrD,OAAO,CAAC,OAAY,EAAE,IAAS,EAAE,EA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