feathersjs/feathers-hooks-common

View on GitHub
src/hooks/keep-query.ts

Summary

Maintainability
A
3 hrs
Test Coverage
import type { HookContext } from '@feathersjs/feathers';
import { pluck } from '../common';
import { checkContext } from '../utils/check-context';

/**
 * Keep certain fields in the query object, deleting the rest.
 * @see https://hooks-common.feathersjs.com/hooks.html#keepquery
 */
export function keepQuery<H extends HookContext = HookContext>(...fieldNames: string[]) {
  return (context: H) => {
    checkContext(context, 'before', null, 'keepQuery');

    const query = context.params.query || {};
    context.params.query = pluck(query, fieldNames);

    return context;
  };
}