feathers-utils 2.0.0-2 → 2.0.0-4

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 (139) hide show
  1. package/dist/esm/filters/array.d.ts +1 -1
  2. package/dist/esm/filters/array.js +2 -2
  3. package/dist/esm/filters/object.d.ts +2 -0
  4. package/dist/esm/filters/object.js +8 -0
  5. package/dist/esm/hooks/checkMulti.d.ts +2 -2
  6. package/dist/esm/hooks/createRelated.d.ts +8 -2
  7. package/dist/esm/hooks/createRelated.js +2 -3
  8. package/dist/esm/hooks/onDelete.d.ts +8 -2
  9. package/dist/esm/hooks/onDelete.js +8 -7
  10. package/dist/esm/hooks/removeRelated.d.ts +7 -2
  11. package/dist/esm/hooks/removeRelated.js +8 -7
  12. package/dist/esm/hooks/runPerItem.d.ts +5 -2
  13. package/dist/esm/hooks/runPerItem.js +1 -1
  14. package/dist/esm/hooks/setData.d.ts +7 -2
  15. package/dist/esm/hooks/setData.js +3 -2
  16. package/dist/esm/index.d.ts +19 -34
  17. package/dist/esm/index.js +21 -33
  18. package/dist/esm/mixins/debounce-mixin/DebouncedStore.d.ts +5 -2
  19. package/dist/esm/mixins/debounce-mixin/DebouncedStore.js +2 -2
  20. package/dist/esm/mixins/debounce-mixin/debounceMixin.d.ts +3 -0
  21. package/dist/esm/mixins/debounce-mixin/debounceMixin.js +19 -0
  22. package/dist/esm/mixins/debounce-mixin/index.d.ts +3 -8
  23. package/dist/esm/mixins/debounce-mixin/index.js +3 -20
  24. package/dist/esm/mixins/debounce-mixin/types.d.ts +13 -0
  25. package/dist/esm/mixins/debounce-mixin/types.js +1 -0
  26. package/dist/esm/types.d.ts +1 -77
  27. package/dist/esm/types.js +0 -1
  28. package/dist/esm/typesInternal.d.ts +3 -0
  29. package/dist/esm/typesInternal.js +3 -0
  30. package/dist/esm/utils/filterQuery.d.ts +6 -1
  31. package/dist/esm/utils/filterQuery.js +5 -3
  32. package/dist/esm/utils/getItemsIsArray.d.ts +5 -2
  33. package/dist/esm/utils/getItemsIsArray.js +7 -12
  34. package/dist/esm/utils/getPaginate.d.ts +2 -6
  35. package/dist/esm/utils/getPaginate.js +1 -1
  36. package/dist/esm/utils/isMulti.d.ts +1 -1
  37. package/dist/esm/utils/isPaginated.d.ts +1 -1
  38. package/dist/esm/utils/markHookForSkip.d.ts +3 -2
  39. package/dist/esm/utils/markHookForSkip.js +6 -5
  40. package/dist/esm/utils/mergeQuery/index.d.ts +3 -3
  41. package/dist/esm/utils/mergeQuery/index.js +3 -338
  42. package/dist/esm/utils/mergeQuery/mergeArrays.d.ts +2 -1
  43. package/dist/esm/utils/mergeQuery/mergeArrays.js +2 -2
  44. package/dist/esm/utils/mergeQuery/mergeQuery.d.ts +3 -0
  45. package/dist/esm/utils/mergeQuery/mergeQuery.js +68 -0
  46. package/dist/esm/utils/mergeQuery/types.d.ts +13 -0
  47. package/dist/esm/utils/mergeQuery/types.js +1 -0
  48. package/dist/esm/utils/mergeQuery/utils.d.ts +11 -0
  49. package/dist/esm/utils/mergeQuery/utils.js +272 -0
  50. package/dist/esm/utils/pushSet.d.ts +4 -1
  51. package/dist/esm/utils/pushSet.js +1 -1
  52. package/dist/esm/utils/setResultEmpty.d.ts +1 -1
  53. package/dist/esm/utils/setResultEmpty.js +1 -1
  54. package/dist/esm/utils/shouldSkip.d.ts +1 -1
  55. package/dist/esm/utils/validateQueryProperty.js +1 -1
  56. package/dist/filters/array.d.ts +1 -1
  57. package/dist/filters/array.js +2 -2
  58. package/dist/filters/object.d.ts +2 -0
  59. package/dist/filters/object.js +15 -0
  60. package/dist/hooks/checkMulti.d.ts +2 -2
  61. package/dist/hooks/createRelated.d.ts +8 -2
  62. package/dist/hooks/createRelated.js +2 -3
  63. package/dist/hooks/onDelete.d.ts +8 -2
  64. package/dist/hooks/onDelete.js +8 -7
  65. package/dist/hooks/removeRelated.d.ts +7 -2
  66. package/dist/hooks/removeRelated.js +8 -7
  67. package/dist/hooks/runPerItem.d.ts +5 -2
  68. package/dist/hooks/runPerItem.js +1 -1
  69. package/dist/hooks/setData.d.ts +7 -2
  70. package/dist/hooks/setData.js +3 -2
  71. package/dist/index.d.ts +19 -34
  72. package/dist/index.js +23 -49
  73. package/dist/mixins/debounce-mixin/DebouncedStore.d.ts +5 -2
  74. package/dist/mixins/debounce-mixin/DebouncedStore.js +2 -2
  75. package/dist/mixins/debounce-mixin/debounceMixin.d.ts +3 -0
  76. package/dist/mixins/debounce-mixin/debounceMixin.js +23 -0
  77. package/dist/mixins/debounce-mixin/index.d.ts +3 -8
  78. package/dist/mixins/debounce-mixin/index.js +17 -22
  79. package/dist/mixins/debounce-mixin/types.d.ts +13 -0
  80. package/dist/mixins/debounce-mixin/types.js +2 -0
  81. package/dist/types.d.ts +1 -77
  82. package/dist/types.js +0 -1
  83. package/dist/typesInternal.d.ts +3 -0
  84. package/dist/typesInternal.js +4 -0
  85. package/dist/utils/filterQuery.d.ts +6 -1
  86. package/dist/utils/filterQuery.js +4 -2
  87. package/dist/utils/getItemsIsArray.d.ts +5 -2
  88. package/dist/utils/getItemsIsArray.js +7 -12
  89. package/dist/utils/getPaginate.d.ts +2 -6
  90. package/dist/utils/isMulti.d.ts +1 -1
  91. package/dist/utils/isPaginated.d.ts +1 -1
  92. package/dist/utils/markHookForSkip.d.ts +3 -2
  93. package/dist/utils/markHookForSkip.js +6 -5
  94. package/dist/utils/mergeQuery/index.d.ts +3 -3
  95. package/dist/utils/mergeQuery/index.js +16 -342
  96. package/dist/utils/mergeQuery/mergeArrays.d.ts +2 -1
  97. package/dist/utils/mergeQuery/mergeArrays.js +2 -2
  98. package/dist/utils/mergeQuery/mergeQuery.d.ts +3 -0
  99. package/dist/utils/mergeQuery/mergeQuery.js +75 -0
  100. package/dist/utils/mergeQuery/types.d.ts +13 -0
  101. package/dist/utils/mergeQuery/types.js +2 -0
  102. package/dist/utils/mergeQuery/utils.d.ts +11 -0
  103. package/dist/utils/mergeQuery/utils.js +287 -0
  104. package/dist/utils/pushSet.d.ts +4 -1
  105. package/dist/utils/pushSet.js +1 -1
  106. package/dist/utils/setResultEmpty.d.ts +1 -1
  107. package/dist/utils/setResultEmpty.js +1 -1
  108. package/dist/utils/shouldSkip.d.ts +1 -1
  109. package/package.json +5 -2
  110. package/src/filters/array.ts +13 -9
  111. package/src/filters/object.ts +15 -0
  112. package/src/hooks/checkMulti.ts +8 -6
  113. package/src/hooks/createRelated.ts +21 -12
  114. package/src/hooks/onDelete.ts +28 -13
  115. package/src/hooks/removeRelated.ts +28 -16
  116. package/src/hooks/runPerItem.ts +19 -10
  117. package/src/hooks/setData.ts +24 -15
  118. package/src/index.ts +21 -38
  119. package/src/mixins/debounce-mixin/DebouncedStore.ts +29 -24
  120. package/src/mixins/debounce-mixin/debounceMixin.ts +33 -0
  121. package/src/mixins/debounce-mixin/index.ts +3 -39
  122. package/src/mixins/debounce-mixin/types.ts +16 -0
  123. package/src/types.ts +6 -117
  124. package/src/typesInternal.ts +6 -0
  125. package/src/utils/filterQuery.ts +22 -10
  126. package/src/utils/getItemsIsArray.ts +15 -16
  127. package/src/utils/getPaginate.ts +11 -14
  128. package/src/utils/isMulti.ts +3 -3
  129. package/src/utils/isPaginated.ts +6 -4
  130. package/src/utils/markHookForSkip.ts +18 -16
  131. package/src/utils/mergeQuery/index.ts +3 -379
  132. package/src/utils/mergeQuery/mergeArrays.ts +25 -18
  133. package/src/utils/mergeQuery/mergeQuery.ts +102 -0
  134. package/src/utils/mergeQuery/types.ts +25 -0
  135. package/src/utils/mergeQuery/utils.ts +342 -0
  136. package/src/utils/pushSet.ts +14 -7
  137. package/src/utils/setResultEmpty.ts +8 -6
  138. package/src/utils/shouldSkip.ts +4 -4
  139. package/src/utils/validateQueryProperty.ts +8 -4
