feathers-utils 1.7.2 → 1.9.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. package/README.md +13 -8
  2. package/dist/esm/hooks/checkMulti.d.ts +2 -2
  3. package/dist/esm/hooks/runPerItem.d.ts +2 -3
  4. package/dist/esm/hooks/runPerItem.js +5 -4
  5. package/dist/esm/hooks/setData.d.ts +2 -3
  6. package/dist/esm/hooks/setData.js +10 -4
  7. package/dist/esm/index.d.ts +3 -2
  8. package/dist/esm/index.js +2 -1
  9. package/dist/esm/types.d.ts +12 -2
  10. package/dist/esm/utils/getItemsIsArray.d.ts +3 -0
  11. package/dist/esm/utils/getItemsIsArray.js +10 -0
  12. package/dist/esm/utils/getPaginate.d.ts +1 -1
  13. package/dist/esm/utils/isPaginated.d.ts +1 -1
  14. package/dist/esm/utils/markHookForSkip.d.ts +2 -2
  15. package/dist/esm/utils/setResultEmpty.d.ts +1 -1
  16. package/dist/hooks/checkMulti.d.ts +2 -2
  17. package/dist/hooks/checkMulti.js +2 -2
  18. package/dist/hooks/runPerItem.d.ts +2 -3
  19. package/dist/hooks/runPerItem.js +6 -5
  20. package/dist/hooks/setData.d.ts +2 -3
  21. package/dist/hooks/setData.js +14 -8
  22. package/dist/index.d.ts +3 -2
  23. package/dist/index.js +5 -3
  24. package/dist/mixins/debounce-mixin/DebouncedStore.js +2 -2
  25. package/dist/mixins/debounce-mixin/index.js +1 -1
  26. package/dist/types.d.ts +12 -2
  27. package/dist/utils/filterQuery.js +2 -2
  28. package/dist/utils/getItemsIsArray.d.ts +3 -0
  29. package/dist/utils/getItemsIsArray.js +14 -0
  30. package/dist/utils/getPaginate.d.ts +1 -1
  31. package/dist/utils/isPaginated.d.ts +1 -1
  32. package/dist/utils/isPaginated.js +1 -1
  33. package/dist/utils/markHookForSkip.d.ts +2 -2
  34. package/dist/utils/markHookForSkip.js +1 -1
  35. package/dist/utils/mergeQuery/index.js +38 -38
  36. package/dist/utils/pushSet.js +3 -3
  37. package/dist/utils/setResultEmpty.d.ts +1 -1
  38. package/dist/utils/setResultEmpty.js +2 -2
  39. package/package.json +27 -19
  40. package/src/hooks/checkMulti.ts +3 -1
  41. package/src/hooks/runPerItem.ts +5 -8
  42. package/src/hooks/setData.ts +14 -8
  43. package/src/index.ts +2 -1
  44. package/src/mixins/debounce-mixin/DebouncedStore.ts +48 -48
  45. package/src/mixins/debounce-mixin/index.ts +3 -1
  46. package/src/types.ts +18 -2
  47. package/src/utils/addHook.ts +5 -1
  48. package/src/utils/getItemsIsArray.ts +15 -0
  49. package/src/utils/getPaginate.ts +1 -1
  50. package/src/utils/isMulti.ts +3 -1
  51. package/src/utils/isPaginated.ts +1 -1
  52. package/src/utils/markHookForSkip.ts +2 -2
  53. package/src/utils/setResultEmpty.ts +1 -1
  54. package/src/utils/shouldSkip.ts +4 -1
  55. package/.eslintignore +0 -3
  56. package/.eslintrc.js +0 -44
  57. package/.gitlab-ci.yml +0 -11
  58. package/.mocharc.js +0 -11
  59. package/.nycrc.json +0 -22
  60. package/index.js +0 -9
  61. package/tsconfig-esm.json +0 -9
  62. package/tsconfig.json +0 -16
  63. package/tsconfig.test.json +0 -13
package/README.md CHANGED
@@ -1,11 +1,11 @@
1
1
  # feathers-utils
2
2
 
