@lenne.tech/nest-server 8.0.2 → 8.1.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.
- package/dist/core/common/args/pagination.args.js +1 -1
- package/dist/core/common/args/pagination.args.js.map +1 -1
- package/dist/core/common/helpers/config.helper.d.ts +2 -1
- package/dist/core/common/helpers/config.helper.js +11 -7
- package/dist/core/common/helpers/config.helper.js.map +1 -1
- package/dist/core/common/helpers/context.helper.d.ts +7 -1
- package/dist/core/common/helpers/context.helper.js +33 -29
- package/dist/core/common/helpers/context.helper.js.map +1 -1
- package/dist/core/common/helpers/db.helper.d.ts +35 -0
- package/dist/core/common/helpers/db.helper.js +345 -0
- package/dist/core/common/helpers/db.helper.js.map +1 -0
- package/dist/core/common/helpers/file.helper.d.ts +8 -1
- package/dist/core/common/helpers/file.helper.js +43 -31
- package/dist/core/common/helpers/file.helper.js.map +1 -1
- package/dist/core/common/helpers/filter.helper.d.ts +3 -0
- package/dist/core/common/helpers/filter.helper.js +93 -81
- package/dist/core/common/helpers/filter.helper.js.map +1 -1
- package/dist/core/common/helpers/graphql.helper.d.ts +24 -1
- package/dist/core/common/helpers/graphql.helper.js +144 -96
- package/dist/core/common/helpers/graphql.helper.js.map +1 -1
- package/dist/core/common/helpers/input.helper.d.ts +29 -3
- package/dist/core/common/helpers/input.helper.js +222 -105
- package/dist/core/common/helpers/input.helper.js.map +1 -1
- package/dist/core/common/helpers/model.helper.d.ts +11 -0
- package/dist/core/common/helpers/model.helper.js +41 -29
- package/dist/core/common/helpers/model.helper.js.map +1 -1
- package/dist/core/common/helpers/service.helper.d.ts +21 -1
- package/dist/core/common/helpers/service.helper.js +80 -72
- package/dist/core/common/helpers/service.helper.js.map +1 -1
- package/dist/core/common/inputs/combined-filter.input.js +1 -1
- package/dist/core/common/inputs/combined-filter.input.js.map +1 -1
- package/dist/core/common/inputs/core-input.input.js +1 -1
- package/dist/core/common/inputs/core-input.input.js.map +1 -1
- package/dist/core/common/interceptors/check-response.interceptor.js +1 -1
- package/dist/core/common/interceptors/check-response.interceptor.js.map +1 -1
- package/dist/core/common/interfaces/resolve-selector.interface.d.ts +5 -0
- package/dist/core/common/interfaces/resolve-selector.interface.js +3 -0
- package/dist/core/common/interfaces/resolve-selector.interface.js.map +1 -0
- package/dist/core/common/interfaces/service-options.interface.d.ts +30 -0
- package/dist/core/common/interfaces/service-options.interface.js +3 -0
- package/dist/core/common/interfaces/service-options.interface.js.map +1 -0
- package/dist/core/common/models/core-model.model.d.ts +4 -0
- package/dist/core/common/models/core-model.model.js +1 -1
- package/dist/core/common/models/core-model.model.js.map +1 -1
- package/dist/core/common/pipes/check-input.pipe.js +2 -2
- package/dist/core/common/pipes/check-input.pipe.js.map +1 -1
- package/dist/core/common/pipes/map-and-validate.pipe.js +1 -1
- package/dist/core/common/pipes/map-and-validate.pipe.js.map +1 -1
- package/dist/core/common/services/crud.service.d.ts +13 -0
- package/dist/core/common/services/crud.service.js +59 -0
- package/dist/core/common/services/crud.service.js.map +1 -0
- package/dist/core/common/services/email.service.js +8 -8
- package/dist/core/common/services/email.service.js.map +1 -1
- package/dist/core/common/services/module.service.d.ts +27 -0
- package/dist/core/common/services/module.service.js +41 -0
- package/dist/core/common/services/module.service.js.map +1 -0
- package/dist/core/common/types/core-model-constructor.type.d.ts +21 -0
- package/dist/core/common/types/core-model-constructor.type.js +3 -0
- package/dist/core/common/types/core-model-constructor.type.js.map +1 -0
- package/dist/core/common/types/field-selection.type.d.ts +4 -0
- package/dist/core/common/types/field-selection.type.js +3 -0
- package/dist/core/common/types/field-selection.type.js.map +1 -0
- package/dist/core/common/types/string-or-object-id.type.d.ts +2 -0
- package/dist/core/common/types/string-or-object-id.type.js +3 -0
- package/dist/core/common/types/string-or-object-id.type.js.map +1 -0
- package/dist/core/modules/auth/core-auth.resolver.d.ts +2 -1
- package/dist/core/modules/auth/core-auth.resolver.js +4 -3
- package/dist/core/modules/auth/core-auth.resolver.js.map +1 -1
- package/dist/core/modules/auth/services/core-auth-user.service.d.ts +3 -1
- package/dist/core/modules/auth/services/core-auth-user.service.js.map +1 -1
- package/dist/core/modules/auth/services/core-auth.service.d.ts +2 -1
- package/dist/core/modules/auth/services/core-auth.service.js +6 -4
- package/dist/core/modules/auth/services/core-auth.service.js.map +1 -1
- package/dist/core/modules/user/core-user.model.js +1 -1
- package/dist/core/modules/user/core-user.model.js.map +1 -1
- package/dist/core/modules/user/core-user.service.d.ts +16 -25
- package/dist/core/modules/user/core-user.service.js +79 -98
- package/dist/core/modules/user/core-user.service.js.map +1 -1
- package/dist/core.module.js +1 -1
- package/dist/core.module.js.map +1 -1
- package/dist/index.d.ts +8 -1
- package/dist/index.js +8 -1
- package/dist/index.js.map +1 -1
- package/dist/server/modules/auth/auth.resolver.d.ts +2 -1
- package/dist/server/modules/auth/auth.resolver.js +4 -3
- package/dist/server/modules/auth/auth.resolver.js.map +1 -1
- package/dist/server/modules/file/file.controller.js +1 -1
- package/dist/server/modules/file/file.controller.js.map +1 -1
- package/dist/server/modules/user/avatar.controller.js +1 -1
- package/dist/server/modules/user/avatar.controller.js.map +1 -1
- package/dist/server/modules/user/user.model.d.ts +1 -0
- package/dist/server/modules/user/user.module.js +7 -3
- package/dist/server/modules/user/user.module.js.map +1 -1
- package/dist/server/modules/user/user.resolver.d.ts +8 -7
- package/dist/server/modules/user/user.resolver.js +65 -45
- package/dist/server/modules/user/user.resolver.js.map +1 -1
- package/dist/server/modules/user/user.service.d.ts +9 -18
- package/dist/server/modules/user/user.service.js +19 -30
- package/dist/server/modules/user/user.service.js.map +1 -1
- package/dist/test/test.helper.d.ts +1 -2
- package/dist/test/test.helper.js +1 -16
- package/dist/test/test.helper.js.map +1 -1
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +44 -45
- package/src/core/common/args/pagination.args.ts +2 -2
- package/src/core/common/helpers/config.helper.ts +26 -6
- package/src/core/common/helpers/context.helper.ts +42 -33
- package/src/core/common/helpers/db.helper.ts +580 -0
- package/src/core/common/helpers/file.helper.ts +76 -49
- package/src/core/common/helpers/filter.helper.ts +119 -96
- package/src/core/common/helpers/graphql.helper.ts +219 -117
- package/src/core/common/helpers/input.helper.ts +296 -106
- package/src/core/common/helpers/model.helper.ts +102 -57
- package/src/core/common/helpers/service.helper.ts +149 -117
- package/src/core/common/inputs/combined-filter.input.ts +2 -2
- package/src/core/common/inputs/core-input.input.ts +2 -2
- package/src/core/common/interceptors/check-response.interceptor.ts +2 -2
- package/src/core/common/interfaces/resolve-selector.interface.ts +9 -0
- package/src/core/common/interfaces/service-options.interface.ts +53 -0
- package/src/core/common/models/core-model.model.ts +6 -2
- package/src/core/common/pipes/check-input.pipe.ts +4 -4
- package/src/core/common/pipes/map-and-validate.pipe.ts +2 -2
- package/src/core/common/services/crud.service.ts +105 -0
- package/src/core/common/services/email.service.ts +9 -9
- package/src/core/common/services/module.service.ts +114 -0
- package/src/core/common/types/core-model-constructor.type.ts +30 -0
- package/src/core/common/types/field-selection.type.ts +8 -0
- package/src/core/common/types/string-or-object-id.type.ts +3 -0
- package/src/core/modules/auth/core-auth.module.ts +1 -1
- package/src/core/modules/auth/core-auth.resolver.ts +8 -3
- package/src/core/modules/auth/services/core-auth-user.service.ts +7 -1
- package/src/core/modules/auth/services/core-auth.service.ts +14 -4
- package/src/core/modules/user/core-user.model.ts +2 -1
- package/src/core/modules/user/core-user.service.ts +131 -194
- package/src/core.module.ts +2 -2
- package/src/index.ts +8 -1
- package/src/main.ts +1 -1
- package/src/server/modules/auth/auth.resolver.ts +8 -3
- package/src/server/modules/file/file.controller.ts +2 -2
- package/src/server/modules/user/avatar.controller.ts +2 -2
- package/src/server/modules/user/user.module.ts +7 -3
- package/src/server/modules/user/user.resolver.ts +62 -37
- package/src/server/modules/user/user.service.ts +23 -53
- package/src/test/test.helper.ts +31 -30
- package/dist/core/modules/user/core-basic-user.service.d.ts +0 -17
- package/dist/core/modules/user/core-basic-user.service.js +0 -73
- package/dist/core/modules/user/core-basic-user.service.js.map +0 -1
- package/src/core/modules/user/core-basic-user.service.ts +0 -138
|
@@ -6,8 +6,9 @@ import { checkRestricted } from '../decorators/restricted.decorator';
|
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* Helper class for inputs
|
|
9
|
+
* @deprecated use functions directly
|
|
9
10
|
*/
|
|
10
|
-
export class InputHelper {
|
|
11
|
+
export default class InputHelper {
|
|
11
12
|
/**
|
|
12
13
|
* Check input
|
|
13
14
|
*/
|
|
@@ -16,53 +17,26 @@ export class InputHelper {
|
|
|
16
17
|
user: { id: any; hasRole: (roles: string[]) => boolean },
|
|
17
18
|
metatype?
|
|
18
19
|
): Promise<any> {
|
|
19
|
-
|
|
20
|
-
if (typeof value !== 'object' || !metatype || this.isBasicType(metatype)) {
|
|
21
|
-
return value;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
// Convert to metatype
|
|
25
|
-
if (!(value instanceof metatype)) {
|
|
26
|
-
if ((metatype as any)?.map) {
|
|
27
|
-
value = (metatype as any)?.map(value);
|
|
28
|
-
} else {
|
|
29
|
-
value = plainToInstance(metatype, value);
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
// Validate
|
|
34
|
-
const errors = await validate(value);
|
|
35
|
-
if (errors.length > 0) {
|
|
36
|
-
throw new BadRequestException('Validation failed');
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
// Remove restricted values if roles are missing
|
|
40
|
-
value = checkRestricted(value, user);
|
|
41
|
-
return value;
|
|
20
|
+
return check(value, user, metatype);
|
|
42
21
|
}
|
|
43
22
|
|
|
44
23
|
// Standard error function
|
|
45
|
-
public static errorFunction(caller: (...params) => any) {
|
|
46
|
-
|
|
47
|
-
Error.captureStackTrace(err, caller);
|
|
48
|
-
throw err;
|
|
24
|
+
public static errorFunction(caller: (...params) => any, message = 'Required parameter is missing or invalid') {
|
|
25
|
+
return errorFunction(caller, message);
|
|
49
26
|
}
|
|
50
27
|
|
|
51
28
|
/**
|
|
52
29
|
* Check if parameter is an array
|
|
53
30
|
*/
|
|
54
|
-
public static isArray(parameter: any, falseFunction: (...params) => any =
|
|
55
|
-
return parameter
|
|
56
|
-
? true
|
|
57
|
-
: falseFunction(InputHelper.isArray);
|
|
31
|
+
public static isArray(parameter: any, falseFunction: (...params) => any = errorFunction): boolean {
|
|
32
|
+
return isArray(parameter, falseFunction);
|
|
58
33
|
}
|
|
59
34
|
|
|
60
35
|
/**
|
|
61
36
|
* Checks if it is a basic type
|
|
62
37
|
*/
|
|
63
|
-
public static isBasicType(metatype: any, falseFunction: (...params) => any =
|
|
64
|
-
|
|
65
|
-
return types.includes(metatype) ? true : falseFunction(InputHelper.isBasicType);
|
|
38
|
+
public static isBasicType(metatype: any, falseFunction: (...params) => any = returnFalse): boolean {
|
|
39
|
+
return isBasicType(metatype, falseFunction);
|
|
66
40
|
}
|
|
67
41
|
|
|
68
42
|
/**
|
|
@@ -72,57 +46,44 @@ export class InputHelper {
|
|
|
72
46
|
parameter: number,
|
|
73
47
|
min: number,
|
|
74
48
|
max: number,
|
|
75
|
-
falseFunction: (...params) => any =
|
|
49
|
+
falseFunction: (...params) => any = errorFunction
|
|
76
50
|
): boolean {
|
|
77
|
-
return
|
|
78
|
-
? true
|
|
79
|
-
: falseFunction(InputHelper.isBetween);
|
|
51
|
+
return isBetween(parameter, min, max, falseFunction);
|
|
80
52
|
}
|
|
81
53
|
|
|
82
54
|
/**
|
|
83
55
|
* Check if parameter is a Date
|
|
84
56
|
*/
|
|
85
|
-
public static isDate(parameter: Date, falseFunction: (...params) => any =
|
|
86
|
-
return parameter
|
|
57
|
+
public static isDate(parameter: Date, falseFunction: (...params) => any = errorFunction): boolean {
|
|
58
|
+
return isDate(parameter, falseFunction);
|
|
87
59
|
}
|
|
88
60
|
|
|
89
61
|
/**
|
|
90
62
|
* Check if parameter is a valid email address
|
|
91
63
|
*/
|
|
92
|
-
public static isEmail(parameter: string, falseFunction: (...params) => any =
|
|
93
|
-
|
|
94
|
-
return regex.test(parameter) ? true : falseFunction(InputHelper.isEmail);
|
|
64
|
+
public static isEmail(parameter: string, falseFunction: (...params) => any = errorFunction): boolean {
|
|
65
|
+
return isEmail(parameter, falseFunction);
|
|
95
66
|
}
|
|
96
67
|
|
|
97
68
|
/**
|
|
98
69
|
* Check whether the parameter can be converted to false
|
|
99
70
|
*/
|
|
100
|
-
public static isFalse(parameter: any, falseFunction: (...params) => any =
|
|
101
|
-
return
|
|
71
|
+
public static isFalse(parameter: any, falseFunction: (...params) => any = errorFunction): boolean {
|
|
72
|
+
return isFalse(parameter, falseFunction);
|
|
102
73
|
}
|
|
103
74
|
|
|
104
75
|
/**
|
|
105
76
|
* Check if parameter is a valid file
|
|
106
77
|
*/
|
|
107
|
-
public static isFile(parameter: any, falseFunction: (...params) => any =
|
|
108
|
-
return parameter
|
|
109
|
-
typeof parameter !== 'undefined' &&
|
|
110
|
-
parameter.name &&
|
|
111
|
-
parameter.path &&
|
|
112
|
-
parameter.type &&
|
|
113
|
-
parameter.size > 0
|
|
114
|
-
? true
|
|
115
|
-
: falseFunction(InputHelper.isFile);
|
|
78
|
+
public static isFile(parameter: any, falseFunction: (...params) => any = errorFunction): boolean {
|
|
79
|
+
return isFile(parameter, falseFunction);
|
|
116
80
|
}
|
|
117
81
|
|
|
118
82
|
/**
|
|
119
83
|
* Check if parameter is a function
|
|
120
84
|
*/
|
|
121
|
-
public static isFunction(
|
|
122
|
-
parameter
|
|
123
|
-
falseFunction: (...params) => any = InputHelper.errorFunction
|
|
124
|
-
): boolean {
|
|
125
|
-
return typeof parameter === 'function' ? true : falseFunction(InputHelper.isFunction);
|
|
85
|
+
public static isFunction(parameter: (...params) => any, falseFunction: (...params) => any = errorFunction): boolean {
|
|
86
|
+
return isFunction(parameter, falseFunction);
|
|
126
87
|
}
|
|
127
88
|
|
|
128
89
|
/**
|
|
@@ -131,9 +92,9 @@ export class InputHelper {
|
|
|
131
92
|
public static isGreater(
|
|
132
93
|
parameter: number,
|
|
133
94
|
compare: number,
|
|
134
|
-
falseFunction: (...params) => any =
|
|
95
|
+
falseFunction: (...params) => any = errorFunction
|
|
135
96
|
): boolean {
|
|
136
|
-
return
|
|
97
|
+
return isGreater(parameter, compare, falseFunction);
|
|
137
98
|
}
|
|
138
99
|
|
|
139
100
|
/**
|
|
@@ -142,97 +103,326 @@ export class InputHelper {
|
|
|
142
103
|
public static isLower(
|
|
143
104
|
parameter: number,
|
|
144
105
|
compare: number,
|
|
145
|
-
falseFunction: (...params) => any =
|
|
106
|
+
falseFunction: (...params) => any = errorFunction
|
|
146
107
|
): boolean {
|
|
147
|
-
return
|
|
108
|
+
return isLower(parameter, compare, falseFunction);
|
|
148
109
|
}
|
|
149
110
|
|
|
150
111
|
/**
|
|
151
|
-
* Check if parameter is a non
|
|
112
|
+
* Check if parameter is a non-empty array
|
|
152
113
|
*/
|
|
153
|
-
public static isNonEmptyArray(
|
|
154
|
-
parameter
|
|
155
|
-
errorFunction: (...params) => any = InputHelper.errorFunction
|
|
156
|
-
): boolean {
|
|
157
|
-
return parameter !== null &&
|
|
158
|
-
typeof parameter !== 'undefined' &&
|
|
159
|
-
parameter.constructor === Array &&
|
|
160
|
-
parameter.length > 0
|
|
161
|
-
? true
|
|
162
|
-
: errorFunction(InputHelper.isNonEmptyArray);
|
|
114
|
+
public static isNonEmptyArray(parameter: any, falseFunction: (...params) => any = errorFunction): boolean {
|
|
115
|
+
return isNonEmptyString(parameter, errorFunction);
|
|
163
116
|
}
|
|
164
117
|
|
|
165
118
|
/**
|
|
166
119
|
* Check if parameter is a non empty object
|
|
167
120
|
*/
|
|
168
|
-
public static isNonEmptyObject(
|
|
169
|
-
parameter
|
|
170
|
-
falseFunction: (...params) => any = InputHelper.errorFunction
|
|
171
|
-
): boolean {
|
|
172
|
-
return parameter !== null &&
|
|
173
|
-
typeof parameter !== 'undefined' &&
|
|
174
|
-
parameter.constructor === Object &&
|
|
175
|
-
Object.keys(parameter).length !== 0
|
|
176
|
-
? true
|
|
177
|
-
: falseFunction(InputHelper.isNonEmptyObject);
|
|
121
|
+
public static isNonEmptyObject(parameter: any, falseFunction: (...params) => any = errorFunction): boolean {
|
|
122
|
+
return isNonEmptyObject(parameter, falseFunction);
|
|
178
123
|
}
|
|
179
124
|
|
|
180
125
|
/**
|
|
181
126
|
* Check if parameter is a non empty string
|
|
182
127
|
*/
|
|
183
|
-
public static isNonEmptyString(
|
|
184
|
-
parameter
|
|
185
|
-
falseFunction: (...params) => any = InputHelper.errorFunction
|
|
186
|
-
): boolean {
|
|
187
|
-
return typeof parameter === 'string' && parameter.length > 0 ? true : falseFunction(InputHelper.isNonEmptyString);
|
|
128
|
+
public static isNonEmptyString(parameter: string, falseFunction: (...params) => any = errorFunction): boolean {
|
|
129
|
+
return isNonEmptyString(parameter, falseFunction);
|
|
188
130
|
}
|
|
189
131
|
|
|
190
132
|
/**
|
|
191
133
|
* Check if parameter is a number
|
|
192
134
|
*/
|
|
193
|
-
public static isNumber(parameter: number, falseFunction: (...params) => any =
|
|
194
|
-
return
|
|
135
|
+
public static isNumber(parameter: number, falseFunction: (...params) => any = errorFunction): boolean {
|
|
136
|
+
return isNumber(parameter, falseFunction);
|
|
195
137
|
}
|
|
196
138
|
|
|
197
139
|
/**
|
|
198
140
|
* Check if parameter is an object
|
|
199
141
|
*/
|
|
200
|
-
public static isObject(parameter: any, falseFunction: (...params) => any =
|
|
201
|
-
return parameter
|
|
202
|
-
? true
|
|
203
|
-
: falseFunction(InputHelper.isObject);
|
|
142
|
+
public static isObject(parameter: any, falseFunction: (...params) => any = errorFunction): boolean {
|
|
143
|
+
return isObject(parameter, falseFunction);
|
|
204
144
|
}
|
|
205
145
|
|
|
206
146
|
/**
|
|
207
147
|
* Check whether the parameter can be converted to true
|
|
208
148
|
*/
|
|
209
|
-
public static isTrue(parameter: any, falseFunction: (...params) => any =
|
|
210
|
-
return
|
|
149
|
+
public static isTrue(parameter: any, falseFunction: (...params) => any = errorFunction): boolean {
|
|
150
|
+
return isTrue(parameter, falseFunction);
|
|
211
151
|
}
|
|
212
152
|
|
|
213
153
|
/**
|
|
214
154
|
* Check if parameter is a string
|
|
215
155
|
*/
|
|
216
|
-
public static isString(parameter: string, falseFunction: (...params) => any =
|
|
217
|
-
return
|
|
156
|
+
public static isString(parameter: string, falseFunction: (...params) => any = errorFunction): boolean {
|
|
157
|
+
return isString(parameter, falseFunction);
|
|
218
158
|
}
|
|
219
159
|
|
|
220
160
|
/**
|
|
221
161
|
* Alternative for errorFunction
|
|
222
162
|
*/
|
|
223
163
|
public static returnFalse(): boolean {
|
|
224
|
-
return
|
|
164
|
+
return returnFalse();
|
|
225
165
|
}
|
|
226
166
|
|
|
227
167
|
/**
|
|
228
168
|
* Map values into specific type
|
|
169
|
+
* @deprecated use mapClass function
|
|
229
170
|
*/
|
|
230
171
|
public static map<T>(values: Partial<T>, ctor: new () => T, cloneDeep = true): T {
|
|
231
|
-
|
|
172
|
+
return mapClass(values, ctor);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Check input
|
|
178
|
+
*/
|
|
179
|
+
export async function check(
|
|
180
|
+
value: any,
|
|
181
|
+
user: { id: any; hasRole: (roles: string[]) => boolean },
|
|
182
|
+
metatype?
|
|
183
|
+
): Promise<any> {
|
|
184
|
+
// Return value if it is only a basic type
|
|
185
|
+
if (typeof value !== 'object' || !metatype || isBasicType(metatype)) {
|
|
186
|
+
return value;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// Convert to metatype
|
|
190
|
+
if (!(value instanceof metatype)) {
|
|
191
|
+
if ((metatype as any)?.map) {
|
|
192
|
+
value = (metatype as any)?.map(value);
|
|
193
|
+
} else {
|
|
194
|
+
value = plainToInstance(metatype, value);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// Validate
|
|
199
|
+
const errors = await validate(value);
|
|
200
|
+
if (errors.length > 0) {
|
|
201
|
+
throw new BadRequestException('Validation failed');
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// Remove restricted values if roles are missing
|
|
205
|
+
value = checkRestricted(value, user);
|
|
206
|
+
return value;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// Standard error function
|
|
210
|
+
export function errorFunction(caller: (...params) => any, message = 'Required parameter is missing or invalid') {
|
|
211
|
+
const err = new Error(message);
|
|
212
|
+
Error.captureStackTrace(err, caller);
|
|
213
|
+
throw err;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Check if parameter is an array
|
|
218
|
+
*/
|
|
219
|
+
export function isArray(parameter: any, falseFunction: (...params) => any = errorFunction): boolean {
|
|
220
|
+
return Array.isArray(parameter) ? true : falseFunction(isArray);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Checks if it is a basic type
|
|
225
|
+
*/
|
|
226
|
+
export function isBasicType(metatype: any, falseFunction: (...params) => any = returnFalse): boolean {
|
|
227
|
+
const types = [String, Boolean, Number, Array, Object, Buffer, ArrayBuffer];
|
|
228
|
+
return types.includes(metatype) ? true : falseFunction(isBasicType);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Check if parameter is between min and max
|
|
233
|
+
*/
|
|
234
|
+
export function isBetween(
|
|
235
|
+
parameter: number,
|
|
236
|
+
min: number,
|
|
237
|
+
max: number,
|
|
238
|
+
falseFunction: (...params) => any = errorFunction
|
|
239
|
+
): boolean {
|
|
240
|
+
return typeof parameter === 'number' && parameter > min && parameter < max ? true : falseFunction(isBetween);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Check if parameter is a Date
|
|
245
|
+
*/
|
|
246
|
+
export function isDate(parameter: Date, falseFunction: (...params) => any = errorFunction): boolean {
|
|
247
|
+
return parameter instanceof Date ? true : falseFunction(isDate);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Check if parameter is a valid email address
|
|
252
|
+
*/
|
|
253
|
+
export function isEmail(parameter: string, falseFunction: (...params) => any = errorFunction): boolean {
|
|
254
|
+
const regex = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,})+$/;
|
|
255
|
+
return regex.test(parameter) ? true : falseFunction(isEmail);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Check whether the parameter can be converted to false
|
|
260
|
+
*/
|
|
261
|
+
export function isFalse(parameter: any, falseFunction: (...params) => any = errorFunction): boolean {
|
|
262
|
+
return !parameter ? true : falseFunction(isFalse);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* Check if parameter is a valid file
|
|
267
|
+
*/
|
|
268
|
+
export function isFile(parameter: any, falseFunction: (...params) => any = errorFunction): boolean {
|
|
269
|
+
return parameter !== null &&
|
|
270
|
+
typeof parameter !== 'undefined' &&
|
|
271
|
+
parameter.name &&
|
|
272
|
+
parameter.path &&
|
|
273
|
+
parameter.type &&
|
|
274
|
+
parameter.size > 0
|
|
275
|
+
? true
|
|
276
|
+
: falseFunction(isFile);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* Check if parameter is a function
|
|
281
|
+
*/
|
|
282
|
+
export function isFunction(parameter: (...params) => any, falseFunction: (...params) => any = errorFunction): boolean {
|
|
283
|
+
return typeof parameter === 'function' ? true : falseFunction(isFunction);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Check if parameter is greater than the compare number
|
|
288
|
+
*/
|
|
289
|
+
export function isGreater(
|
|
290
|
+
parameter: number,
|
|
291
|
+
compare: number,
|
|
292
|
+
falseFunction: (...params) => any = errorFunction
|
|
293
|
+
): boolean {
|
|
294
|
+
return typeof parameter === 'number' && parameter > compare ? true : falseFunction(isGreater);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* Check if parameter is lower than the compare number
|
|
299
|
+
*/
|
|
300
|
+
export function isLower(
|
|
301
|
+
parameter: number,
|
|
302
|
+
compare: number,
|
|
303
|
+
falseFunction: (...params) => any = errorFunction
|
|
304
|
+
): boolean {
|
|
305
|
+
return typeof parameter === 'number' && parameter < compare ? true : falseFunction(isLower);
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* Check if parameter is a non empty array
|
|
310
|
+
*/
|
|
311
|
+
export function isNonEmptyArray(parameter: any, falseFunction: (...params) => any = errorFunction): boolean {
|
|
312
|
+
return parameter !== null &&
|
|
313
|
+
typeof parameter !== 'undefined' &&
|
|
314
|
+
parameter.constructor === Array &&
|
|
315
|
+
parameter.length > 0
|
|
316
|
+
? true
|
|
317
|
+
: falseFunction(isNonEmptyArray);
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* Check if parameter is a non empty object
|
|
322
|
+
*/
|
|
323
|
+
export function isNonEmptyObject(parameter: any, falseFunction: (...params) => any = errorFunction): boolean {
|
|
324
|
+
return parameter !== null &&
|
|
325
|
+
typeof parameter !== 'undefined' &&
|
|
326
|
+
parameter.constructor === Object &&
|
|
327
|
+
Object.keys(parameter).length !== 0
|
|
328
|
+
? true
|
|
329
|
+
: falseFunction(isNonEmptyObject);
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* Check if parameter is a non empty string
|
|
334
|
+
*/
|
|
335
|
+
export function isNonEmptyString(parameter: string, falseFunction: (...params) => any = errorFunction): boolean {
|
|
336
|
+
return typeof parameter === 'string' && parameter.length > 0 ? true : falseFunction(isNonEmptyString);
|
|
337
|
+
}
|
|
232
338
|
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
339
|
+
/**
|
|
340
|
+
* Check if parameter is a number
|
|
341
|
+
*/
|
|
342
|
+
export function isNumber(parameter: number, falseFunction: (...params) => any = errorFunction): boolean {
|
|
343
|
+
return typeof parameter === 'number' ? true : falseFunction(isNumber);
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/**
|
|
347
|
+
* Check if parameter is an object
|
|
348
|
+
*/
|
|
349
|
+
export function isObject(parameter: any, falseFunction: (...params) => any = errorFunction): boolean {
|
|
350
|
+
return parameter !== null && typeof parameter !== 'undefined' && parameter.constructor === Object
|
|
351
|
+
? true
|
|
352
|
+
: falseFunction(isObject);
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/**
|
|
356
|
+
* Check whether the parameter can be converted to true
|
|
357
|
+
*/
|
|
358
|
+
export function isTrue(parameter: any, falseFunction: (...params) => any = errorFunction): boolean {
|
|
359
|
+
return !!parameter ? true : falseFunction(isTrue);
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/**
|
|
363
|
+
* Check if parameter is a string
|
|
364
|
+
*/
|
|
365
|
+
export function isString(parameter: string, falseFunction: (...params) => any = errorFunction): boolean {
|
|
366
|
+
return typeof parameter === 'string' ? true : falseFunction(isString);
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
/**
|
|
370
|
+
* Alternative for errorFunction
|
|
371
|
+
*/
|
|
372
|
+
export function returnFalse(): boolean {
|
|
373
|
+
return false;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
/**
|
|
377
|
+
* Map values into specific type
|
|
378
|
+
*/
|
|
379
|
+
export function mapClass<T>(values: Partial<T>, ctor: new () => T, cloneDeep = true): T {
|
|
380
|
+
const instance = new ctor();
|
|
381
|
+
|
|
382
|
+
return Object.keys(instance).reduce((obj, key) => {
|
|
383
|
+
obj[key] = cloneDeep ? _.cloneDeep(values[key]) : values[key];
|
|
384
|
+
return obj;
|
|
385
|
+
}, instance);
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/**
|
|
389
|
+
* Get type of array (via first item)
|
|
390
|
+
*/
|
|
391
|
+
export function typeofArray(arr: any[], strict = false): string {
|
|
392
|
+
let type: string = undefined;
|
|
393
|
+
if (!arr?.length) {
|
|
394
|
+
return type;
|
|
395
|
+
}
|
|
396
|
+
type = typeof arr[0];
|
|
397
|
+
if (strict) {
|
|
398
|
+
for (const item of arr) {
|
|
399
|
+
if (typeof item !== type) {
|
|
400
|
+
return undefined;
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
return type;
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
/**
|
|
408
|
+
* Get instance of array items (via first item)
|
|
409
|
+
*/
|
|
410
|
+
export function instanceofArray(arr: any[], strict = false): string {
|
|
411
|
+
let constructor: string = undefined;
|
|
412
|
+
if (!arr?.length) {
|
|
413
|
+
return constructor;
|
|
414
|
+
}
|
|
415
|
+
try {
|
|
416
|
+
constructor = arr[0].constructor;
|
|
417
|
+
if (strict) {
|
|
418
|
+
for (const item of arr) {
|
|
419
|
+
if (item.constructor !== constructor) {
|
|
420
|
+
return undefined;
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
} catch (e) {
|
|
425
|
+
return undefined;
|
|
237
426
|
}
|
|
427
|
+
return constructor;
|
|
238
428
|
}
|