@@ -0,0 +1,272 @@
1
+ import { Forbidden } from "@feathersjs/errors";
2
+ import _get from "lodash/get.js";
3
+ import _has from "lodash/has.js";
4
+ import _isEmpty from "lodash/isEmpty.js";
5
+ import _isEqual from "lodash/isEqual.js";
6
+ import _set from "lodash/set.js";
7
+ import _uniqWith from "lodash/uniqWith.js";
8
+ import { mergeArrays } from "./mergeArrays";
9
+ export const hasOwnProperty = (obj, key) => {
10
+ return Object.prototype.hasOwnProperty.call(obj, key);
11
+ };
12
+ export function handleArray(target, source, key, options) {
13
+ const targetVal = _get(target, key);
14
+ const sourceVal = _get(source, key);
15
+ if (!sourceVal && !targetVal) {
16
+ return;
17
+ }
18
+ const handle = _get(options, ["handle", ...key], options.defaultHandle);
19
+ const arr = mergeArrays(targetVal, sourceVal, handle, key, options.actionOnEmptyIntersect);
20
+ _set(target, key, arr);
21
+ }
22
+ export function handleCircular(target, source, prependKey, options) {
23
+ if (target?.$or) {
24
+ target.$or = cleanOr(target.$or);
25
+ if (!target.$or) {
26
+ delete target.$or;
27
+ }
28
+ }
29
+ if (source?.$or) {
30
+ source.$or = cleanOr(source.$or);
31
+ if (!source.$or) {
32
+ delete source.$or;
33
+ }
34
+ }
35
+ if (target?.$and) {
36
+ target.$and = cleanAnd(target.$and);
37
+ if (!target.$and) {
38
+ delete target.$and;
39
+ }
40
+ }
41
+ if (source?.$and) {
42
+ source.$and = cleanAnd(source.$and);
43
+ if (!source.$and) {
44
+ delete source.$and;
45
+ }
46
+ }
47
+ if (!_has(source, prependKey)) {
48
+ return;
49
+ }
50
+ if (!_has(target, prependKey)) {
51
+ _set(target, prependKey, _get(source, prependKey));
52
+ return;
53
+ }
54
+ const { defaultHandle, actionOnEmptyIntersect } = options;
55
+ if (defaultHandle === "target") {
56
+ return;
57
+ }
58
+ const getTargetVal = () => {
59
+ return prependKey.length > 0 ? _get(target, prependKey) : target;
60
+ };
61
+ const getSourceVal = () => {
62
+ return prependKey.length > 0 ? _get(source, prependKey) : source;
63
+ };
64
+ const targetVal = getTargetVal();
65
+ const sourceVal = getSourceVal();
66
+ if (_isEqual(targetVal, sourceVal)) {
67
+ return;
68
+ }
69
+ if (defaultHandle === "source") {
70
+ _set(target, prependKey, sourceVal);
71
+ return;
72
+ }
73
+ if (targetVal === null || sourceVal === null) {
74
+ _set(target, prependKey, sourceVal);
75
+ return;
76
+ }
77
+ const typeOfTargetVal = typeof targetVal;
78
+ if (["boolean"].includes(typeOfTargetVal)) {
79
+ if (defaultHandle === "intersect") {
80
+ actionOnEmptyIntersect(target, source, prependKey);
81
+ }
82
+ _set(target, prependKey, sourceVal);
83
+ return;
84
+ }
85
+ const typeOfSourceVal = typeof sourceVal;
86
+ const isTargetSimple = ["string", "number"].includes(typeOfTargetVal);
87
+ const isSourceSimple = ["string", "number"].includes(typeOfSourceVal);
88
+ if (isTargetSimple || isSourceSimple) {
89
+ if (isTargetSimple && isSourceSimple) {
90
+ if (defaultHandle === "combine") {
91
+ _set(target, prependKey, { $in: [...new Set([targetVal, sourceVal])] });
92
+ return;
93
+ }
94
+ else if (defaultHandle === "intersect") {
95
+ actionOnEmptyIntersect(target, source, prependKey);
96
+ }
97
+ else {
98
+ throw new Error("should not reach here");
99
+ }
100
+ }
101
+ else if (hasOwnProperty(targetVal, "$in") ||
102
+ hasOwnProperty(sourceVal, "$in")) {
103
+ const targetHasIn = hasOwnProperty(targetVal, "$in");
104
+ const $in = targetHasIn ? targetVal["$in"] : sourceVal["$in"];
105
+ const otherVal = isTargetSimple ? targetVal : sourceVal;
106
+ if ($in.length === 1 && _isEqual($in[0], otherVal)) {
107
+ _set(target, prependKey, otherVal);
108
+ return;
109
+ }
110
+ else if (defaultHandle === "combine") {
111
+ if (!$in.some((x) => _isEqual(x, otherVal))) {
112
+ $in.push(otherVal);
113
+ }
114
+ _set(target, `${prependKey}.$in`, $in);
115
+ return;
116
+ }
117
+ else if (defaultHandle === "intersect") {
118
+ if ($in.some((x) => _isEqual(x, otherVal))) {
119
+ _set(target, prependKey, otherVal);
120
+ }
121
+ else {
122
+ actionOnEmptyIntersect(target, source, prependKey);
123
+ }
124
+ return;
125
+ }
126
+ return;
127
+ }
128
+ }
129
+ const isTargetArray = Array.isArray(targetVal);
130
+ const isSourceArray = Array.isArray(sourceVal);
131
+ if (isTargetArray && isSourceArray) {
132
+ const key = prependKey[prependKey.length - 1];
133
+ if (key === "$or") {
134
+ if (defaultHandle === "combine") {
135
+ const newVals = sourceVal.filter((x) => !targetVal.some((y) => _isEqual(x, y)));
136
+ targetVal.push(...newVals);
137
+ }
138
+ else if (defaultHandle === "intersect") {
139
+ // combine into "$and"
140
+ const targetParent = getParentProp(target, prependKey);
141
+ const sourceParent = getParentProp(source, prependKey);
142
+ targetParent.$and = targetParent.$and || [];
143
+ targetParent.$and.push({ $or: targetVal }, { $or: sourceVal });
144
+ targetParent.$and = cleanAnd(targetParent.$and);
145
+ if (!targetParent.$and) {
146
+ delete targetParent.$and;
147
+ }
148
+ delete targetParent.$or;
149
+ delete sourceParent.$or;
150
+ handleCircular(target, source, [...prependKey, "$and"], options);
151
+ return;
152
+ }
153
+ return;
154
+ }
155
+ else if (key === "$and") {
156
+ if (defaultHandle === "combine") {
157
+ // combine into "$or"
158
+ const targetParent = getParentProp(target, prependKey);
159
+ const sourceParent = getParentProp(source, prependKey);
160
+ targetParent.$or = targetParent.$or || [];
161
+ targetParent.$or.push({ $and: targetVal }, { $and: sourceVal });
162
+ targetParent.$or = cleanOr(targetParent.$or);
163
+ if (!targetParent.$or) {
164
+ delete targetParent.$or;
165
+ }
166
+ delete targetParent.$and;
167
+ delete sourceParent.$and;
168
+ handleCircular(target, source, [...prependKey, "$or"], options);
169
+ return;
170
+ }
171
+ else if (defaultHandle === "intersect") {
172
+ const newVals = sourceVal.filter((x) => !targetVal.some((y) => _isEqual(x, y)));
173
+ targetVal.push(...newVals);
174
+ return;
175
+ }
176
+ }
177
+ else if (key === "$in") {
178
+ if (defaultHandle === "combine") {
179
+ let $in = targetVal.concat(sourceVal);
180
+ $in = [...new Set($in)];
181
+ _set(target, prependKey, $in);
182
+ return;
183
+ }
184
+ else if (defaultHandle === "intersect") {
185
+ const $in = targetVal.filter((x) => sourceVal.some((y) => _isEqual(x, y)));
186
+ if ($in.length === 0) {
187
+ actionOnEmptyIntersect(target, source, prependKey);
188
+ }
189
+ else if ($in.length === 1) {
190
+ _set(target, prependKey.slice(0, -1), $in[0]);
191
+ return;
192
+ }
193
+ else {
194
+ _set(target, prependKey, $in);
195
+ }
196
+ }
197
+ return;
198
+ }
199
+ _set(target, prependKey, sourceVal);
200
+ return;
201
+ }
202
+ if (typeOfTargetVal !== "object" || typeOfSourceVal !== "object") {
203
+ _set(target, prependKey, sourceVal);
204
+ return;
205
+ }
206
+ // both are objects
207
+ const sourceKeys = Object.keys(sourceVal);
208
+ for (let i = 0, n = sourceKeys.length; i < n; i++) {
209
+ const key = sourceKeys[i];
210
+ handleCircular(target, source, [...prependKey, key], options);
211
+ }
212
+ }
213
+ export function makeDefaultOptions(options) {
214
+ options = options || {};
215
+ options.defaultHandle = options.defaultHandle || "combine";
216
+ options.useLogicalConjunction = Object.prototype.hasOwnProperty.call(options, "useLogicalConjunction")
217
+ ? options.useLogicalConjunction
218
+ : false;
219
+ options.actionOnEmptyIntersect =
220
+ options.actionOnEmptyIntersect ||
221
+ (() => {
222
+ throw new Forbidden("You're not allowed to make this request");
223
+ });
224
+ options.handle = options.handle || {};
225
+ if (options.defaultHandle === "intersect") {
226
+ options.handle.$select = options.handle.$select || "intersectOrFull";
227
+ }
228
+ return options;
229
+ }
230
+ export function moveProperty(source, target, key) {
231
+ if (!Object.prototype.hasOwnProperty.call(source, key)) {
232
+ return;
233
+ }
234
+ target[key] = source[key];
235
+ delete source[key];
236
+ }
237
+ export function getParentProp(target, path) {
238
+ if (path.length <= 1) {
239
+ return target;
240
+ }
241
+ const pathOneUp = path.slice(0, -1);
242
+ return _get(target, pathOneUp);
243
+ }
244
+ export function cleanOr(target) {
245
+ if (!target || !Array.isArray(target) || target.length <= 0) {
246
+ return target;
247
+ }
248
+ if (target.some((x) => _isEmpty(x))) {
249
+ return undefined;
250
+ }
251
+ else {
252
+ return arrayWithoutDuplicates(target);
253
+ }
254
+ }
255
+ export function cleanAnd(target) {
256
+ if (!target || !Array.isArray(target) || target.length <= 0) {
257
+ return target;
258
+ }
259
+ if (target.every((x) => _isEmpty(x))) {
260
+ return undefined;
261
+ }
262
+ else {
263
+ target = target.filter((x) => !_isEmpty(x));
264
+ return arrayWithoutDuplicates(target);
265
+ }
266
+ }
267
+ export function arrayWithoutDuplicates(target) {
268
+ if (!target || !Array.isArray(target)) {
269
+ return target;
270
+ }
271
+ return _uniqWith(target, _isEqual);
272
+ }
@@ -1,2 +1,5 @@
1
- import type { Path, PushSetOptions } from "../types";
1
+ import type { Path } from "../typesInternal";
2
+ export interface PushSetOptions {
3
+ unique?: boolean;
4
+ }
2
5
  export declare const pushSet: (obj: Record<string, unknown>, path: string | Path, val: unknown, options?: PushSetOptions) => unknown[];
