feathers-utils 6.0.0 → 7.0.1
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.
- package/README.md +3 -4
- package/dist/index.cjs +81 -99
- package/dist/index.d.cts +35 -33
- package/dist/index.d.mts +35 -33
- package/dist/index.d.ts +35 -33
- package/dist/index.mjs +81 -99
- package/package.json +29 -32
- package/src/.DS_Store +0 -0
- package/src/filters/array.ts +11 -13
- package/src/filters/index.ts +2 -2
- package/src/filters/object.ts +11 -11
- package/src/hooks/.DS_Store +0 -0
- package/src/hooks/checkMulti.ts +98 -82
- package/src/hooks/createRelated.ts +41 -41
- package/src/hooks/forEach.ts +32 -32
- package/src/hooks/from-client-for-server/common.ts +1 -1
- package/src/hooks/from-client-for-server/index.ts +2 -2
- package/src/hooks/from-client-for-server/paramsForServer.ts +32 -32
- package/src/hooks/from-client-for-server/paramsFromClient.ts +25 -25
- package/src/hooks/index.ts +9 -9
- package/src/hooks/onDelete.ts +54 -55
- package/src/hooks/parseFields.ts +13 -13
- package/src/hooks/removeRelated.ts +22 -20
- package/src/hooks/runPerItem.ts +17 -18
- package/src/hooks/setData.ts +295 -264
- package/src/index.ts +6 -6
- package/src/mixins/debounce-mixin/DebouncedStore.ts +29 -29
- package/src/mixins/debounce-mixin/debounceMixin.ts +17 -17
- package/src/mixins/debounce-mixin/index.ts +3 -3
- package/src/mixins/debounce-mixin/types.ts +9 -9
- package/src/mixins/debounce-mixin/utils.ts +3 -3
- package/src/mixins/index.ts +1 -1
- package/src/types.ts +3 -5
- package/src/typesInternal.ts +14 -14
- package/src/utility-types/index.ts +48 -48
- package/src/utils/_utils.internal.ts +5 -5
- package/src/utils/defineHooks.ts +8 -8
- package/src/utils/deflattenQuery.ts +31 -31
- package/src/utils/filterQuery.ts +58 -58
- package/src/utils/flattenQuery.ts +54 -54
- package/src/utils/getItemsIsArray.ts +148 -149
- package/src/utils/getPaginate.ts +31 -31
- package/src/utils/index.ts +17 -17
- package/src/utils/isMulti.ts +48 -40
- package/src/utils/isPaginated.ts +30 -30
- package/src/utils/markHookForSkip.ts +177 -178
- package/src/utils/mergeQuery/index.ts +3 -3
- package/src/utils/mergeQuery/mergeArrays.ts +67 -67
- package/src/utils/mergeQuery/mergeQuery.ts +211 -211
- package/src/utils/mergeQuery/types.ts +12 -12
- package/src/utils/mergeQuery/utils.ts +224 -224
- package/src/utils/optimizeBatchPatch.ts +42 -42
- package/src/utils/pushSet.ts +57 -57
- package/src/utils/setQueryKeySafely.ts +68 -68
- package/src/utils/setResultEmpty.ts +125 -123
- package/src/utils/shouldSkip.ts +72 -72
- package/src/utils/toJSON.ts +4 -4
- package/src/utils/validateQueryProperty.ts +10 -10
- package/src/hooks/makeSequelizeQuery.ts_ +0 -90
package/src/hooks/setData.ts
CHANGED
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
import _get from
|
|
2
|
-
import _set from
|
|
3
|
-
import _has from
|
|
1
|
+
import _get from 'lodash/get.js'
|
|
2
|
+
import _set from 'lodash/set.js'
|
|
3
|
+
import _has from 'lodash/has.js'
|
|
4
4
|
|
|
5
|
-
import { Forbidden } from
|
|
6
|
-
import { getItemsIsArray, shouldSkip } from
|
|
5
|
+
import { Forbidden } from '@feathersjs/errors'
|
|
6
|
+
import { getItemsIsArray, shouldSkip } from '../utils/index.js'
|
|
7
7
|
|
|
8
|
-
import type { HookContext } from
|
|
9
|
-
import type { PropertyPath } from
|
|
10
|
-
import type { PredicateWithContext } from
|
|
11
|
-
import { toJSON } from
|
|
8
|
+
import type { HookContext } from '@feathersjs/feathers'
|
|
9
|
+
import type { PropertyPath } from 'lodash'
|
|
10
|
+
import type { PredicateWithContext } from '../types.js'
|
|
11
|
+
import { toJSON } from '../utils/toJSON.js'
|
|
12
12
|
|
|
13
13
|
export interface HookSetDataOptions {
|
|
14
|
-
allowUndefined?: boolean
|
|
15
|
-
overwrite?: boolean | PredicateWithContext
|
|
14
|
+
allowUndefined?: boolean
|
|
15
|
+
overwrite?: boolean | PredicateWithContext
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
const defaultOptions: Required<HookSetDataOptions> = {
|
|
19
19
|
allowUndefined: false,
|
|
20
20
|
overwrite: true,
|
|
21
|
-
}
|
|
21
|
+
}
|
|
22
22
|
|
|
23
23
|
/**
|
|
24
24
|
* hook to set properties on `context.result` (if existent) or `context.data` (otherwise)
|
|
@@ -31,61 +31,62 @@ export function setData<H extends HookContext = HookContext>(
|
|
|
31
31
|
const options: Required<HookSetDataOptions> = {
|
|
32
32
|
...defaultOptions,
|
|
33
33
|
..._options,
|
|
34
|
-
}
|
|
34
|
+
}
|
|
35
35
|
return (context: H) => {
|
|
36
|
-
if (shouldSkip(
|
|
37
|
-
return context
|
|
36
|
+
if (shouldSkip('setData', context)) {
|
|
37
|
+
return context
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
-
const { items } = getItemsIsArray(context)
|
|
40
|
+
const { items } = getItemsIsArray(context)
|
|
41
41
|
|
|
42
|
-
const contextJson = toJSON(context)
|
|
42
|
+
const contextJson = toJSON(context)
|
|
43
43
|
|
|
44
44
|
if (!_has(contextJson, from)) {
|
|
45
45
|
if (!context.params?.provider || options.allowUndefined === true) {
|
|
46
|
-
return context
|
|
46
|
+
return context
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
if (
|
|
50
50
|
!options.overwrite &&
|
|
51
51
|
items.every((item: Record<string, unknown>) => _has(item, to))
|
|
52
52
|
) {
|
|
53
|
-
return context
|
|
53
|
+
return context
|
|
54
54
|
}
|
|
55
55
|
|
|
56
|
-
throw new Forbidden(`Expected field ${from.toString()} not available`)
|
|
56
|
+
throw new Forbidden(`Expected field ${from.toString()} not available`)
|
|
57
57
|
}
|
|
58
58
|
|
|
59
|
-
const val = _get(contextJson, from)
|
|
59
|
+
const val = _get(contextJson, from)
|
|
60
60
|
|
|
61
61
|
items.forEach((item: Record<string, unknown>) => {
|
|
62
|
-
let overwrite: boolean
|
|
63
|
-
if (typeof options.overwrite ===
|
|
64
|
-
overwrite = options.overwrite(item, context)
|
|
62
|
+
let overwrite: boolean
|
|
63
|
+
if (typeof options.overwrite === 'function') {
|
|
64
|
+
overwrite = options.overwrite(item, context)
|
|
65
65
|
} else {
|
|
66
|
-
overwrite = options.overwrite
|
|
66
|
+
overwrite = options.overwrite
|
|
67
67
|
}
|
|
68
68
|
|
|
69
69
|
if (!overwrite && _has(item, to)) {
|
|
70
|
-
return
|
|
70
|
+
return
|
|
71
71
|
}
|
|
72
72
|
|
|
73
|
-
_set(item, to, val)
|
|
74
|
-
})
|
|
73
|
+
_set(item, to, val)
|
|
74
|
+
})
|
|
75
75
|
|
|
76
|
-
return context
|
|
77
|
-
}
|
|
76
|
+
return context
|
|
77
|
+
}
|
|
78
78
|
}
|
|
79
79
|
|
|
80
80
|
if (import.meta.vitest) {
|
|
81
|
-
const { describe, it, assert, expect } = import.meta.vitest
|
|
81
|
+
const { describe, it, assert, expect } = import.meta.vitest
|
|
82
82
|
|
|
83
|
-
it(
|
|
83
|
+
it('sets userId for single item', function () {
|
|
84
84
|
const methodsByType = {
|
|
85
|
-
before: [
|
|
86
|
-
after: [
|
|
87
|
-
}
|
|
88
|
-
Object.keys(methodsByType).forEach((
|
|
85
|
+
before: ['create', 'update', 'patch', 'remove'],
|
|
86
|
+
after: ['find', 'get', 'create', 'update', 'patch', 'remove'],
|
|
87
|
+
}
|
|
88
|
+
Object.keys(methodsByType).forEach((_type) => {
|
|
89
|
+
const type = _type as 'before' | 'after'
|
|
89
90
|
methodsByType[type].forEach((method) => {
|
|
90
91
|
const context = {
|
|
91
92
|
method,
|
|
@@ -95,27 +96,29 @@ if (import.meta.vitest) {
|
|
|
95
96
|
id: 1,
|
|
96
97
|
},
|
|
97
98
|
},
|
|
98
|
-
} as HookContext
|
|
99
|
+
} as HookContext
|
|
99
100
|
|
|
100
|
-
const dataOrResult = type ===
|
|
101
|
-
context[dataOrResult] = {}
|
|
101
|
+
const dataOrResult = type === 'before' ? 'data' : 'result'
|
|
102
|
+
context[dataOrResult] = {}
|
|
102
103
|
|
|
103
|
-
const result = setData(
|
|
104
|
+
const result = setData('params.user.id', 'userId')(context)
|
|
104
105
|
assert.strictEqual(
|
|
105
106
|
result[dataOrResult].userId,
|
|
106
107
|
1,
|
|
107
108
|
`'${type}/${method}': ${dataOrResult} has 'userId:1'`,
|
|
108
|
-
)
|
|
109
|
-
})
|
|
110
|
-
})
|
|
111
|
-
})
|
|
109
|
+
)
|
|
110
|
+
})
|
|
111
|
+
})
|
|
112
|
+
})
|
|
112
113
|
|
|
113
|
-
it(
|
|
114
|
+
it('overwrites userId for single item', function () {
|
|
114
115
|
const methodsByType = {
|
|
115
|
-
before: [
|
|
116
|
-
after: [
|
|
117
|
-
}
|
|
118
|
-
Object.keys(methodsByType).forEach((
|
|
116
|
+
before: ['create', 'update', 'patch', 'remove'],
|
|
117
|
+
after: ['find', 'get', 'create', 'update', 'patch', 'remove'],
|
|
118
|
+
}
|
|
119
|
+
Object.keys(methodsByType).forEach((_type) => {
|
|
120
|
+
const type = _type as 'before' | 'after'
|
|
121
|
+
|
|
119
122
|
methodsByType[type].forEach((method) => {
|
|
120
123
|
const context = {
|
|
121
124
|
method,
|
|
@@ -125,27 +128,29 @@ if (import.meta.vitest) {
|
|
|
125
128
|
id: 1,
|
|
126
129
|
},
|
|
127
130
|
},
|
|
128
|
-
} as HookContext
|
|
131
|
+
} as HookContext
|
|
129
132
|
|
|
130
|
-
const dataOrResult = type ===
|
|
131
|
-
context[dataOrResult] = { userId: 2 }
|
|
133
|
+
const dataOrResult = type === 'before' ? 'data' : 'result'
|
|
134
|
+
context[dataOrResult] = { userId: 2 }
|
|
132
135
|
|
|
133
|
-
const result = setData(
|
|
136
|
+
const result = setData('params.user.id', 'userId')(context)
|
|
134
137
|
assert.strictEqual(
|
|
135
138
|
result[dataOrResult].userId,
|
|
136
139
|
1,
|
|
137
140
|
`'${type}/${method}': ${dataOrResult} has 'userId:1'`,
|
|
138
|
-
)
|
|
139
|
-
})
|
|
140
|
-
})
|
|
141
|
-
})
|
|
141
|
+
)
|
|
142
|
+
})
|
|
143
|
+
})
|
|
144
|
+
})
|
|
142
145
|
|
|
143
|
-
it(
|
|
146
|
+
it('sets userId for multiple items', function () {
|
|
144
147
|
const methodsByType = {
|
|
145
|
-
before: [
|
|
146
|
-
after: [
|
|
147
|
-
}
|
|
148
|
-
Object.keys(methodsByType).forEach((
|
|
148
|
+
before: ['create', 'update', 'patch', 'remove'],
|
|
149
|
+
after: ['find', 'get', 'create', 'update', 'patch', 'remove'],
|
|
150
|
+
}
|
|
151
|
+
Object.keys(methodsByType).forEach((_type) => {
|
|
152
|
+
const type = _type as 'before' | 'after'
|
|
153
|
+
|
|
149
154
|
methodsByType[type].forEach((method) => {
|
|
150
155
|
const context = {
|
|
151
156
|
method,
|
|
@@ -155,29 +160,31 @@ if (import.meta.vitest) {
|
|
|
155
160
|
id: 1,
|
|
156
161
|
},
|
|
157
162
|
},
|
|
158
|
-
} as HookContext
|
|
163
|
+
} as HookContext
|
|
159
164
|
|
|
160
|
-
const dataOrResult = type ===
|
|
161
|
-
context[dataOrResult] = [{}, {}, {}]
|
|
165
|
+
const dataOrResult = type === 'before' ? 'data' : 'result'
|
|
166
|
+
context[dataOrResult] = [{}, {}, {}]
|
|
162
167
|
|
|
163
|
-
const result = setData(
|
|
164
|
-
result[dataOrResult].forEach((item) => {
|
|
168
|
+
const result = setData('params.user.id', 'userId')(context)
|
|
169
|
+
result[dataOrResult].forEach((item: any) => {
|
|
165
170
|
assert.strictEqual(
|
|
166
171
|
item.userId,
|
|
167
172
|
1,
|
|
168
173
|
`'${type}/${method}': ${dataOrResult} has 'userId:1'`,
|
|
169
|
-
)
|
|
170
|
-
})
|
|
171
|
-
})
|
|
172
|
-
})
|
|
173
|
-
})
|
|
174
|
+
)
|
|
175
|
+
})
|
|
176
|
+
})
|
|
177
|
+
})
|
|
178
|
+
})
|
|
174
179
|
|
|
175
|
-
it(
|
|
180
|
+
it('overwrites userId for multiple items', function () {
|
|
176
181
|
const methodsByType = {
|
|
177
|
-
before: [
|
|
178
|
-
after: [
|
|
179
|
-
}
|
|
180
|
-
Object.keys(methodsByType).forEach((
|
|
182
|
+
before: ['create', 'update', 'patch', 'remove'],
|
|
183
|
+
after: ['find', 'get', 'create', 'update', 'patch', 'remove'],
|
|
184
|
+
}
|
|
185
|
+
Object.keys(methodsByType).forEach((_type) => {
|
|
186
|
+
const type = _type as 'before' | 'after'
|
|
187
|
+
|
|
181
188
|
methodsByType[type].forEach((method) => {
|
|
182
189
|
const context = {
|
|
183
190
|
method,
|
|
@@ -187,135 +194,145 @@ if (import.meta.vitest) {
|
|
|
187
194
|
id: 1,
|
|
188
195
|
},
|
|
189
196
|
},
|
|
190
|
-
} as HookContext
|
|
197
|
+
} as HookContext
|
|
191
198
|
|
|
192
|
-
const dataOrResult = type ===
|
|
193
|
-
context[dataOrResult] = [{ userId: 2 }, {}, { userId:
|
|
199
|
+
const dataOrResult = type === 'before' ? 'data' : 'result'
|
|
200
|
+
context[dataOrResult] = [{ userId: 2 }, {}, { userId: 'abc' }]
|
|
194
201
|
|
|
195
|
-
const result = setData(
|
|
196
|
-
result[dataOrResult].forEach((item) => {
|
|
202
|
+
const result = setData('params.user.id', 'userId')(context)
|
|
203
|
+
result[dataOrResult].forEach((item: any) => {
|
|
197
204
|
assert.strictEqual(
|
|
198
205
|
item.userId,
|
|
199
206
|
1,
|
|
200
207
|
`'${type}/${method}': ${dataOrResult} has 'userId:1'`,
|
|
201
|
-
)
|
|
202
|
-
})
|
|
203
|
-
})
|
|
204
|
-
})
|
|
205
|
-
})
|
|
208
|
+
)
|
|
209
|
+
})
|
|
210
|
+
})
|
|
211
|
+
})
|
|
212
|
+
})
|
|
206
213
|
|
|
207
214
|
it("does not change createdById if 'params.user.id' is not provided", function () {
|
|
208
215
|
const methodsByType = {
|
|
209
|
-
before: [
|
|
210
|
-
after: [
|
|
211
|
-
}
|
|
212
|
-
Object.keys(methodsByType).forEach((
|
|
216
|
+
before: ['create', 'update', 'patch', 'remove'],
|
|
217
|
+
after: ['find', 'get', 'create', 'update', 'patch', 'remove'],
|
|
218
|
+
}
|
|
219
|
+
Object.keys(methodsByType).forEach((_type) => {
|
|
220
|
+
const type = _type as 'before' | 'after'
|
|
221
|
+
|
|
213
222
|
methodsByType[type].forEach((method) => {
|
|
214
223
|
const context = {
|
|
215
224
|
method,
|
|
216
225
|
type,
|
|
217
226
|
params: {},
|
|
218
|
-
} as HookContext
|
|
227
|
+
} as HookContext
|
|
219
228
|
|
|
220
|
-
const dataOrResult = type ===
|
|
221
|
-
context[dataOrResult] = { userId: 2 }
|
|
229
|
+
const dataOrResult = type === 'before' ? 'data' : 'result'
|
|
230
|
+
context[dataOrResult] = { userId: 2 }
|
|
222
231
|
|
|
223
|
-
const result = setData(
|
|
232
|
+
const result = setData('params.user.id', 'userId')(context)
|
|
224
233
|
|
|
225
234
|
assert.strictEqual(
|
|
226
235
|
result[dataOrResult].userId,
|
|
227
236
|
2,
|
|
228
237
|
`'${type}/${method}': ${dataOrResult} has 'userId:2'`,
|
|
229
|
-
)
|
|
230
|
-
})
|
|
231
|
-
})
|
|
232
|
-
})
|
|
238
|
+
)
|
|
239
|
+
})
|
|
240
|
+
})
|
|
241
|
+
})
|
|
233
242
|
|
|
234
243
|
it("throws if 'external' is set and context.user.id is undefined", function () {
|
|
235
244
|
const methodsByType = {
|
|
236
|
-
before: [
|
|
237
|
-
after: [
|
|
238
|
-
}
|
|
239
|
-
Object.keys(methodsByType).forEach((
|
|
245
|
+
before: ['create', 'update', 'patch', 'remove'],
|
|
246
|
+
after: ['find', 'get', 'create', 'update', 'patch', 'remove'],
|
|
247
|
+
}
|
|
248
|
+
Object.keys(methodsByType).forEach((_type) => {
|
|
249
|
+
const type = _type as 'before' | 'after'
|
|
250
|
+
|
|
240
251
|
methodsByType[type].forEach((method) => {
|
|
241
252
|
const context = {
|
|
242
253
|
method,
|
|
243
254
|
type,
|
|
244
255
|
params: {
|
|
245
|
-
provider:
|
|
256
|
+
provider: 'socket.io',
|
|
246
257
|
},
|
|
247
|
-
} as HookContext
|
|
258
|
+
} as HookContext
|
|
248
259
|
|
|
249
|
-
const dataOrResult = type ===
|
|
250
|
-
context[dataOrResult] = {}
|
|
260
|
+
const dataOrResult = type === 'before' ? 'data' : 'result'
|
|
261
|
+
context[dataOrResult] = {}
|
|
251
262
|
|
|
252
|
-
expect(() => setData(
|
|
263
|
+
expect(() => setData('params.user.id', 'userId')(context)).toThrow(
|
|
253
264
|
Forbidden,
|
|
254
|
-
)
|
|
255
|
-
})
|
|
256
|
-
})
|
|
257
|
-
})
|
|
265
|
+
)
|
|
266
|
+
})
|
|
267
|
+
})
|
|
268
|
+
})
|
|
258
269
|
|
|
259
270
|
it("passes if 'external' and 'allowUndefined: true'", function () {
|
|
260
271
|
const methodsByType = {
|
|
261
|
-
before: [
|
|
262
|
-
after: [
|
|
263
|
-
}
|
|
264
|
-
Object.keys(methodsByType).forEach((
|
|
272
|
+
before: ['create', 'update', 'patch', 'remove'],
|
|
273
|
+
after: ['find', 'get', 'create', 'update', 'patch', 'remove'],
|
|
274
|
+
}
|
|
275
|
+
Object.keys(methodsByType).forEach((_type) => {
|
|
276
|
+
const type = _type as 'before' | 'after'
|
|
277
|
+
|
|
265
278
|
methodsByType[type].forEach((method) => {
|
|
266
279
|
const context = {
|
|
267
280
|
method,
|
|
268
281
|
type,
|
|
269
|
-
provider:
|
|
282
|
+
provider: 'socket.io',
|
|
270
283
|
params: {},
|
|
271
284
|
data: {},
|
|
272
|
-
} as unknown as HookContext
|
|
285
|
+
} as unknown as HookContext
|
|
273
286
|
|
|
274
287
|
assert.doesNotThrow(
|
|
275
288
|
() =>
|
|
276
|
-
setData(
|
|
289
|
+
setData('params.user.id', 'userId', { allowUndefined: true })(
|
|
277
290
|
context,
|
|
278
291
|
),
|
|
279
292
|
`'${type}/${method}': passes`,
|
|
280
|
-
)
|
|
281
|
-
})
|
|
282
|
-
})
|
|
283
|
-
})
|
|
293
|
+
)
|
|
294
|
+
})
|
|
295
|
+
})
|
|
296
|
+
})
|
|
284
297
|
|
|
285
298
|
it("passes if 'external' is set and context.user.id is undefined but overwrite: false", function () {
|
|
286
299
|
const methodsByType = {
|
|
287
|
-
before: [
|
|
288
|
-
after: [
|
|
289
|
-
}
|
|
290
|
-
Object.keys(methodsByType).forEach((
|
|
300
|
+
before: ['create', 'update', 'patch', 'remove'],
|
|
301
|
+
after: ['find', 'get', 'create', 'update', 'patch', 'remove'],
|
|
302
|
+
}
|
|
303
|
+
Object.keys(methodsByType).forEach((_type) => {
|
|
304
|
+
const type = _type as 'before' | 'after'
|
|
305
|
+
|
|
291
306
|
methodsByType[type].forEach((method) => {
|
|
292
307
|
const context = {
|
|
293
308
|
method,
|
|
294
309
|
type,
|
|
295
310
|
params: {
|
|
296
|
-
provider:
|
|
311
|
+
provider: 'socket.io',
|
|
297
312
|
},
|
|
298
|
-
} as unknown as HookContext
|
|
313
|
+
} as unknown as HookContext
|
|
299
314
|
|
|
300
|
-
const dataOrResult = type ===
|
|
301
|
-
context[dataOrResult] = { userId: 1 }
|
|
315
|
+
const dataOrResult = type === 'before' ? 'data' : 'result'
|
|
316
|
+
context[dataOrResult] = { userId: 1 }
|
|
302
317
|
|
|
303
318
|
assert.doesNotThrow(
|
|
304
319
|
() =>
|
|
305
|
-
setData(
|
|
320
|
+
setData('params.user.id', 'userId', { overwrite: false })(context),
|
|
306
321
|
`'${type}/${method}': passes`,
|
|
307
|
-
)
|
|
308
|
-
})
|
|
309
|
-
})
|
|
310
|
-
})
|
|
322
|
+
)
|
|
323
|
+
})
|
|
324
|
+
})
|
|
325
|
+
})
|
|
311
326
|
|
|
312
|
-
describe(
|
|
313
|
-
it(
|
|
327
|
+
describe('overwrite: false', function () {
|
|
328
|
+
it('sets userId for single item', function () {
|
|
314
329
|
const methodsByType = {
|
|
315
|
-
before: [
|
|
316
|
-
after: [
|
|
317
|
-
}
|
|
318
|
-
Object.keys(methodsByType).forEach((
|
|
330
|
+
before: ['create', 'update', 'patch', 'remove'],
|
|
331
|
+
after: ['find', 'get', 'create', 'update', 'patch', 'remove'],
|
|
332
|
+
}
|
|
333
|
+
Object.keys(methodsByType).forEach((_type) => {
|
|
334
|
+
const type = _type as 'before' | 'after'
|
|
335
|
+
|
|
319
336
|
methodsByType[type].forEach((method) => {
|
|
320
337
|
const context = {
|
|
321
338
|
method,
|
|
@@ -325,29 +342,31 @@ if (import.meta.vitest) {
|
|
|
325
342
|
id: 1,
|
|
326
343
|
},
|
|
327
344
|
},
|
|
328
|
-
} as unknown as HookContext
|
|
345
|
+
} as unknown as HookContext
|
|
329
346
|
|
|
330
|
-
const dataOrResult = type ===
|
|
331
|
-
context[dataOrResult] = {}
|
|
347
|
+
const dataOrResult = type === 'before' ? 'data' : 'result'
|
|
348
|
+
context[dataOrResult] = {}
|
|
332
349
|
|
|
333
|
-
const result = setData(
|
|
350
|
+
const result = setData('params.user.id', 'userId', {
|
|
334
351
|
overwrite: false,
|
|
335
|
-
})(context)
|
|
352
|
+
})(context)
|
|
336
353
|
assert.strictEqual(
|
|
337
354
|
result[dataOrResult].userId,
|
|
338
355
|
1,
|
|
339
356
|
`'${type}/${method}': ${dataOrResult} has 'userId:1'`,
|
|
340
|
-
)
|
|
341
|
-
})
|
|
342
|
-
})
|
|
343
|
-
})
|
|
357
|
+
)
|
|
358
|
+
})
|
|
359
|
+
})
|
|
360
|
+
})
|
|
344
361
|
|
|
345
|
-
it(
|
|
362
|
+
it('does not overwrite userId for single item', function () {
|
|
346
363
|
const methodsByType = {
|
|
347
|
-
before: [
|
|
348
|
-
after: [
|
|
349
|
-
}
|
|
350
|
-
Object.keys(methodsByType).forEach((
|
|
364
|
+
before: ['create', 'update', 'patch', 'remove'],
|
|
365
|
+
after: ['find', 'get', 'create', 'update', 'patch', 'remove'],
|
|
366
|
+
}
|
|
367
|
+
Object.keys(methodsByType).forEach((_type) => {
|
|
368
|
+
const type = _type as 'before' | 'after'
|
|
369
|
+
|
|
351
370
|
methodsByType[type].forEach((method) => {
|
|
352
371
|
const context = {
|
|
353
372
|
method,
|
|
@@ -357,29 +376,31 @@ if (import.meta.vitest) {
|
|
|
357
376
|
id: 1,
|
|
358
377
|
},
|
|
359
378
|
},
|
|
360
|
-
} as unknown as HookContext
|
|
379
|
+
} as unknown as HookContext
|
|
361
380
|
|
|
362
|
-
const dataOrResult = type ===
|
|
363
|
-
context[dataOrResult] = { userId: 2 }
|
|
381
|
+
const dataOrResult = type === 'before' ? 'data' : 'result'
|
|
382
|
+
context[dataOrResult] = { userId: 2 }
|
|
364
383
|
|
|
365
|
-
const result = setData(
|
|
384
|
+
const result = setData('params.user.id', 'userId', {
|
|
366
385
|
overwrite: false,
|
|
367
|
-
})(context)
|
|
386
|
+
})(context)
|
|
368
387
|
assert.strictEqual(
|
|
369
388
|
result[dataOrResult].userId,
|
|
370
389
|
2,
|
|
371
390
|
`'${type}/${method}': ${dataOrResult} has 'userId:2'`,
|
|
372
|
-
)
|
|
373
|
-
})
|
|
374
|
-
})
|
|
375
|
-
})
|
|
391
|
+
)
|
|
392
|
+
})
|
|
393
|
+
})
|
|
394
|
+
})
|
|
376
395
|
|
|
377
|
-
it(
|
|
396
|
+
it('sets userId for multiple items', function () {
|
|
378
397
|
const methodsByType = {
|
|
379
|
-
before: [
|
|
380
|
-
after: [
|
|
381
|
-
}
|
|
382
|
-
Object.keys(methodsByType).forEach((
|
|
398
|
+
before: ['create', 'update', 'patch', 'remove'],
|
|
399
|
+
after: ['find', 'get', 'create', 'update', 'patch', 'remove'],
|
|
400
|
+
}
|
|
401
|
+
Object.keys(methodsByType).forEach((_type) => {
|
|
402
|
+
const type = _type as 'before' | 'after'
|
|
403
|
+
|
|
383
404
|
methodsByType[type].forEach((method) => {
|
|
384
405
|
const context = {
|
|
385
406
|
method,
|
|
@@ -389,31 +410,33 @@ if (import.meta.vitest) {
|
|
|
389
410
|
id: 1,
|
|
390
411
|
},
|
|
391
412
|
},
|
|
392
|
-
} as unknown as HookContext
|
|
413
|
+
} as unknown as HookContext
|
|
393
414
|
|
|
394
|
-
const dataOrResult = type ===
|
|
395
|
-
context[dataOrResult] = [{}, {}, {}]
|
|
415
|
+
const dataOrResult = type === 'before' ? 'data' : 'result'
|
|
416
|
+
context[dataOrResult] = [{}, {}, {}]
|
|
396
417
|
|
|
397
|
-
const result = setData(
|
|
418
|
+
const result = setData('params.user.id', 'userId', {
|
|
398
419
|
overwrite: false,
|
|
399
|
-
})(context)
|
|
400
|
-
result[dataOrResult].forEach((item) => {
|
|
420
|
+
})(context)
|
|
421
|
+
result[dataOrResult].forEach((item: any) => {
|
|
401
422
|
assert.strictEqual(
|
|
402
423
|
item.userId,
|
|
403
424
|
1,
|
|
404
425
|
`${type}/${method}': ${dataOrResult} has 'userId:1'`,
|
|
405
|
-
)
|
|
406
|
-
})
|
|
407
|
-
})
|
|
408
|
-
})
|
|
409
|
-
})
|
|
426
|
+
)
|
|
427
|
+
})
|
|
428
|
+
})
|
|
429
|
+
})
|
|
430
|
+
})
|
|
410
431
|
|
|
411
|
-
it(
|
|
432
|
+
it('overwrites userId for multiple items', function () {
|
|
412
433
|
const methodsByType = {
|
|
413
|
-
before: [
|
|
414
|
-
after: [
|
|
415
|
-
}
|
|
416
|
-
Object.keys(methodsByType).forEach((
|
|
434
|
+
before: ['create', 'update', 'patch', 'remove'],
|
|
435
|
+
after: ['find', 'get', 'create', 'update', 'patch', 'remove'],
|
|
436
|
+
}
|
|
437
|
+
Object.keys(methodsByType).forEach((_type) => {
|
|
438
|
+
const type = _type as 'before' | 'after'
|
|
439
|
+
|
|
417
440
|
methodsByType[type].forEach((method) => {
|
|
418
441
|
const context = {
|
|
419
442
|
method,
|
|
@@ -423,33 +446,35 @@ if (import.meta.vitest) {
|
|
|
423
446
|
id: 1,
|
|
424
447
|
},
|
|
425
448
|
},
|
|
426
|
-
} as unknown as HookContext
|
|
449
|
+
} as unknown as HookContext
|
|
427
450
|
|
|
428
|
-
const dataOrResult = type ===
|
|
429
|
-
context[dataOrResult] = [{ userId: 0 }, {}, { userId: 2 }]
|
|
451
|
+
const dataOrResult = type === 'before' ? 'data' : 'result'
|
|
452
|
+
context[dataOrResult] = [{ userId: 0 }, {}, { userId: 2 }]
|
|
430
453
|
|
|
431
|
-
const result = setData(
|
|
454
|
+
const result = setData('params.user.id', 'userId', {
|
|
432
455
|
overwrite: false,
|
|
433
|
-
})(context)
|
|
434
|
-
result[dataOrResult].forEach((item, i) => {
|
|
456
|
+
})(context)
|
|
457
|
+
result[dataOrResult].forEach((item: any, i: any) => {
|
|
435
458
|
assert.strictEqual(
|
|
436
459
|
item.userId,
|
|
437
460
|
i,
|
|
438
461
|
`${type}/${method}': ${dataOrResult} has 'userId:${i}`,
|
|
439
|
-
)
|
|
440
|
-
})
|
|
441
|
-
})
|
|
442
|
-
})
|
|
443
|
-
})
|
|
444
|
-
})
|
|
445
|
-
|
|
446
|
-
describe(
|
|
447
|
-
it(
|
|
462
|
+
)
|
|
463
|
+
})
|
|
464
|
+
})
|
|
465
|
+
})
|
|
466
|
+
})
|
|
467
|
+
})
|
|
468
|
+
|
|
469
|
+
describe('overwrite: predicate', function () {
|
|
470
|
+
it('overwrites userId for multiple items per predicate', function () {
|
|
448
471
|
const methodsByType = {
|
|
449
|
-
before: [
|
|
450
|
-
after: [
|
|
451
|
-
}
|
|
452
|
-
Object.keys(methodsByType).forEach((
|
|
472
|
+
before: ['create', 'update', 'patch', 'remove'],
|
|
473
|
+
after: ['find', 'get', 'create', 'update', 'patch', 'remove'],
|
|
474
|
+
}
|
|
475
|
+
Object.keys(methodsByType).forEach((_type) => {
|
|
476
|
+
const type = _type as 'before' | 'after'
|
|
477
|
+
|
|
453
478
|
methodsByType[type].forEach((method) => {
|
|
454
479
|
const context = {
|
|
455
480
|
method,
|
|
@@ -459,31 +484,33 @@ if (import.meta.vitest) {
|
|
|
459
484
|
id: 1,
|
|
460
485
|
},
|
|
461
486
|
},
|
|
462
|
-
} as unknown as HookContext
|
|
487
|
+
} as unknown as HookContext
|
|
463
488
|
|
|
464
|
-
const dataOrResult = type ===
|
|
465
|
-
context[dataOrResult] = [{ userId: 2 }, {}, { userId:
|
|
489
|
+
const dataOrResult = type === 'before' ? 'data' : 'result'
|
|
490
|
+
context[dataOrResult] = [{ userId: 2 }, {}, { userId: 'abc' }]
|
|
466
491
|
|
|
467
|
-
const result = setData(
|
|
492
|
+
const result = setData('params.user.id', 'userId', {
|
|
468
493
|
overwrite: () => true,
|
|
469
|
-
})(context)
|
|
470
|
-
result[dataOrResult].forEach((item) => {
|
|
494
|
+
})(context)
|
|
495
|
+
result[dataOrResult].forEach((item: any) => {
|
|
471
496
|
assert.strictEqual(
|
|
472
497
|
item.userId,
|
|
473
498
|
1,
|
|
474
499
|
`'${type}/${method}': ${dataOrResult} has 'userId:1'`,
|
|
475
|
-
)
|
|
476
|
-
})
|
|
477
|
-
})
|
|
478
|
-
})
|
|
479
|
-
})
|
|
500
|
+
)
|
|
501
|
+
})
|
|
502
|
+
})
|
|
503
|
+
})
|
|
504
|
+
})
|
|
480
505
|
|
|
481
|
-
it(
|
|
506
|
+
it('does not overwrite userId for single item by predicate', function () {
|
|
482
507
|
const methodsByType = {
|
|
483
|
-
before: [
|
|
484
|
-
after: [
|
|
485
|
-
}
|
|
486
|
-
Object.keys(methodsByType).forEach((
|
|
508
|
+
before: ['create', 'update', 'patch', 'remove'],
|
|
509
|
+
after: ['find', 'get', 'create', 'update', 'patch', 'remove'],
|
|
510
|
+
}
|
|
511
|
+
Object.keys(methodsByType).forEach((_type) => {
|
|
512
|
+
const type = _type as 'before' | 'after'
|
|
513
|
+
|
|
487
514
|
methodsByType[type].forEach((method) => {
|
|
488
515
|
const context = {
|
|
489
516
|
method,
|
|
@@ -493,29 +520,31 @@ if (import.meta.vitest) {
|
|
|
493
520
|
id: 1,
|
|
494
521
|
},
|
|
495
522
|
},
|
|
496
|
-
} as unknown as HookContext
|
|
523
|
+
} as unknown as HookContext
|
|
497
524
|
|
|
498
|
-
const dataOrResult = type ===
|
|
499
|
-
context[dataOrResult] = { userId: 2 }
|
|
525
|
+
const dataOrResult = type === 'before' ? 'data' : 'result'
|
|
526
|
+
context[dataOrResult] = { userId: 2 }
|
|
500
527
|
|
|
501
|
-
const result = setData(
|
|
528
|
+
const result = setData('params.user.id', 'userId', {
|
|
502
529
|
overwrite: (item) => item.userId == null,
|
|
503
|
-
})(context)
|
|
530
|
+
})(context)
|
|
504
531
|
assert.strictEqual(
|
|
505
532
|
result[dataOrResult].userId,
|
|
506
533
|
2,
|
|
507
534
|
`'${type}/${method}': ${dataOrResult} has 'userId:2'`,
|
|
508
|
-
)
|
|
509
|
-
})
|
|
510
|
-
})
|
|
511
|
-
})
|
|
535
|
+
)
|
|
536
|
+
})
|
|
537
|
+
})
|
|
538
|
+
})
|
|
512
539
|
|
|
513
|
-
it(
|
|
540
|
+
it('predicate based on context', function () {
|
|
514
541
|
const methodsByType = {
|
|
515
|
-
before: [
|
|
516
|
-
after: [
|
|
517
|
-
}
|
|
518
|
-
Object.keys(methodsByType).forEach((
|
|
542
|
+
before: ['create', 'update', 'patch', 'remove'],
|
|
543
|
+
after: ['find', 'get', 'create', 'update', 'patch', 'remove'],
|
|
544
|
+
}
|
|
545
|
+
Object.keys(methodsByType).forEach((_type) => {
|
|
546
|
+
const type = _type as 'before' | 'after'
|
|
547
|
+
|
|
519
548
|
methodsByType[type].forEach((method) => {
|
|
520
549
|
const context = {
|
|
521
550
|
method,
|
|
@@ -525,37 +554,39 @@ if (import.meta.vitest) {
|
|
|
525
554
|
id: 1,
|
|
526
555
|
},
|
|
527
556
|
},
|
|
528
|
-
} as unknown as HookContext
|
|
557
|
+
} as unknown as HookContext
|
|
529
558
|
|
|
530
|
-
const dataOrResult = type ===
|
|
531
|
-
context[dataOrResult] = { userId: 2 }
|
|
559
|
+
const dataOrResult = type === 'before' ? 'data' : 'result'
|
|
560
|
+
context[dataOrResult] = { userId: 2 }
|
|
532
561
|
|
|
533
|
-
const result = setData(
|
|
534
|
-
overwrite: (item, context) => context.type ===
|
|
535
|
-
})(context)
|
|
536
|
-
if (type ===
|
|
562
|
+
const result = setData('params.user.id', 'userId', {
|
|
563
|
+
overwrite: (item, context) => context.type === 'before',
|
|
564
|
+
})(context)
|
|
565
|
+
if (type === 'before') {
|
|
537
566
|
assert.strictEqual(
|
|
538
567
|
result[dataOrResult].userId,
|
|
539
568
|
1,
|
|
540
569
|
`'${type}/${method}': ${dataOrResult} has 'userId:1'`,
|
|
541
|
-
)
|
|
570
|
+
)
|
|
542
571
|
} else {
|
|
543
572
|
assert.strictEqual(
|
|
544
573
|
result[dataOrResult].userId,
|
|
545
574
|
2,
|
|
546
575
|
`'${type}/${method}': ${dataOrResult} has 'userId:2'`,
|
|
547
|
-
)
|
|
576
|
+
)
|
|
548
577
|
}
|
|
549
|
-
})
|
|
550
|
-
})
|
|
551
|
-
})
|
|
578
|
+
})
|
|
579
|
+
})
|
|
580
|
+
})
|
|
552
581
|
|
|
553
|
-
it(
|
|
582
|
+
it('overwrites userId for multiple items by predicate', function () {
|
|
554
583
|
const methodsByType = {
|
|
555
|
-
before: [
|
|
556
|
-
after: [
|
|
557
|
-
}
|
|
558
|
-
Object.keys(methodsByType).forEach((
|
|
584
|
+
before: ['create', 'update', 'patch', 'remove'],
|
|
585
|
+
after: ['find', 'get', 'create', 'update', 'patch', 'remove'],
|
|
586
|
+
}
|
|
587
|
+
Object.keys(methodsByType).forEach((_type) => {
|
|
588
|
+
const type = _type as 'before' | 'after'
|
|
589
|
+
|
|
559
590
|
methodsByType[type].forEach((method) => {
|
|
560
591
|
const context = {
|
|
561
592
|
method,
|
|
@@ -565,23 +596,23 @@ if (import.meta.vitest) {
|
|
|
565
596
|
id: 1,
|
|
566
597
|
},
|
|
567
598
|
},
|
|
568
|
-
} as unknown as HookContext
|
|
599
|
+
} as unknown as HookContext
|
|
569
600
|
|
|
570
|
-
const dataOrResult = type ===
|
|
571
|
-
context[dataOrResult] = [{ userId: 0 }, {}, { userId: 2 }]
|
|
601
|
+
const dataOrResult = type === 'before' ? 'data' : 'result'
|
|
602
|
+
context[dataOrResult] = [{ userId: 0 }, {}, { userId: 2 }]
|
|
572
603
|
|
|
573
|
-
const result = setData(
|
|
604
|
+
const result = setData('params.user.id', 'userId', {
|
|
574
605
|
overwrite: (item) => item.userId == null,
|
|
575
|
-
})(context)
|
|
576
|
-
result[dataOrResult].forEach((item, i) => {
|
|
606
|
+
})(context)
|
|
607
|
+
result[dataOrResult].forEach((item: any, i: any) => {
|
|
577
608
|
assert.strictEqual(
|
|
578
609
|
item.userId,
|
|
579
610
|
i,
|
|
580
611
|
`${type}/${method}': ${dataOrResult} has 'userId:${i}`,
|
|
581
|
-
)
|
|
582
|
-
})
|
|
583
|
-
})
|
|
584
|
-
})
|
|
585
|
-
})
|
|
586
|
-
})
|
|
612
|
+
)
|
|
613
|
+
})
|
|
614
|
+
})
|
|
615
|
+
})
|
|
616
|
+
})
|
|
617
|
+
})
|
|
587
618
|
}
|