diff --git a/packages/schema/src/hooks/resolve.ts b/packages/schema/src/hooks/resolve.ts index 10991ff121..8c36b5778a 100644 --- a/packages/schema/src/hooks/resolve.ts +++ b/packages/schema/src/hooks/resolve.ts @@ -28,7 +28,9 @@ const runResolvers = async ( let current: any = data for (const resolver of resolvers) { - current = await resolver.resolve(current, ctx, status) + if (resolver && typeof resolver.resolve === 'function') { + current = await resolver.resolve(current, ctx, status) + } } return current as T @@ -51,6 +53,9 @@ export type ResolveAllSettings = { export const DISPATCH = Symbol('@feathersjs/schema/dispatch') +export const getDispatch = (value: any) => + typeof value === 'object' && value !== null && value[DISPATCH] !== undefined ? value[DISPATCH] : value + export const resolveQuery = (...resolvers: Resolver[]) => async (context: H, next?: NextFunction) => { @@ -137,20 +142,19 @@ export const resolveDispatch = const ctx = getContext(context) const status = context.params.resolve const { isPaginated, data } = getData(context) - const resolveDispatch = async (current: any) => { + const resolveAndGetDispatch = async (current: any) => { const resolved = await runResolvers(resolvers, current, ctx, status) return Object.keys(resolved).reduce((res, key) => { - const value = current[key] - const hasDispatch = typeof value === 'object' && value !== null && value[DISPATCH] !== undefined - - res[key] = hasDispatch ? value[DISPATCH] : value + res[key] = getDispatch(current[key]) return res }, {} as any) } - const result = await (Array.isArray(data) ? Promise.all(data.map(resolveDispatch)) : resolveDispatch(data)) + const result = await (Array.isArray(data) + ? Promise.all(data.map(resolveAndGetDispatch)) + : resolveAndGetDispatch(data)) const dispatch = isPaginated ? { ...context.result, @@ -169,9 +173,7 @@ export const resolveDispatch = export const resolveAll = (map: ResolveAllSettings) => { const middleware = [] - if (map.dispatch) { - middleware.push(resolveDispatch(map.dispatch)) - } + middleware.push(resolveDispatch(map.dispatch)) if (map.result) { middleware.push(resolveResult(map.result)) diff --git a/packages/schema/test/fixture.ts b/packages/schema/test/fixture.ts index b9fb9eed5e..a5159e7f61 100644 --- a/packages/schema/test/fixture.ts +++ b/packages/schema/test/fixture.ts @@ -81,8 +81,7 @@ export const messageSchema = schema({ required: ['text', 'userId'], properties: { text: { type: 'string' }, - userId: { type: 'number' }, - secret: { type: 'boolean' } + userId: { type: 'number' } } } as const) @@ -121,12 +120,6 @@ export const messageResultResolver = resolve>({ - properties: { - secret: () => undefined - } -}) - export const messageQuerySchema = schema({ $id: 'MessageQuery', type: 'object', @@ -182,7 +175,6 @@ app.use('paginatedMessages', memory({ paginate: { default: 10 } })) app.service('messages').hooks([ resolveAll({ - dispatch: messageDispatchResolver, result: messageResultResolver, query: messageQueryResolver }), diff --git a/packages/schema/test/hooks.test.ts b/packages/schema/test/hooks.test.ts index 1c51f96130..84e10c0d1f 100644 --- a/packages/schema/test/hooks.test.ts +++ b/packages/schema/test/hooks.test.ts @@ -20,8 +20,7 @@ describe('@feathersjs/schema/hooks', () => { )[0] message = await app.service('messages').create({ text, - userId: user.id, - secret: true + userId: user.id }) messageOnPaginatedService = await app.service('paginatedMessages').create({ text, @@ -42,7 +41,6 @@ describe('@feathersjs/schema/hooks', () => { it('resolves results and handles resolver errors (#2534)', async () => { const payload = { userId: user.id, - secret: true, text } @@ -92,7 +90,6 @@ describe('@feathersjs/schema/hooks', () => { it('resolves get result with the object on result', async () => { const payload = { userId: user.id, - secret: true, text } @@ -155,7 +152,6 @@ describe('@feathersjs/schema/hooks', () => { const service = app.service('messages') const context = await service.get(0, {}, createContext(service as any, 'get')) - assert.ok(context.result.secret) assert.strictEqual(context.result.user.password, 'hashed') assert.deepStrictEqual(context.dispatch, {