@feathersjs/feathers 5.0.0-pre.22 → 5.0.0-pre.23

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,169 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.enableRegularHooks = exports.convertHookData = exports.collectRegularHooks = exports.fromErrorHooks = exports.fromAfterHooks = exports.fromBeforeHooks = exports.fromErrorHook = exports.fromAfterHook = exports.fromBeforeHook = void 0;
4
- const service_1 = require("../service");
5
- const runHook = (hook, context, type) => {
6
- if (type)
7
- context.type = type;
8
- return Promise.resolve(hook.call(context.self, context))
9
- .then((res) => {
10
- if (type)
11
- context.type = null;
12
- if (res && res !== context) {
13
- Object.assign(context, res);
14
- }
15
- });
16
- };
17
- function fromBeforeHook(hook) {
18
- return (context, next) => {
19
- return runHook(hook, context, 'before').then(next);
20
- };
21
- }
22
- exports.fromBeforeHook = fromBeforeHook;
23
- function fromAfterHook(hook) {
24
- return (context, next) => {
25
- return next().then(() => runHook(hook, context, 'after'));
26
- };
27
- }
28
- exports.fromAfterHook = fromAfterHook;
29
- function fromErrorHook(hook) {
30
- return (context, next) => {
31
- return next().catch((error) => {
32
- if (context.error !== error || context.result !== undefined) {
33
- context.original = { ...context };
34
- context.error = error;
35
- delete context.result;
36
- }
37
- return runHook(hook, context, 'error').then(() => {
38
- if (context.result === undefined && context.error !== undefined) {
39
- throw context.error;
40
- }
41
- });
42
- });
43
- };
44
- }
45
- exports.fromErrorHook = fromErrorHook;
46
- const RunHooks = (hooks) => (context) => {
47
- return hooks.reduce((promise, hook) => {
48
- return promise.then(() => runHook(hook, context));
49
- }, Promise.resolve(undefined));
50
- };
51
- function fromBeforeHooks(hooks) {
52
- return fromBeforeHook(RunHooks(hooks));
53
- }
54
- exports.fromBeforeHooks = fromBeforeHooks;
55
- function fromAfterHooks(hooks) {
56
- return fromAfterHook(RunHooks(hooks));
57
- }
58
- exports.fromAfterHooks = fromAfterHooks;
59
- function fromErrorHooks(hooks) {
60
- return fromErrorHook(RunHooks(hooks));
61
- }
62
- exports.fromErrorHooks = fromErrorHooks;
63
- function collectRegularHooks(target, method) {
64
- return target.__hooks.hooks[method] || [];
65
- }
66
- exports.collectRegularHooks = collectRegularHooks;
67
- // Converts different hook registration formats into the
68
- // same internal format
69
- function convertHookData(input) {
70
- const result = {};
71
- if (Array.isArray(input)) {
72
- result.all = input;
73
- }
74
- else if (typeof input !== 'object') {
75
- result.all = [input];
76
- }
77
- else {
78
- for (const key of Object.keys(input)) {
79
- const value = input[key];
80
- result[key] = Array.isArray(value) ? value : [value];
81
- }
82
- }
83
- return result;
84
- }
85
- exports.convertHookData = convertHookData;
86
- const types = ['before', 'after', 'error'];
87
- const isType = (value) => types.includes(value);
88
- const wrappers = {
89
- before: fromBeforeHooks,
90
- after: fromAfterHooks,
91
- error: fromErrorHooks
92
- };
93
- const createStore = (methods) => {
94
- const store = {
95
- before: {},
96
- after: {},
97
- error: {},
98
- hooks: {}
99
- };
100
- for (const method of methods) {
101
- store.hooks[method] = [];
102
- }
103
- return store;
104
- };
105
- const setStore = (object, store) => {
106
- Object.defineProperty(object, '__hooks', {
107
- configurable: true,
108
- value: store,
109
- writable: true
110
- });
111
- };
112
- const getStore = (object) => object.__hooks;
113
- const createMap = (input, methods) => {
114
- const map = {};
115
- Object.keys(input).forEach((type) => {
116
- if (!isType(type)) {
117
- throw new Error(`'${type}' is not a valid hook type`);
118
- }
119
- const data = convertHookData(input[type]);
120
- Object.keys(data).forEach((method) => {
121
- if (method !== 'all' && !methods.includes(method) && !service_1.defaultServiceMethods.includes(method)) {
122
- throw new Error(`'${method}' is not a valid hook method`);
123
- }
124
- });
125
- map[type] = data;
126
- });
127
- return map;
128
- };
129
- const createAdapter = (type) => {
130
- const hooks = [];
131
- const hook = wrappers[type](hooks);
132
- const adapter = Object.assign(hook, { hooks });
133
- return adapter;
134
- };
135
- const updateStore = (store, map) => {
136
- Object.keys(store.hooks).forEach((method) => {
137
- let adapted = false;
138
- Object.keys(map).forEach((key) => {
139
- var _a;
140
- const type = key;
141
- const allHooks = map[type].all || [];
142
- const methodHooks = map[type][method] || [];
143
- if (allHooks.length || methodHooks.length) {
144
- const adapter = (_a = store[type])[method] || (_a[method] = (adapted = true, createAdapter(type)));
145
- adapter.hooks.push(...allHooks, ...methodHooks);
146
- }
147
- });
148
- if (adapted) {
149
- store.hooks[method] = [
150
- store.error[method],
151
- store.before[method],
152
- store.after[method]
153
- ].filter(hook => hook);
154
- }
155
- });
156
- };
157
- // Add `.hooks` functionality to an object
158
- function enableRegularHooks(object, methods = service_1.defaultServiceMethods) {
159
- const store = createStore(methods);
160
- setStore(object, store);
161
- return function regularHooks(input) {
162
- const store = getStore(this);
163
- const map = createMap(input, methods);
164
- updateStore(store, map);
165
- return this;
166
- };
167
- }
168
- exports.enableRegularHooks = enableRegularHooks;
169
- //# sourceMappingURL=regular.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"regular.js","sourceRoot":"","sources":["../../src/hooks/regular.ts"],"names":[],"mappings":";;;AACA,wCAAmD;AAEnD,MAAM,OAAO,GAAG,CAAQ,IAA+B,EAAE,OAAY,EAAE,IAAa,EAAE,EAAE;IACtF,IAAI,IAAI;QAAE,OAAO,CAAC,IAAI,GAAG,IAAI,CAAC;IAC9B,OAAO,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;SACrD,IAAI,CAAC,CAAC,GAAQ,EAAE,EAAE;QACjB,IAAI,IAAI;YAAE,OAAO,CAAC,IAAI,GAAG,IAAI,CAAC;QAC9B,IAAI,GAAG,IAAI,GAAG,KAAK,OAAO,EAAE;YAC1B,MAAM,CAAC,MAAM,CAAC,OAAO,EAAE,GAAG,CAAC,CAAC;SAC7B;IACH,CAAC,CAAC,CAAC;AACP,CAAC,CAAC;AAEF,SAAgB,cAAc,CAAQ,IAA+B;IACnE,OAAO,CAA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C;AAhBD,gDAgBC"}
@@ -1,5 +0,0 @@
1
- import { EventEmitter } from 'events';
2
- export * from '@feathersjs/commons';
3
- export * from '@feathersjs/hooks';
4
-
5
- export { EventEmitter };
@@ -1,122 +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
- collectRegularHooks,
10
- enableRegularHooks
11
- } from './regular';
12
-
13
- export {
14
- fromBeforeHook,
15
- fromBeforeHooks,
16
- fromAfterHook,
17
- fromAfterHooks,
18
- fromErrorHook,
19
- fromErrorHooks
20
- } from './regular';
21
-
22
- export function createContext (service: Service, method: string, data: HookContextData = {}) {
23
- const createContext = (service as any)[method].createContext;
24
-
25
- if (typeof createContext !== 'function') {
26
- throw new Error(`Can not create context for method ${method}`);
27
- }
28
-
29
- return createContext(data) as HookContext;
30
- }
31
-
32
- export class FeathersHookManager<A> extends HookManager {
33
- constructor (public app: A, public method: string) {
34
- super();
35
- this._middleware = [];
36
- }
37
-
38
- collectMiddleware (self: any, args: any[]): Middleware[] {
39
- const app = this.app as any as Application;
40
- const appHooks = app.appHooks[HOOKS].concat(app.appHooks[this.method] || []);
41
- const regularAppHooks = collectRegularHooks(this.app, this.method);
42
- const middleware = super.collectMiddleware(self, args);
43
- const regularHooks = collectRegularHooks(self, this.method);
44
-
45
- return [...appHooks, ...regularAppHooks, ...middleware, ...regularHooks];
46
- }
47
-
48
- initializeContext (self: any, args: any[], context: HookContext) {
49
- const ctx = super.initializeContext(self, args, context);
50
-
51
- ctx.params = ctx.params || {};
52
-
53
- return ctx;
54
- }
55
-
56
- middleware (mw: Middleware[]) {
57
- this._middleware.push(...mw);
58
- return this;
59
- }
60
- }
61
-
62
- export function hookMixin<A> (
63
- this: A, service: FeathersService<A>, path: string, options: ServiceOptions
64
- ) {
65
- if (typeof service.hooks === 'function') {
66
- return service;
67
- }
68
-
69
- const app = this;
70
- const hookMethods = getHookMethods(service, options);
71
-
72
- const serviceMethodHooks = hookMethods.reduce((res, method) => {
73
- const params = (defaultServiceArguments as any)[method] || [ 'data', 'params' ];
74
-
75
- res[method] = new FeathersHookManager<A>(app, method)
76
- .params(...params)
77
- .props({
78
- app,
79
- path,
80
- method,
81
- service,
82
- event: null,
83
- type: null,
84
- get statusCode () {
85
- return this.http?.status;
86
- },
87
- set statusCode (value: number) {
88
- (this.http ||= {}).status = value;
89
- }
90
- });
91
-
92
- return res;
93
- }, {} as HookMap);
94
-
95
- const handleRegularHooks = enableRegularHooks(service, hookMethods);
96
-
97
- hooks(service, serviceMethodHooks);
98
-
99
- service.hooks = function (this: any, hookOptions: any) {
100
- if (hookOptions.before || hookOptions.after || hookOptions.error) {
101
- return handleRegularHooks.call(this, hookOptions);
102
- }
103
-
104
- if (Array.isArray(hookOptions)) {
105
- return hooks(this, hookOptions);
106
- }
107
-
108
- Object.keys(hookOptions).forEach(method => {
109
- const manager = getManager(this[method]);
110
-
111
- if (!(manager instanceof FeathersHookManager)) {
112
- throw new Error(`Method ${method} is not a Feathers hooks enabled service method`);
113
- }
114
-
115
- manager.middleware(hookOptions[method]);
116
- });
117
-
118
- return this;
119
- }
120
-
121
- return service;
122
- }
@@ -1,207 +0,0 @@
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) && !defaultServiceMethods.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
- }