Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/authentication-local/src/hooks/hash-password.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export default function hashPassword (field: string, options: HashPasswordOption
}

if (typeof next === 'function') {
await next();
return next();
}
};
}
38 changes: 20 additions & 18 deletions packages/authentication-local/src/hooks/protect.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import omit from 'lodash/omit';
import { HookContext, NextFunction } from '@feathersjs/feathers';

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

if (typeof next === 'function') {
await next();
}
return async (context: HookContext, next?: NextFunction) => {
if (typeof next === 'function') {
await next();
}

const result = context.dispatch || context.result;
const result = context.dispatch || context.result;

if (result) {
if (Array.isArray(result)) {
context.dispatch = result.map(o);
} else if (result.data && context.method === 'find') {
context.dispatch = Object.assign({}, result, {
data: result.data.map(o)
});
} else {
context.dispatch = o(result);
}
if (result) {
if (Array.isArray(result)) {
context.dispatch = result.map(o);
} else if (result.data && context.method === 'find') {
context.dispatch = Object.assign({}, result, {
data: result.data.map(o)
});
} else {
context.dispatch = o(result);
}

if (context.params && context.params.provider) {
context.result = context.dispatch;
if (context.params && context.params.provider) {
context.result = context.dispatch;
}
}
}
};
};
53 changes: 32 additions & 21 deletions packages/schema/src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const getContext = (context: HookContext) => {
}

export const resolveQuery = <T> (resolver: Resolver<T, HookContext>) =>
async (context: HookContext, next: NextFunction) => {
async (context: HookContext, next?: NextFunction) => {
const ctx = getContext(context);
const data = context?.params?.query || {};
const query = await resolver.resolve(data, ctx, {
Expand All @@ -26,11 +26,13 @@ export const resolveQuery = <T> (resolver: Resolver<T, HookContext>) =>
query
}

return next();
if (typeof next === 'function') {
return next();
}
};

export const resolveData = <T> (resolver: Resolver<T, HookContext>) =>
async (context: HookContext, next: NextFunction) => {
async (context: HookContext, next?: NextFunction) => {
const ctx = getContext(context);
const data = context.data;
const status = {
Expand All @@ -45,27 +47,32 @@ export const resolveData = <T> (resolver: Resolver<T, HookContext>) =>
context.data = await resolver.resolve(data, ctx, status);
}

return next();
if (typeof next === 'function') {
return next();
}
};

export const resolveResult = <T> (resolver: Resolver<T, HookContext>) =>
async (context: HookContext, next: NextFunction) => {
const { $resolve: properties, ...query } = context.params?.query || {};
const { resolve } = context.params;
const status = {
originalContext: context,
...resolve,
properties
};
async (context: HookContext, next?: NextFunction) => {
if (typeof next === 'function') {
const { $resolve: properties, ...query } = context.params?.query || {};
const resolve = {
originalContext: context,
...context.params.resolve,
properties
};

context.params = {
...context.params,
query
}
context.params = {
...context.params,
resolve,
query
}

await next();
await next();
}

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

export const validateQuery = (schema: Schema<any>) =>
async (context: HookContext, next: NextFunction) => {
async (context: HookContext, next?: NextFunction) => {
const data = context?.params?.query || {};

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

return next();
if (typeof next === 'function') {
return next();
}
} catch (error: any) {
throw (error.ajv ? new BadRequest(error.message, error.errors) : error);
}
};

export const validateData = (schema: Schema<any>) =>
async (context: HookContext, next: NextFunction) => {
async (context: HookContext, next?: NextFunction) => {
const data = context.data;

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

return next();
if (typeof next === 'function') {
return next();
}
};
2 changes: 1 addition & 1 deletion packages/schema/src/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export interface ResolverConfig<T, C> {
export interface ResolverStatus<T, C> {
path: string[];
originalContext?: C;
properties?: (keyof T)[];
properties?: string[];
stack: PropertyResolver<T, any, C>[];
}

Expand Down