Skip to content

Commit 145b366

Browse files
authored
fix(schema): Always resolve dispatch in resolveAll and add getDispatch method (#2645)
1 parent 2e45efc commit 145b366

3 files changed

Lines changed: 14 additions & 24 deletions

File tree

packages/schema/src/hooks/resolve.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ const runResolvers = async <T, H extends HookContext>(
2828
let current: any = data
2929

3030
for (const resolver of resolvers) {
31-
current = await resolver.resolve(current, ctx, status)
31+
if (resolver && typeof resolver.resolve === 'function') {
32+
current = await resolver.resolve(current, ctx, status)
33+
}
3234
}
3335

3436
return current as T
@@ -51,6 +53,9 @@ export type ResolveAllSettings<H extends HookContext> = {
5153

5254
export const DISPATCH = Symbol('@feathersjs/schema/dispatch')
5355

56+
export const getDispatch = (value: any) =>
57+
typeof value === 'object' && value !== null && value[DISPATCH] !== undefined ? value[DISPATCH] : value
58+
5459
export const resolveQuery =
5560
<T, H extends HookContext>(...resolvers: Resolver<T, H>[]) =>
5661
async (context: H, next?: NextFunction) => {
@@ -137,20 +142,19 @@ export const resolveDispatch =
137142
const ctx = getContext(context)
138143
const status = context.params.resolve
139144
const { isPaginated, data } = getData(context)
140-
const resolveDispatch = async (current: any) => {
145+
const resolveAndGetDispatch = async (current: any) => {
141146
const resolved = await runResolvers(resolvers, current, ctx, status)
142147

143148
return Object.keys(resolved).reduce((res, key) => {
144-
const value = current[key]
145-
const hasDispatch = typeof value === 'object' && value !== null && value[DISPATCH] !== undefined
146-
147-
res[key] = hasDispatch ? value[DISPATCH] : value
149+
res[key] = getDispatch(current[key])
148150

149151
return res
150152
}, {} as any)
151153
}
152154

153-
const result = await (Array.isArray(data) ? Promise.all(data.map(resolveDispatch)) : resolveDispatch(data))
155+
const result = await (Array.isArray(data)
156+
? Promise.all(data.map(resolveAndGetDispatch))
157+
: resolveAndGetDispatch(data))
154158
const dispatch = isPaginated
155159
? {
156160
...context.result,
@@ -169,9 +173,7 @@ export const resolveDispatch =
169173
export const resolveAll = <H extends HookContext>(map: ResolveAllSettings<H>) => {
170174
const middleware = []
171175

172-
if (map.dispatch) {
173-
middleware.push(resolveDispatch(map.dispatch))
174-
}
176+
middleware.push(resolveDispatch(map.dispatch))
175177

176178
if (map.result) {
177179
middleware.push(resolveResult(map.result))

packages/schema/test/fixture.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,7 @@ export const messageSchema = schema({
8181
required: ['text', 'userId'],
8282
properties: {
8383
text: { type: 'string' },
84-
userId: { type: 'number' },
85-
secret: { type: 'boolean' }
84+
userId: { type: 'number' }
8685
}
8786
} as const)
8887

@@ -121,12 +120,6 @@ export const messageResultResolver = resolve<MessageResult, HookContext<Applicat
121120
}
122121
})
123122

124-
export const messageDispatchResolver = resolve<MessageResult, HookContext<Application>>({
125-
properties: {
126-
secret: () => undefined
127-
}
128-
})
129-
130123
export const messageQuerySchema = schema({
131124
$id: 'MessageQuery',
132125
type: 'object',
@@ -182,7 +175,6 @@ app.use('paginatedMessages', memory({ paginate: { default: 10 } }))
182175

183176
app.service('messages').hooks([
184177
resolveAll({
185-
dispatch: messageDispatchResolver,
186178
result: messageResultResolver,
187179
query: messageQueryResolver
188180
}),

packages/schema/test/hooks.test.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ describe('@feathersjs/schema/hooks', () => {
2020
)[0]
2121
message = await app.service('messages').create({
2222
text,
23-
userId: user.id,
24-
secret: true
23+
userId: user.id
2524
})
2625
messageOnPaginatedService = await app.service('paginatedMessages').create({
2726
text,
@@ -42,7 +41,6 @@ describe('@feathersjs/schema/hooks', () => {
4241
it('resolves results and handles resolver errors (#2534)', async () => {
4342
const payload = {
4443
userId: user.id,
45-
secret: true,
4644
text
4745
}
4846

@@ -92,7 +90,6 @@ describe('@feathersjs/schema/hooks', () => {
9290
it('resolves get result with the object on result', async () => {
9391
const payload = {
9492
userId: user.id,
95-
secret: true,
9693
text
9794
}
9895

@@ -155,7 +152,6 @@ describe('@feathersjs/schema/hooks', () => {
155152
const service = app.service('messages')
156153
const context = await service.get(0, {}, createContext(service as any, 'get'))
157154

158-
assert.ok(context.result.secret)
159155
assert.strictEqual(context.result.user.password, 'hashed')
160156

161157
assert.deepStrictEqual(context.dispatch, {

0 commit comments

Comments
 (0)