@privateaim/server-http-kit 0.8.3 → 0.8.5
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/CHANGELOG.md +52 -0
- package/dist/constants.d.ts +5 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +15 -0
- package/dist/constants.js.map +1 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/middlewares/error.d.ts.map +1 -1
- package/dist/middlewares/error.js +38 -10
- package/dist/middlewares/error.js.map +1 -1
- package/dist/middlewares/force-logged-in.d.ts.map +1 -1
- package/dist/middlewares/force-logged-in.js +1 -5
- package/dist/middlewares/force-logged-in.js.map +1 -1
- package/dist/middlewares/rate-limit.d.ts.map +1 -1
- package/dist/middlewares/rate-limit.js +8 -12
- package/dist/middlewares/rate-limit.js.map +1 -1
- package/dist/request/identity-realm.d.ts +8 -0
- package/dist/request/identity-realm.d.ts.map +1 -0
- package/dist/request/identity-realm.js +21 -0
- package/dist/request/identity-realm.js.map +1 -0
- package/dist/request/identity.d.ts +7 -0
- package/dist/request/identity.d.ts.map +1 -0
- package/dist/request/identity.js +32 -0
- package/dist/request/identity.js.map +1 -0
- package/dist/request/index.d.ts +3 -0
- package/dist/request/index.d.ts.map +1 -1
- package/dist/request/index.js +3 -0
- package/dist/request/index.js.map +1 -1
- package/dist/request/permission/helper.d.ts +5 -0
- package/dist/request/permission/helper.d.ts.map +1 -0
- package/dist/request/permission/helper.js +23 -0
- package/dist/request/permission/helper.js.map +1 -0
- package/dist/request/permission/index.d.ts +3 -0
- package/dist/request/permission/index.d.ts.map +1 -0
- package/dist/{validation → request/permission}/index.js +2 -4
- package/dist/request/permission/index.js.map +1 -0
- package/dist/request/permission/module.d.ts +13 -0
- package/dist/request/permission/module.d.ts.map +1 -0
- package/dist/request/permission/module.js +47 -0
- package/dist/request/permission/module.js.map +1 -0
- package/dist/request/types.d.ts +11 -12
- package/dist/request/types.d.ts.map +1 -1
- package/dist/services/authup/middleware.js +3 -3
- package/dist/services/authup/middleware.js.map +1 -1
- package/dist/services/authup/permission-provider.d.ts +5 -0
- package/dist/services/authup/permission-provider.d.ts.map +1 -0
- package/dist/services/authup/permission-provider.js +20 -0
- package/dist/services/authup/permission-provider.js.map +1 -0
- package/dist/services/authup/utils.d.ts +2 -2
- package/dist/services/authup/utils.d.ts.map +1 -1
- package/dist/services/authup/utils.js +21 -33
- package/dist/services/authup/utils.js.map +1 -1
- package/package.json +16 -16
- package/{dist/validation/type.js → src/constants.ts} +6 -4
- package/src/index.ts +1 -1
- package/src/middlewares/error.ts +46 -12
- package/src/middlewares/force-logged-in.ts +2 -8
- package/src/middlewares/rate-limit.ts +11 -16
- package/src/request/identity-realm.ts +26 -0
- package/src/request/identity.ts +33 -0
- package/src/request/index.ts +3 -0
- package/src/request/permission/helper.ts +24 -0
- package/src/{validation → request/permission}/index.ts +2 -4
- package/src/request/permission/module.ts +60 -0
- package/src/request/types.ts +12 -10
- package/src/services/authup/middleware.ts +3 -3
- package/src/services/authup/permission-provider.ts +18 -0
- package/src/services/authup/utils.ts +27 -39
- package/dist/validation/index.d.ts +0 -5
- package/dist/validation/index.d.ts.map +0 -1
- package/dist/validation/index.js.map +0 -1
- package/dist/validation/message.d.ts +0 -2
- package/dist/validation/message.d.ts.map +0 -1
- package/dist/validation/message.js +0 -17
- package/dist/validation/message.js.map +0 -1
- package/dist/validation/module.d.ts +0 -6
- package/dist/validation/module.d.ts.map +0 -1
- package/dist/validation/module.js +0 -45
- package/dist/validation/module.js.map +0 -1
- package/dist/validation/result.d.ts +0 -7
- package/dist/validation/result.d.ts.map +0 -1
- package/dist/validation/result.js +0 -43
- package/dist/validation/result.js.map +0 -1
- package/dist/validation/type.d.ts +0 -15
- package/dist/validation/type.d.ts.map +0 -1
- package/dist/validation/type.js.map +0 -1
- package/src/validation/message.ts +0 -17
- package/src/validation/module.ts +0 -49
- package/src/validation/result.ts +0 -58
- package/src/validation/type.ts +0 -28
|
@@ -5,13 +5,13 @@
|
|
|
5
5
|
* view the LICENSE file that was distributed with this source code.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
import type {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
import type { TokenVerificationData } from '@authup/server-
|
|
12
|
-
import { PermissionName } from '@privateaim/kit';
|
|
8
|
+
import type { PermissionProvider } from '@authup/access';
|
|
9
|
+
import { PermissionChecker, PermissionMemoryProvider } from '@authup/access';
|
|
10
|
+
import { REALM_MASTER_NAME, transformOAuth2ScopeToArray } from '@authup/core-kit';
|
|
11
|
+
import type { TokenVerificationData } from '@authup/server-adapter-kit';
|
|
13
12
|
import type { Request } from 'routup';
|
|
14
|
-
import { setRequestEnv } from '../../request';
|
|
13
|
+
import { RequestPermissionChecker, setRequestEnv } from '../../request';
|
|
14
|
+
import { FakePermissionProvider } from './permission-provider';
|
|
15
15
|
|
|
16
16
|
type TokenVerificationDataMinimal = Pick<
|
|
17
17
|
TokenVerificationData,
|
|
@@ -20,18 +20,10 @@ TokenVerificationData,
|
|
|
20
20
|
'realm_name' |
|
|
21
21
|
'sub' |
|
|
22
22
|
'sub_kind' |
|
|
23
|
-
'sub_name'
|
|
23
|
+
'sub_name' |
|
|
24
|
+
'scope'
|
|
24
25
|
>;
|
|
25
26
|
|
|
26
|
-
function generateAbilities(): Ability[] {
|
|
27
|
-
return Object.values({
|
|
28
|
-
...PermissionName,
|
|
29
|
-
...AuthupPermissionName,
|
|
30
|
-
}).map((name) => ({
|
|
31
|
-
name,
|
|
32
|
-
} satisfies Ability));
|
|
33
|
-
}
|
|
34
|
-
|
|
35
27
|
export function createFakeTokenVerificationData(): TokenVerificationDataMinimal {
|
|
36
28
|
return {
|
|
37
29
|
realm_id: 'd94b2f28-29e3-4ced-b8f1-6923a01dc1ee',
|
|
@@ -41,7 +33,7 @@ export function createFakeTokenVerificationData(): TokenVerificationDataMinimal
|
|
|
41
33
|
sub: 'd94b2f28-29e3-4ced-b8f1-6923a01dc1ee',
|
|
42
34
|
sub_name: 'admin',
|
|
43
35
|
|
|
44
|
-
permissions:
|
|
36
|
+
permissions: [],
|
|
45
37
|
};
|
|
46
38
|
}
|
|
47
39
|
|
|
@@ -50,33 +42,29 @@ export function applyTokenVerificationData(
|
|
|
50
42
|
data: TokenVerificationDataMinimal,
|
|
51
43
|
fakeAbilities?: boolean,
|
|
52
44
|
) {
|
|
53
|
-
let
|
|
45
|
+
let provider : PermissionProvider;
|
|
54
46
|
if (fakeAbilities) {
|
|
55
|
-
|
|
47
|
+
provider = new FakePermissionProvider();
|
|
56
48
|
} else {
|
|
57
|
-
|
|
49
|
+
provider = new PermissionMemoryProvider(data.permissions);
|
|
58
50
|
}
|
|
59
51
|
|
|
60
|
-
const
|
|
61
|
-
|
|
52
|
+
const permissionChecker = new PermissionChecker({
|
|
53
|
+
provider,
|
|
54
|
+
});
|
|
55
|
+
const requestPermissionChecker = new RequestPermissionChecker(req, permissionChecker);
|
|
56
|
+
setRequestEnv(req, 'permissionChecker', requestPermissionChecker);
|
|
62
57
|
|
|
63
|
-
setRequestEnv(req, '
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
58
|
+
setRequestEnv(req, 'identity', {
|
|
59
|
+
id: data.sub,
|
|
60
|
+
type: data.sub_kind,
|
|
61
|
+
realmId: data.realm_id,
|
|
62
|
+
realmName: data.realm_name,
|
|
63
|
+
attributes: {
|
|
64
|
+
id: data.sub,
|
|
65
|
+
name: data.sub_name,
|
|
66
|
+
},
|
|
68
67
|
});
|
|
69
68
|
|
|
70
|
-
|
|
71
|
-
case 'user': {
|
|
72
|
-
setRequestEnv(req, 'userId', data.sub);
|
|
73
|
-
setRequestEnv(req, 'userName', data.sub_name);
|
|
74
|
-
break;
|
|
75
|
-
}
|
|
76
|
-
case 'robot': {
|
|
77
|
-
setRequestEnv(req, 'robotId', data.sub);
|
|
78
|
-
setRequestEnv(req, 'robotName', data.sub_name);
|
|
79
|
-
break;
|
|
80
|
-
}
|
|
81
|
-
}
|
|
69
|
+
setRequestEnv(req, 'scopes', transformOAuth2ScopeToArray(data.scope));
|
|
82
70
|
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/validation/index.ts"],"names":[],"mappings":"AAOA,cAAc,WAAW,CAAC;AAC1B,cAAc,UAAU,CAAC;AACzB,cAAc,QAAQ,CAAC;AACvB,cAAc,UAAU,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/validation/index.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;;;;;;;;;;;;;;AAEH,4CAA0B;AAC1B,2CAAyB;AACzB,yCAAuB;AACvB,2CAAyB"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"message.d.ts","sourceRoot":"","sources":["../../src/validation/message.ts"],"names":[],"mappings":"AAOA,wBAAgB,+BAA+B,CAC3C,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EACrD,IAAI,EAAE,MAAM,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,EAAE,UAO5B"}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/*
|
|
3
|
-
* Copyright (c) 2024.
|
|
4
|
-
* Author Peter Placzek (tada5hi)
|
|
5
|
-
* For the full copyright and license information,
|
|
6
|
-
* view the LICENSE file that was distributed with this source code.
|
|
7
|
-
*/
|
|
8
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
-
exports.buildHTTPValidationErrorMessage = buildHTTPValidationErrorMessage;
|
|
10
|
-
function buildHTTPValidationErrorMessage(name) {
|
|
11
|
-
const names = Array.isArray(name) ? name : [name];
|
|
12
|
-
if (names.length > 1) {
|
|
13
|
-
return `The parameters ${names.join(', ')} is invalid.`;
|
|
14
|
-
}
|
|
15
|
-
return `The parameter ${String(names[0])} is invalid.`;
|
|
16
|
-
}
|
|
17
|
-
//# sourceMappingURL=message.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"message.js","sourceRoot":"","sources":["../../src/validation/message.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;AAEH,0EASC;AATD,SAAgB,+BAA+B,CAE7C,IAA2B;IACzB,MAAM,KAAK,GAAG,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;IAElD,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACnB,OAAO,kBAAkB,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,cAAc,CAAC;IAC5D,CAAC;IACD,OAAO,iBAAiB,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,cAAc,CAAC;AAC3D,CAAC"}
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { BadRequestError } from '@ebec/http';
|
|
2
|
-
import type { Result, ValidationError } from 'express-validator';
|
|
3
|
-
export declare class HTTPValidationError extends BadRequestError {
|
|
4
|
-
constructor(validation: Result<ValidationError>);
|
|
5
|
-
}
|
|
6
|
-
//# sourceMappingURL=module.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"module.d.ts","sourceRoot":"","sources":["../../src/validation/module.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAC7C,OAAO,KAAK,EAAE,MAAM,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAGjE,qBAAa,mBAAoB,SAAQ,eAAe;gBACxC,UAAU,EAAE,MAAM,CAAC,eAAe,CAAC;CAoClD"}
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/*
|
|
3
|
-
* Copyright (c) 2022-2024.
|
|
4
|
-
* Author Peter Placzek (tada5hi)
|
|
5
|
-
* For the full copyright and license information,
|
|
6
|
-
* view the LICENSE file that was distributed with this source code.
|
|
7
|
-
*/
|
|
8
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
-
exports.HTTPValidationError = void 0;
|
|
10
|
-
const http_1 = require("@ebec/http");
|
|
11
|
-
const message_1 = require("./message");
|
|
12
|
-
class HTTPValidationError extends http_1.BadRequestError {
|
|
13
|
-
constructor(validation) {
|
|
14
|
-
const errors = validation.array();
|
|
15
|
-
const parameterNames = [];
|
|
16
|
-
for (let i = 0; i < errors.length; i++) {
|
|
17
|
-
const item = errors[i];
|
|
18
|
-
switch (item.type) {
|
|
19
|
-
case 'field': {
|
|
20
|
-
parameterNames.push(item.path);
|
|
21
|
-
break;
|
|
22
|
-
}
|
|
23
|
-
case 'alternative': {
|
|
24
|
-
parameterNames.push(item.nestedErrors.map(((el) => el.path)).join('|'));
|
|
25
|
-
break;
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
let message;
|
|
30
|
-
if (parameterNames.length > 0) {
|
|
31
|
-
message = (0, message_1.buildHTTPValidationErrorMessage)(Array.from(new Set(parameterNames)));
|
|
32
|
-
}
|
|
33
|
-
else {
|
|
34
|
-
message = 'An unexpected validation error occurred.';
|
|
35
|
-
}
|
|
36
|
-
super({
|
|
37
|
-
message,
|
|
38
|
-
data: {
|
|
39
|
-
errors,
|
|
40
|
-
},
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
exports.HTTPValidationError = HTTPValidationError;
|
|
45
|
-
//# sourceMappingURL=module.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"module.js","sourceRoot":"","sources":["../../src/validation/module.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAEH,qCAA6C;AAE7C,uCAA4D;AAE5D,MAAa,mBAAoB,SAAQ,sBAAe;IACpD,YAAY,UAAmC;QAC3C,MAAM,MAAM,GAAuB,UAAU,CAAC,KAAK,EAAE,CAAC;QAEtD,MAAM,cAAc,GAAG,EAAE,CAAC;QAC1B,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;YACrC,MAAM,IAAI,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC;YAEvB,QAAQ,IAAI,CAAC,IAAI,EAAE,CAAC;gBAChB,KAAK,OAAO,CAAC,CAAC,CAAC;oBACX,cAAc,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;oBAC/B,MAAM;gBACV,CAAC;gBACD,KAAK,aAAa,CAAC,CAAC,CAAC;oBACjB,cAAc,CAAC,IAAI,CAAC,IAAI,CAAC,YAAY,CAAC,GAAG,CACrC,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,CAAC,IAAI,CAAC,CACpB,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;oBACb,MAAM;gBACV,CAAC;YACL,CAAC;QACL,CAAC;QAED,IAAI,OAAgB,CAAC;QAErB,IAAI,cAAc,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YAC5B,OAAO,GAAG,IAAA,yCAA+B,EAAC,KAAK,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC;QACnF,CAAC;aAAM,CAAC;YACJ,OAAO,GAAG,0CAA0C,CAAC;QACzD,CAAC;QAED,KAAK,CAAC;YACF,OAAO;YACP,IAAI,EAAE;gBACF,MAAM;aACT;SACJ,CAAC,CAAC;IACP,CAAC;CACJ;AArCD,kDAqCC"}
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import type { MatchedDataOptions } from 'express-validator';
|
|
2
|
-
import type { Request } from 'routup';
|
|
3
|
-
import type { EntityTarget } from 'typeorm';
|
|
4
|
-
import type { HTTPValidationExtendKeys, HTTPValidationResult } from './type';
|
|
5
|
-
export declare function createHTTPValidationResult<R extends Record<string, any>, M extends Record<string, any> = Record<string, any>>(req: Request, options?: Partial<MatchedDataOptions>): HTTPValidationResult<R, M>;
|
|
6
|
-
export declare function extendHTTPValidationResultWithRelation<R extends Record<string, any>>(result: HTTPValidationResult<R>, target: EntityTarget<any>, keys: HTTPValidationExtendKeys<R>): Promise<HTTPValidationResult<R>>;
|
|
7
|
-
//# sourceMappingURL=result.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"result.d.ts","sourceRoot":"","sources":["../../src/validation/result.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AAE5D,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,QAAQ,CAAC;AACtC,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAI5C,OAAO,KAAK,EAAE,wBAAwB,EAAE,oBAAoB,EAAE,MAAM,QAAQ,CAAC;AAE7E,wBAAgB,0BAA0B,CACtC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAC7B,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EACrD,GAAG,EAAE,OAAO,EAAE,OAAO,GAAE,OAAO,CAAC,kBAAkB,CAAM,GAAI,oBAAoB,CAAC,CAAC,EAAE,CAAC,CAAC,CActF;AAED,wBAAsB,sCAAsC,CACxD,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAE7B,MAAM,EAAE,oBAAoB,CAAC,CAAC,CAAC,EAC/B,MAAM,EAAE,YAAY,CAAC,GAAG,CAAC,EACzB,IAAI,EAAE,wBAAwB,CAAC,CAAC,CAAC,GACjC,OAAO,CAAC,oBAAoB,CAAC,CAAC,CAAC,CAAC,CAcnC"}
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/*
|
|
3
|
-
* Copyright (c) 2022-2024.
|
|
4
|
-
* Author Peter Placzek (tada5hi)
|
|
5
|
-
* For the full copyright and license information,
|
|
6
|
-
* view the LICENSE file that was distributed with this source code.
|
|
7
|
-
*/
|
|
8
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
-
exports.createHTTPValidationResult = createHTTPValidationResult;
|
|
10
|
-
exports.extendHTTPValidationResultWithRelation = extendHTTPValidationResultWithRelation;
|
|
11
|
-
const http_1 = require("@ebec/http");
|
|
12
|
-
const kit_1 = require("@privateaim/kit");
|
|
13
|
-
const express_validator_1 = require("express-validator");
|
|
14
|
-
const typeorm_extension_1 = require("typeorm-extension");
|
|
15
|
-
const message_1 = require("./message");
|
|
16
|
-
const module_1 = require("./module");
|
|
17
|
-
function createHTTPValidationResult(req, options = {}) {
|
|
18
|
-
const validation = (0, express_validator_1.validationResult)(req);
|
|
19
|
-
if (!validation.isEmpty()) {
|
|
20
|
-
throw new module_1.HTTPValidationError(validation);
|
|
21
|
-
}
|
|
22
|
-
return {
|
|
23
|
-
data: (0, kit_1.deleteUndefinedObjectProperties)((0, express_validator_1.matchedData)(req, {
|
|
24
|
-
includeOptionals: true,
|
|
25
|
-
...options,
|
|
26
|
-
})),
|
|
27
|
-
meta: {},
|
|
28
|
-
relation: {},
|
|
29
|
-
};
|
|
30
|
-
}
|
|
31
|
-
async function extendHTTPValidationResultWithRelation(result, target, keys) {
|
|
32
|
-
if (result.data[keys.id]) {
|
|
33
|
-
const dataSource = await (0, typeorm_extension_1.useDataSource)();
|
|
34
|
-
const repository = dataSource.getRepository(target);
|
|
35
|
-
const entity = await repository.findOneBy({ id: result.data[keys.id] });
|
|
36
|
-
if (!entity) {
|
|
37
|
-
throw new http_1.BadRequestError((0, message_1.buildHTTPValidationErrorMessage)(keys.id));
|
|
38
|
-
}
|
|
39
|
-
result.relation[keys.entity] = entity;
|
|
40
|
-
}
|
|
41
|
-
return result;
|
|
42
|
-
}
|
|
43
|
-
//# sourceMappingURL=result.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"result.js","sourceRoot":"","sources":["../../src/validation/result.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;AAaH,gEAiBC;AAED,wFAoBC;AAlDD,qCAA6C;AAC7C,yCAAkE;AAElE,yDAAkE;AAGlE,yDAAkD;AAClD,uCAA4D;AAC5D,qCAA+C;AAG/C,SAAgB,0BAA0B,CAGxC,GAAY,EAAE,UAAuC,EAAE;IACrD,MAAM,UAAU,GAAG,IAAA,oCAAgB,EAAC,GAAG,CAAC,CAAC;IACzC,IAAI,CAAC,UAAU,CAAC,OAAO,EAAE,EAAE,CAAC;QACxB,MAAM,IAAI,4BAAmB,CAAC,UAAU,CAAC,CAAC;IAC9C,CAAC;IAED,OAAO;QACH,IAAI,EAAE,IAAA,qCAA+B,EAAC,IAAA,+BAAW,EAAC,GAAG,EAAE;YACnD,gBAAgB,EAAE,IAAI;YACtB,GAAG,OAAO;SACb,CAAC,CAAM;QACR,IAAI,EAAE,EAAO;QACb,QAAQ,EAAE,EAAE;KACf,CAAC;AACN,CAAC;AAEM,KAAK,UAAU,sCAAsC,CAGxD,MAA+B,EAC/B,MAAyB,EACzB,IAAiC;IAEjC,IAAI,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,CAAC;QACvB,MAAM,UAAU,GAAG,MAAM,IAAA,iCAAa,GAAE,CAAC;QAEzC,MAAM,UAAU,GAAG,UAAU,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC;QACpD,MAAM,MAAM,GAAG,MAAM,UAAU,CAAC,SAAS,CAAC,EAAE,EAAE,EAAE,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;QACxE,IAAI,CAAC,MAAM,EAAE,CAAC;YACV,MAAM,IAAI,sBAAe,CAAC,IAAA,yCAA+B,EAAC,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC;QACxE,CAAC;QAED,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,MAAmD,CAAC,GAAG,MAAM,CAAC;IACvF,CAAC;IAED,OAAO,MAAM,CAAC;AAClB,CAAC"}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
export type HTTPValidationResultMetaKey<T extends Record<string, any>> = ({
|
|
2
|
-
[K in keyof T]?: T[K] extends Record<string, any> ? T[K] extends Date ? never : K : never;
|
|
3
|
-
})[keyof T];
|
|
4
|
-
export type HTTPValidationResult<T extends Record<string, any>, M extends Record<string, any> = Record<string, any>> = {
|
|
5
|
-
data: Partial<T>;
|
|
6
|
-
relation: {
|
|
7
|
-
[K in HTTPValidationResultMetaKey<T>]?: T[K];
|
|
8
|
-
};
|
|
9
|
-
meta: M;
|
|
10
|
-
};
|
|
11
|
-
export type HTTPValidationExtendKeys<T extends Record<string, any>> = {
|
|
12
|
-
id: keyof T;
|
|
13
|
-
entity: HTTPValidationResultMetaKey<T>;
|
|
14
|
-
};
|
|
15
|
-
//# sourceMappingURL=type.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../src/validation/type.ts"],"names":[],"mappings":"AAOA,MAAM,MAAM,2BAA2B,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,IAAI,CAAC;KACrE,CAAC,IAAI,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAC7C,CAAC,CAAC,CAAC,CAAC,SAAS,IAAI,GAAG,KAAK,GAAG,CAAC,GAC7B,KAAK;CACZ,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC;AAEZ,MAAM,MAAM,oBAAoB,CAC5B,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAC7B,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,IAC/C;IACA,IAAI,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC;IACjB,QAAQ,EAAE;SACL,CAAC,IAAI,2BAA2B,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;KAC/C,CAAC;IACF,IAAI,EAAE,CAAC,CAAA;CACV,CAAC;AAEN,MAAM,MAAM,wBAAwB,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,IAAI;IAClE,EAAE,EAAE,MAAM,CAAC,CAAC;IACZ,MAAM,EAAE,2BAA2B,CAAC,CAAC,CAAC,CAAA;CACzC,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"type.js","sourceRoot":"","sources":["../../src/validation/type.ts"],"names":[],"mappings":";AAAA;;;;;GAKG"}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright (c) 2024.
|
|
3
|
-
* Author Peter Placzek (tada5hi)
|
|
4
|
-
* For the full copyright and license information,
|
|
5
|
-
* view the LICENSE file that was distributed with this source code.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
export function buildHTTPValidationErrorMessage<
|
|
9
|
-
T extends Record<string, any> = Record<string, any>,
|
|
10
|
-
>(name: keyof T | (keyof T)[]) {
|
|
11
|
-
const names = Array.isArray(name) ? name : [name];
|
|
12
|
-
|
|
13
|
-
if (names.length > 1) {
|
|
14
|
-
return `The parameters ${names.join(', ')} is invalid.`;
|
|
15
|
-
}
|
|
16
|
-
return `The parameter ${String(names[0])} is invalid.`;
|
|
17
|
-
}
|
package/src/validation/module.ts
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright (c) 2022-2024.
|
|
3
|
-
* Author Peter Placzek (tada5hi)
|
|
4
|
-
* For the full copyright and license information,
|
|
5
|
-
* view the LICENSE file that was distributed with this source code.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import { BadRequestError } from '@ebec/http';
|
|
9
|
-
import type { Result, ValidationError } from 'express-validator';
|
|
10
|
-
import { buildHTTPValidationErrorMessage } from './message';
|
|
11
|
-
|
|
12
|
-
export class HTTPValidationError extends BadRequestError {
|
|
13
|
-
constructor(validation: Result<ValidationError>) {
|
|
14
|
-
const errors : ValidationError[] = validation.array();
|
|
15
|
-
|
|
16
|
-
const parameterNames = [];
|
|
17
|
-
for (let i = 0; i < errors.length; i++) {
|
|
18
|
-
const item = errors[i];
|
|
19
|
-
|
|
20
|
-
switch (item.type) {
|
|
21
|
-
case 'field': {
|
|
22
|
-
parameterNames.push(item.path);
|
|
23
|
-
break;
|
|
24
|
-
}
|
|
25
|
-
case 'alternative': {
|
|
26
|
-
parameterNames.push(item.nestedErrors.map(
|
|
27
|
-
((el) => el.path),
|
|
28
|
-
).join('|'));
|
|
29
|
-
break;
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
let message : string;
|
|
35
|
-
|
|
36
|
-
if (parameterNames.length > 0) {
|
|
37
|
-
message = buildHTTPValidationErrorMessage(Array.from(new Set(parameterNames)));
|
|
38
|
-
} else {
|
|
39
|
-
message = 'An unexpected validation error occurred.';
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
super({
|
|
43
|
-
message,
|
|
44
|
-
data: {
|
|
45
|
-
errors,
|
|
46
|
-
},
|
|
47
|
-
});
|
|
48
|
-
}
|
|
49
|
-
}
|
package/src/validation/result.ts
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright (c) 2022-2024.
|
|
3
|
-
* Author Peter Placzek (tada5hi)
|
|
4
|
-
* For the full copyright and license information,
|
|
5
|
-
* view the LICENSE file that was distributed with this source code.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import { BadRequestError } from '@ebec/http';
|
|
9
|
-
import { deleteUndefinedObjectProperties } from '@privateaim/kit';
|
|
10
|
-
import type { MatchedDataOptions } from 'express-validator';
|
|
11
|
-
import { matchedData, validationResult } from 'express-validator';
|
|
12
|
-
import type { Request } from 'routup';
|
|
13
|
-
import type { EntityTarget } from 'typeorm';
|
|
14
|
-
import { useDataSource } from 'typeorm-extension';
|
|
15
|
-
import { buildHTTPValidationErrorMessage } from './message';
|
|
16
|
-
import { HTTPValidationError } from './module';
|
|
17
|
-
import type { HTTPValidationExtendKeys, HTTPValidationResult } from './type';
|
|
18
|
-
|
|
19
|
-
export function createHTTPValidationResult<
|
|
20
|
-
R extends Record<string, any>,
|
|
21
|
-
M extends Record<string, any> = Record<string, any>,
|
|
22
|
-
>(req: Request, options: Partial<MatchedDataOptions> = {}) : HTTPValidationResult<R, M> {
|
|
23
|
-
const validation = validationResult(req);
|
|
24
|
-
if (!validation.isEmpty()) {
|
|
25
|
-
throw new HTTPValidationError(validation);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
return {
|
|
29
|
-
data: deleteUndefinedObjectProperties(matchedData(req, {
|
|
30
|
-
includeOptionals: true,
|
|
31
|
-
...options,
|
|
32
|
-
})) as R,
|
|
33
|
-
meta: {} as M,
|
|
34
|
-
relation: {},
|
|
35
|
-
};
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export async function extendHTTPValidationResultWithRelation<
|
|
39
|
-
R extends Record<string, any>,
|
|
40
|
-
>(
|
|
41
|
-
result: HTTPValidationResult<R>,
|
|
42
|
-
target: EntityTarget<any>,
|
|
43
|
-
keys: HTTPValidationExtendKeys<R>,
|
|
44
|
-
) : Promise<HTTPValidationResult<R>> {
|
|
45
|
-
if (result.data[keys.id]) {
|
|
46
|
-
const dataSource = await useDataSource();
|
|
47
|
-
|
|
48
|
-
const repository = dataSource.getRepository(target);
|
|
49
|
-
const entity = await repository.findOneBy({ id: result.data[keys.id] });
|
|
50
|
-
if (!entity) {
|
|
51
|
-
throw new BadRequestError(buildHTTPValidationErrorMessage(keys.id));
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
result.relation[keys.entity as keyof HTTPValidationResult<R>['relation']] = entity;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
return result;
|
|
58
|
-
}
|
package/src/validation/type.ts
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright (c) 2022-2024.
|
|
3
|
-
* Author Peter Placzek (tada5hi)
|
|
4
|
-
* For the full copyright and license information,
|
|
5
|
-
* view the LICENSE file that was distributed with this source code.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
export type HTTPValidationResultMetaKey<T extends Record<string, any>> = ({
|
|
9
|
-
[K in keyof T]?: T[K] extends Record<string, any> ?
|
|
10
|
-
T[K] extends Date ? never : K :
|
|
11
|
-
never
|
|
12
|
-
})[keyof T];
|
|
13
|
-
|
|
14
|
-
export type HTTPValidationResult<
|
|
15
|
-
T extends Record<string, any>,
|
|
16
|
-
M extends Record<string, any> = Record<string, any>,
|
|
17
|
-
> = {
|
|
18
|
-
data: Partial<T>,
|
|
19
|
-
relation: {
|
|
20
|
-
[K in HTTPValidationResultMetaKey<T>]?: T[K]
|
|
21
|
-
},
|
|
22
|
-
meta: M
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
export type HTTPValidationExtendKeys<T extends Record<string, any>> = {
|
|
26
|
-
id: keyof T,
|
|
27
|
-
entity: HTTPValidationResultMetaKey<T>
|
|
28
|
-
};
|