From 8756c022127e85827e6792c847e3f064a0914d2b Mon Sep 17 00:00:00 2001 From: daffl Date: Mon, 23 May 2022 20:28:41 -0700 Subject: [PATCH] fix(schema): Allows resolveData with different resolvers based on method --- package.json | 2 +- packages/feathers/tsconfig.json | 2 +- packages/schema/src/hooks/resolve.ts | 55 ++++++++++++++++++---------- packages/schema/test/fixture.ts | 6 ++- 4 files changed, 42 insertions(+), 23 deletions(-) diff --git a/package.json b/package.json index 02f26d7728..94e1d17a35 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,7 @@ "install": "lerna bootstrap", "publish": "lerna publish && git commit -am \"chore: Update changelog\" && git push origin", "publish:premajor": "lerna publish premajor --preid pre --pre-dist-tag pre && git commit -am \"chore: Update version and changelog\" && git push origin", - "publish:prerelease": "lerna publish prerelease --preid pre --pre-dist-tag pre --dist-tag pre && git commit -am \"chore: Update version and changelog\" && git push origin", + "publish:prerelease": "lerna publish prerelease --preid pre --pre-dist-tag pre --dist-tag pre --force-publish && git commit -am \"chore: Update version and changelog\" && git push origin", "lint": "eslint packages/**/src/**/*.ts packages/**/test/**/*.ts --fix", "update-dependencies": "ncu -u && lerna exec -- ncu -u -x node-fetch", "clean": "find . -name node_modules -exec rm -rf '{}' + && find . -name package-lock.json -exec rm -rf '{}' +", diff --git a/packages/feathers/tsconfig.json b/packages/feathers/tsconfig.json index 316fd41336..b184375928 100644 --- a/packages/feathers/tsconfig.json +++ b/packages/feathers/tsconfig.json @@ -4,6 +4,6 @@ "src/**/*.ts" ], "compilerOptions": { - "outDir": "lib" + "outDir": "lib" } } diff --git a/packages/schema/src/hooks/resolve.ts b/packages/schema/src/hooks/resolve.ts index b9a3bf595d..ffcd897d0b 100644 --- a/packages/schema/src/hooks/resolve.ts +++ b/packages/schema/src/hooks/resolve.ts @@ -34,6 +34,21 @@ const runResolvers = async ( return current as T; } +export type ResolverSetting = Resolver|Resolver[]; + +export type DataResolvers = { + create: Resolver + patch: Resolver + update: Resolver +} + +export type ResolveAllSettings = { + data?: DataResolvers + query?: Resolver + result?: Resolver + dispatch?: Resolver +} + export const DISPATCH = Symbol('@feathersjs/schema/dispatch'); export const resolveQuery = (...resolvers: Resolver[]) => @@ -52,9 +67,10 @@ export const resolveQuery = (...resolvers: Resolver (...resolvers: Resolver[]) => +export const resolveData = (settings: DataResolvers|Resolver) => async (context: H, next?: NextFunction) => { if (context.method === 'create' || context.method === 'patch' || context.method === 'update') { + const resolvers = settings instanceof Resolver ? [ settings ] : [ settings[context.method] ]; const ctx = getContext(context); const data = context.data; @@ -146,25 +162,24 @@ export const resolveDispatch = (...resolvers: Resolve }); }; -export type ResolveAllSettings = { - data?: Resolver|Resolver[] - query?: Resolver|Resolver[] - result?: Resolver|Resolver[] - dispatch?: Resolver|Resolver[] -} +export const resolveAll = (map: ResolveAllSettings) => { + const middleware = []; -const getResolvers = ( - map: ResolveAllSettings, - name: keyof ResolveAllSettings -) => { - const value = map[name]; + if (map.dispatch) { + middleware.push(resolveDispatch(map.dispatch)); + } - return Array.isArray(value) ? value : (value !== undefined ? [ value ] : []); -} + if (map.result) { + middleware.push(resolveResult(map.result)); + } + + if (map.query) { + middleware.push(resolveQuery(map.query)); + } + + if (map.data) { + middleware.push(resolveData(map.data)); + } -export const resolveAll = (map: ResolveAllSettings) => compose([ - resolveDispatch(...getResolvers(map, 'dispatch')), - resolveResult(...getResolvers(map, 'result')), - resolveQuery(...getResolvers(map, 'query')), - resolveData(...getResolvers(map, 'data')) -]) + return compose(middleware); +} diff --git a/packages/schema/test/fixture.ts b/packages/schema/test/fixture.ts index d139fc0a3b..6d3b9110a0 100644 --- a/packages/schema/test/fixture.ts +++ b/packages/schema/test/fixture.ts @@ -189,7 +189,11 @@ app.service('users').hooks([ app.service('users').hooks({ create: [ validateData(userSchema), - resolveData(userDataResolver) + resolveData({ + create: userDataResolver, + patch: userDataResolver, + update: userDataResolver + }) ] });