@@ -10,7 +10,7 @@ export const pushSet = (obj, path, val, options) => {
10
10
  return arr;
11
11
  }
12
12
  else {
13
- if (options.unique && arr.some(x => _isEqual(x, val))) {
13
+ if (options.unique && arr.some((x) => _isEqual(x, val))) {
14
14
  return arr;
15
15
  }
16
16
  arr.push(val);
@@ -1,2 +1,2 @@
1
1
  import type { HookContext } from "@feathersjs/feathers";
2
- export declare const setResultEmpty: (context: HookContext) => HookContext;
2
+ export declare const setResultEmpty: <H extends HookContext<import("@feathersjs/feathers").Application<any, any>, any> = HookContext<import("@feathersjs/feathers").Application<any, any>, any>>(context: H) => H;
@@ -11,7 +11,7 @@ export const setResultEmpty = (context) => {
11
11
  total: 0,
12
12
  skip: 0,
13
13
  limit: 0,
14
- data: []
14
+ data: [],
15
15
  };
16
16
  }
17
17
  else {
@@ -1,2 +1,2 @@
1
1
  import type { HookContext } from "@feathersjs/feathers";
2
- export declare const shouldSkip: (hookName: string, context: HookContext) => boolean;
2
+ export declare const shouldSkip: <H extends HookContext<import("@feathersjs/feathers").Application<any, any>, any> = HookContext<import("@feathersjs/feathers").Application<any, any>, any>>(hookName: string, context: H) => boolean;
@@ -15,6 +15,6 @@ export const validateQueryProperty = (query, operators = []) => {
15
15
  }
16
16
  }
17
17
  return {
18
- ...query
18
+ ...query,
19
19
  };
20
20
  };
