feathers-utils 1.6.1 → 2.0.0-0

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 (84) hide show
  1. package/dist/esm/hooks/checkMulti.d.ts +2 -0
  2. package/dist/esm/hooks/checkMulti.js +18 -0
  3. package/dist/esm/hooks/runPerItem.d.ts +4 -0
  4. package/dist/esm/hooks/runPerItem.js +26 -0
  5. package/dist/esm/hooks/setData.d.ts +4 -0
  6. package/dist/esm/hooks/setData.js +34 -0
  7. package/dist/esm/index.d.ts +30 -0
  8. package/dist/esm/index.js +30 -0
  9. package/dist/esm/mixins/debounce-mixin/DebouncedStore.d.ts +15 -0
  10. package/dist/esm/mixins/debounce-mixin/DebouncedStore.js +46 -0
  11. package/dist/esm/mixins/debounce-mixin/index.d.ts +8 -0
  12. package/dist/esm/mixins/debounce-mixin/index.js +20 -0
  13. package/dist/esm/types.d.ts +60 -0
  14. package/dist/esm/types.js +2 -0
  15. package/dist/esm/utils/filterQuery.d.ts +3 -0
  16. package/dist/esm/utils/filterQuery.js +28 -0
  17. package/dist/esm/utils/getPaginate.d.ts +7 -0
  18. package/dist/esm/utils/getPaginate.js +14 -0
  19. package/dist/esm/utils/isMulti.d.ts +2 -0
  20. package/dist/esm/utils/isMulti.js +17 -0
  21. package/dist/esm/utils/isPaginated.d.ts +2 -0
  22. package/dist/esm/utils/isPaginated.js +8 -0
  23. package/dist/esm/utils/markHookForSkip.d.ts +3 -0
  24. package/dist/esm/utils/markHookForSkip.js +14 -0
  25. package/dist/esm/utils/mergeQuery/index.d.ts +3 -0
  26. package/dist/esm/utils/mergeQuery/index.js +327 -0
  27. package/dist/esm/utils/mergeQuery/mergeArrays.d.ts +2 -0
  28. package/dist/esm/utils/mergeQuery/mergeArrays.js +37 -0
  29. package/dist/esm/utils/pushSet.d.ts +2 -0
  30. package/dist/esm/utils/pushSet.js +19 -0
  31. package/dist/esm/utils/setResultEmpty.d.ts +2 -0
  32. package/dist/esm/utils/setResultEmpty.js +25 -0
  33. package/dist/esm/utils/shouldSkip.d.ts +2 -0
  34. package/dist/esm/utils/shouldSkip.js +29 -0
  35. package/dist/hooks/checkMulti.js +2 -2
  36. package/dist/hooks/runPerItem.d.ts +2 -1
  37. package/dist/hooks/runPerItem.js +3 -2
  38. package/dist/hooks/setData.js +7 -6
  39. package/dist/index.d.ts +6 -3
  40. package/dist/index.js +7 -3
  41. package/dist/mixins/debounce-mixin/DebouncedStore.d.ts +2 -2
  42. package/dist/mixins/debounce-mixin/DebouncedStore.js +2 -2
  43. package/dist/mixins/debounce-mixin/index.d.ts +4 -1
  44. package/dist/mixins/debounce-mixin/index.js +1 -1
  45. package/dist/types.d.ts +3 -2
  46. package/dist/utils/filterQuery.js +2 -2
  47. package/dist/utils/getPaginate.d.ts +7 -0
  48. package/dist/utils/getPaginate.js +15 -0
  49. package/dist/utils/isMulti.js +2 -3
  50. package/dist/utils/isPaginated.d.ts +2 -0
  51. package/dist/utils/isPaginated.js +12 -0
  52. package/dist/utils/markHookForSkip.js +1 -1
  53. package/dist/utils/mergeQuery/index.js +38 -38
  54. package/dist/utils/pushSet.js +3 -3
  55. package/dist/utils/setResultEmpty.d.ts +2 -0
  56. package/dist/utils/setResultEmpty.js +29 -0
  57. package/package.json +19 -13
  58. package/src/hooks/runPerItem.ts +4 -1
  59. package/src/hooks/setData.ts +1 -0
  60. package/src/index.ts +6 -3
  61. package/src/mixins/debounce-mixin/DebouncedStore.ts +1 -1
  62. package/src/mixins/debounce-mixin/index.ts +7 -3
  63. package/src/types.ts +4 -3
  64. package/src/utils/filterQuery.ts +2 -2
  65. package/src/utils/getPaginate.ts +26 -0
  66. package/src/utils/isMulti.ts +2 -3
  67. package/src/utils/isPaginated.ts +12 -0
  68. package/src/utils/setResultEmpty.ts +28 -0
  69. package/tsconfig-esm.json +9 -0
  70. package/.github/workflows/node.js.yml +0 -45
  71. package/.vscode/launch.json +0 -28
  72. package/.vscode/settings.json +0 -14
  73. package/dist/utils/addHook.d.ts +0 -3
  74. package/dist/utils/addHook.js +0 -40
  75. package/src/utils/addHook.ts +0 -49
  76. package/test/hooks/checkMulti.test.ts +0 -121
  77. package/test/hooks/setData.test.ts +0 -333
  78. package/test/mixins/debounce-mixin.test.ts +0 -174
  79. package/test/utils/addHook.test.ts +0 -307
  80. package/test/utils/isMulti.test.ts +0 -53
  81. package/test/utils/markHookForSkip.test.ts +0 -292
  82. package/test/utils/mergeQuery.test.ts +0 -407
  83. package/test/utils/pushSet.test.ts +0 -66
  84. package/test/utils/shouldSkip.test.ts +0 -67