3
- ![npm](https://img.shields.io/npm/v/feathers-utils)
4
- ![GitHub Workflow Status](https://img.shields.io/github/workflow/status/fratzinger/feathers-utils/Node.js%20CI)
5
- ![Code Climate maintainability](https://img.shields.io/codeclimate/maintainability/fratzinger/feathers-utils)
6
- ![Code Climate coverage](https://img.shields.io/codeclimate/coverage/fratzinger/feathers-utils)
7
- ![David](https://img.shields.io/david/fratzinger/feathers-casl)
8
- ![npm](https://img.shields.io/npm/dm/feathers-utils)
3
+ [![npm](https://img.shields.io/npm/v/feathers-utils)](https://www.npmjs.com/package/feathers-utils)
4
+ [![GitHub Workflow Status](https://img.shields.io/github/workflow/status/fratzinger/feathers-utils/Node.js%20CI)](https://github.com/fratzinger/feathers-utils/actions/workflows/node.js.yml?query=branch%3Amain)
5
+ [![Code Climate maintainability](https://img.shields.io/codeclimate/maintainability/fratzinger/feathers-utils)](https://codeclimate.com/github/fratzinger/feathers-utils)
6
+ [![Code Climate coverage](https://img.shields.io/codeclimate/coverage/fratzinger/feathers-utils)](https://codeclimate.com/github/fratzinger/feathers-utils)
7
+ [![libraries.io](https://img.shields.io/librariesio/release/npm/feathers-utils)](https://libraries.io/npm/feathers-utils)
8
+ [![npm](https://img.shields.io/npm/dm/feathers-utils)](https://www.npmjs.com/package/feathers-utils)
9
9
  [![GitHub license](https://img.shields.io/github/license/fratzinger/feathers-utils)](https://github.com/fratzinger/feathers-utils/blob/main/LICENSE.md)
10
10
 
11
11
 
@@ -19,8 +19,9 @@ npm i feathers-utils
19
19
 
20
20
  ### Hooks
21
21
 
22
- - `hooks/checkMulti()`: throws if the request is **multi** data, but the service has `allowsMulti(method)` returns `false`
23
- - `hooks/setData({ allowUndefined: Boolean })`
22
+ - `checkMulti()`: throws if the request is **multi** data, but the service has `allowsMulti(method)` returns `false`
23
+ - `runPerItem`
24
+ - `setData({ allowUndefined: Boolean })`
24
25
 
25
26
  ### Mixins
26
27
 
@@ -30,9 +31,13 @@ npm i feathers-utils
30
31
 
31
32
  - `addHook`: add hooks to specific services
32
33
  - `filterQuery`
34
+ - `getItemsIsArray(context)`: returns `{ items: any[], isArray: boolean }`
35
+ - `getPaginate`
33
36
  - `isMulti(context) => Boolean`: returns true, if `find`, `create/patch/remove`: multi
37
+ - `isPaginated`
34
38
  - `markHookForSkip`: add hookName to `context.params.skipHooks` - also see `shouldSkip`
35
39
  - `mergeQuery`: deeply merges queries
36
40
  - `mergeArrays`: merges arrays with intersection options
37
41
  - `pushSet`: if existing array: *push*, else *set*
42
+ - `setResultEmpty`
38
43
  - `shouldSkip`: checks `context.params.skipHooks` for `'all' | '${hookName}' | '${type}:${hookName}'` - also see `markHookForSkip`
@@ -1,2 +1,2 @@
1
- import type { HookContext } from "@feathersjs/feathers";
2
- export declare function checkMulti(): ((context: HookContext) => HookContext);
1
+ import type { ReturnSyncHook } from "../types";
2
+ export declare function checkMulti(): ReturnSyncHook;
@@ -1,4 +1,3 @@
1
- import type { HookRunPerItemOptions } from "../types";
1
+ import type { HookRunPerItemOptions, ReturnAsyncHook, Promisable } from "../types";
2
2
  import type { HookContext } from "@feathersjs/feathers";
3
- import type { Promisable } from "type-fest";
4
- export declare const runPerItem: (actionPerItem: (item: any, context: HookContext) => Promisable<any>, options: HookRunPerItemOptions) => (context: HookContext) => Promise<HookContext>;
3
+ export declare const runPerItem: (actionPerItem: (item: any, context: HookContext) => Promisable<any>, options: HookRunPerItemOptions) => ReturnAsyncHook;
@@ -1,19 +1,20 @@
1
- import { getItems } from "feathers-hooks-common";
2
1
  import { shouldSkip } from "../utils/shouldSkip";
2
+ import { getItemsIsArray } from "../utils/getItemsIsArray";
3
3
  const makeOptions = (options) => {
4
4
  options = options || {};
5
5
  return Object.assign({
6
6
  wait: true
7
7
  }, options);
8
8
  };
9
- export const runPerItem = (actionPerItem, options) => {
9
+ export const runPerItem = (
10
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
11
+ actionPerItem, options) => {
10
12
  options = makeOptions(options);
11
13
  return async (context) => {
12
14
  if (shouldSkip("runForItems", context)) {
13
15
  return context;
14
16
  }
15
- let items = getItems(context);
16
- items = (Array.isArray(items)) ? items : [items];
17
+ const { items } = getItemsIsArray(context);
17
18
  const promises = items.map(async (item) => {
18
19
  await actionPerItem(item, context);
19
20
  });
@@ -1,4 +1,3 @@
1
- import type { HookContext } from "@feathersjs/feathers";
2
- import type { HookSetDataOptions } from "../types";
3
1
  import type { PropertyPath } from "lodash";
4
- export declare function setData(from: PropertyPath, to: PropertyPath, _options?: HookSetDataOptions): ((context: HookContext) => HookContext);
2
+ import type { HookSetDataOptions, ReturnSyncHook } from "../types";
3
+ export declare function setData(from: PropertyPath, to: PropertyPath, _options?: HookSetDataOptions): ReturnSyncHook;
@@ -1,8 +1,8 @@
1
1
  import _get from "lodash/get";
2
2
  import _set from "lodash/set";
3
3
  import _has from "lodash/has";
4
- import { getItems } from "feathers-hooks-common";
5
4
  import { Forbidden } from "@feathersjs/errors";
5
+ import { getItemsIsArray } from "../utils/getItemsIsArray";
6
6
  const defaultOptions = {
7
7
  allowUndefined: false,
8
8
  overwrite: true
@@ -10,8 +10,7 @@ const defaultOptions = {
10
10
  export function setData(from, to, _options) {
11
11
  const options = Object.assign({}, defaultOptions, _options);
12
12
  return (context) => {
13
- let items = getItems(context);
14
- items = (Array.isArray(items)) ? items : [items];
13
+ const { items } = getItemsIsArray(context);
15
14
  if (!_has(context, from)) {
16
15
  if (!context.params?.provider || options.allowUndefined === true) {
17
16
  return context;
@@ -23,7 +22,14 @@ export function setData(from, to, _options) {
23
22
  }
24
23
  const val = _get(context, from);
25
24
  items.forEach((item) => {
26
- if (!options.overwrite && _has(item, to)) {
25
+ let overwrite;
26
+ if (typeof options.overwrite === "function") {
27
+ overwrite = options.overwrite(item, context);
28
+ }
29
+ else {
30
+ overwrite = options.overwrite;
31
+ }
32
+ if (!overwrite && _has(item, to)) {
27
33
  return;
28
34
  }
29
35
  _set(item, to, val);
@@ -4,7 +4,7 @@ import { runPerItem } from "./hooks/runPerItem";
4
4
  export declare const hooks: {
5
5
  checkMulti: typeof checkMulti;
6
6
  setData: typeof setData;
7
- runPerItem: (actionPerItem: (item: any, context: import("@feathersjs/feathers").HookContext<any, import("@feathersjs/feathers").Service<any>>) => any, options: import("./types").HookRunPerItemOptions) => (context: import("@feathersjs/feathers").HookContext<any, import("@feathersjs/feathers").Service<any>>) => Promise<import("@feathersjs/feathers").HookContext<any, import("@feathersjs/feathers").Service<any>>>;
7
+ runPerItem: (actionPerItem: (item: any, context: import("@feathersjs/feathers").HookContext<any, import("@feathersjs/feathers").Service<any>>) => any, options: import("./types").HookRunPerItemOptions) => import("./types").ReturnAsyncHook;
8
8
  };
9
9
  export { checkMulti };
10
10
  export { setData };
@@ -25,6 +25,7 @@ export { mergeArrays } from "./utils/mergeQuery/mergeArrays";
25
25
  export { pushSet } from "./utils/pushSet";
26
26
  export { setResultEmpty } from "./utils/setResultEmpty";
27
27
  export { markHookForSkip } from "./utils/markHookForSkip";
28
- export { shouldSkip } from "./utils/shouldSkip";
29
28
  export { filterQuery } from "./utils/filterQuery";
29
+ export { getItemsIsArray } from "./utils/getItemsIsArray";
30
+ export { shouldSkip } from "./utils/shouldSkip";
30
31
  export * from "./types";
package/dist/esm/index.js CHANGED
@@ -26,6 +26,7 @@ export { mergeArrays } from "./utils/mergeQuery/mergeArrays";
26
26
  export { pushSet } from "./utils/pushSet";
27
27
  export { setResultEmpty } from "./utils/setResultEmpty";
28
28
  export { markHookForSkip } from "./utils/markHookForSkip";
29
- export { shouldSkip } from "./utils/shouldSkip";
30
29
  export { filterQuery } from "./utils/filterQuery";
30
+ export { getItemsIsArray } from "./utils/getItemsIsArray";
31
+ export { shouldSkip } from "./utils/shouldSkip";
31
32
  export * from "./types";
@@ -1,12 +1,18 @@
1
- import type { Application, Service } from "@feathersjs/feathers";
1
+ import type { Application, HookContext, Service } from "@feathersjs/feathers";
2
2
  export declare type Path = Array<string | number>;
3
+ export declare type MaybeArray<T> = T | T[];
4
+ export declare type Promisable<T> = T | Promise<T>;
3
5
  export declare type HookType = "before" | "after" | "error";
4
6
  export declare type ServiceMethodName = "find" | "get" | "create" | "update" | "patch" | "remove";
7
+ export declare type ReturnSyncHook = (context: HookContext) => HookContext;
8
+ export declare type ReturnAsyncHook = (context: HookContext) => Promise<HookContext>;
5
9
  export declare type Handle = "target" | "source" | "combine" | "intersect" | "intersectOrFull";
6
10
  export declare type FirstLast = "first" | "last";
11
+ export declare type Predicate<T = any> = (item: T) => boolean;
12
+ export declare type PredicateWithContext<T = any> = (item: T, context: HookContext) => boolean;
7
13
  export interface HookSetDataOptions {
8
14
  allowUndefined?: boolean;
9
- overwrite?: boolean;
15
+ overwrite?: boolean | PredicateWithContext;
10
16
  }
11
17
  export interface AddHookOptions {
12
18
  types: HookType[];
@@ -57,3 +63,7 @@ export interface FilterQueryResult {
57
63
  paginate?: unknown;
58
64
  [key: string]: unknown;
59
65
  }
66
+ export interface GetItemsIsArrayOptions<T = any> {
67
+ items: T[];
68
+ isArray: boolean;
69
+ }
@@ -0,0 +1,3 @@
1
+ import type { HookContext } from "@feathersjs/feathers";
2
+ import type { GetItemsIsArrayOptions } from "..";
3
+ export declare const getItemsIsArray: <T = any>(context: HookContext) => GetItemsIsArrayOptions<T>;
@@ -0,0 +1,10 @@
1
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
2
+ export const getItemsIsArray = (context) => {
3
+ let itemOrItems = context.type === "before" ? context.data : context.result;
4
+ itemOrItems = itemOrItems && context.method === "find" ? (itemOrItems.data || itemOrItems) : itemOrItems;
5
+ const isArray = Array.isArray(itemOrItems);
6
+ return {
7
+ items: (isArray) ? itemOrItems : [itemOrItems],
8
+ isArray
9
+ };
10
+ };
@@ -1,2 +1,2 @@
1
- import { HookContext, PaginationOptions } from "@feathersjs/feathers";
1
+ import type { HookContext, PaginationOptions } from "@feathersjs/feathers";
2
2
  export declare const getPaginate: (context: HookContext) => PaginationOptions | undefined;
@@ -1,2 +1,2 @@
1
- import { HookContext } from "@feathersjs/feathers";
1
+ import type { HookContext } from "@feathersjs/feathers";
2
2
  export declare const isPaginated: (context: HookContext) => boolean;
@@ -1,3 +1,3 @@
1
1
  import type { HookContext } from "@feathersjs/feathers";
2
- import type { HookType } from "feathers-hooks-common/types";
3
- export declare function markHookForSkip<T>(hookName: string, type: "all" | HookType | HookType[], context?: Partial<HookContext<T>>): Partial<HookContext<T>>;
2
+ import type { HookType, MaybeArray } from "../types";
3
+ export declare function markHookForSkip<T>(hookName: string, type: "all" | MaybeArray<HookType>, context?: Partial<HookContext<T>>): Partial<HookContext<T>>;
@@ -1,2 +1,2 @@
1
- import { HookContext } from "@feathersjs/feathers";
1
+ import type { HookContext } from "@feathersjs/feathers";
2
2
  export declare const setResultEmpty: (context: HookContext) => HookContext;
@@ -1,2 +1,2 @@
1
- import type { HookContext } from "@feathersjs/feathers";
2
- export declare function checkMulti(): ((context: HookContext) => HookContext);
1
+ import type { ReturnSyncHook } from "../types";
2
+ export declare function checkMulti(): ReturnSyncHook;
@@ -6,11 +6,11 @@ const shouldSkip_1 = require("../utils/shouldSkip");
6
6
  const isMulti_1 = require("../utils/isMulti");
7
7
  function checkMulti() {
8
8
  return (context) => {
9
- if (shouldSkip_1.shouldSkip("checkMulti", context)) {
9
+ if ((0, shouldSkip_1.shouldSkip)("checkMulti", context)) {
10
10
  return context;
11
11
  }
12
12
  const { service, method } = context;
13
- if (!service.allowsMulti || !isMulti_1.isMulti(context) || method === "find") {
13
+ if (!service.allowsMulti || !(0, isMulti_1.isMulti)(context) || method === "find") {
14
14
  return context;
15
15
  }
16
16
  if (!service.allowsMulti(method)) {
@@ -1,4 +1,3 @@
1
- import type { HookRunPerItemOptions } from "../types";
1
+ import type { HookRunPerItemOptions, ReturnAsyncHook, Promisable } from "../types";
2
2
  import type { HookContext } from "@feathersjs/feathers";
3
- import type { Promisable } from "type-fest";
4
- export declare const runPerItem: (actionPerItem: (item: any, context: HookContext) => Promisable<any>, options: HookRunPerItemOptions) => (context: HookContext) => Promise<HookContext>;
3
+ export declare const runPerItem: (actionPerItem: (item: any, context: HookContext) => Promisable<any>, options: HookRunPerItemOptions) => ReturnAsyncHook;
@@ -10,22 +10,23 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.runPerItem = void 0;
13
- const feathers_hooks_common_1 = require("feathers-hooks-common");
14
13
  const shouldSkip_1 = require("../utils/shouldSkip");
14
+ const getItemsIsArray_1 = require("../utils/getItemsIsArray");
15
15
  const makeOptions = (options) => {
16
16
  options = options || {};
17
17
  return Object.assign({
18
18
  wait: true
19
19
  }, options);
20
20
  };
21
- const runPerItem = (actionPerItem, options) => {
21
+ const runPerItem = (
22
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
23
+ actionPerItem, options) => {
22
24
  options = makeOptions(options);
23
25
  return (context) => __awaiter(void 0, void 0, void 0, function* () {
24
- if (shouldSkip_1.shouldSkip("runForItems", context)) {
26
+ if ((0, shouldSkip_1.shouldSkip)("runForItems", context)) {
25
27
  return context;
26
28
  }
27
- let items = feathers_hooks_common_1.getItems(context);
28
- items = (Array.isArray(items)) ? items : [items];
29
+ const { items } = (0, getItemsIsArray_1.getItemsIsArray)(context);
29
30
  const promises = items.map((item) => __awaiter(void 0, void 0, void 0, function* () {
30
31
  yield actionPerItem(item, context);
31
32
  }));
@@ -1,4 +1,3 @@
1
- import type { HookContext } from "@feathersjs/feathers";
2
- import type { HookSetDataOptions } from "../types";
3
1
  import type { PropertyPath } from "lodash";
4
- export declare function setData(from: PropertyPath, to: PropertyPath, _options?: HookSetDataOptions): ((context: HookContext) => HookContext);
2
+ import type { HookSetDataOptions, ReturnSyncHook } from "../types";
3
+ export declare function setData(from: PropertyPath, to: PropertyPath, _options?: HookSetDataOptions): ReturnSyncHook;
@@ -7,8 +7,8 @@ exports.setData = void 0;
7
7
  const get_1 = __importDefault(require("lodash/get"));
8
8
  const set_1 = __importDefault(require("lodash/set"));
9
9
  const has_1 = __importDefault(require("lodash/has"));
10
- const feathers_hooks_common_1 = require("feathers-hooks-common");
11
10
  const errors_1 = require("@feathersjs/errors");
11
+ const getItemsIsArray_1 = require("../utils/getItemsIsArray");
12
12
  const defaultOptions = {
13
13
  allowUndefined: false,
14
14
  overwrite: true
@@ -17,23 +17,29 @@ function setData(from, to, _options) {
17
17
  const options = Object.assign({}, defaultOptions, _options);
18
18
  return (context) => {
19
19
  var _a;
20
- let items = feathers_hooks_common_1.getItems(context);
21
- items = (Array.isArray(items)) ? items : [items];
22
- if (!has_1.default(context, from)) {
20
+ const { items } = (0, getItemsIsArray_1.getItemsIsArray)(context);
21
+ if (!(0, has_1.default)(context, from)) {
23
22
  if (!((_a = context.params) === null || _a === void 0 ? void 0 : _a.provider) || options.allowUndefined === true) {
24
23
  return context;
25
24
  }
26
- if (!options.overwrite && items.every((item) => has_1.default(item, to))) {
25
+ if (!options.overwrite && items.every((item) => (0, has_1.default)(item, to))) {
27
26
  return context;
28
27
  }
29
28
  throw new errors_1.Forbidden(`Expected field ${from.toString()} not available`);
30
29
  }
31
- const val = get_1.default(context, from);
30
+ const val = (0, get_1.default)(context, from);
32
31
  items.forEach((item) => {
33
- if (!options.overwrite && has_1.default(item, to)) {
32
+ let overwrite;
33
+ if (typeof options.overwrite === "function") {
34
+ overwrite = options.overwrite(item, context);
35
+ }
36
+ else {
37
+ overwrite = options.overwrite;
38
+ }
39
+ if (!overwrite && (0, has_1.default)(item, to)) {
34
40
  return;
35
41
  }
36
- set_1.default(item, to, val);
42
+ (0, set_1.default)(item, to, val);
37
43
  });
38
44
  return context;
39
45
  };
package/dist/index.d.ts CHANGED
@@ -4,7 +4,7 @@ import { runPerItem } from "./hooks/runPerItem";
4
4
  export declare const hooks: {
5
5
  checkMulti: typeof checkMulti;
6
6
  setData: typeof setData;
7
- runPerItem: (actionPerItem: (item: any, context: import("@feathersjs/feathers").HookContext<any, import("@feathersjs/feathers").Service<any>>) => any, options: import("./types").HookRunPerItemOptions) => (context: import("@feathersjs/feathers").HookContext<any, import("@feathersjs/feathers").Service<any>>) => Promise<import("@feathersjs/feathers").HookContext<any, import("@feathersjs/feathers").Service<any>>>;
7
+ runPerItem: (actionPerItem: (item: any, context: import("@feathersjs/feathers").HookContext<any, import("@feathersjs/feathers").Service<any>>) => any, options: import("./types").HookRunPerItemOptions) => import("./types").ReturnAsyncHook;
8
8
  };
9
9
  export { checkMulti };
10
10
  export { setData };
@@ -25,6 +25,7 @@ export { mergeArrays } from "./utils/mergeQuery/mergeArrays";
25
25
  export { pushSet } from "./utils/pushSet";
26
26
  export { setResultEmpty } from "./utils/setResultEmpty";
27
27
  export { markHookForSkip } from "./utils/markHookForSkip";
28
- export { shouldSkip } from "./utils/shouldSkip";
29
28
  export { filterQuery } from "./utils/filterQuery";
29
+ export { getItemsIsArray } from "./utils/getItemsIsArray";
30
+ export { shouldSkip } from "./utils/shouldSkip";
30
31
  export * from "./types";
package/dist/index.js CHANGED
@@ -10,7 +10,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
10
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
11
  };
12
12
  Object.defineProperty(exports, "__esModule", { value: true });
13
- exports.filterQuery = exports.shouldSkip = exports.markHookForSkip = exports.setResultEmpty = exports.pushSet = exports.mergeArrays = exports.mergeQuery = exports.isPaginated = exports.isMulti = exports.getPaginate = exports.addHook = exports.DebouncedStore = exports.debounceMixin = exports.mixins = exports.runPerItem = exports.setData = exports.checkMulti = exports.hooks = void 0;
13
+ exports.shouldSkip = exports.getItemsIsArray = exports.filterQuery = exports.markHookForSkip = exports.setResultEmpty = exports.pushSet = exports.mergeArrays = exports.mergeQuery = exports.isPaginated = exports.isMulti = exports.getPaginate = exports.addHook = exports.DebouncedStore = exports.debounceMixin = exports.mixins = exports.runPerItem = exports.setData = exports.checkMulti = exports.hooks = void 0;
14
14
  // hooks
15
15
  const checkMulti_1 = require("./hooks/checkMulti");
16
16
  Object.defineProperty(exports, "checkMulti", { enumerable: true, get: function () { return checkMulti_1.checkMulti; } });
@@ -48,8 +48,10 @@ var setResultEmpty_1 = require("./utils/setResultEmpty");
48
48
  Object.defineProperty(exports, "setResultEmpty", { enumerable: true, get: function () { return setResultEmpty_1.setResultEmpty; } });
49
49
  var markHookForSkip_1 = require("./utils/markHookForSkip");
50
50
  Object.defineProperty(exports, "markHookForSkip", { enumerable: true, get: function () { return markHookForSkip_1.markHookForSkip; } });
51
- var shouldSkip_1 = require("./utils/shouldSkip");
52
- Object.defineProperty(exports, "shouldSkip", { enumerable: true, get: function () { return shouldSkip_1.shouldSkip; } });
53
51
  var filterQuery_1 = require("./utils/filterQuery");
54
52
  Object.defineProperty(exports, "filterQuery", { enumerable: true, get: function () { return filterQuery_1.filterQuery; } });
53
+ var getItemsIsArray_1 = require("./utils/getItemsIsArray");
54
+ Object.defineProperty(exports, "getItemsIsArray", { enumerable: true, get: function () { return getItemsIsArray_1.getItemsIsArray; } });
55
+ var shouldSkip_1 = require("./utils/shouldSkip");
56
+ Object.defineProperty(exports, "shouldSkip", { enumerable: true, get: function () { return shouldSkip_1.shouldSkip; } });
55
57
  __exportStar(require("./types"), exports);
@@ -26,7 +26,7 @@ exports.makeDefaultOptions = makeDefaultOptions;
26
26
  class DebouncedStore {
27
27
  constructor(app, options) {
28
28
  this._app = app;
29
- this._options = Object.assign(exports.makeDefaultOptions(), options);
29
+ this._options = Object.assign((0, exports.makeDefaultOptions)(), options);
30
30
  this._queueById = {};
31
31
  this._isRunningById = {};
32
32
  //this._waitingById = {};
@@ -52,7 +52,7 @@ class DebouncedStore {
52
52
  if (typeof this._queueById[id] === "function") {
53
53
  return this._queueById[id](id, action);
54
54
  }
55
- this._queueById[id] = debounce_1.default((id, action) => {
55
+ this._queueById[id] = (0, debounce_1.default)((id, action) => {
56
56
  this.unbounced(id, action);
57
57
  }, wait, Object.assign(Object.assign({}, options), { leading: false })); // leading required for return promise
58
58
  return this._queueById[id](id, action);
@@ -6,7 +6,7 @@ Object.defineProperty(exports, "DebouncedStore", { enumerable: true, get: functi
6
6
  function debounceMixin(options) {
7
7
  return (app) => {
8
8
  options = options || {};
9
- const defaultOptions = Object.assign(DebouncedStore_1.makeDefaultOptions(), options === null || options === void 0 ? void 0 : options.default);
9
+ const defaultOptions = Object.assign((0, DebouncedStore_1.makeDefaultOptions)(), options === null || options === void 0 ? void 0 : options.default);
10
10
  app.mixins.push((service, path) => {
11
11
  // if path is on blacklist, don't add debouncedStore to service
12
12
  if ((options === null || options === void 0 ? void 0 : options.blacklist) && options.blacklist.includes(path))
package/dist/types.d.ts CHANGED
@@ -1,12 +1,18 @@
1
- import type { Application, Service } from "@feathersjs/feathers";
1
+ import type { Application, HookContext, Service } from "@feathersjs/feathers";
2
2
  export declare type Path = Array<string | number>;
3
+ export declare type MaybeArray<T> = T | T[];
4
+ export declare type Promisable<T> = T | Promise<T>;
3
5
  export declare type HookType = "before" | "after" | "error";
4
6
  export declare type ServiceMethodName = "find" | "get" | "create" | "update" | "patch" | "remove";
7
+ export declare type ReturnSyncHook = (context: HookContext) => HookContext;
8
+ export declare type ReturnAsyncHook = (context: HookContext) => Promise<HookContext>;
5
9
  export declare type Handle = "target" | "source" | "combine" | "intersect" | "intersectOrFull";
6
10
  export declare type FirstLast = "first" | "last";
11
+ export declare type Predicate<T = any> = (item: T) => boolean;
12
+ export declare type PredicateWithContext<T = any> = (item: T, context: HookContext) => boolean;
7
13
  export interface HookSetDataOptions {
8
14
  allowUndefined?: boolean;
9
- overwrite?: boolean;
15
+ overwrite?: boolean | PredicateWithContext;
10
16
  }
11
17
  export interface AddHookOptions {
12
18
  types: HookType[];
@@ -57,3 +63,7 @@ export interface FilterQueryResult {
57
63
  paginate?: unknown;
58
64
  [key: string]: unknown;
59
65
  }
66
+ export interface GetItemsIsArrayOptions<T = any> {
67
+ items: T[];
68
+ isArray: boolean;
69
+ }
@@ -25,9 +25,9 @@ function filterQuery(query, options) {
25
25
  return service.filterQuery({ query }, optionsForFilterQuery);
26
26
  }
27
27
  else {
28
- return adapter_commons_1.filterQuery(query, optionsForFilterQuery);
28
+ return (0, adapter_commons_1.filterQuery)(query, optionsForFilterQuery);
29
29
  }
30
30
  }
31
- return adapter_commons_1.filterQuery(query, options);
31
+ return (0, adapter_commons_1.filterQuery)(query, options);
32
32
  }
33
33
  exports.filterQuery = filterQuery;
@@ -0,0 +1,3 @@
1
+ import type { HookContext } from "@feathersjs/feathers";
2
+ import type { GetItemsIsArrayOptions } from "..";
3
+ export declare const getItemsIsArray: <T = any>(context: HookContext) => GetItemsIsArrayOptions<T>;
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getItemsIsArray = void 0;
4
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
5
+ const getItemsIsArray = (context) => {
6
+ let itemOrItems = context.type === "before" ? context.data : context.result;
7
+ itemOrItems = itemOrItems && context.method === "find" ? (itemOrItems.data || itemOrItems) : itemOrItems;
8
+ const isArray = Array.isArray(itemOrItems);
9
+ return {
10
+ items: (isArray) ? itemOrItems : [itemOrItems],
11
+ isArray
12
+ };
13
+ };
14
+ exports.getItemsIsArray = getItemsIsArray;
@@ -1,2 +1,2 @@
1
- import { HookContext, PaginationOptions } from "@feathersjs/feathers";
1
+ import type { HookContext, PaginationOptions } from "@feathersjs/feathers";
2
2
  export declare const getPaginate: (context: HookContext) => PaginationOptions | undefined;
@@ -1,2 +1,2 @@
1
- import { HookContext } from "@feathersjs/feathers";
1
+ import type { HookContext } from "@feathersjs/feathers";
2
2
  export declare const isPaginated: (context: HookContext) => boolean;
@@ -6,7 +6,7 @@ const isPaginated = (context) => {
6
6
  if (context.params.paginate === false) {
7
7
  return false;
8
8
  }
9
- const paginate = getPaginate_1.getPaginate(context);
9
+ const paginate = (0, getPaginate_1.getPaginate)(context);
10
10
  return !!paginate;
11
11
  };
12
12
  exports.isPaginated = isPaginated;
@@ -1,3 +1,3 @@
1
1
  import type { HookContext } from "@feathersjs/feathers";
2
- import type { HookType } from "feathers-hooks-common/types";
3
- export declare function markHookForSkip<T>(hookName: string, type: "all" | HookType | HookType[], context?: Partial<HookContext<T>>): Partial<HookContext<T>>;
2
+ import type { HookType, MaybeArray } from "../types";
3
+ export declare function markHookForSkip<T>(hookName: string, type: "all" | MaybeArray<HookType>, context?: Partial<HookContext<T>>): Partial<HookContext<T>>;
@@ -10,7 +10,7 @@ function markHookForSkip(hookName, type, context) {
10
10
  const combinedName = (t === "all")
11
11
  ? hookName
12
12
  : `${type}:${hookName}`;
13
- pushSet_1.pushSet(params, ["skipHooks"], combinedName, { unique: true });
13
+ (0, pushSet_1.pushSet)(params, ["skipHooks"], combinedName, { unique: true });
14
14
  });
15
15
  context.params = params;
16
16
  return context;