@@ -1,2 +1,2 @@
1
1
  import type { FilterQueryOptions } from "@feathersjs/adapter-commons";
2
- export declare const filterArray: () => (arr: any, { operators }: FilterQueryOptions) => any;
2
+ export declare const filterArray: (key: string) => (arr: any, { operators }: FilterQueryOptions) => any;
@@ -2,9 +2,9 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.filterArray = void 0;
4
4
  const validateQueryProperty_1 = require("../utils/validateQueryProperty");
5
- const filterArray = () => (arr, { operators }) => {
5
+ const filterArray = (key) => (arr, { operators }) => {
6
6
  if (arr && !Array.isArray(arr)) {
7
- throw new Error("Invalid query parameter $and. It has to be an array");
7
+ throw new Error(`Invalid query parameter '${key}'. It has to be an array`);
8
8
  }
9
9
  if (Array.isArray(arr)) {
10
10
  return arr.map((current) => (0, validateQueryProperty_1.validateQueryProperty)(current, operators));
@@ -0,0 +1,2 @@
1
+ import type { FilterQueryOptions } from "@feathersjs/adapter-commons";
2
+ export declare const filterObject: (key: string) => (obj: any, { operators }: FilterQueryOptions) => import("@feathersjs/feathers/lib").Query;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.filterObject = void 0;
7
+ const validateQueryProperty_1 = require("../utils/validateQueryProperty");
8
+ const isObject_1 = __importDefault(require("lodash/isObject"));
9
+ const filterObject = (key) => (obj, { operators }) => {
10
+ if (obj && !(0, isObject_1.default)(obj)) {
11
+ throw new Error(`Invalid query parameter: '${key}'. It has to be an object`);
12
+ }
13
+ return (0, validateQueryProperty_1.validateQueryProperty)(obj, operators);
14
+ };
15
+ exports.filterObject = filterObject;
@@ -1,2 +1,2 @@
1
- import type { ReturnSyncHook } from "../types";
2
- export declare function checkMulti(): ReturnSyncHook;
1
+ import type { HookContext } from "@feathersjs/feathers";
2
+ export declare function checkMulti<H extends HookContext = HookContext>(): (context: H) => H;
@@ -1,3 +1,9 @@
1
1
  import type { HookContext } from "@feathersjs/feathers";
2
- import type { CreateRelatedOptions } from "../types";
3
- export declare function createRelated<S = Record<string, any>>({ service, multi, data, createItemsInDataArraySeparately }: CreateRelatedOptions<S>): (context: HookContext) => Promise<HookContext>;
2
+ import type { Promisable } from "../typesInternal";
3
+ export interface CreateRelatedOptions<S = Record<string, any>> {
4
+ service: keyof S;
5
+ multi?: boolean;
6
+ data: (item: any, context: HookContext) => Promisable<Record<string, any>>;
7
+ createItemsInDataArraySeparately?: boolean;
8
+ }
9
+ export declare function createRelated<S = Record<string, any>, H extends HookContext = HookContext>({ service, multi, data, createItemsInDataArraySeparately, }: CreateRelatedOptions<S>): (context: H) => Promise<H>;
@@ -12,15 +12,14 @@ Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.createRelated = void 0;
13
13
  const feathers_hooks_common_1 = require("feathers-hooks-common");
14
14
  const getItemsIsArray_1 = require("../utils/getItemsIsArray");
15
- function createRelated({ service, multi = true, data, createItemsInDataArraySeparately = true }) {
15
+ function createRelated({ service, multi = true, data, createItemsInDataArraySeparately = true, }) {
16
16
  if (!service || !data) {
17
17
  throw "initialize hook 'createRelated' completely!";
18
18
  }
19
19
  return (context) => __awaiter(this, void 0, void 0, function* () {
20
- // @ts-expect-error wait for feathers-hooks-common to update
21
20
  (0, feathers_hooks_common_1.checkContext)(context, "after", undefined, "createRelated");
22
21
  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);
22
+ let dataToCreate = (yield Promise.all(items.map((item) => __awaiter(this, void 0, void 0, function* () { return data(item, context); })))).filter((x) => !!x);
24
23
  if (createItemsInDataArraySeparately) {
25
24
  dataToCreate = dataToCreate.flat();
26
25
  }
@@ -1,3 +1,9 @@
1
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>;
2
+ export declare type OnDeleteAction = "cascade" | "set null";
3
+ export interface OnDeleteOptions {
4
+ keyThere: string;
5
+ keyHere: string;
6
+ onDelete: OnDeleteAction;
7
+ blocking?: boolean;
8
+ }
9
+ export declare function onDelete<S = Record<string, any>, H extends HookContext = HookContext>(service: keyof S, { keyThere, keyHere, onDelete, blocking, }: OnDeleteOptions): (context: H) => Promise<H>;
@@ -12,7 +12,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.onDelete = void 0;
13
13
  const feathers_hooks_common_1 = require("feathers-hooks-common");
14
14
  const getItemsIsArray_1 = require("../utils/getItemsIsArray");
15
- function onDelete(service, { keyThere, keyHere = "id", onDelete = "cascade", blocking = true }) {
15
+ function onDelete(service, { keyThere, keyHere = "id", onDelete = "cascade", blocking = true, }) {
16
16
  if (!service || !keyThere) {
17
17
  throw "initialize hook 'removeRelated' completely!";
18
18
  }
@@ -20,10 +20,9 @@ function onDelete(service, { keyThere, keyHere = "id", onDelete = "cascade", blo
20
20
  throw "onDelete must be 'cascade' or 'set null'";
21
21
  }
22
22
  return (context) => __awaiter(this, void 0, void 0, function* () {
23
- // @ts-expect-error wait for feathers-hooks-common to update
24
23
  (0, feathers_hooks_common_1.checkContext)(context, "after", "remove", "onDelete");
25
24
  const { items } = (0, getItemsIsArray_1.getItemsIsArray)(context);
26
- let ids = items.map(x => x[keyHere]).filter(x => !!x);
25
+ let ids = items.map((x) => x[keyHere]).filter((x) => !!x);
27
26
  ids = [...new Set(ids)];
28
27
  if (!ids || ids.length <= 0) {
29
28
  return context;
@@ -31,10 +30,10 @@ function onDelete(service, { keyThere, keyHere = "id", onDelete = "cascade", blo
31
30
  const params = {
32
31
  query: {
33
32
  [keyThere]: {
34
- $in: ids
35
- }
33
+ $in: ids,
34
+ },
36
35
  },
37
- paginate: false
36
+ paginate: false,
38
37
  };
39
38
  let promise;
40
39
  if (onDelete === "cascade") {
@@ -42,7 +41,9 @@ function onDelete(service, { keyThere, keyHere = "id", onDelete = "cascade", blo
42
41
  }
43
42
  else if (onDelete === "set null") {
44
43
  const data = { [keyThere]: null };
45
- promise = context.app.service(service).patch(null, data, params);
44
+ promise = context.app
45
+ .service(service)
46
+ .patch(null, data, params);
46
47
  }
47
48
  if (blocking) {
48
49
  yield promise;
@@ -1,3 +1,8 @@
1
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>;
2
+ export interface RemoveRelatedOptions<S = Record<string, any>> {
3
+ service: keyof S;
4
+ keyThere: string;
5
+ keyHere: string;
6
+ blocking?: boolean;
7
+ }
8
+ export declare function removeRelated<S = Record<string, any>, H extends HookContext = HookContext>({ service, keyThere, keyHere, blocking, }: RemoveRelatedOptions<S>): (context: H) => Promise<H>;
@@ -12,27 +12,28 @@ Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.removeRelated = void 0;
13
13
  const feathers_hooks_common_1 = require("feathers-hooks-common");
14
14
  const getItemsIsArray_1 = require("../utils/getItemsIsArray");
15
- function removeRelated({ service, keyThere, keyHere = "id", blocking = true }) {
15
+ function removeRelated({ service, keyThere, keyHere = "id", blocking = true, }) {
16
16
  if (!service || !keyThere) {
17
17
  throw "initialize hook 'removeRelated' completely!";
18
18
  }
19
19
  return (context) => __awaiter(this, void 0, void 0, function* () {
20
- // @ts-expect-error wait for feathers-hooks-common to update
21
20
  (0, feathers_hooks_common_1.checkContext)(context, "after", "remove", "removeRelated");
22
21
  const { items } = (0, getItemsIsArray_1.getItemsIsArray)(context);
23
- let ids = items.map(x => x[keyHere]).filter(x => !!x);
22
+ let ids = items.map((x) => x[keyHere]).filter((x) => !!x);
24
23
  ids = [...new Set(ids)];
25
24
  if (!ids || ids.length <= 0) {
26
25
  return context;
27
26
  }
28
27
  // 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, {
28
+ const promise = context.app
29
+ .service(service)
30
+ .remove(null, {
30
31
  query: {
31
32
  [keyThere]: {
32
- $in: ids
33
- }
33
+ $in: ids,
34
+ },
34
35
  },
35
- paginate: false
36
+ paginate: false,
36
37
  });
37
38
  if (blocking) {
38
39
  yield promise;
@@ -1,3 +1,6 @@
1
- import type { HookRunPerItemOptions, ReturnAsyncHook, Promisable } from "../types";
2
1
  import type { HookContext } from "@feathersjs/feathers";
3
- export declare const runPerItem: (actionPerItem: (item: any, context: HookContext) => Promisable<any>, _options?: HookRunPerItemOptions) => ReturnAsyncHook;
2
+ import type { Promisable } from "../typesInternal";
3
+ export interface HookRunPerItemOptions {
4
+ wait?: boolean;
5
+ }
6
+ export declare const runPerItem: <H extends HookContext<import("@feathersjs/feathers").Application<any, any>, any> = HookContext<import("@feathersjs/feathers").Application<any, any>, any>>(actionPerItem: (item: any, context: H) => Promisable<any>, _options?: HookRunPerItemOptions) => (context: H) => Promise<H>;
@@ -15,7 +15,7 @@ const getItemsIsArray_1 = require("../utils/getItemsIsArray");
15
15
  const makeOptions = (options) => {
16
16
  options = options || {};
17
17
  return Object.assign({
18
- wait: true
18
+ wait: true,
19
19
  }, options);
20
20
  };
21
21
  const runPerItem = (
@@ -1,3 +1,8 @@
1
+ import type { HookContext } from "@feathersjs/feathers";
1
2
  import type { PropertyPath } from "lodash";
2
- import type { HookSetDataOptions, ReturnSyncHook } from "../types";
3
- export declare function setData(from: PropertyPath, to: PropertyPath, _options?: HookSetDataOptions): ReturnSyncHook;
3
+ import type { PredicateWithContext } from "../types";
4
+ export interface HookSetDataOptions {
5
+ allowUndefined?: boolean;
6
+ overwrite?: boolean | PredicateWithContext;
7
+ }
8
+ export declare function setData<H extends HookContext = HookContext>(from: PropertyPath, to: PropertyPath, _options?: HookSetDataOptions): (context: H) => H;
@@ -11,7 +11,7 @@ const errors_1 = require("@feathersjs/errors");
11
11
  const getItemsIsArray_1 = require("../utils/getItemsIsArray");
12
12
  const defaultOptions = {
13
13
  allowUndefined: false,
14
- overwrite: true
14
+ overwrite: true,
15
15
  };
16
16
  function setData(from, to, _options) {
17
17
  const options = Object.assign({}, defaultOptions, _options);
@@ -22,7 +22,8 @@ function setData(from, to, _options) {
22
22
  if (!((_a = context.params) === null || _a === void 0 ? void 0 : _a.provider) || options.allowUndefined === true) {
23
23
  return context;
24
24
  }
25
- if (!options.overwrite && items.every((item) => (0, has_js_1.default)(item, to))) {
25
+ if (!options.overwrite &&
26
+ items.every((item) => (0, has_js_1.default)(item, to))) {
26
27
  return context;
27
28
  }
28
29
  throw new errors_1.Forbidden(`Expected field ${from.toString()} not available`);
package/dist/index.d.ts CHANGED
@@ -1,36 +1,21 @@
1
- import { checkMulti } from "./hooks/checkMulti";
2
- import { setData } from "./hooks/setData";
3
- import { runPerItem } from "./hooks/runPerItem";
4
- export declare const hooks: {
5
- checkMulti: typeof checkMulti;
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 | undefined) => import("./types").ReturnAsyncHook;
8
- };
9
- export { checkMulti };
10
- export { createRelated } from "./hooks/createRelated";
11
- export { setData };
12
- export { removeRelated } from "./hooks/removeRelated";
13
- export { runPerItem };
14
- import { debounceMixin, DebouncedService, DebouncedStore } from "./mixins/debounce-mixin";
15
- export declare const mixins: {
16
- debounceMixin: typeof debounceMixin;
17
- DebouncedStore: typeof DebouncedStore;
18
- };
19
- export { debounceMixin };
20
- export { DebouncedService };
21
- export { DebouncedStore };
22
- export { getPaginate } from "./utils/getPaginate";
23
- export { isMulti } from "./utils/isMulti";
24
- export { isPaginated } from "./utils/isPaginated";
25
- export { mergeQuery } from "./utils/mergeQuery/index";
26
- export { mergeArrays } from "./utils/mergeQuery/mergeArrays";
27
- export { pushSet } from "./utils/pushSet";
28
- export { setResultEmpty } from "./utils/setResultEmpty";
29
- export { markHookForSkip } from "./utils/markHookForSkip";
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";
1
+ export * from "./hooks/checkMulti";
2
+ export * from "./hooks/createRelated";
3
+ export * from "./hooks/onDelete";
4
+ export * from "./hooks/removeRelated";
5
+ export * from "./hooks/runPerItem";
6
+ export * from "./hooks/setData";
7
+ export * from "./mixins/debounce-mixin";
8
+ export * from "./utils/isMulti";
9
+ export * from "./utils/getPaginate";
10
+ export * from "./utils/isPaginated";
11
+ export * from "./utils/mergeQuery";
12
+ export * from "./utils/pushSet";
13
+ export * from "./utils/setResultEmpty";
14
+ export * from "./utils/filterQuery";
15
+ export * from "./utils/getItemsIsArray";
16
+ export * from "./utils/shouldSkip";
17
+ export * from "./utils/markHookForSkip";
18
+ export * from "./utils/validateQueryProperty";
35
19
  export { filterArray } from "./filters/array";
20
+ export { filterObject } from "./filters/object";
36
21
  export * from "./types";