Skip to content

Commit

Permalink
fix(schema): Always resolve dispatch in resolveAll and add getDispatc…
Browse files Browse the repository at this point in the history
…h method (#2645)
  • Loading branch information
daffl authored May 28, 2022
1 parent 2e45efc commit 145b366
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 24 deletions.
22 changes: 12 additions & 10 deletions packages/schema/src/hooks/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ const runResolvers = async <T, H extends HookContext>(
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
Expand All @@ -51,6 +53,9 @@ export type ResolveAllSettings<H extends HookContext> = {

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 =
<T, H extends HookContext>(...resolvers: Resolver<T, H>[]) =>
async (context: H, next?: NextFunction) => {
Expand Down Expand Up @@ -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,
Expand All @@ -169,9 +173,7 @@ export const resolveDispatch =
export const resolveAll = <H extends HookContext>(map: ResolveAllSettings<H>) => {
const middleware = []

if (map.dispatch) {
middleware.push(resolveDispatch(map.dispatch))
}
middleware.push(resolveDispatch(map.dispatch))

if (map.result) {
middleware.push(resolveResult(map.result))
Expand Down
10 changes: 1 addition & 9 deletions packages/schema/test/fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -121,12 +120,6 @@ export const messageResultResolver = resolve<MessageResult, HookContext<Applicat
}
})

export const messageDispatchResolver = resolve<MessageResult, HookContext<Application>>({
properties: {
secret: () => undefined
}
})

export const messageQuerySchema = schema({
$id: 'MessageQuery',
type: 'object',
Expand Down Expand Up @@ -182,7 +175,6 @@ app.use('paginatedMessages', memory({ paginate: { default: 10 } }))

app.service('messages').hooks([
resolveAll({
dispatch: messageDispatchResolver,
result: messageResultResolver,
query: messageQueryResolver
}),
Expand Down
6 changes: 1 addition & 5 deletions packages/schema/test/hooks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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, {
Expand Down

0 comments on commit 145b366

Please # to comment.