@@ -1,174 +0,0 @@
1
- import assert from "assert";
2
- import feathers from "@feathersjs/feathers";
3
- import createService from "feathers-memory";
4
- import { debounceMixin as makeMixin } from "../../src";
5
- import { performance } from "perf_hooks";
6
-
7
- const mockApp = () => {
8
- const app = feathers();
9
- app.configure(makeMixin({
10
- default: {
11
- wait: 50,
12
- maxWait: 1000
13
- },
14
- blacklist: ["authentication"]
15
- }));
16
- app.use("users", createService());
17
- app.use("tasks", createService());
18
- app.use("posts", createService());
19
-
20
- app.use("authentication", createService());
21
-
22
- const usersService = app.service("users");
23
- const tasksService = app.service("tasks");
24
- const postsService = app.service("posts");
25
- const authenticationService = app.service("authentication");
26
-
27
- return {
28
- app,
29
- usersService,
30
- tasksService,
31
- postsService,
32
- authenticationService
33
- };
34
- };
35
-
36
- describe("mixin: debounce-mixin", function() {
37
- it("initializes debounce-mixin", function() {
38
- const {
39
- usersService,
40
- tasksService,
41
- postsService,
42
- authenticationService
43
- } = mockApp();
44
-
45
- [usersService, tasksService, postsService].forEach(service => {
46
- assert.ok(service.debouncedStore, "service has debounceStore");
47
- assert.strictEqual(typeof service.debouncedStore.add, "function", "service has 'add' function");
48
- });
49
-
50
- assert.strictEqual(authenticationService.debouncedStore, undefined, "authentication has no debounced Store");
51
- });
52
-
53
- it("only gets called once", async function() {
54
- const {
55
- usersService
56
- } = mockApp();
57
- let callCounter = 0;
58
- for (let i = 0; i < 50; i++) {
59
- usersService.debouncedStore.add(1, () => {
60
- callCounter++;
61
- return usersService.create({ id: i, test: true });
62
- });
63
- }
64
- await new Promise(resolve => setTimeout(resolve, 400));
65
- const items = await usersService.find({ query: {}, paginate: false });
66
- assert.strictEqual(items.length, 1, "only has one item");
67
- assert.strictEqual(items[0].id, 49, "called with last iteration");
68
- assert.strictEqual(callCounter, 1, "only called once");
69
- assert.deepStrictEqual(usersService.debouncedStore._queueById, {}, "queue is empty");
70
- assert.deepStrictEqual(usersService.debouncedStore._isRunningById, {}, "nothing is running");
71
- });
72
-
73
- it("doesn't call instantly", async function() {
74
- const {
75
- usersService
76
- } = mockApp();
77
- let callCounter = 0;
78
- for (let i = 0; i < 50; i++) {
79
- usersService.debouncedStore.add(1, () => {
80
- callCounter++;
81
- return usersService.create({ id: i, test: true });
82
- });
83
- }
84
- const items = await usersService.find({ query: {}, paginate: false });
85
- assert.strictEqual(items.length, 0, "hasn't any items yet");
86
- assert.strictEqual(callCounter, 0, "not called yet");
87
- });
88
-
89
- it("stores per id", async function() {
90
- const {
91
- usersService
92
- } = mockApp();
93
- const callCounter = {};
94
- const times = 100;
95
- for (let i = 0; i < times; i++) {
96
- callCounter[i] = 0;
97
- for (let j = 0; j < 50; j++) {
98
- usersService.debouncedStore.add(i, () => {
99
- callCounter[i]++;
100
- return usersService.create({ id: i, test: true });
101
- });
102
- }
103
- }
104
-
105
- await new Promise(resolve => setTimeout(resolve, 400));
106
- const items = await usersService.find({ query: { $sort: { id: 1 } }, paginate: false });
107
- assert.strictEqual(items.length, times, `has ${times} items`);
108
- items.forEach((item, i) => {
109
- assert.strictEqual(item.id, i, "continuous ids");
110
- assert.strictEqual(callCounter[i], 1, "only called once");
111
- });
112
- });
113
-
114
- it("waits per id", function(done) {
115
- const {
116
- usersService
117
- } = mockApp();
118
- const callCounter = {
119
- 1: 0,
120
- 2: 0
121
- };
122
- const times = 60;
123
- usersService.debouncedStore.add(1, () => {
124
- callCounter[1]++;
125
- return usersService.create({ id: 1, test: true });
126
- });
127
- let counter = 0;
128
- const started = performance.now();
129
- const callTwo = () => {
130
- if (counter > times) {
131
- assert(performance.now() - started > 200, "enough time elapsed");
132
- usersService
133
- .find({ query: { $sort: { id: 1 } }, paginate: false })
134
- .then(items => {
135
- assert.strictEqual(items.length, 1, "has just one item yet");
136
- assert.strictEqual(items[0].id, 1, "id is one");
137
- const timeElapsed = performance.now() - started;
138
- assert(timeElapsed > 200, "enough time elapsed");
139
- assert(timeElapsed < 1000, "less than maxWait");
140
- done();
141
- });
142
-
143
- return;
144
- }
145
- setTimeout(() => {
146
- usersService.debouncedStore.add(2, () => {
147
- callCounter[2]++;
148
- return usersService.create({ id: 2, test: true });
149
- });
150
- counter++;
151
- callTwo();
152
- }, 5);
153
- };
154
- callTwo();
155
- });
156
-
157
- it("cancel works", async function() {
158
- const {
159
- usersService
160
- } = mockApp();
161
- let callCounter = 0;
162
- for (let i = 0; i < 50; i++) {
163
- usersService.debouncedStore.add(1, () => {
164
- callCounter++;
165
- return usersService.create({ id: i, test: true });
166
- });
167
- }
168
- usersService.debouncedStore.cancel(1);
169
- await new Promise(resolve => setTimeout(resolve, 400));
170
- const items = await usersService.find({ query: {}, paginate: false });
171
- assert.strictEqual(items.length, 0, "hasn't items");
172
- assert.strictEqual(callCounter, 0, "not called once");
173
- });
174
- });
@@ -1,307 +0,0 @@
1
- import assert from "assert";
2
- import feathers from "@feathersjs/feathers";
3
- import createService from "feathers-memory";
4
-
5
- import { addHook } from "../../src";
6
-
7
- import { HookType, ServiceMethodName } from "../../src/types";
8
-
9
- const mockApp = (withHooks?: boolean) => {
10
- const app = feathers();
11
- app.use("users", createService());
12
- app.use("tasks", createService());
13
- app.use("authentication", createService());
14
- const usersService = app.service("users");
15
- const tasksService = app.service("tasks");
16
- const authenticationService = app.service("authentication");
17
- usersService.path = "users";
18
- tasksService.path = "tasks";
19
- authenticationService.path = "authentication";
20
- if (withHooks) {
21
- [usersService, tasksService, authenticationService].forEach(service => {
22
- service.hooks({
23
- before: {
24
- // eslint-disable-next-line @typescript-eslint/no-empty-function
25
- all: [() => {}]
26
- },
27
- after: {
28
- // eslint-disable-next-line @typescript-eslint/no-empty-function
29
- all: [() => {}]
30
- },
31
- error: {
32
- // eslint-disable-next-line @typescript-eslint/no-empty-function
33
- all: [() => {}]
34
- }
35
- });
36
- });
37
- } else {
38
- [usersService, tasksService, authenticationService].forEach(service => {
39
- service.hooks({
40
- before: { all: [] },
41
- after: { all: [] },
42
- error: { all: [] }
43
- });
44
- });
45
- }
46
- return {
47
- app,
48
- usersService,
49
- tasksService,
50
- authenticationService
51
- };
52
- };
53
-
54
- describe("util - addHook", function() {
55
- it("adds all hooks", function() {
56
- const {
57
- app,
58
- usersService,
59
- tasksService,
60
- authenticationService
61
- } = mockApp();
62
-
63
- const types = ["before", "after", "error"] as HookType[];
64
- const methods = ["find", "get", "create", "update", "patch", "remove"] as ServiceMethodName[];
65
-
66
- addHook(app, "addHook", {
67
- types,
68
- methods,
69
- orderByType: {
70
- before: "first",
71
- after: "first",
72
- error: "first"
73
- }
74
- });
75
-
76
- [usersService, tasksService, authenticationService].forEach(service => {
77
- types.forEach(type => {
78
- methods.forEach(method => {
79
- const hooks = service.__hooks[type][method];
80
- assert.strictEqual(hooks.length, 1, `'${service.path}/${type}:${method}': has 1 hook`);
81
- assert.strictEqual(hooks[0], "addHook", `'${service.path}/${type}:${method}': hook is 'addHook'`);
82
- });
83
- });
84
- });
85
- });
86
-
87
- it("adds hooks to specific type", function() {
88
- const types = ["before", "after", "error"] as HookType[];
89
- const methods = ["find", "get", "create", "update", "patch", "remove"] as ServiceMethodName[];
90
-
91
- types.forEach(type => {
92
- const {
93
- app,
94
- usersService,
95
- tasksService,
96
- authenticationService
97
- } = mockApp();
98
-
99
- addHook(app, "addHook", {
100
- types: [type],
101
- methods,
102
- orderByType: {
103
- before: "first",
104
- after: "first",
105
- error: "first"
106
- }
107
- });
108
-
109
- const remainingTypes = types.filter(x => x !== type);
110
-
111
- [usersService, tasksService, authenticationService].forEach(service => {
112
- methods.forEach(method => {
113
- const hooks = service.__hooks[type][method];
114
- assert.strictEqual(hooks.length, 1, `'${service.path}/${type}:${method}': has 1 hook`);
115
- assert.strictEqual(hooks[0], "addHook", `'${service.path}/${type}:${method}': hook is 'addHook'`);
116
- remainingTypes.forEach(type => {
117
- const hooks = service.__hooks[type][method];
118
- assert.strictEqual(hooks.length, 0, `'${service.path}/${type}:${method}': has zero hooks`);
119
- });
120
- });
121
- });
122
- });
123
- });
124
-
125
- it("adds hooks to specific type and method", function() {
126
- const types = ["before", "after", "error"] as HookType[];
127
- const methods = ["find", "get", "create", "update", "patch", "remove"] as ServiceMethodName[];
128
-
129
- const method = "find";
130
-
131
- types.forEach(type => {
132
- const {
133
- app,
134
- usersService,
135
- tasksService,
136
- authenticationService
137
- } = mockApp();
138
-
139
- addHook(app, "addHook", {
140
- types: [type],
141
- methods: [method],
142
- orderByType: {
143
- before: "first",
144
- after: "first",
145
- error: "first"
146
- }
147
- });
148
-
149
- const remainingTypes = types.filter(x => x !== type);
150
- const remainingMethods = methods.filter(x => x != method);
151
-
152
- [usersService, tasksService, authenticationService].forEach(service => {
153
- const hooks = service.__hooks[type][method];
154
- assert.strictEqual(hooks.length, 1, `'${service.path}/${type}:${method}': has 1 hook`);
155
- assert.strictEqual(hooks[0], "addHook", `'${service.path}/${type}:${method}': hook is 'addHook'`);
156
- remainingTypes.forEach(type => {
157
- remainingMethods.forEach(method => {
158
- const hooks = service.__hooks[type][method];
159
- assert.strictEqual(hooks.length, 0, `'${service.path}/${type}:${method}': has zero hooks`);
160
- });
161
- });
162
- });
163
- });
164
- });
165
-
166
- it("appends all hooks with 'last'", function() {
167
- const {
168
- app,
169
- usersService,
170
- tasksService,
171
- authenticationService
172
- } = mockApp(true);
173
-
174
- const types = ["before", "after", "error"] as HookType[];
175
- const methods = ["find", "get", "create", "update", "patch", "remove"] as ServiceMethodName[];
176
-
177
- addHook(app, "addHook", {
178
- types,
179
- methods,
180
- orderByType: {
181
- before: "last",
182
- after: "last",
183
- error: "last"
184
- }
185
- });
186
-
187
- [usersService, tasksService, authenticationService].forEach(service => {
188
- types.forEach(type => {
189
- methods.forEach(method => {
190
- const hooks = service.__hooks[type][method];
191
- assert.strictEqual(hooks.length, 2, `'${service.path}/${type}:${method}': has 2 hooks`);
192
- assert.strictEqual(hooks[1], "addHook", `'${service.path}/${type}:${method}': last hook is 'addHook'`);
193
- });
194
- });
195
- });
196
- });
197
-
198
- it("prepends all hooks with 'first'", function() {
199
- const {
200
- app,
201
- usersService,
202
- tasksService,
203
- authenticationService
204
- } = mockApp(true);
205
-
206
- const types = ["before", "after", "error"] as HookType[];
207
- const methods = ["find", "get", "create", "update", "patch", "remove"] as ServiceMethodName[];
208
-
209
- addHook(app, "addHook", {
210
- types,
211
- methods,
212
- orderByType: {
213
- before: "first",
214
- after: "first",
215
- error: "first"
216
- }
217
- });
218
-
219
- [usersService, tasksService, authenticationService].forEach(service => {
220
- types.forEach(type => {
221
- methods.forEach(method => {
222
- const hooks = service.__hooks[type][method];
223
- assert.strictEqual(hooks.length, 2, `'${service.path}/${type}:${method}' has 2 hooks`);
224
- assert.strictEqual(hooks[0], "addHook", `'${service.path}/${type}:${method}': first hook is 'addHook'`);
225
- });
226
- });
227
- });
228
- });
229
-
230
- it("ignores blacklisted", function() {
231
- const {
232
- app,
233
- usersService,
234
- tasksService,
235
- authenticationService
236
- } = mockApp();
237
-
238
- const types = ["before", "after", "error"] as HookType[];
239
- const methods = ["find", "get", "create", "update", "patch", "remove"] as ServiceMethodName[];
240
-
241
- const blacklist = ["authentication"];
242
-
243
- addHook(app, "addHook", {
244
- types,
245
- methods,
246
- orderByType: {
247
- before: "first",
248
- after: "first",
249
- error: "first"
250
- },
251
- blacklist: ["authentication"]
252
- });
253
-
254
- [usersService, tasksService, authenticationService].forEach(service => {
255
- types.forEach(type => {
256
- methods.forEach(method => {
257
- const hooks = service.__hooks[type][method];
258
- if (blacklist.includes(service.path)) {
259
- assert.strictEqual(hooks.length, 0, `'${service.path}/${type}:${method}' has zero hooks`);
260
- } else {
261
- assert.strictEqual(hooks.length, 1, `'${service.path}/${type}:${method}' has 1 hook`);
262
- assert.strictEqual(hooks[0], "addHook", "hook is 'addHook'");
263
- }
264
- });
265
- });
266
- });
267
- });
268
-
269
- it("only sets whitelist", function() {
270
- const {
271
- app,
272
- usersService,
273
- tasksService,
274
- authenticationService
275
- } = mockApp();
276
-
277
- const types = ["before", "after", "error"] as HookType[];
278
- const methods = ["find", "get", "create", "update", "patch", "remove"] as ServiceMethodName[];
279
-
280
- const whitelist = ["users"];
281
-
282
- addHook(app, "addHook", {
283
- types,
284
- methods,
285
- orderByType: {
286
- before: "first",
287
- after: "first",
288
- error: "first"
289
- },
290
- whitelist
291
- });
292
-
293
- [usersService, tasksService, authenticationService].forEach(service => {
294
- types.forEach(type => {
295
- methods.forEach(method => {
296
- const hooks = service.__hooks[type][method];
297
- if (whitelist.includes(service.path)) {
298
- assert.strictEqual(hooks.length, 1, `'${service.path}/${type}:${method}' has 1 hook`);
299
- assert.strictEqual(hooks[0], "addHook", "hook is 'addHook'");
300
- } else {
301
- assert.strictEqual(hooks.length, 0, `'${service.path}/${type}:${method}' has zero hooks`);
302
- }
303
- });
304
- });
305
- });
306
- });
307
- });
@@ -1,53 +0,0 @@
1
- import assert from "assert";
2
- import { isMulti } from "../../src";
3
-
4
- describe("util - isMulti", function() {
5
- it("returns true", function() {
6
- const makeContext = (type: string, method: string) => {
7
- const context = {
8
- method,
9
- type,
10
- };
11
- if (method === "create") {
12
- (type === "before")
13
- //@ts-ignore
14
- ? context.data = []
15
- //@ts-ignore
16
- : context.result = [];
17
- }
18
- return context;
19
- };
20
- ["before", "after"].forEach(type => {
21
- ["find", "create", "patch", "remove"].forEach(method => {
22
- const context = makeContext(type, method);
23
- //@ts-ignore
24
- assert.strictEqual(isMulti(context), true, `'${type}:${method}': returns true`);
25
- });
26
- });
27
- });
28
-
29
- it("returns false", function() {
30
- const makeContext = (type: string, method: string) => {
31
- const context = {
32
- method,
33
- type,
34
- id: 0
35
- };
36
- if (method === "create") {
37
- (type === "before")
38
- //@ts-ignore
39
- ? context.data = {}
40
- //@ts-ignore
41
- : context.result = {};
42
- }
43
- return context;
44
- };
45
- ["before", "after"].forEach(type => {
46
- ["get", "create", "update", "patch", "remove"].forEach(method => {
47
- const context = makeContext(type, method);
48
- //@ts-ignore
49
- assert.strictEqual(isMulti(context), false, `'${type}:${method}': returns false`);
50
- });
51
- });
52
- });
53
- });