forked from feathersjs-ecosystem/feathers-hooks-common
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalling-params.ts
More file actions
executable file
·140 lines (127 loc) · 3.78 KB
/
calling-params.ts
File metadata and controls
executable file
·140 lines (127 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import type { HookContext, Params } from '@feathersjs/feathers';
import _get from 'lodash/get.js';
import _set from 'lodash/set.js';
export type Disablable =
| 'populate'
| 'fastJoin'
| 'ignoreDeletedAt'
| 'softDelete'
| 'softDelete2'
| 'stashBefore';
export interface CallingParamsOptions {
/**
* The params.query for the calling params.
*/
query?: any;
/**
* The names of the props in context.params to include in the new params.
*/
propNames?: string[];
/**
* Additional props to add to the new params.
*/
newProps?: any;
/**
* The names of hooks to disable during the service call. populate, fastJoin, softDelete and stashBefore are supported.
*/
hooksToDisable?: Disablable[] | Disablable;
/**
* Ignore the defaults propNames and newProps.
*/
ignoreDefaults?: boolean;
}
const stndAuthProps = ['provider', 'authenticated', 'user']; // feathers-authentication
// App wide defaults
const defaults = {
propNames: stndAuthProps,
newProps: {},
};
/**
* Set defaults for building params for service calls with callingParams. (Utility function.)
* @see https://hooks-common.feathersjs.com/utilities.html#callingparamsdefaults
*/
export function callingParamsDefaults(propNames: string[], newProps?: any): void {
if (propNames) {
defaults.propNames = Array.isArray(propNames) ? propNames : [propNames];
}
if (newProps) {
defaults.newProps = newProps;
}
}
/**
* Build params for a service call. (Utility function.)
* @see https://hooks-common.feathersjs.com/utilities.html#callingparams
*/
export function callingParams<H extends HookContext = HookContext>({
query,
propNames = [],
newProps = {},
hooksToDisable = [],
ignoreDefaults,
}: CallingParamsOptions = {}) {
return (context: H) => {
propNames = Array.isArray(propNames) ? propNames : [propNames];
hooksToDisable = Array.isArray(hooksToDisable) ? hooksToDisable : [hooksToDisable];
const newParams: Params = query ? { query } : {};
const allPropNames = ignoreDefaults ? propNames : [...defaults.propNames, ...propNames];
allPropNames.forEach(name => {
if (name) {
// for makeCallingParams compatibility
const value = _get(context.params, name);
if (value !== undefined) {
_set(newParams, name, value);
}
}
});
Object.assign(newParams, ignoreDefaults ? {} : defaults.newProps, newProps);
hooksToDisable.forEach(name => {
switch (name) {
case 'populate': // fall through
case 'fastJoin':
// @ts-ignore
newParams._populate = 'skip';
break;
case 'softDelete':
newParams.query = newParams.query || {};
newParams.query.$disableSoftDelete = true;
break;
case 'softDelete2':
// @ts-ignore
newParams.$disableSoftDelete2 = true;
break;
case 'ignoreDeletedAt':
// @ts-ignore
newParams.$ignoreDeletedAt = true;
break;
case 'stashBefore':
// @ts-ignore
newParams.disableStashBefore = true;
break;
}
});
return newParams;
};
}
/**
* You should prefer using the callingParams utility to makeCallingParams.
* Build context.params for service calls. (Utility function.)
* @see https://hooks-common.feathersjs.com/utilities.html#makecallingparams
*/
export function makeCallingParams<H extends HookContext = HookContext>(
context: H,
query?: any,
include?: string | string[],
inject = {},
) {
return callingParams({
query,
propNames:
include === undefined
? ['provider', 'authenticated', 'user']
: Array.isArray(include)
? include
: [include],
newProps: Object.assign({}, { _populate: 'skip' }, inject),
ignoreDefaults: true,
})(context);
}