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
@@ -0,0 +1,28 @@
1
+ import { HookContext } from "@feathersjs/feathers";
2
+ import { isMulti } from "..";
3
+ import { isPaginated } from "./isPaginated";
4
+
5
+ export const setResultEmpty = (
6
+ context: HookContext
7
+ ): HookContext => {
8
+ if (context.result) { return context; }
9
+
10
+ const multi = isMulti(context);
11
+
12
+ if (multi) {
13
+ if (context.method === "find" && isPaginated(context)) {
14
+ context.result = {
15
+ total: 0,
16
+ skip: 0,
17
+ limit: 0,
18
+ data: []
19
+ };
20
+ } else {
21
+ context.result = [];
22
+ }
23
+ } else {
24
+ context.result = null;
25
+ }
26
+
27
+ return context;
28
+ };
@@ -0,0 +1,9 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "compilerOptions": {
4
+ "outDir": "dist/esm",
5
+ "moduleResolution": "node",
6
+ "target": "ES2020",
7
+ "module": "ESNext"
8
+ }
9
+ }
@@ -1,45 +0,0 @@
1
- # This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
2
- # For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
3
-
4
- name: Node.js CI
5
-
6
- on:
7
- push:
8
- branches: [ main ]
9
- pull_request:
10
- branches: [ main ]
11
-
12
- jobs:
13
- test:
14
-
15
- runs-on: ubuntu-latest
16
-
17
- strategy:
18
- matrix:
19
- node-version: [12.x, 14.x, 16.x]
20
- # See supported Node.js release schedule at https://nodejs.org/en/about/releases/
21
-
22
- steps:
23
- - uses: actions/checkout@v2
24
- - name: Use Node.js ${{ matrix.node-version }}
25
- uses: actions/setup-node@v1
26
- with:
27
- node-version: ${{ matrix.node-version }}
28
- - run: npm ci
29
- - run: npm run test
30
-
31
- coverage:
32
- needs: [ test ]
33
- name: coverage
34
- runs-on: ubuntu-latest
35
- steps:
36
- - uses: actions/checkout@master
37
- - uses: actions/setup-node@master
38
- with:
39
- node-version: '14'
40
- - run: npm ci
41
- - uses: paambaati/codeclimate-action@v2.7.5
42
- env:
43
- CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }}
44
- with:
45
- coverageCommand: npm run coverage
@@ -1,28 +0,0 @@
1
- {
2
- "version": "0.2.0",
3
- "configurations": [
4
- {
5
- "type": "node",
6
- "request": "launch",
7
- "name": "Mocha Tests",
8
- "program": "${workspaceFolder}/node_modules/mocha/bin/mocha",
9
- "args": [
10
- "--require",
11
- "ts-node/register",
12
- "--timeout",
13
- "999999",
14
- "--colors",
15
- "--recursive",
16
- ],
17
- "internalConsoleOptions": "openOnSessionStart",
18
- "env": {
19
- "NODE_ENV": "test",
20
- "TS_NODE_PROJECT": "tsconfig.test.json"
21
- },
22
- "outputCapture": "std",
23
- "skipFiles": [
24
- "<node_internals>/**"
25
- ]
26
- }
27
- ]
28
- }
@@ -1,14 +0,0 @@
1
- // Place your settings in this file to overwrite default and user settings.
2
- {
3
- "search.exclude": {
4
- "**/node_modules": true,
5
- "**/public": true,
6
- "**/package-lock.json": true
7
- },
8
- "workbench.colorCustomizations": {
9
- "activityBar.background": "#525252",
10
- "titleBar.activeBackground": "#525252",
11
- "titleBar.activeForeground": "#FAFBF4"
12
- }
13
- }
14
-
@@ -1,3 +0,0 @@
1
- import type { Application } from "@feathersjs/feathers";
2
- import type { AddHookOptions } from "../types";
3
- export declare const addHook: (app: Application, hook: unknown, options: AddHookOptions) => void;
@@ -1,40 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.addHook = void 0;
4
- const defaultOptions = () => {
5
- return {
6
- whitelist: ["*"],
7
- blacklist: []
8
- };
9
- };
10
- const addHook = (app, hook, options) => {
11
- options = Object.assign(defaultOptions(), options);
12
- const { types, methods, whitelist, blacklist, orderByType } = options;
13
- if (types.some(x => !(Object.keys(orderByType).includes(x)))) {
14
- throw new Error("'feathers-utils/addHook': some types in 'orderByType' are undefined");
15
- }
16
- if (Object.keys(orderByType).some(type => !((["first", "last"]).includes(orderByType[type])))) {
17
- throw new Error("'feathers-utils/addHook': only allowed values for 'orderByType' are 'first' or 'last'");
18
- }
19
- const allServices = app.services;
20
- for (const servicePath in allServices) {
21
- if (!Object.prototype.hasOwnProperty.call(allServices, servicePath)) {
22
- continue;
23
- }
24
- const service = app.service(servicePath);
25
- if (blacklist && blacklist.includes(servicePath)) {
26
- continue;
27
- }
28
- if (whitelist && !whitelist.includes("*") && !whitelist.includes(servicePath)) {
29
- continue;
30
- }
31
- types.forEach(type => {
32
- const order = orderByType[type];
33
- const unshiftOrPush = (order === "first") ? "unshift" : "push";
34
- methods.forEach(method => {
35
- service.__hooks[type][method][unshiftOrPush](hook);
36
- });
37
- });
38
- }
39
- };
40
- exports.addHook = addHook;
@@ -1,49 +0,0 @@
1
- import type { Application } from "@feathersjs/feathers";
2
- import type { AddHookOptions, HookType } from "../types";
3
-
4
- const defaultOptions = (): Partial<AddHookOptions> => {
5
- return {
6
- whitelist: ["*"],
7
- blacklist: []
8
- };
9
- };
10
-
11
- export const addHook = (app: Application, hook: unknown, options: AddHookOptions): void => {
12
- options = Object.assign(defaultOptions(), options);
13
-
14
- const {
15
- types,
16
- methods,
17
- whitelist,
18
- blacklist,
19
- orderByType
20
- } = options;
21
-
22
- if (types.some(
23
- x => !(Object.keys(orderByType).includes(x)))
24
- ) {
25
- throw new Error("'feathers-utils/addHook': some types in 'orderByType' are undefined");
26
- }
27
- if (
28
- (Object.keys(orderByType) as HookType[]).some(
29
- type => !((["first", "last"]).includes(orderByType[type])))
30
- ) {
31
- throw new Error("'feathers-utils/addHook': only allowed values for 'orderByType' are 'first' or 'last'");
32
- }
33
-
34
- const allServices = app.services;
35
-
36
- for (const servicePath in allServices) {
37
- if(!Object.prototype.hasOwnProperty.call(allServices, servicePath)) { continue; }
38
- const service = app.service(servicePath);
39
- if (blacklist && blacklist.includes(servicePath)) { continue; }
40
- if (whitelist && !whitelist.includes("*") && !whitelist.includes(servicePath)) { continue; }
41
- types.forEach(type => {
42
- const order = orderByType[type];
43
- const unshiftOrPush = (order === "first") ? "unshift" : "push";
44
- methods.forEach(method => {
45
- service.__hooks[type][method][unshiftOrPush](hook);
46
- });
47
- });
48
- }
49
- };
@@ -1,121 +0,0 @@
1
- import assert from "assert";
2
- import { checkMulti } from "../../src";
3
-
4
- describe("hook - checkMulti", function() {
5
- it("passes if 'allowsMulti' not defined", function() {
6
- const makeContext = (type: string, method: string) => ({
7
- service: {},
8
- type,
9
- method
10
- });
11
- ["before", "after"].forEach(type => {
12
- ["find", "get", "create", "update", "patch", "remove"].forEach(method => {
13
- const context = makeContext(type, method);
14
- //@ts-ignore
15
- assert.doesNotThrow(() => checkMulti()(context), `'${type}:${method}': does not throw`);
16
- });
17
- });
18
- });
19
-
20
- it("passes if 'allowsMulti' returns true", function() {
21
- const makeContext = (type: string, method: string) => {
22
- const context = {
23
- service: {
24
- allowsMulti: () => true
25
- },
26
- method,
27
- type,
28
- };
29
- if (method === "create") {
30
- (type === "before")
31
- //@ts-ignore
32
- ? context.data = []
33
- //@ts-ignore
34
- : context.result = [];
35
- }
36
- return context;
37
- };
38
- ["before", "after"].forEach(type => {
39
- ["find", "get", "create", "update", "patch", "remove"].forEach(method => {
40
- const context = makeContext(type, method);
41
- //@ts-ignore
42
- assert.doesNotThrow(() => checkMulti()(context), `'${type}:${method}': does not throw`);
43
- });
44
- });
45
- });
46
-
47
- it("passes for 'find', 'get' and 'update'", function() {
48
- const makeContext = (type: string, method: string) => ({
49
- service: {
50
- allowsMulti: () => false
51
- },
52
- type,
53
- method
54
- });
55
- ["before", "after"].forEach(type => {
56
- ["find", "get", "update"].forEach(method => {
57
- const context = makeContext(type, method);
58
- //@ts-ignore
59
- assert.doesNotThrow(() => checkMulti()(context), `'${type}:${method}': does not throw`);
60
- });
61
- });
62
- });
63
-
64
- it("passes if 'allowsMulti' returns false and is no multi data", function() {
65
- const makeContext = (type: string, method: string) => {
66
- const context = {
67
- service: {
68
- allowsMulti: (method: string) => method !== "find"
69
- },
70
- method,
71
- type,
72
- };
73
- if (method === "create") {
74
- (type === "before")
75
- //@ts-ignore
76
- ? context.data = {}
77
- //@ts-ignore
78
- : context.result = {};
79
- }
80
- if (["patch", "remove"].includes(method)) {
81
- //@ts-ignore
82
- context.id = 1;
83
- }
84
- return context;
85
- };
86
- ["before", "after"].forEach(type => {
87
- ["create", "patch", "remove"].forEach(method => {
88
- const context = makeContext(type, method);
89
- //@ts-ignore
90
- assert.doesNotThrow(() => checkMulti()(context), `'${type}:${method}': does not throw`);
91
- });
92
- });
93
- });
94
-
95
- it("throws if 'allowsMulti' returns false and multi data", function() {
96
- const makeContext = (type: string, method: string) => {
97
- const context = {
98
- service: {
99
- allowsMulti: () => false
100
- },
101
- method,
102
- type,
103
- };
104
- if (method === "create") {
105
- (type === "before")
106
- //@ts-ignore
107
- ? context.data = []
108
- //@ts-ignore
109
- : context.result = [];
110
- }
111
- return context;
112
- };
113
- ["before", "after"].forEach(type => {
114
- ["create", "patch", "remove"].forEach(method => {
115
- const context = makeContext(type, method);
116
- //@ts-ignore
117
- assert.throws(() => checkMulti()(context), `'${type}:${method}': throws`);
118
- });
119
- });
120
- });
121
- });
@@ -1,333 +0,0 @@
1
- import assert from "assert";
2
- import { setData } from "../../src";
3
-
4
- describe("hook - setData", function() {
5
- it("sets userId for single item", function() {
6
- const methodsByType = {
7
- "before": ["create", "update", "patch", "remove"],
8
- "after": ["find", "get", "create", "update", "patch", "remove"]
9
- };
10
- Object.keys(methodsByType).forEach(type => {
11
- methodsByType[type].forEach(method => {
12
- const context = {
13
- method,
14
- type,
15
- params: {
16
- user: {
17
- id: 1
18
- }
19
- }
20
- };
21
-
22
- const dataOrResult = (type === "before") ? "data" : "result";
23
- context[dataOrResult] = {};
24
-
25
- //@ts-ignore
26
- const result = setData("params.user.id", "userId")(context);
27
- assert.strictEqual(result[dataOrResult].userId, 1, `'${type}/${method}': ${dataOrResult} has 'userId:1'`);
28
- });
29
- });
30
- });
31
-
32
- it("overwrites userId for single item", function() {
33
- const methodsByType = {
34
- "before": ["create", "update", "patch", "remove"],
35
- "after": ["find", "get", "create", "update", "patch", "remove"]
36
- };
37
- Object.keys(methodsByType).forEach(type => {
38
- methodsByType[type].forEach(method => {
39
- const context = {
40
- method,
41
- type,
42
- params: {
43
- user: {
44
- id: 1
45
- }
46
- }
47
- };
48
-
49
- const dataOrResult = (type === "before") ? "data" : "result";
50
- context[dataOrResult] = { userId: 2 };
51
-
52
- //@ts-ignore
53
- const result = setData("params.user.id", "userId")(context);
54
- assert.strictEqual(result[dataOrResult].userId, 1, `'${type}/${method}': ${dataOrResult} has 'userId:1'`);
55
- });
56
- });
57
- });
58
-
59
- it("sets userId for multiple items", function() {
60
- const methodsByType = {
61
- "before": ["create", "update", "patch", "remove"],
62
- "after": ["find", "get", "create", "update", "patch", "remove"]
63
- };
64
- Object.keys(methodsByType).forEach(type => {
65
- methodsByType[type].forEach(method => {
66
- const context = {
67
- method,
68
- type,
69
- params: {
70
- user: {
71
- id: 1
72
- }
73
- },
74
- };
75
-
76
- const dataOrResult = (type === "before") ? "data" : "result";
77
- context[dataOrResult] = [{}, {}, {}];
78
-
79
- //@ts-ignore
80
- const result = setData("params.user.id", "userId")(context);
81
- result[dataOrResult].forEach(item => {
82
- assert.strictEqual(item.userId, 1, `'${type}/${method}': ${dataOrResult} has 'userId:1'`);
83
- });
84
- });
85
- });
86
- });
87
-
88
- it("overwrites userId for multiple items", function() {
89
- const methodsByType = {
90
- "before": ["create", "update", "patch", "remove"],
91
- "after": ["find", "get", "create", "update", "patch", "remove"]
92
- };
93
- Object.keys(methodsByType).forEach(type => {
94
- methodsByType[type].forEach(method => {
95
- const context = {
96
- method,
97
- type,
98
- params: {
99
- user: {
100
- id: 1
101
- }
102
- }
103
- };
104
-
105
- const dataOrResult = (type === "before") ? "data" : "result";
106
- context[dataOrResult] = [{ userId: 2 }, {}, { userId: "abc" }];
107
-
108
- //@ts-ignore
109
- const result = setData("params.user.id", "userId")(context);
110
- result[dataOrResult].forEach(item => {
111
- assert.strictEqual(item.userId, 1, `'${type}/${method}': ${dataOrResult} has 'userId:1'`);
112
- });
113
- });
114
- });
115
- });
116
-
117
- it("does not change createdById if 'params.user.id' is not provided", function() {
118
- const methodsByType = {
119
- "before": ["create", "update", "patch", "remove"],
120
- "after": ["find", "get", "create", "update", "patch", "remove"]
121
- };
122
- Object.keys(methodsByType).forEach(type => {
123
- methodsByType[type].forEach(method => {
124
- const context = {
125
- method,
126
- type,
127
- params: {}
128
- };
129
-
130
- const dataOrResult = (type === "before") ? "data" : "result";
131
- context[dataOrResult] = { userId: 2 };
132
-
133
- //@ts-ignore
134
- const result = setData("params.user.id", "userId")(context);
135
-
136
- assert.strictEqual(result[dataOrResult].userId, 2, `'${type}/${method}': ${dataOrResult} has 'userId:2'`);
137
- });
138
- });
139
- });
140
-
141
- it("throws if 'external' is set and context.user.id is undefined", function() {
142
- const methodsByType = {
143
- "before": ["create", "update", "patch", "remove"],
144
- "after": ["find", "get", "create", "update", "patch", "remove"]
145
- };
146
- Object.keys(methodsByType).forEach(type => {
147
- methodsByType[type].forEach(method => {
148
- const context = {
149
- method,
150
- type,
151
- params: {
152
- provider: "socket.io"
153
- }
154
- };
155
-
156
- const dataOrResult = (type === "before") ? "data" : "result";
157
- context[dataOrResult] = {};
158
-
159
- assert.throws(
160
- //@ts-ignore
161
- () => setData("params.user.id", "userId")(context),
162
- err => err.name === "Forbidden",
163
- `'${type}/${method}': throws 'Forbidden' error`
164
- );
165
- });
166
- });
167
- });
168
-
169
- it("passes if 'external' and 'allowUndefined: true'", function() {
170
- const methodsByType = {
171
- "before": ["create", "update", "patch", "remove"],
172
- "after": ["find", "get", "create", "update", "patch", "remove"]
173
- };
174
- Object.keys(methodsByType).forEach(type => {
175
- methodsByType[type].forEach(method => {
176
- const context = {
177
- method,
178
- type,
179
- provider: "socket.io",
180
- params: {},
181
- data: {}
182
- };
183
-
184
- assert.doesNotThrow(
185
- //@ts-ignore
186
- () => setData("params.user.id", "userId", { allowUndefined: true })(context),
187
- `'${type}/${method}': passes`
188
- );
189
- });
190
- });
191
- });
192
-
193
- it("passes if 'external' is set and context.user.id is undefined but overwrite: false", function() {
194
- const methodsByType = {
195
- "before": ["create", "update", "patch", "remove"],
196
- "after": ["find", "get", "create", "update", "patch", "remove"]
197
- };
198
- Object.keys(methodsByType).forEach(type => {
199
- methodsByType[type].forEach(method => {
200
- const context = {
201
- method,
202
- type,
203
- params: {
204
- provider: "socket.io"
205
- }
206
- };
207
-
208
- const dataOrResult = (type === "before") ? "data" : "result";
209
- context[dataOrResult] = { userId: 1 };
210
-
211
- assert.doesNotThrow(
212
- //@ts-ignore
213
- () => setData("params.user.id", "userId", { overwrite: false })(context),
214
- `'${type}/${method}': passes`
215
- );
216
- });
217
- });
218
- });
219
-
220
- describe("overwrite: false", function() {
221
- it("sets userId for single item", function() {
222
- const methodsByType = {
223
- "before": ["create", "update", "patch", "remove"],
224
- "after": ["find", "get", "create", "update", "patch", "remove"]
225
- };
226
- Object.keys(methodsByType).forEach(type => {
227
- methodsByType[type].forEach(method => {
228
- const context = {
229
- method,
230
- type,
231
- params: {
232
- user: {
233
- id: 1
234
- }
235
- }
236
- };
237
-
238
- const dataOrResult = (type === "before") ? "data" : "result";
239
- context[dataOrResult] = {};
240
-
241
- //@ts-ignore
242
- const result = setData("params.user.id", "userId", { overwrite: false })(context);
243
- assert.strictEqual(result[dataOrResult].userId, 1, `'${type}/${method}': ${dataOrResult} has 'userId:1'`);
244
- });
245
- });
246
- });
247
-
248
- it("does not overwrite userId for single item", function() {
249
- const methodsByType = {
250
- "before": ["create", "update", "patch", "remove"],
251
- "after": ["find", "get", "create", "update", "patch", "remove"]
252
- };
253
- Object.keys(methodsByType).forEach(type => {
254
- methodsByType[type].forEach(method => {
255
- const context = {
256
- method,
257
- type,
258
- params: {
259
- user: {
260
- id: 1
261
- }
262
- }
263
- };
264
-
265
- const dataOrResult = (type === "before") ? "data" : "result";
266
- context[dataOrResult] = { userId: 2 };
267
-
268
- //@ts-ignore
269
- const result = setData("params.user.id", "userId", { overwrite: false })(context);
270
- assert.strictEqual(result[dataOrResult].userId, 2, `'${type}/${method}': ${dataOrResult} has 'userId:2'`);
271
- });
272
- });
273
- });
274
-
275
- it("sets userId for multiple items", function() {
276
- const methodsByType = {
277
- "before": ["create", "update", "patch", "remove"],
278
- "after": ["find", "get", "create", "update", "patch", "remove"]
279
- };
280
- Object.keys(methodsByType).forEach(type => {
281
- methodsByType[type].forEach(method => {
282
- const context = {
283
- method,
284
- type,
285
- params: {
286
- user: {
287
- id: 1
288
- }
289
- },
290
- };
291
-
292
- const dataOrResult = (type === "before") ? "data" : "result";
293
- context[dataOrResult] = [{}, {}, {}];
294
-
295
- //@ts-ignore
296
- const result = setData("params.user.id", "userId", { overwrite: false })(context);
297
- result[dataOrResult].forEach((item) => {
298
- assert.strictEqual(item.userId, 1, `${type}/${method}': ${dataOrResult} has 'userId:1'`);
299
- });
300
- });
301
- });
302
- });
303
-
304
- it("overwrites userId for multiple items", function() {
305
- const methodsByType = {
306
- "before": ["create", "update", "patch", "remove"],
307
- "after": ["find", "get", "create", "update", "patch", "remove"]
308
- };
309
- Object.keys(methodsByType).forEach(type => {
310
- methodsByType[type].forEach(method => {
311
- const context = {
312
- method,
313
- type,
314
- params: {
315
- user: {
316
- id: 1
317
- }
318
- }
319
- };
320
-
321
- const dataOrResult = (type === "before") ? "data" : "result";
322
- context[dataOrResult] = [{ userId: 0 }, {}, { userId: 2 }];
323
-
324
- //@ts-ignore
325
- const result = setData("params.user.id", "userId", { overwrite: false })(context);
326
- result[dataOrResult].forEach((item, i) => {
327
- assert.strictEqual(item.userId, i, `${type}/${method}': ${dataOrResult} has 'userId:${i}`);
328
- });
329
- });
330
- });
331
- });
332
- });
333
- });