forked from feathersjs-ecosystem/feathers-hooks-common
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsifter.ts
More file actions
executable file
·28 lines (22 loc) · 924 Bytes
/
sifter.ts
File metadata and controls
executable file
·28 lines (22 loc) · 924 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import { BadRequest } from '@feathersjs/errors';
import type { HookContext } from '@feathersjs/feathers';
import type { SyncContextFunction } from '../types';
import { checkContext } from '../utils/check-context';
import { getItems } from '../utils/get-items';
import { replaceItems } from '../utils/replace-items';
export function sifter<H extends HookContext = HookContext>(
siftFunc: SyncContextFunction<(item: any) => boolean, H>,
) {
return (context: H) => {
checkContext(context, 'after', 'find', 'sifter');
if (typeof siftFunc !== 'function') {
throw new BadRequest('The sifter param must be a function. (sifter)');
}
const sifter = siftFunc(context);
if (typeof sifter !== 'function') {
throw new BadRequest('The result of calling the sifter param must be a function. (sifter)');
}
replaceItems(context, getItems(context).filter(sifter));
return context;
};
}