Skip to content

Commit

Permalink
Add getDispatch
Browse files Browse the repository at this point in the history
  • Loading branch information
daffl committed May 28, 2022
1 parent e3cb3ac commit bbc5b2d
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions packages/schema/src/hooks/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,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 @@ -139,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 Down

0 comments on commit bbc5b2d

Please # to comment.