Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(schema): Allows resolveData with different resolvers based on method #2644

Merged
merged 1 commit into from
May 24, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix(schema): Allows resolveData with different resolvers based on method
  • Loading branch information
daffl committed May 24, 2022
commit 8756c022127e85827e6792c847e3f064a0914d2b
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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 '{}' +",
2 changes: 1 addition & 1 deletion packages/feathers/tsconfig.json
Original file line number Diff line number Diff line change
@@ -4,6 +4,6 @@
"src/**/*.ts"
],
"compilerOptions": {
"outDir": "lib"
"outDir": "lib"
}
}
55 changes: 35 additions & 20 deletions packages/schema/src/hooks/resolve.ts
Original file line number Diff line number Diff line change
@@ -34,6 +34,21 @@ const runResolvers = async <T, H extends HookContext> (
return current as T;
}

export type ResolverSetting<H extends HookContext> = Resolver<any, H>|Resolver<any, H>[];

export type DataResolvers<H extends HookContext> = {
create: Resolver<any, H>
patch: Resolver<any, H>
update: Resolver<any, H>
}

export type ResolveAllSettings<H extends HookContext> = {
data?: DataResolvers<H>
query?: Resolver<any, H>
result?: Resolver<any, H>
dispatch?: Resolver<any, H>
}

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

export const resolveQuery = <T, H extends HookContext> (...resolvers: Resolver<T, H>[]) =>
@@ -52,9 +67,10 @@ export const resolveQuery = <T, H extends HookContext> (...resolvers: Resolver<T
}
};

export const resolveData = <T, H extends HookContext> (...resolvers: Resolver<T, H>[]) =>
export const resolveData = <H extends HookContext> (settings: DataResolvers<H>|Resolver<any, H>) =>
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 = <T, H extends HookContext> (...resolvers: Resolve
});
};

export type ResolveAllSettings<H extends HookContext> = {
data?: Resolver<any, H>|Resolver<any, H>[]
query?: Resolver<any, H>|Resolver<any, H>[]
result?: Resolver<any, H>|Resolver<any, H>[]
dispatch?: Resolver<any, H>|Resolver<any, H>[]
}
export const resolveAll = <H extends HookContext> (map: ResolveAllSettings<H>) => {
const middleware = [];

const getResolvers = <H extends HookContext> (
map: ResolveAllSettings<H>,
name: keyof ResolveAllSettings<H>
) => {
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 = <H extends HookContext> (map: ResolveAllSettings<H>) => compose([
resolveDispatch(...getResolvers(map, 'dispatch')),
resolveResult(...getResolvers(map, 'result')),
resolveQuery(...getResolvers(map, 'query')),
resolveData(...getResolvers(map, 'data'))
])
return compose(middleware);
}
6 changes: 5 additions & 1 deletion packages/schema/test/fixture.ts
Original file line number Diff line number Diff line change
@@ -189,7 +189,11 @@ app.service('users').hooks([
app.service('users').hooks({
create: [
validateData(userSchema),
resolveData(userDataResolver)
resolveData({
create: userDataResolver,
patch: userDataResolver,
update: userDataResolver
})
]
});