Skip to content

Commit 2522edd

Browse files
authored
fix(hooks): Allow all built-in hooks to be used the async and regular way (#2559)
1 parent 4a02435 commit 2522edd

File tree

4 files changed

+54
-41
lines changed

4 files changed

+54
-41
lines changed

packages/authentication-local/src/hooks/hash-password.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export default function hashPassword (field: string, options: HashPasswordOption
5353
}
5454

5555
if (typeof next === 'function') {
56-
await next();
56+
return next();
5757
}
5858
};
5959
}
Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import omit from 'lodash/omit';
22
import { HookContext, NextFunction } from '@feathersjs/feathers';
33

4-
export default (...fields: string[]) => async (context: HookContext, next?: NextFunction) => {
4+
export default (...fields: string[]) => {
55
const o = (current: any) => {
66
if (typeof current === 'object' && !Array.isArray(current)) {
77
const data = typeof current.toJSON === 'function'
@@ -13,25 +13,27 @@ export default (...fields: string[]) => async (context: HookContext, next?: Next
1313
return current;
1414
};
1515

16-
if (typeof next === 'function') {
17-
await next();
18-
}
16+
return async (context: HookContext, next?: NextFunction) => {
17+
if (typeof next === 'function') {
18+
await next();
19+
}
1920

20-
const result = context.dispatch || context.result;
21+
const result = context.dispatch || context.result;
2122

22-
if (result) {
23-
if (Array.isArray(result)) {
24-
context.dispatch = result.map(o);
25-
} else if (result.data && context.method === 'find') {
26-
context.dispatch = Object.assign({}, result, {
27-
data: result.data.map(o)
28-
});
29-
} else {
30-
context.dispatch = o(result);
31-
}
23+
if (result) {
24+
if (Array.isArray(result)) {
25+
context.dispatch = result.map(o);
26+
} else if (result.data && context.method === 'find') {
27+
context.dispatch = Object.assign({}, result, {
28+
data: result.data.map(o)
29+
});
30+
} else {
31+
context.dispatch = o(result);
32+
}
3233

33-
if (context.params && context.params.provider) {
34-
context.result = context.dispatch;
34+
if (context.params && context.params.provider) {
35+
context.result = context.dispatch;
36+
}
3537
}
36-
}
38+
};
3739
};

packages/schema/src/hooks.ts

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const getContext = (context: HookContext) => {
1414
}
1515

1616
export const resolveQuery = <T> (resolver: Resolver<T, HookContext>) =>
17-
async (context: HookContext, next: NextFunction) => {
17+
async (context: HookContext, next?: NextFunction) => {
1818
const ctx = getContext(context);
1919
const data = context?.params?.query || {};
2020
const query = await resolver.resolve(data, ctx, {
@@ -26,11 +26,13 @@ export const resolveQuery = <T> (resolver: Resolver<T, HookContext>) =>
2626
query
2727
}
2828

29-
return next();
29+
if (typeof next === 'function') {
30+
return next();
31+
}
3032
};
3133

3234
export const resolveData = <T> (resolver: Resolver<T, HookContext>) =>
33-
async (context: HookContext, next: NextFunction) => {
35+
async (context: HookContext, next?: NextFunction) => {
3436
const ctx = getContext(context);
3537
const data = context.data;
3638
const status = {
@@ -45,27 +47,32 @@ export const resolveData = <T> (resolver: Resolver<T, HookContext>) =>
4547
context.data = await resolver.resolve(data, ctx, status);
4648
}
4749

48-
return next();
50+
if (typeof next === 'function') {
51+
return next();
52+
}
4953
};
5054

5155
export const resolveResult = <T> (resolver: Resolver<T, HookContext>) =>
52-
async (context: HookContext, next: NextFunction) => {
53-
const { $resolve: properties, ...query } = context.params?.query || {};
54-
const { resolve } = context.params;
55-
const status = {
56-
originalContext: context,
57-
...resolve,
58-
properties
59-
};
56+
async (context: HookContext, next?: NextFunction) => {
57+
if (typeof next === 'function') {
58+
const { $resolve: properties, ...query } = context.params?.query || {};
59+
const resolve = {
60+
originalContext: context,
61+
...context.params.resolve,
62+
properties
63+
};
6064

61-
context.params = {
62-
...context.params,
63-
query
64-
}
65+
context.params = {
66+
...context.params,
67+
resolve,
68+
query
69+
}
6570

66-
await next();
71+
await next();
72+
}
6773

6874
const ctx = getContext(context);
75+
const status = context.params.resolve;
6976
const data = context.method === 'find' && context.result.data
7077
? context.result.data
7178
: context.result;
@@ -80,7 +87,7 @@ export const resolveResult = <T> (resolver: Resolver<T, HookContext>) =>
8087
};
8188

8289
export const validateQuery = (schema: Schema<any>) =>
83-
async (context: HookContext, next: NextFunction) => {
90+
async (context: HookContext, next?: NextFunction) => {
8491
const data = context?.params?.query || {};
8592

8693
try {
@@ -91,14 +98,16 @@ export const validateQuery = (schema: Schema<any>) =>
9198
query
9299
}
93100

94-
return next();
101+
if (typeof next === 'function') {
102+
return next();
103+
}
95104
} catch (error: any) {
96105
throw (error.ajv ? new BadRequest(error.message, error.errors) : error);
97106
}
98107
};
99108

100109
export const validateData = (schema: Schema<any>) =>
101-
async (context: HookContext, next: NextFunction) => {
110+
async (context: HookContext, next?: NextFunction) => {
102111
const data = context.data;
103112

104113
try {
@@ -113,5 +122,7 @@ export const validateData = (schema: Schema<any>) =>
113122
throw (error.ajv ? new BadRequest(error.message, error.errors) : error);
114123
}
115124

116-
return next();
125+
if (typeof next === 'function') {
126+
return next();
127+
}
117128
};

packages/schema/src/resolver.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export interface ResolverConfig<T, C> {
2222
export interface ResolverStatus<T, C> {
2323
path: string[];
2424
originalContext?: C;
25-
properties?: (keyof T)[];
25+
properties?: string[];
2626
stack: PropertyResolver<T, any, C>[];
2727
}
2828

0 commit comments

Comments
 (0)