feathers-utils 2.0.0-0 → 2.0.0-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 (97) hide show
  1. package/README.md +16 -9
  2. package/dist/esm/filters/array.d.ts +2 -0
  3. package/dist/esm/filters/array.js +10 -0
  4. package/dist/esm/hooks/checkMulti.d.ts +2 -2
  5. package/dist/esm/hooks/createRelated.d.ts +3 -0
  6. package/dist/esm/hooks/createRelated.js +26 -0
  7. package/dist/esm/hooks/onDelete.d.ts +3 -0
  8. package/dist/esm/hooks/onDelete.js +40 -0
  9. package/dist/esm/hooks/removeRelated.d.ts +3 -0
  10. package/dist/esm/hooks/removeRelated.js +30 -0
  11. package/dist/esm/hooks/runPerItem.d.ts +2 -3
  12. package/dist/esm/hooks/runPerItem.js +6 -6
  13. package/dist/esm/hooks/setData.d.ts +2 -3
  14. package/dist/esm/hooks/setData.js +13 -8
  15. package/dist/esm/index.d.ts +8 -2
  16. package/dist/esm/index.js +8 -1
  17. package/dist/esm/mixins/debounce-mixin/DebouncedStore.d.ts +1 -1
  18. package/dist/esm/mixins/debounce-mixin/DebouncedStore.js +1 -1
  19. package/dist/esm/mixins/debounce-mixin/index.d.ts +1 -1
  20. package/dist/esm/types.d.ts +35 -16
  21. package/dist/esm/utils/filterQuery.d.ts +2 -2
  22. package/dist/esm/utils/filterQuery.js +9 -7
  23. package/dist/esm/utils/getItemsIsArray.d.ts +3 -0
  24. package/dist/esm/utils/getItemsIsArray.js +18 -0
  25. package/dist/esm/utils/getPaginate.d.ts +1 -1
  26. package/dist/esm/utils/isPaginated.d.ts +1 -1
  27. package/dist/esm/utils/markHookForSkip.d.ts +2 -2
  28. package/dist/esm/utils/mergeQuery/index.js +18 -7
  29. package/dist/esm/utils/pushSet.d.ts +1 -1
  30. package/dist/esm/utils/pushSet.js +3 -3
  31. package/dist/esm/utils/setResultEmpty.d.ts +1 -1
  32. package/dist/esm/utils/validateQueryProperty.d.ts +2 -0
  33. package/dist/esm/utils/validateQueryProperty.js +20 -0
  34. package/dist/filters/array.d.ts +2 -0
  35. package/dist/filters/array.js +14 -0
  36. package/dist/hooks/checkMulti.d.ts +2 -2
  37. package/dist/hooks/createRelated.d.ts +3 -0
  38. package/dist/hooks/createRelated.js +39 -0
  39. package/dist/hooks/onDelete.d.ts +3 -0
  40. package/dist/hooks/onDelete.js +53 -0
  41. package/dist/hooks/removeRelated.d.ts +3 -0
  42. package/dist/hooks/removeRelated.js +43 -0
  43. package/dist/hooks/runPerItem.d.ts +2 -3
  44. package/dist/hooks/runPerItem.js +6 -6
  45. package/dist/hooks/setData.d.ts +2 -3
  46. package/dist/hooks/setData.js +17 -12
  47. package/dist/index.d.ts +8 -2
  48. package/dist/index.js +21 -4
  49. package/dist/mixins/debounce-mixin/DebouncedStore.d.ts +1 -1
  50. package/dist/mixins/debounce-mixin/DebouncedStore.js +2 -2
  51. package/dist/mixins/debounce-mixin/index.d.ts +1 -1
  52. package/dist/types.d.ts +35 -16
  53. package/dist/utils/filterQuery.d.ts +2 -2
  54. package/dist/utils/filterQuery.js +19 -6
  55. package/dist/utils/getItemsIsArray.d.ts +3 -0
  56. package/dist/utils/getItemsIsArray.js +22 -0
  57. package/dist/utils/getPaginate.d.ts +1 -1
  58. package/dist/utils/isPaginated.d.ts +1 -1
  59. package/dist/utils/markHookForSkip.d.ts +2 -2
  60. package/dist/utils/mergeQuery/index.js +53 -42
  61. package/dist/utils/pushSet.d.ts +1 -1
  62. package/dist/utils/pushSet.js +6 -6
  63. package/dist/utils/setResultEmpty.d.ts +1 -1
  64. package/dist/utils/validateQueryProperty.d.ts +2 -0
  65. package/dist/utils/validateQueryProperty.js +22 -0
  66. package/package.json +33 -19
  67. package/src/filters/array.ts +14 -0
  68. package/src/hooks/checkMulti.ts +3 -1
  69. package/src/hooks/createRelated.ts +45 -0
  70. package/src/hooks/onDelete.ts +56 -0
  71. package/src/hooks/removeRelated.ts +42 -0
  72. package/src/hooks/runPerItem.ts +9 -12
  73. package/src/hooks/setData.ts +17 -12
  74. package/src/index.ts +11 -1
  75. package/src/mixins/debounce-mixin/DebouncedStore.ts +49 -49
  76. package/src/mixins/debounce-mixin/index.ts +6 -3
  77. package/src/types.ts +46 -16
  78. package/src/utils/filterQuery.ts +15 -14
  79. package/src/utils/getItemsIsArray.ts +23 -0
  80. package/src/utils/getPaginate.ts +1 -2
  81. package/src/utils/isMulti.ts +3 -1
  82. package/src/utils/isPaginated.ts +1 -1
  83. package/src/utils/markHookForSkip.ts +2 -2
  84. package/src/utils/mergeQuery/index.ts +20 -8
  85. package/src/utils/pushSet.ts +3 -3
  86. package/src/utils/setResultEmpty.ts +1 -1
  87. package/src/utils/shouldSkip.ts +4 -1
  88. package/src/utils/validateQueryProperty.ts +27 -0
  89. package/.eslintignore +0 -3
  90. package/.eslintrc.js +0 -44
  91. package/.gitlab-ci.yml +0 -11
  92. package/.mocharc.js +0 -11
  93. package/.nycrc.json +0 -22
  94. package/index.js +0 -9
  95. package/tsconfig-esm.json +0 -9
  96. package/tsconfig.json +0 -16
  97. package/tsconfig.test.json +0 -13
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.createRelated = void 0;
13
+ const feathers_hooks_common_1 = require("feathers-hooks-common");
14
+ const getItemsIsArray_1 = require("../utils/getItemsIsArray");
15
+ function createRelated({ service, multi = true, data, createItemsInDataArraySeparately = true }) {
16
+ if (!service || !data) {
17
+ throw "initialize hook 'createRelated' completely!";
18
+ }
19
+ return (context) => __awaiter(this, void 0, void 0, function* () {
20
+ // @ts-expect-error wait for feathers-hooks-common to update
21
+ (0, feathers_hooks_common_1.checkContext)(context, "after", undefined, "createRelated");
22
+ const { items } = (0, getItemsIsArray_1.getItemsIsArray)(context);
23
+ let dataToCreate = (yield Promise.all(items.map((item) => __awaiter(this, void 0, void 0, function* () { return data(item, context); })))).filter(x => !!x);
24
+ if (createItemsInDataArraySeparately) {
25
+ dataToCreate = dataToCreate.flat();
26
+ }
27
+ if (!dataToCreate || dataToCreate.length <= 0) {
28
+ return context;
29
+ }
30
+ if (multi) {
31
+ yield context.app.service(service).create(dataToCreate);
32
+ }
33
+ else {
34
+ yield Promise.all(dataToCreate.map((item) => __awaiter(this, void 0, void 0, function* () { return context.app.service(service).create(item); })));
35
+ }
36
+ return context;
37
+ });
38
+ }
39
+ exports.createRelated = createRelated;
@@ -0,0 +1,3 @@
1
+ import type { HookContext } from "@feathersjs/feathers";
2
+ import type { OnDeleteOptions } from "../types";
3
+ export declare function onDelete<S = Record<string, any>>(service: keyof S, { keyThere, keyHere, onDelete, blocking }: OnDeleteOptions): (context: HookContext) => Promise<HookContext>;
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.onDelete = void 0;
13
+ const feathers_hooks_common_1 = require("feathers-hooks-common");
14
+ const getItemsIsArray_1 = require("../utils/getItemsIsArray");
15
+ function onDelete(service, { keyThere, keyHere = "id", onDelete = "cascade", blocking = true }) {
16
+ if (!service || !keyThere) {
17
+ throw "initialize hook 'removeRelated' completely!";
18
+ }
19
+ if (!["cascade", "set null"].includes(onDelete)) {
20
+ throw "onDelete must be 'cascade' or 'set null'";
21
+ }
22
+ return (context) => __awaiter(this, void 0, void 0, function* () {
23
+ // @ts-expect-error wait for feathers-hooks-common to update
24
+ (0, feathers_hooks_common_1.checkContext)(context, "after", "remove", "onDelete");
25
+ const { items } = (0, getItemsIsArray_1.getItemsIsArray)(context);
26
+ let ids = items.map(x => x[keyHere]).filter(x => !!x);
27
+ ids = [...new Set(ids)];
28
+ if (!ids || ids.length <= 0) {
29
+ return context;
30
+ }
31
+ const params = {
32
+ query: {
33
+ [keyThere]: {
34
+ $in: ids
35
+ }
36
+ },
37
+ paginate: false
38
+ };
39
+ let promise;
40
+ if (onDelete === "cascade") {
41
+ promise = context.app.service(service).remove(null, params);
42
+ }
43
+ else if (onDelete === "set null") {
44
+ const data = { [keyThere]: null };
45
+ promise = context.app.service(service).patch(null, data, params);
46
+ }
47
+ if (blocking) {
48
+ yield promise;
49
+ }
50
+ return context;
51
+ });
52
+ }
53
+ exports.onDelete = onDelete;
@@ -0,0 +1,3 @@
1
+ import type { HookContext } from "@feathersjs/feathers";
2
+ import type { RemoveRelatedOptions } from "../types";
3
+ export declare function removeRelated<S = Record<string, any>>({ service, keyThere, keyHere, blocking }: RemoveRelatedOptions<S>): (context: HookContext) => Promise<HookContext>;
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.removeRelated = void 0;
13
+ const feathers_hooks_common_1 = require("feathers-hooks-common");
14
+ const getItemsIsArray_1 = require("../utils/getItemsIsArray");
15
+ function removeRelated({ service, keyThere, keyHere = "id", blocking = true }) {
16
+ if (!service || !keyThere) {
17
+ throw "initialize hook 'removeRelated' completely!";
18
+ }
19
+ return (context) => __awaiter(this, void 0, void 0, function* () {
20
+ // @ts-expect-error wait for feathers-hooks-common to update
21
+ (0, feathers_hooks_common_1.checkContext)(context, "after", "remove", "removeRelated");
22
+ const { items } = (0, getItemsIsArray_1.getItemsIsArray)(context);
23
+ let ids = items.map(x => x[keyHere]).filter(x => !!x);
24
+ ids = [...new Set(ids)];
25
+ if (!ids || ids.length <= 0) {
26
+ return context;
27
+ }
28
+ // feathers does not accept `paginate: false` for remove, but some adapters need it to work properly
29
+ const promise = context.app.service(service).remove(null, {
30
+ query: {
31
+ [keyThere]: {
32
+ $in: ids
33
+ }
34
+ },
35
+ paginate: false
36
+ });
37
+ if (blocking) {
38
+ yield promise;
39
+ }
40
+ return context;
41
+ });
42
+ }
43
+ exports.removeRelated = removeRelated;
@@ -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,23 +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) => {
22
- options = makeOptions(options);
21
+ const runPerItem = (
22
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
23
+ actionPerItem, _options) => {
24
+ const options = makeOptions(_options);
23
25
  return (context) => __awaiter(void 0, void 0, void 0, function* () {
24
26
  if ((0, shouldSkip_1.shouldSkip)("runForItems", context)) {
25
27
  return context;
26
28
  }
27
- //@ts-expect-error type error because feathers-hooks-common is feathers@4
28
- let items = (0, feathers_hooks_common_1.getItems)(context);
29
- items = (Array.isArray(items)) ? items : [items];
29
+ const { items } = (0, getItemsIsArray_1.getItemsIsArray)(context);
30
30
  const promises = items.map((item) => __awaiter(void 0, void 0, void 0, function* () {
31
31
  yield actionPerItem(item, context);
32
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;
@@ -4,11 +4,11 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.setData = void 0;
7
- const get_1 = __importDefault(require("lodash/get"));
8
- const set_1 = __importDefault(require("lodash/set"));
9
- const has_1 = __importDefault(require("lodash/has"));
10
- const feathers_hooks_common_1 = require("feathers-hooks-common");
7
+ const get_js_1 = __importDefault(require("lodash/get.js"));
8
+ const set_js_1 = __importDefault(require("lodash/set.js"));
9
+ const has_js_1 = __importDefault(require("lodash/has.js"));
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,24 +17,29 @@ function setData(from, to, _options) {
17
17
  const options = Object.assign({}, defaultOptions, _options);
18
18
  return (context) => {
19
19
  var _a;
20
- //@ts-expect-error type error because feathers-hooks-common is feathers@4
21
- let items = (0, feathers_hooks_common_1.getItems)(context);
22
- items = (Array.isArray(items)) ? items : [items];
23
- if (!(0, has_1.default)(context, from)) {
20
+ const { items } = (0, getItemsIsArray_1.getItemsIsArray)(context);
21
+ if (!(0, has_js_1.default)(context, from)) {
24
22
  if (!((_a = context.params) === null || _a === void 0 ? void 0 : _a.provider) || options.allowUndefined === true) {
25
23
  return context;
26
24
  }
27
- if (!options.overwrite && items.every((item) => (0, has_1.default)(item, to))) {
25
+ if (!options.overwrite && items.every((item) => (0, has_js_1.default)(item, to))) {
28
26
  return context;
29
27
  }
30
28
  throw new errors_1.Forbidden(`Expected field ${from.toString()} not available`);
31
29
  }
32
- const val = (0, get_1.default)(context, from);
30
+ const val = (0, get_js_1.default)(context, from);
33
31
  items.forEach((item) => {
34
- if (!options.overwrite && (0, 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_js_1.default)(item, to)) {
35
40
  return;
36
41
  }
37
- (0, set_1.default)(item, to, val);
42
+ (0, set_js_1.default)(item, to, val);
38
43
  });
39
44
  return context;
40
45
  };
package/dist/index.d.ts CHANGED
@@ -4,10 +4,12 @@ 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/lib").HookContext<import("@feathersjs/feathers/lib").Application<any, any>, any>) => any, options: import("./types").HookRunPerItemOptions) => (context: import("@feathersjs/feathers/lib").HookContext<import("@feathersjs/feathers/lib").Application<any, any>, any>) => Promise<import("@feathersjs/feathers/lib").HookContext<import("@feathersjs/feathers/lib").Application<any, any>, any>>;
7
+ runPerItem: (actionPerItem: (item: any, context: import("@feathersjs/feathers/lib").HookContext<import("@feathersjs/feathers/lib").Application<any, any>, any>) => any, _options?: import("./types").HookRunPerItemOptions | undefined) => import("./types").ReturnAsyncHook;
8
8
  };
9
9
  export { checkMulti };
10
+ export { createRelated } from "./hooks/createRelated";
10
11
  export { setData };
12
+ export { removeRelated } from "./hooks/removeRelated";
11
13
  export { runPerItem };
12
14
  import { debounceMixin, DebouncedService, DebouncedStore } from "./mixins/debounce-mixin";
13
15
  export declare const mixins: {
@@ -25,6 +27,10 @@ export { mergeArrays } from "./utils/mergeQuery/mergeArrays";
25
27
  export { pushSet } from "./utils/pushSet";
26
28
  export { setResultEmpty } from "./utils/setResultEmpty";
27
29
  export { markHookForSkip } from "./utils/markHookForSkip";
28
- export { shouldSkip } from "./utils/shouldSkip";
29
30
  export { filterQuery } from "./utils/filterQuery";
31
+ export { getItemsIsArray } from "./utils/getItemsIsArray";
32
+ export { onDelete } from "./hooks/onDelete";
33
+ export { shouldSkip } from "./utils/shouldSkip";
34
+ export { validateQueryProperty } from "./utils/validateQueryProperty";
35
+ export { filterArray } from "./filters/array";
30
36
  export * from "./types";
package/dist/index.js CHANGED
@@ -1,7 +1,11 @@
1
1
  "use strict";
2
2
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
3
  if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
5
9
  }) : (function(o, m, k, k2) {
6
10
  if (k2 === undefined) k2 = k;
7
11
  o[k2] = m[k];
@@ -10,7 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
15
  };
12
16
  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.DebouncedStore = exports.debounceMixin = exports.mixins = exports.runPerItem = exports.setData = exports.checkMulti = exports.hooks = void 0;
17
+ exports.filterArray = exports.validateQueryProperty = exports.shouldSkip = exports.onDelete = exports.getItemsIsArray = exports.filterQuery = exports.markHookForSkip = exports.setResultEmpty = exports.pushSet = exports.mergeArrays = exports.mergeQuery = exports.isPaginated = exports.isMulti = exports.getPaginate = exports.DebouncedStore = exports.debounceMixin = exports.mixins = exports.runPerItem = exports.removeRelated = exports.setData = exports.createRelated = exports.checkMulti = exports.hooks = void 0;
14
18
  // hooks
15
19
  const checkMulti_1 = require("./hooks/checkMulti");
16
20
  Object.defineProperty(exports, "checkMulti", { enumerable: true, get: function () { return checkMulti_1.checkMulti; } });
@@ -23,6 +27,10 @@ exports.hooks = {
23
27
  setData: setData_1.setData,
24
28
  runPerItem: runPerItem_1.runPerItem
25
29
  };
30
+ var createRelated_1 = require("./hooks/createRelated");
31
+ Object.defineProperty(exports, "createRelated", { enumerable: true, get: function () { return createRelated_1.createRelated; } });
32
+ var removeRelated_1 = require("./hooks/removeRelated");
33
+ Object.defineProperty(exports, "removeRelated", { enumerable: true, get: function () { return removeRelated_1.removeRelated; } });
26
34
  const debounce_mixin_1 = require("./mixins/debounce-mixin");
27
35
  Object.defineProperty(exports, "debounceMixin", { enumerable: true, get: function () { return debounce_mixin_1.debounceMixin; } });
28
36
  Object.defineProperty(exports, "DebouncedStore", { enumerable: true, get: function () { return debounce_mixin_1.DebouncedStore; } });
@@ -46,8 +54,17 @@ var setResultEmpty_1 = require("./utils/setResultEmpty");
46
54
  Object.defineProperty(exports, "setResultEmpty", { enumerable: true, get: function () { return setResultEmpty_1.setResultEmpty; } });
47
55
  var markHookForSkip_1 = require("./utils/markHookForSkip");
48
56
  Object.defineProperty(exports, "markHookForSkip", { enumerable: true, get: function () { return markHookForSkip_1.markHookForSkip; } });
49
- var shouldSkip_1 = require("./utils/shouldSkip");
50
- Object.defineProperty(exports, "shouldSkip", { enumerable: true, get: function () { return shouldSkip_1.shouldSkip; } });
51
57
  var filterQuery_1 = require("./utils/filterQuery");
52
58
  Object.defineProperty(exports, "filterQuery", { enumerable: true, get: function () { return filterQuery_1.filterQuery; } });
59
+ var getItemsIsArray_1 = require("./utils/getItemsIsArray");
60
+ Object.defineProperty(exports, "getItemsIsArray", { enumerable: true, get: function () { return getItemsIsArray_1.getItemsIsArray; } });
61
+ var onDelete_1 = require("./hooks/onDelete");
62
+ Object.defineProperty(exports, "onDelete", { enumerable: true, get: function () { return onDelete_1.onDelete; } });
63
+ var shouldSkip_1 = require("./utils/shouldSkip");
64
+ Object.defineProperty(exports, "shouldSkip", { enumerable: true, get: function () { return shouldSkip_1.shouldSkip; } });
65
+ var validateQueryProperty_1 = require("./utils/validateQueryProperty");
66
+ Object.defineProperty(exports, "validateQueryProperty", { enumerable: true, get: function () { return validateQueryProperty_1.validateQueryProperty; } });
67
+ // query filters
68
+ var array_1 = require("./filters/array");
69
+ Object.defineProperty(exports, "filterArray", { enumerable: true, get: function () { return array_1.filterArray; } });
53
70
  __exportStar(require("./types"), exports);
@@ -7,7 +7,7 @@ export declare class DebouncedStore {
7
7
  private _options;
8
8
  private _isRunningById;
9
9
  _queueById: Record<string, DebouncedFunc<((id: Id, action: DebouncedFunctionApp) => void | Promise<void>)>>;
10
- add: (id: Id, action: (app?: Application<any, any> | undefined) => void | Promise<void>) => void | Promise<void> | undefined;
10
+ add: (id: Id, action: (app?: Application) => void | Promise<void>) => void | Promise<void> | undefined;
11
11
  constructor(app: Application, options?: Partial<DebouncedStoreOptions>);
12
12
  private unbounced;
13
13
  private debounceById;
@@ -13,7 +13,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.DebouncedStore = exports.makeDefaultOptions = void 0;
16
- const debounce_1 = __importDefault(require("lodash/debounce"));
16
+ const debounce_js_1 = __importDefault(require("lodash/debounce.js"));
17
17
  const makeDefaultOptions = () => {
18
18
  return {
19
19
  leading: false,
@@ -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] = (0, debounce_1.default)((id, action) => {
55
+ this._queueById[id] = (0, debounce_js_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);
@@ -2,7 +2,7 @@ import { DebouncedStore } from "./DebouncedStore";
2
2
  import type { Application, FeathersService } from "@feathersjs/feathers";
3
3
  import type { InitDebounceMixinOptions } from "../../types";
4
4
  export declare type DebouncedService = FeathersService & {
5
- debouncedStore?: DebouncedStore;
5
+ debouncedStore: DebouncedStore;
6
6
  };
7
7
  export declare function debounceMixin(options?: Partial<InitDebounceMixinOptions>): ((app: Application) => void);
8
8
  export { DebouncedStore };
package/dist/types.d.ts CHANGED
@@ -1,13 +1,19 @@
1
- import type { Application } from "@feathersjs/feathers";
2
- import type { AdapterService } from "@feathersjs/adapter-commons";
1
+ import type { Application, HookContext } from "@feathersjs/feathers";
2
+ import type { AdapterBase, FilterQueryOptions as PlainFilterQueryOptions } from "@feathersjs/adapter-commons";
3
3
  export declare type Path = Array<string | number>;
4
+ export declare type MaybeArray<T> = T | T[];
5
+ export declare type Promisable<T> = T | Promise<T>;
4
6
  export declare type HookType = "before" | "after" | "error";
5
7
  export declare type ServiceMethodName = "find" | "get" | "create" | "update" | "patch" | "remove";
8
+ export declare type ReturnSyncHook = (context: HookContext) => HookContext;
9
+ export declare type ReturnAsyncHook = (context: HookContext) => Promise<HookContext>;
6
10
  export declare type Handle = "target" | "source" | "combine" | "intersect" | "intersectOrFull";
7
11
  export declare type FirstLast = "first" | "last";
12
+ export declare type Predicate<T = any> = (item: T) => boolean;
13
+ export declare type PredicateWithContext<T = any> = (item: T, context: HookContext) => boolean;
8
14
  export interface HookSetDataOptions {
9
15
  allowUndefined?: boolean;
10
- overwrite?: boolean;
16
+ overwrite?: boolean | PredicateWithContext;
11
17
  }
12
18
  export interface AddHookOptions {
13
19
  types: HookType[];
@@ -19,6 +25,25 @@ export interface AddHookOptions {
19
25
  export interface HookRunPerItemOptions {
20
26
  wait?: boolean;
21
27
  }
28
+ export interface RemoveRelatedOptions<S = Record<string, any>> {
29
+ service: keyof S;
30
+ keyThere: string;
31
+ keyHere: string;
32
+ blocking?: boolean;
33
+ }
34
+ export interface CreateRelatedOptions<S = Record<string, any>> {
35
+ service: keyof S;
36
+ multi?: boolean;
37
+ data: (item: any, context: HookContext) => Promisable<Record<string, any>>;
38
+ createItemsInDataArraySeparately?: boolean;
39
+ }
40
+ export declare type OnDeleteAction = "cascade" | "set null";
41
+ export interface OnDeleteOptions {
42
+ keyThere: string;
43
+ keyHere: string;
44
+ onDelete: OnDeleteAction;
45
+ blocking?: boolean;
46
+ }
22
47
  export interface InitDebounceMixinOptions {
23
48
  default: Partial<DebouncedStoreOptions>;
24
49
  blacklist: string[];
@@ -44,17 +69,11 @@ export interface MergeQueryOptions<T> extends FilterQueryOptions<T> {
44
69
  };
45
70
  }
46
71
  export interface FilterQueryOptions<T> {
47
- service?: AdapterService<T>;
48
- operators?: string[];
49
- filters?: string[];
50
- }
51
- export interface PlainFilterQueryOptions {
52
- operators?: string[];
53
- filters?: string[];
54
- }
55
- export interface FilterQueryResult {
56
- filters: Record<string, unknown>;
57
- query: Record<string, unknown>;
58
- paginate?: unknown;
59
- [key: string]: unknown;
72
+ service?: AdapterBase<T>;
73
+ operators?: PlainFilterQueryOptions["operators"];
74
+ filters?: PlainFilterQueryOptions["filters"];
75
+ }
76
+ export interface GetItemsIsArrayOptions<T = any> {
77
+ items: T[];
78
+ isArray: boolean;
60
79
  }
@@ -1,3 +1,3 @@
1
- import type { FilterQueryOptions, FilterQueryResult } from "../types";
2
1
  import type { Query } from "@feathersjs/feathers";
3
- export declare function filterQuery<T>(query: Query, options?: FilterQueryOptions<T>): FilterQueryResult;
2
+ import type { FilterQueryOptions } from "../types";
3
+ export declare function filterQuery<T>(query: Query, _options?: FilterQueryOptions<T>): any;
@@ -1,16 +1,27 @@
1
1
  "use strict";
2
+ var __rest = (this && this.__rest) || function (s, e) {
3
+ var t = {};
4
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
5
+ t[p] = s[p];
6
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
7
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
8
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
9
+ t[p[i]] = s[p[i]];
10
+ }
11
+ return t;
12
+ };
2
13
  Object.defineProperty(exports, "__esModule", { value: true });
3
14
  exports.filterQuery = void 0;
4
15
  const adapter_commons_1 = require("@feathersjs/adapter-commons");
5
- function filterQuery(query, options) {
16
+ function filterQuery(query, _options) {
6
17
  var _a, _b;
7
18
  query = query || {};
8
- options = options || {};
9
- if (options === null || options === void 0 ? void 0 : options.service) {
10
- const { service } = options;
19
+ _options = _options || {};
20
+ const { service } = _options, options = __rest(_options, ["service"]);
21
+ if (service) {
11
22
  const operators = options.operators
12
23
  ? options.operators
13
- : (_a = service.options) === null || _a === void 0 ? void 0 : _a.whitelist;
24
+ : (_a = service.options) === null || _a === void 0 ? void 0 : _a.operators;
14
25
  const filters = options.filters
15
26
  ? options.filters
16
27
  : (_b = service.options) === null || _b === void 0 ? void 0 : _b.filters;
@@ -21,7 +32,9 @@ function filterQuery(query, options) {
21
32
  if (filters) {
22
33
  optionsForFilterQuery.filters = filters;
23
34
  }
24
- if (typeof (service === null || service === void 0 ? void 0 : service.filterQuery) === "function") {
35
+ // @ts-expect-error service has no filterQuery method
36
+ if (service && "filterQuery" in service && typeof service.filterQuery === "function") {
37
+ // @ts-expect-error service has no filterQuery method
25
38
  return service.filterQuery({ query }, optionsForFilterQuery);
26
39
  }
27
40
  else {
@@ -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,22 @@
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"
7
+ ? context.data
8
+ : context.result;
9
+ itemOrItems = itemOrItems && context.method === "find"
10
+ ? (itemOrItems.data || itemOrItems)
11
+ : itemOrItems;
12
+ const isArray = Array.isArray(itemOrItems);
13
+ return {
14
+ items: (isArray)
15
+ ? itemOrItems
16
+ : (itemOrItems != null)
17
+ ? [itemOrItems]
18
+ : [],
19
+ isArray
20
+ };
21
+ };
22
+ exports.getItemsIsArray = getItemsIsArray;
@@ -1,4 +1,4 @@
1
- import { HookContext } from "@feathersjs/feathers";
1
+ import type { HookContext } from "@feathersjs/feathers";
2
2
  interface PaginationOptions {
3
3
  default: number;
4
4
  max: number;
@@ -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>>;