@telperion/nest-pack 0.0.0-stage → 1.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.
- package/.build-manifest.json +76 -0
- package/LICENSE +21 -0
- package/README.md +133 -2
- package/access/access-guard-options.cjs +3 -0
- package/access/access-guard-options.cjs.map +1 -0
- package/access/access-guard-options.d.cts +81 -0
- package/access/access-guard-options.d.ts +81 -0
- package/access/access-guard-options.js +2 -0
- package/access/access-guard-options.js.map +1 -0
- package/access/create-access-guard.cjs +103 -0
- package/access/create-access-guard.cjs.map +1 -0
- package/access/create-access-guard.d.cts +50 -0
- package/access/create-access-guard.d.ts +50 -0
- package/access/create-access-guard.js +101 -0
- package/access/create-access-guard.js.map +1 -0
- package/access/index.cjs +8 -0
- package/access/index.cjs.map +1 -0
- package/access/index.d.cts +3 -0
- package/access/index.d.ts +3 -0
- package/access/index.js +2 -0
- package/access/index.js.map +1 -0
- package/fesm/nest-pack.mjs +101 -0
- package/fesm/nest-pack.mjs.map +1 -0
- package/index.cjs +8 -0
- package/index.cjs.map +1 -0
- package/index.d.cts +3 -0
- package/index.d.ts +3 -0
- package/index.js +2 -0
- package/index.js.map +1 -0
- package/package.json +138 -4
- package/validation/index.cjs +8 -0
- package/validation/index.cjs.map +1 -0
- package/validation/index.d.cts +1 -0
- package/validation/index.d.ts +1 -0
- package/validation/index.js +2 -0
- package/validation/index.js.map +1 -0
- package/validation/trim.cjs +40 -0
- package/validation/trim.cjs.map +1 -0
- package/validation/trim.d.cts +33 -0
- package/validation/trim.d.ts +33 -0
- package/validation/trim.js +38 -0
- package/validation/trim.js.map +1 -0
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
{
|
|
2
|
+
"schemaVersion": 1,
|
|
3
|
+
"name": "nest-pack",
|
|
4
|
+
"version": "0.0.0",
|
|
5
|
+
"formats": [
|
|
6
|
+
"esm",
|
|
7
|
+
"cjs",
|
|
8
|
+
"dts",
|
|
9
|
+
"dcts",
|
|
10
|
+
"esmFlat"
|
|
11
|
+
],
|
|
12
|
+
"entries": [
|
|
13
|
+
{
|
|
14
|
+
"name": "access/access-guard-options",
|
|
15
|
+
"sourcePath": "src/access/access-guard-options.ts",
|
|
16
|
+
"outputs": {
|
|
17
|
+
"esm": "access/access-guard-options.js",
|
|
18
|
+
"cjs": "access/access-guard-options.cjs",
|
|
19
|
+
"dts": "access/access-guard-options.d.ts",
|
|
20
|
+
"dcts": "access/access-guard-options.d.cts"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
"name": "access/create-access-guard",
|
|
25
|
+
"sourcePath": "src/access/create-access-guard.ts",
|
|
26
|
+
"outputs": {
|
|
27
|
+
"esm": "access/create-access-guard.js",
|
|
28
|
+
"cjs": "access/create-access-guard.cjs",
|
|
29
|
+
"dts": "access/create-access-guard.d.ts",
|
|
30
|
+
"dcts": "access/create-access-guard.d.cts"
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
"name": "access/index",
|
|
35
|
+
"sourcePath": "src/access/index.ts",
|
|
36
|
+
"outputs": {
|
|
37
|
+
"esm": "access/index.js",
|
|
38
|
+
"cjs": "access/index.cjs",
|
|
39
|
+
"dts": "access/index.d.ts",
|
|
40
|
+
"dcts": "access/index.d.cts"
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
"name": "index",
|
|
45
|
+
"sourcePath": "src/index.ts",
|
|
46
|
+
"outputs": {
|
|
47
|
+
"esm": "index.js",
|
|
48
|
+
"esmFlat": "fesm/nest-pack.mjs",
|
|
49
|
+
"cjs": "index.cjs",
|
|
50
|
+
"dts": "index.d.ts",
|
|
51
|
+
"dcts": "index.d.cts"
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
"name": "validation/index",
|
|
56
|
+
"sourcePath": "src/validation/index.ts",
|
|
57
|
+
"outputs": {
|
|
58
|
+
"esm": "validation/index.js",
|
|
59
|
+
"cjs": "validation/index.cjs",
|
|
60
|
+
"dts": "validation/index.d.ts",
|
|
61
|
+
"dcts": "validation/index.d.cts"
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
"name": "validation/trim",
|
|
66
|
+
"sourcePath": "src/validation/trim.ts",
|
|
67
|
+
"outputs": {
|
|
68
|
+
"esm": "validation/trim.js",
|
|
69
|
+
"cjs": "validation/trim.cjs",
|
|
70
|
+
"dts": "validation/trim.d.ts",
|
|
71
|
+
"dcts": "validation/trim.d.cts"
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
],
|
|
75
|
+
"generatedAt": "2026-11-30T09:36:10.845Z"
|
|
76
|
+
}
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Telperion Technology
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,134 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @telperion/nest-pack
|
|
2
|
+
NestJS utilities: access guards and validation helpers
|
|
2
3
|
|
|
3
|
-
|
|
4
|
+
[](https://www.npmjs.com/package/@telperion/nest-pack)
|
|
5
|
+
[](https://www.npmjs.com/package/@telperion/nest-pack)
|
|
6
|
+
[](https://app.codecov.io/gh/telperiontech/telperion/tree/main?flags%5B0%5D=nest-pack)
|
|
7
|
+
[](https://github.com/telperiontech/telperion/blob/main/LICENSE)
|
|
8
|
+
|
|
9
|
+
The NestJS counterpart of [`@telperion/ng-pack`](../ng-pack).
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
* npm: `npm install @telperion/nest-pack --save`
|
|
13
|
+
* yarn: `yarn add @telperion/nest-pack`
|
|
14
|
+
* pnpm: `pnpm i -S @telperion/nest-pack`
|
|
15
|
+
|
|
16
|
+
Requires `@nestjs/common` and `@nestjs/core` (11 or 12).
|
|
17
|
+
|
|
18
|
+
## Entry points
|
|
19
|
+
|
|
20
|
+
| Import path | Contents | Extra requirements |
|
|
21
|
+
| --- | --- | --- |
|
|
22
|
+
| `@telperion/nest-pack` | `createAccessGuard` | |
|
|
23
|
+
| `@telperion/nest-pack/access` | `createAccessGuard` | |
|
|
24
|
+
| `@telperion/nest-pack/validation` | `Trim` | `class-transformer` (optional peer dependency); usually with `class-validator` |
|
|
25
|
+
|
|
26
|
+
The root entry point does not include `validation`, so projects without class-transformer are not affected.
|
|
27
|
+
|
|
28
|
+
## Access
|
|
29
|
+
|
|
30
|
+
### Create Access Guard
|
|
31
|
+
Creates a guard for routes about one entity, e.g. a project in `/projects/:projectId/...`. The guard resolves the caller's access to the entity in a route parameter, then checks the route's requirements:
|
|
32
|
+
|
|
33
|
+
- malformed parameter (`validate`), or no access (`resolve` gives `null` or `undefined`): **404**, so callers can't probe which ids exist
|
|
34
|
+
- access that does not meet every requirement of the route (`allows`): **403**
|
|
35
|
+
- otherwise the request passes, and the access is kept on the request
|
|
36
|
+
|
|
37
|
+
```typescript
|
|
38
|
+
import { createAccessGuard } from '@telperion/nest-pack/access';
|
|
39
|
+
import { isUUID } from 'class-validator';
|
|
40
|
+
|
|
41
|
+
export const ProjectAccessGuard = createAccessGuard({
|
|
42
|
+
param: 'projectId', // the route parameter
|
|
43
|
+
validate: isUUID, // malformed ids answer 404 without a lookup
|
|
44
|
+
inject: [ProjectAccessService], // providers passed to resolve, like a factory provider's inject
|
|
45
|
+
resolve: (projectId, request: AuthenticatedRequest, access) => request.actor && access.of(request.actor, projectId),
|
|
46
|
+
allows: (access, permission: ProjectPermission) => access.permissions.has(permission),
|
|
47
|
+
notFound: 'Project not found', // default 'Not Found'
|
|
48
|
+
forbidden: 'Missing project permission', // default 'Forbidden'
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
@Controller('projects/:projectId/items')
|
|
52
|
+
@ProjectAccessGuard.require('project.view') // controller-wide requirements
|
|
53
|
+
export class ItemsController {
|
|
54
|
+
@Post()
|
|
55
|
+
@ProjectAccessGuard.require('items.edit') // replaces the controller's requirements
|
|
56
|
+
create(@ProjectAccessGuard.access() access: ProjectAccess, @Body() body: CreateItemDto) {
|
|
57
|
+
// access: what resolve returned for this request
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
The guard class has:
|
|
63
|
+
- `require(...requirements)`: route or controller decorator that applies the guard (`UseGuards`) and sets the requirements (`SetMetadata`). A method's requirements replace its controller's; with no requirements any resolved access passes
|
|
64
|
+
- `access()`: parameter decorator giving the resolved access. It throws (500) on routes without the guard
|
|
65
|
+
- `accessOf(request)`: the resolved access of a request, e.g. in an interceptor; `undefined` when the guard did not let it through
|
|
66
|
+
|
|
67
|
+
Types are inferred: `require` accepts only the `allows` requirement type, and the `inject` providers arrive typed in `resolve` (string and symbol tokens arrive as `unknown`; annotate those parameters).
|
|
68
|
+
|
|
69
|
+
Nest creates the guard in the module of the controller using it, so its `inject` providers must be available there. To give the guard its own class name, extend it:
|
|
70
|
+
|
|
71
|
+
```typescript
|
|
72
|
+
@Injectable()
|
|
73
|
+
export class ProjectAccessGuard extends createAccessGuard({ /* ... */ }) {}
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
HTTP only: the guard reads `request.params`.
|
|
77
|
+
|
|
78
|
+
#### Example: two guards, a few lines each
|
|
79
|
+
|
|
80
|
+
A project guard, where the access is an object with permissions and only the owner of the token's actor may see the project:
|
|
81
|
+
|
|
82
|
+
```typescript
|
|
83
|
+
export const ProjectAccessGuard = createAccessGuard({
|
|
84
|
+
param: 'projectId',
|
|
85
|
+
validate: isUUID,
|
|
86
|
+
inject: [ProjectAccessService],
|
|
87
|
+
resolve: (projectId, request: AuthenticatedRequest, access) => request.actor && access.of(request.actor, projectId),
|
|
88
|
+
allows: (access, permission: ProjectPermission) => access.permissions.has(permission),
|
|
89
|
+
notFound: 'Project not found',
|
|
90
|
+
forbidden: 'Missing project permission',
|
|
91
|
+
});
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
An organization guard, where the access is the set of permissions a user holds in the organization, checked against the database instead of the token:
|
|
95
|
+
|
|
96
|
+
```typescript
|
|
97
|
+
export const OrgAccessGuard = createAccessGuard({
|
|
98
|
+
param: 'orgId',
|
|
99
|
+
validate: isUUID,
|
|
100
|
+
inject: [OrganizationsService],
|
|
101
|
+
resolve: (orgId, request: AuthenticatedRequest, organizations) =>
|
|
102
|
+
request.actor?.type === 'user' ? organizations.permissionsIn(request.actor.id, orgId) : null,
|
|
103
|
+
allows: (held, permission: OrgPermission) => held.has(permission),
|
|
104
|
+
notFound: 'Organization not found',
|
|
105
|
+
forbidden: 'Missing organization permission',
|
|
106
|
+
});
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
Each replaces a hand-written guard class with the same steps: reading the metadata, checking the parameter, the 404 and 403 answers, and the parameter decorator.
|
|
110
|
+
|
|
111
|
+
## Validation
|
|
112
|
+
|
|
113
|
+
### Trim
|
|
114
|
+
A class-transformer decorator that trims string values when a plain object becomes a DTO, so `' '` fails `@IsNotEmpty()` and `' a@example.com '` passes `@IsEmail()`. Other values pass unchanged.
|
|
115
|
+
|
|
116
|
+
```typescript
|
|
117
|
+
import { Trim } from '@telperion/nest-pack/validation';
|
|
118
|
+
import { IsNotEmpty, MaxLength } from 'class-validator';
|
|
119
|
+
|
|
120
|
+
export class CreateProjectDto {
|
|
121
|
+
@Trim()
|
|
122
|
+
@IsNotEmpty()
|
|
123
|
+
@MaxLength(80)
|
|
124
|
+
name!: string;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
app.useGlobalPipes(new ValidationPipe({ transform: true }));
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
`ValidationPipe` validates the transformed DTO either way; its `transform` option makes the handler receive the trimmed values too.
|
|
131
|
+
|
|
132
|
+
## License
|
|
133
|
+
|
|
134
|
+
[MIT](./LICENSE)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"access-guard-options.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;"}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { InjectionToken, Type, CanActivate } from '@nestjs/common';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The part of an HTTP request an access guard reads: the route parameters
|
|
5
|
+
*/
|
|
6
|
+
interface AccessGuardRequest {
|
|
7
|
+
params?: Record<string, unknown>;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* The instances of the injection tokens in {@link AccessGuardOptions.inject}, in order:
|
|
11
|
+
* class tokens give their instance type, string and symbol tokens give `unknown` (annotate those parameters)
|
|
12
|
+
*/
|
|
13
|
+
type InjectedDependencies<Tokens extends readonly InjectionToken[]> = {
|
|
14
|
+
-readonly [Index in keyof Tokens]: Tokens[Index] extends abstract new (...args: never) => infer Instance ? Instance : unknown;
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Options of {@link createAccessGuard}
|
|
18
|
+
*/
|
|
19
|
+
interface AccessGuardOptions<Access, Requirement, Request extends AccessGuardRequest, Tokens extends readonly InjectionToken[]> {
|
|
20
|
+
/**
|
|
21
|
+
* The route parameter holding the id of what is accessed, e.g. `'projectId'` for `/projects/:projectId/items`
|
|
22
|
+
*/
|
|
23
|
+
param: string;
|
|
24
|
+
/**
|
|
25
|
+
* Checks the parameter's format before `resolve` runs, e.g. `isUUID` from class-validator.
|
|
26
|
+
* Malformed values answer 404 without a lookup.
|
|
27
|
+
*
|
|
28
|
+
* @default accepts every string
|
|
29
|
+
*/
|
|
30
|
+
validate?: (value: string) => boolean;
|
|
31
|
+
/**
|
|
32
|
+
* Providers `resolve` receives after the request, like `inject` of a factory provider
|
|
33
|
+
*/
|
|
34
|
+
inject?: Tokens;
|
|
35
|
+
/**
|
|
36
|
+
* Resolves the caller's access to what the parameter identifies; `null` or `undefined` when there is none
|
|
37
|
+
* (no such entity, or the caller may not see it), which answers 404
|
|
38
|
+
*
|
|
39
|
+
* @param value The route parameter, after `validate`
|
|
40
|
+
* @param request The HTTP request, e.g. with the authenticated user
|
|
41
|
+
* @param dependencies The instances of the `inject` tokens
|
|
42
|
+
*/
|
|
43
|
+
resolve: (value: string, request: Request, ...dependencies: InjectedDependencies<Tokens>) => Access | null | undefined | Promise<Access | null | undefined>;
|
|
44
|
+
/**
|
|
45
|
+
* Whether the access meets one requirement of the route, e.g. holds a permission. A route passes when the access meets all of them.
|
|
46
|
+
*/
|
|
47
|
+
allows: (access: Access, requirement: Requirement) => boolean;
|
|
48
|
+
/**
|
|
49
|
+
* Message of the 404 answer
|
|
50
|
+
*
|
|
51
|
+
* @default 'Not Found'
|
|
52
|
+
*/
|
|
53
|
+
notFound?: string;
|
|
54
|
+
/**
|
|
55
|
+
* Message of the 403 answer
|
|
56
|
+
*
|
|
57
|
+
* @default 'Forbidden'
|
|
58
|
+
*/
|
|
59
|
+
forbidden?: string;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* The guard class {@link createAccessGuard} returns
|
|
63
|
+
*/
|
|
64
|
+
interface AccessGuardType<Access, Requirement> extends Type<CanActivate> {
|
|
65
|
+
/**
|
|
66
|
+
* Route or controller decorator: applies the guard and sets the requirements of the route.
|
|
67
|
+
* A method's requirements replace its controller's. Without requirements, any resolved access passes.
|
|
68
|
+
*/
|
|
69
|
+
require(...requirements: Requirement[]): MethodDecorator & ClassDecorator;
|
|
70
|
+
/**
|
|
71
|
+
* Parameter decorator: the access the guard resolved for this request.
|
|
72
|
+
* Throws when the route does not use the guard.
|
|
73
|
+
*/
|
|
74
|
+
access(...pipes: unknown[]): ParameterDecorator;
|
|
75
|
+
/**
|
|
76
|
+
* The access the guard resolved for a request, or `undefined` before the guard ran or when it did not let the request through
|
|
77
|
+
*/
|
|
78
|
+
accessOf(request: object): Access | undefined;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export type { AccessGuardOptions, AccessGuardRequest, AccessGuardType, InjectedDependencies };
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { InjectionToken, Type, CanActivate } from '@nestjs/common';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The part of an HTTP request an access guard reads: the route parameters
|
|
5
|
+
*/
|
|
6
|
+
interface AccessGuardRequest {
|
|
7
|
+
params?: Record<string, unknown>;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* The instances of the injection tokens in {@link AccessGuardOptions.inject}, in order:
|
|
11
|
+
* class tokens give their instance type, string and symbol tokens give `unknown` (annotate those parameters)
|
|
12
|
+
*/
|
|
13
|
+
type InjectedDependencies<Tokens extends readonly InjectionToken[]> = {
|
|
14
|
+
-readonly [Index in keyof Tokens]: Tokens[Index] extends abstract new (...args: never) => infer Instance ? Instance : unknown;
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Options of {@link createAccessGuard}
|
|
18
|
+
*/
|
|
19
|
+
interface AccessGuardOptions<Access, Requirement, Request extends AccessGuardRequest, Tokens extends readonly InjectionToken[]> {
|
|
20
|
+
/**
|
|
21
|
+
* The route parameter holding the id of what is accessed, e.g. `'projectId'` for `/projects/:projectId/items`
|
|
22
|
+
*/
|
|
23
|
+
param: string;
|
|
24
|
+
/**
|
|
25
|
+
* Checks the parameter's format before `resolve` runs, e.g. `isUUID` from class-validator.
|
|
26
|
+
* Malformed values answer 404 without a lookup.
|
|
27
|
+
*
|
|
28
|
+
* @default accepts every string
|
|
29
|
+
*/
|
|
30
|
+
validate?: (value: string) => boolean;
|
|
31
|
+
/**
|
|
32
|
+
* Providers `resolve` receives after the request, like `inject` of a factory provider
|
|
33
|
+
*/
|
|
34
|
+
inject?: Tokens;
|
|
35
|
+
/**
|
|
36
|
+
* Resolves the caller's access to what the parameter identifies; `null` or `undefined` when there is none
|
|
37
|
+
* (no such entity, or the caller may not see it), which answers 404
|
|
38
|
+
*
|
|
39
|
+
* @param value The route parameter, after `validate`
|
|
40
|
+
* @param request The HTTP request, e.g. with the authenticated user
|
|
41
|
+
* @param dependencies The instances of the `inject` tokens
|
|
42
|
+
*/
|
|
43
|
+
resolve: (value: string, request: Request, ...dependencies: InjectedDependencies<Tokens>) => Access | null | undefined | Promise<Access | null | undefined>;
|
|
44
|
+
/**
|
|
45
|
+
* Whether the access meets one requirement of the route, e.g. holds a permission. A route passes when the access meets all of them.
|
|
46
|
+
*/
|
|
47
|
+
allows: (access: Access, requirement: Requirement) => boolean;
|
|
48
|
+
/**
|
|
49
|
+
* Message of the 404 answer
|
|
50
|
+
*
|
|
51
|
+
* @default 'Not Found'
|
|
52
|
+
*/
|
|
53
|
+
notFound?: string;
|
|
54
|
+
/**
|
|
55
|
+
* Message of the 403 answer
|
|
56
|
+
*
|
|
57
|
+
* @default 'Forbidden'
|
|
58
|
+
*/
|
|
59
|
+
forbidden?: string;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* The guard class {@link createAccessGuard} returns
|
|
63
|
+
*/
|
|
64
|
+
interface AccessGuardType<Access, Requirement> extends Type<CanActivate> {
|
|
65
|
+
/**
|
|
66
|
+
* Route or controller decorator: applies the guard and sets the requirements of the route.
|
|
67
|
+
* A method's requirements replace its controller's. Without requirements, any resolved access passes.
|
|
68
|
+
*/
|
|
69
|
+
require(...requirements: Requirement[]): MethodDecorator & ClassDecorator;
|
|
70
|
+
/**
|
|
71
|
+
* Parameter decorator: the access the guard resolved for this request.
|
|
72
|
+
* Throws when the route does not use the guard.
|
|
73
|
+
*/
|
|
74
|
+
access(...pipes: unknown[]): ParameterDecorator;
|
|
75
|
+
/**
|
|
76
|
+
* The access the guard resolved for a request, or `undefined` before the guard ran or when it did not let the request through
|
|
77
|
+
*/
|
|
78
|
+
accessOf(request: object): Access | undefined;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export type { AccessGuardOptions, AccessGuardRequest, AccessGuardType, InjectedDependencies };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"access-guard-options.js","sources":[],"sourcesContent":[],"names":[],"mappings":""}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var tslib = require('tslib');
|
|
4
|
+
var common = require('@nestjs/common');
|
|
5
|
+
var core = require('@nestjs/core');
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* #### Create Access Guard
|
|
9
|
+
*
|
|
10
|
+
* Creates a guard for routes about one entity, e.g. a project in `/projects/:projectId/...`.
|
|
11
|
+
* The guard resolves the caller's access to the entity in a route parameter and checks the route's requirements:
|
|
12
|
+
*
|
|
13
|
+
* - malformed parameter (see `validate`), or no access (`resolve` gives `null`/`undefined`): **404**,
|
|
14
|
+
* so callers can't probe which ids exist
|
|
15
|
+
* - access that does not meet every requirement of the route (see `allows`): **403**
|
|
16
|
+
* - otherwise the request passes, and the resolved access is kept on the request for `@Guard.access()`
|
|
17
|
+
*
|
|
18
|
+
* `resolve` receives the `inject` providers, like a factory provider's `useFactory`, so the guard works
|
|
19
|
+
* with Nest's dependency injection: provide it where its dependencies are (e.g. in the module of the controllers using it).
|
|
20
|
+
* HTTP only.
|
|
21
|
+
*
|
|
22
|
+
* * * *
|
|
23
|
+
* Example:
|
|
24
|
+
* ```typescript
|
|
25
|
+
* import { createAccessGuard } from '@telperion/nest-pack/access';
|
|
26
|
+
* import { isUUID } from 'class-validator';
|
|
27
|
+
*
|
|
28
|
+
* export const ProjectAccessGuard = createAccessGuard({
|
|
29
|
+
* param: 'projectId',
|
|
30
|
+
* validate: isUUID,
|
|
31
|
+
* inject: [ProjectAccessService],
|
|
32
|
+
* resolve: (projectId, request: AuthenticatedRequest, access) => request.user && access.of(request.user, projectId),
|
|
33
|
+
* allows: (access, permission: ProjectPermission) => access.permissions.has(permission),
|
|
34
|
+
* notFound: 'Project not found',
|
|
35
|
+
* });
|
|
36
|
+
*
|
|
37
|
+
* @Controller('projects/:projectId/items')
|
|
38
|
+
* export class ItemsController {
|
|
39
|
+
* @Post()
|
|
40
|
+
* @ProjectAccessGuard.require('items.edit')
|
|
41
|
+
* create(@ProjectAccessGuard.access() access: ProjectAccess, @Body() body: CreateItemDto) {}
|
|
42
|
+
* }
|
|
43
|
+
* ```
|
|
44
|
+
*
|
|
45
|
+
* To give the guard a class name of its own (in stack traces, or to inject it), extend it:
|
|
46
|
+
* `@Injectable() export class ProjectAccessGuard extends createAccessGuard({ ... }) {}`.
|
|
47
|
+
* * * *
|
|
48
|
+
* @param options Which parameter to read, how to resolve the access, and how to check a requirement
|
|
49
|
+
* @returns An injectable guard class with `require()`, `access()` and `accessOf()`
|
|
50
|
+
*/
|
|
51
|
+
function createAccessGuard(options) {
|
|
52
|
+
var AccessGuard_1;
|
|
53
|
+
const { param, validate = () => true, inject = [], resolve, allows, notFound = 'Not Found', forbidden = 'Forbidden' } = options;
|
|
54
|
+
const requirementsKey = Symbol(`access-guard:${param}:requirements`);
|
|
55
|
+
const accessKey = Symbol(`access-guard:${param}:access`);
|
|
56
|
+
let AccessGuard = class AccessGuard {
|
|
57
|
+
static { AccessGuard_1 = this; }
|
|
58
|
+
static access = common.createParamDecorator((_data, context) => AccessGuard_1.accessOf(context.switchToHttp().getRequest()) ?? AccessGuard_1.notGuarded());
|
|
59
|
+
reflector;
|
|
60
|
+
dependencies;
|
|
61
|
+
/**
|
|
62
|
+
* @param injected The `Reflector`, then the `inject` providers
|
|
63
|
+
*/
|
|
64
|
+
constructor(...injected) {
|
|
65
|
+
const [reflector, ...dependencies] = injected;
|
|
66
|
+
this.reflector = reflector;
|
|
67
|
+
this.dependencies = dependencies;
|
|
68
|
+
}
|
|
69
|
+
static require(...requirements) {
|
|
70
|
+
return common.applyDecorators(common.SetMetadata(requirementsKey, requirements), common.UseGuards(this));
|
|
71
|
+
}
|
|
72
|
+
static accessOf(request) {
|
|
73
|
+
return request[accessKey];
|
|
74
|
+
}
|
|
75
|
+
static notGuarded() {
|
|
76
|
+
throw new Error(`The access of :${param} is read on a route without its access guard; add \`.require()\` to the route or controller`);
|
|
77
|
+
}
|
|
78
|
+
canActivate(context) {
|
|
79
|
+
const request = context.switchToHttp().getRequest();
|
|
80
|
+
const value = request.params?.[param];
|
|
81
|
+
const required = this.reflector.getAllAndOverride(requirementsKey, [context.getHandler(), context.getClass()]) ?? [];
|
|
82
|
+
return Promise.resolve(typeof value === 'string' && validate(value) ? resolve(value, request, ...this.dependencies) : null).then(access => {
|
|
83
|
+
if (access === null || access === undefined) {
|
|
84
|
+
throw new common.NotFoundException(notFound);
|
|
85
|
+
}
|
|
86
|
+
if (!required.every(requirement => allows(access, requirement))) {
|
|
87
|
+
throw new common.ForbiddenException(forbidden);
|
|
88
|
+
}
|
|
89
|
+
Object.assign(request, { [accessKey]: access });
|
|
90
|
+
return true;
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
AccessGuard = AccessGuard_1 = tslib.__decorate([
|
|
95
|
+
common.Injectable(),
|
|
96
|
+
tslib.__metadata("design:paramtypes", [Object])
|
|
97
|
+
], AccessGuard);
|
|
98
|
+
[core.Reflector, ...inject].forEach((token, index) => common.Inject(token)(AccessGuard, undefined, index));
|
|
99
|
+
return AccessGuard;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
exports.createAccessGuard = createAccessGuard;
|
|
103
|
+
//# sourceMappingURL=create-access-guard.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-access-guard.cjs","sources":["../../../../libs/nest-pack/src/access/create-access-guard.ts"],"sourcesContent":[null],"names":["createParamDecorator","applyDecorators","SetMetadata","UseGuards","NotFoundException","ForbiddenException","__decorate","Injectable","Reflector","Inject"],"mappings":";;;;;;AAkBA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2CG;AACG,SAAU,iBAAiB,CAK/B,OAAiE,EAAA;;AACjE,IAAA,MAAM,EAAE,KAAK,EAAE,QAAQ,GAAG,MAAM,IAAI,EAAE,MAAM,GAAG,EAAE,EAAE,OAAO,EAAE,MAAM,EAAE,QAAQ,GAAG,WAAW,EAAE,SAAS,GAAG,WAAW,EAAE,GAAG,OAAO;IAC/H,MAAM,eAAe,GAAG,MAAM,CAAC,gBAAgB,KAAK,CAAA,aAAA,CAAe,CAAC;IACpE,MAAM,SAAS,GAAG,MAAM,CAAC,gBAAgB,KAAK,CAAA,OAAA,CAAS,CAAC;IAExD,IACM,WAAW,GADjB,MACM,WAAW,CAAA;;AACf,QAAA,OAAgB,MAAM,GAAGA,2BAAoB,CAC3C,CAAC,KAAc,EAAE,OAAyB,KACxC,aAAW,CAAC,QAAQ,CAAC,OAAO,CAAC,YAAY,EAAE,CAAC,UAAU,EAAU,CAAC,IAAI,aAAW,CAAC,UAAU,EAAE,CAChG;AAEgB,QAAA,SAAS;AACT,QAAA,YAAY;AAE7B;;AAEG;A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vC;AAEA,gBAAA,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,WAAW,IAAI,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC,EAAE;AAC/D,oBAAA,MAAM,IAAIC,yBAAkB,CAAC,SAAS,CAAC;gBACzC;AAEA,gBAAA,MAAM,CAAC,MAAM,CAAC,OAAO,EAAE,EAAE,CAAC,SAAS,GAAG,MAAM,EAAE,CAAC;AAE/C,gBAAA,OAAO,IAAI;AACb,YAAA,CAAC,CAAC;QACJ;;IAjDI,WAAW,GAAA,aAAA,GAAAC,gBAAA,CAAA;AADhB,QAAAC,iBAAU,EAAE;;AACP,KAAA,EAAA,WAAW,CAkDhB;IAED,CAACC,cAAS,EAAE,GAAG,MAAM,CAAC,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,KAAK,KAAKC,aAAM,CAAC,KAAK,CAAC,CAAC,WAAW,EAAE,SAAS,EAAE,KAAK,CAAC,CAAC;AAE9F,IAAA,OAAO,WAAW;AACpB;;;;"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { AccessGuardRequest, AccessGuardOptions, AccessGuardType } from './access-guard-options.cjs';
|
|
2
|
+
import { InjectionToken } from '@nestjs/common';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* #### Create Access Guard
|
|
6
|
+
*
|
|
7
|
+
* Creates a guard for routes about one entity, e.g. a project in `/projects/:projectId/...`.
|
|
8
|
+
* The guard resolves the caller's access to the entity in a route parameter and checks the route's requirements:
|
|
9
|
+
*
|
|
10
|
+
* - malformed parameter (see `validate`), or no access (`resolve` gives `null`/`undefined`): **404**,
|
|
11
|
+
* so callers can't probe which ids exist
|
|
12
|
+
* - access that does not meet every requirement of the route (see `allows`): **403**
|
|
13
|
+
* - otherwise the request passes, and the resolved access is kept on the request for `@Guard.access()`
|
|
14
|
+
*
|
|
15
|
+
* `resolve` receives the `inject` providers, like a factory provider's `useFactory`, so the guard works
|
|
16
|
+
* with Nest's dependency injection: provide it where its dependencies are (e.g. in the module of the controllers using it).
|
|
17
|
+
* HTTP only.
|
|
18
|
+
*
|
|
19
|
+
* * * *
|
|
20
|
+
* Example:
|
|
21
|
+
* ```typescript
|
|
22
|
+
* import { createAccessGuard } from '@telperion/nest-pack/access';
|
|
23
|
+
* import { isUUID } from 'class-validator';
|
|
24
|
+
*
|
|
25
|
+
* export const ProjectAccessGuard = createAccessGuard({
|
|
26
|
+
* param: 'projectId',
|
|
27
|
+
* validate: isUUID,
|
|
28
|
+
* inject: [ProjectAccessService],
|
|
29
|
+
* resolve: (projectId, request: AuthenticatedRequest, access) => request.user && access.of(request.user, projectId),
|
|
30
|
+
* allows: (access, permission: ProjectPermission) => access.permissions.has(permission),
|
|
31
|
+
* notFound: 'Project not found',
|
|
32
|
+
* });
|
|
33
|
+
*
|
|
34
|
+
* @Controller('projects/:projectId/items')
|
|
35
|
+
* export class ItemsController {
|
|
36
|
+
* @Post()
|
|
37
|
+
* @ProjectAccessGuard.require('items.edit')
|
|
38
|
+
* create(@ProjectAccessGuard.access() access: ProjectAccess, @Body() body: CreateItemDto) {}
|
|
39
|
+
* }
|
|
40
|
+
* ```
|
|
41
|
+
*
|
|
42
|
+
* To give the guard a class name of its own (in stack traces, or to inject it), extend it:
|
|
43
|
+
* `@Injectable() export class ProjectAccessGuard extends createAccessGuard({ ... }) {}`.
|
|
44
|
+
* * * *
|
|
45
|
+
* @param options Which parameter to read, how to resolve the access, and how to check a requirement
|
|
46
|
+
* @returns An injectable guard class with `require()`, `access()` and `accessOf()`
|
|
47
|
+
*/
|
|
48
|
+
declare function createAccessGuard<Access, Requirement, Request extends AccessGuardRequest = AccessGuardRequest, const Tokens extends readonly InjectionToken[] = []>(options: AccessGuardOptions<Access, Requirement, Request, Tokens>): AccessGuardType<Access, Requirement>;
|
|
49
|
+
|
|
50
|
+
export { createAccessGuard };
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { AccessGuardRequest, AccessGuardOptions, AccessGuardType } from './access-guard-options.js';
|
|
2
|
+
import { InjectionToken } from '@nestjs/common';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* #### Create Access Guard
|
|
6
|
+
*
|
|
7
|
+
* Creates a guard for routes about one entity, e.g. a project in `/projects/:projectId/...`.
|
|
8
|
+
* The guard resolves the caller's access to the entity in a route parameter and checks the route's requirements:
|
|
9
|
+
*
|
|
10
|
+
* - malformed parameter (see `validate`), or no access (`resolve` gives `null`/`undefined`): **404**,
|
|
11
|
+
* so callers can't probe which ids exist
|
|
12
|
+
* - access that does not meet every requirement of the route (see `allows`): **403**
|
|
13
|
+
* - otherwise the request passes, and the resolved access is kept on the request for `@Guard.access()`
|
|
14
|
+
*
|
|
15
|
+
* `resolve` receives the `inject` providers, like a factory provider's `useFactory`, so the guard works
|
|
16
|
+
* with Nest's dependency injection: provide it where its dependencies are (e.g. in the module of the controllers using it).
|
|
17
|
+
* HTTP only.
|
|
18
|
+
*
|
|
19
|
+
* * * *
|
|
20
|
+
* Example:
|
|
21
|
+
* ```typescript
|
|
22
|
+
* import { createAccessGuard } from '@telperion/nest-pack/access';
|
|
23
|
+
* import { isUUID } from 'class-validator';
|
|
24
|
+
*
|
|
25
|
+
* export const ProjectAccessGuard = createAccessGuard({
|
|
26
|
+
* param: 'projectId',
|
|
27
|
+
* validate: isUUID,
|
|
28
|
+
* inject: [ProjectAccessService],
|
|
29
|
+
* resolve: (projectId, request: AuthenticatedRequest, access) => request.user && access.of(request.user, projectId),
|
|
30
|
+
* allows: (access, permission: ProjectPermission) => access.permissions.has(permission),
|
|
31
|
+
* notFound: 'Project not found',
|
|
32
|
+
* });
|
|
33
|
+
*
|
|
34
|
+
* @Controller('projects/:projectId/items')
|
|
35
|
+
* export class ItemsController {
|
|
36
|
+
* @Post()
|
|
37
|
+
* @ProjectAccessGuard.require('items.edit')
|
|
38
|
+
* create(@ProjectAccessGuard.access() access: ProjectAccess, @Body() body: CreateItemDto) {}
|
|
39
|
+
* }
|
|
40
|
+
* ```
|
|
41
|
+
*
|
|
42
|
+
* To give the guard a class name of its own (in stack traces, or to inject it), extend it:
|
|
43
|
+
* `@Injectable() export class ProjectAccessGuard extends createAccessGuard({ ... }) {}`.
|
|
44
|
+
* * * *
|
|
45
|
+
* @param options Which parameter to read, how to resolve the access, and how to check a requirement
|
|
46
|
+
* @returns An injectable guard class with `require()`, `access()` and `accessOf()`
|
|
47
|
+
*/
|
|
48
|
+
declare function createAccessGuard<Access, Requirement, Request extends AccessGuardRequest = AccessGuardRequest, const Tokens extends readonly InjectionToken[] = []>(options: AccessGuardOptions<Access, Requirement, Request, Tokens>): AccessGuardType<Access, Requirement>;
|
|
49
|
+
|
|
50
|
+
export { createAccessGuard };
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { __decorate, __metadata } from 'tslib';
|
|
2
|
+
import { createParamDecorator, applyDecorators, SetMetadata, UseGuards, NotFoundException, ForbiddenException, Injectable, Inject } from '@nestjs/common';
|
|
3
|
+
import { Reflector } from '@nestjs/core';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* #### Create Access Guard
|
|
7
|
+
*
|
|
8
|
+
* Creates a guard for routes about one entity, e.g. a project in `/projects/:projectId/...`.
|
|
9
|
+
* The guard resolves the caller's access to the entity in a route parameter and checks the route's requirements:
|
|
10
|
+
*
|
|
11
|
+
* - malformed parameter (see `validate`), or no access (`resolve` gives `null`/`undefined`): **404**,
|
|
12
|
+
* so callers can't probe which ids exist
|
|
13
|
+
* - access that does not meet every requirement of the route (see `allows`): **403**
|
|
14
|
+
* - otherwise the request passes, and the resolved access is kept on the request for `@Guard.access()`
|
|
15
|
+
*
|
|
16
|
+
* `resolve` receives the `inject` providers, like a factory provider's `useFactory`, so the guard works
|
|
17
|
+
* with Nest's dependency injection: provide it where its dependencies are (e.g. in the module of the controllers using it).
|
|
18
|
+
* HTTP only.
|
|
19
|
+
*
|
|
20
|
+
* * * *
|
|
21
|
+
* Example:
|
|
22
|
+
* ```typescript
|
|
23
|
+
* import { createAccessGuard } from '@telperion/nest-pack/access';
|
|
24
|
+
* import { isUUID } from 'class-validator';
|
|
25
|
+
*
|
|
26
|
+
* export const ProjectAccessGuard = createAccessGuard({
|
|
27
|
+
* param: 'projectId',
|
|
28
|
+
* validate: isUUID,
|
|
29
|
+
* inject: [ProjectAccessService],
|
|
30
|
+
* resolve: (projectId, request: AuthenticatedRequest, access) => request.user && access.of(request.user, projectId),
|
|
31
|
+
* allows: (access, permission: ProjectPermission) => access.permissions.has(permission),
|
|
32
|
+
* notFound: 'Project not found',
|
|
33
|
+
* });
|
|
34
|
+
*
|
|
35
|
+
* @Controller('projects/:projectId/items')
|
|
36
|
+
* export class ItemsController {
|
|
37
|
+
* @Post()
|
|
38
|
+
* @ProjectAccessGuard.require('items.edit')
|
|
39
|
+
* create(@ProjectAccessGuard.access() access: ProjectAccess, @Body() body: CreateItemDto) {}
|
|
40
|
+
* }
|
|
41
|
+
* ```
|
|
42
|
+
*
|
|
43
|
+
* To give the guard a class name of its own (in stack traces, or to inject it), extend it:
|
|
44
|
+
* `@Injectable() export class ProjectAccessGuard extends createAccessGuard({ ... }) {}`.
|
|
45
|
+
* * * *
|
|
46
|
+
* @param options Which parameter to read, how to resolve the access, and how to check a requirement
|
|
47
|
+
* @returns An injectable guard class with `require()`, `access()` and `accessOf()`
|
|
48
|
+
*/
|
|
49
|
+
function createAccessGuard(options) {
|
|
50
|
+
var AccessGuard_1;
|
|
51
|
+
const { param, validate = () => true, inject = [], resolve, allows, notFound = 'Not Found', forbidden = 'Forbidden' } = options;
|
|
52
|
+
const requirementsKey = Symbol(`access-guard:${param}:requirements`);
|
|
53
|
+
const accessKey = Symbol(`access-guard:${param}:access`);
|
|
54
|
+
let AccessGuard = class AccessGuard {
|
|
55
|
+
static { AccessGuard_1 = this; }
|
|
56
|
+
static access = createParamDecorator((_data, context) => AccessGuard_1.accessOf(context.switchToHttp().getRequest()) ?? AccessGuard_1.notGuarded());
|
|
57
|
+
reflector;
|
|
58
|
+
dependencies;
|
|
59
|
+
/**
|
|
60
|
+
* @param injected The `Reflector`, then the `inject` providers
|
|
61
|
+
*/
|
|
62
|
+
constructor(...injected) {
|
|
63
|
+
const [reflector, ...dependencies] = injected;
|
|
64
|
+
this.reflector = reflector;
|
|
65
|
+
this.dependencies = dependencies;
|
|
66
|
+
}
|
|
67
|
+
static require(...requirements) {
|
|
68
|
+
return applyDecorators(SetMetadata(requirementsKey, requirements), UseGuards(this));
|
|
69
|
+
}
|
|
70
|
+
static accessOf(request) {
|
|
71
|
+
return request[accessKey];
|
|
72
|
+
}
|
|
73
|
+
static notGuarded() {
|
|
74
|
+
throw new Error(`The access of :${param} is read on a route without its access guard; add \`.require()\` to the route or controller`);
|
|
75
|
+
}
|
|
76
|
+
canActivate(context) {
|
|
77
|
+
const request = context.switchToHttp().getRequest();
|
|
78
|
+
const value = request.params?.[param];
|
|
79
|
+
const required = this.reflector.getAllAndOverride(requirementsKey, [context.getHandler(), context.getClass()]) ?? [];
|
|
80
|
+
return Promise.resolve(typeof value === 'string' && validate(value) ? resolve(value, request, ...this.dependencies) : null).then(access => {
|
|
81
|
+
if (access === null || access === undefined) {
|
|
82
|
+
throw new NotFoundException(notFound);
|
|
83
|
+
}
|
|
84
|
+
if (!required.every(requirement => allows(access, requirement))) {
|
|
85
|
+
throw new ForbiddenException(forbidden);
|
|
86
|
+
}
|
|
87
|
+
Object.assign(request, { [accessKey]: access });
|
|
88
|
+
return true;
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
AccessGuard = AccessGuard_1 = __decorate([
|
|
93
|
+
Injectable(),
|
|
94
|
+
__metadata("design:paramtypes", [Object])
|
|
95
|
+
], AccessGuard);
|
|
96
|
+
[Reflector, ...inject].forEach((token, index) => Inject(token)(AccessGuard, undefined, index));
|
|
97
|
+
return AccessGuard;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export { createAccessGuard };
|
|
101
|
+
//# sourceMappingURL=create-access-guard.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-access-guard.js","sources":["../../../../libs/nest-pack/src/access/create-access-guard.ts"],"sourcesContent":[null],"names":[],"mappings":";;;;AAkBA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2CG;AACG,SAAU,iBAAiB,CAK/B,OAAiE,EAAA;;AACjE,IAAA,MAAM,EAAE,KAAK,EAAE,QAAQ,GAAG,MAAM,IAAI,EAAE,MAAM,GAAG,EAAE,EAAE,OAAO,EAAE,MAAM,EAAE,QAAQ,GAAG,WAAW,EAAE,SAAS,GAAG,WAAW,EAAE,GAAG,OAAO;IAC/H,MAAM,eAAe,GAAG,MAAM,CAAC,gBAAgB,KAAK,CAAA,aAAA,CAAe,CAAC;IACpE,MAAM,SAAS,GAAG,MAAM,CAAC,gBAAgB,KAAK,CAAA,OAAA,CAAS,CAAC;IAExD,IACM,WAAW,GADjB,MACM,WAAW,CAAA;;AACf,QAAA,OAAgB,MAAM,GAAG,oBAAoB,CAC3C,CAAC,KAAc,EAAE,OAAyB,KACxC,aAAW,CAAC,QAAQ,CAAC,OAAO,CAAC,YAAY,EAAE,CAAC,UAAU,EAAU,CAAC,IAAI,aAAW,CAAC,UAAU,EAAE,CAChG;AAEgB,QAAA,SAAS;AACT,QAAA,YAAY;AAE7B;;AAEG;AACH,QAAA,WAAA,CAAY,GAAG,QAAmC,EAAA;YAChD,MAAM,CAAC,SAAS,EAAE,GAAG,YAAY,CAAC,GAAG,QAAQ;AAE7C,YAAA,IAAI,CAAC,SAAS,GAAG,SAAS;AAC1B,YAAA,IAAI,CAAC,YAAY,GAAG,YAA4C;QAClE;AAEA,QAAA,OAAO,OAAO,CAA0B,GAAG,YAA2B,EAAA;AACpE,YAAA,OAAO,eAAe,CAAC,WAAW,CAAC,eAAe,EAAE,YAAY,CAAC,EAAE,SAAS,CAAC,IAAI,CAAC,CAAC;QACrF;QAEA,OAAO,QAAQ,CAAC,OAAe,EAAA;AAC7B,Y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WAAW,CAkDhB;IAED,CAAC,SAAS,EAAE,GAAG,MAAM,CAAC,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,KAAK,KAAK,MAAM,CAAC,KAAK,CAAC,CAAC,WAAW,EAAE,SAAS,EAAE,KAAK,CAAC,CAAC;AAE9F,IAAA,OAAO,WAAW;AACpB;;;;"}
|
package/access/index.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;"}
|
package/access/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":""}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { __decorate, __metadata } from 'tslib';
|
|
2
|
+
import { createParamDecorator, applyDecorators, SetMetadata, UseGuards, NotFoundException, ForbiddenException, Injectable, Inject } from '@nestjs/common';
|
|
3
|
+
import { Reflector } from '@nestjs/core';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* #### Create Access Guard
|
|
7
|
+
*
|
|
8
|
+
* Creates a guard for routes about one entity, e.g. a project in `/projects/:projectId/...`.
|
|
9
|
+
* The guard resolves the caller's access to the entity in a route parameter and checks the route's requirements:
|
|
10
|
+
*
|
|
11
|
+
* - malformed parameter (see `validate`), or no access (`resolve` gives `null`/`undefined`): **404**,
|
|
12
|
+
* so callers can't probe which ids exist
|
|
13
|
+
* - access that does not meet every requirement of the route (see `allows`): **403**
|
|
14
|
+
* - otherwise the request passes, and the resolved access is kept on the request for `@Guard.access()`
|
|
15
|
+
*
|
|
16
|
+
* `resolve` receives the `inject` providers, like a factory provider's `useFactory`, so the guard works
|
|
17
|
+
* with Nest's dependency injection: provide it where its dependencies are (e.g. in the module of the controllers using it).
|
|
18
|
+
* HTTP only.
|
|
19
|
+
*
|
|
20
|
+
* * * *
|
|
21
|
+
* Example:
|
|
22
|
+
* ```typescript
|
|
23
|
+
* import { createAccessGuard } from '@telperion/nest-pack/access';
|
|
24
|
+
* import { isUUID } from 'class-validator';
|
|
25
|
+
*
|
|
26
|
+
* export const ProjectAccessGuard = createAccessGuard({
|
|
27
|
+
* param: 'projectId',
|
|
28
|
+
* validate: isUUID,
|
|
29
|
+
* inject: [ProjectAccessService],
|
|
30
|
+
* resolve: (projectId, request: AuthenticatedRequest, access) => request.user && access.of(request.user, projectId),
|
|
31
|
+
* allows: (access, permission: ProjectPermission) => access.permissions.has(permission),
|
|
32
|
+
* notFound: 'Project not found',
|
|
33
|
+
* });
|
|
34
|
+
*
|
|
35
|
+
* @Controller('projects/:projectId/items')
|
|
36
|
+
* export class ItemsController {
|
|
37
|
+
* @Post()
|
|
38
|
+
* @ProjectAccessGuard.require('items.edit')
|
|
39
|
+
* create(@ProjectAccessGuard.access() access: ProjectAccess, @Body() body: CreateItemDto) {}
|
|
40
|
+
* }
|
|
41
|
+
* ```
|
|
42
|
+
*
|
|
43
|
+
* To give the guard a class name of its own (in stack traces, or to inject it), extend it:
|
|
44
|
+
* `@Injectable() export class ProjectAccessGuard extends createAccessGuard({ ... }) {}`.
|
|
45
|
+
* * * *
|
|
46
|
+
* @param options Which parameter to read, how to resolve the access, and how to check a requirement
|
|
47
|
+
* @returns An injectable guard class with `require()`, `access()` and `accessOf()`
|
|
48
|
+
*/
|
|
49
|
+
function createAccessGuard(options) {
|
|
50
|
+
var AccessGuard_1;
|
|
51
|
+
const { param, validate = () => true, inject = [], resolve, allows, notFound = 'Not Found', forbidden = 'Forbidden' } = options;
|
|
52
|
+
const requirementsKey = Symbol(`access-guard:${param}:requirements`);
|
|
53
|
+
const accessKey = Symbol(`access-guard:${param}:access`);
|
|
54
|
+
let AccessGuard = class AccessGuard {
|
|
55
|
+
static { AccessGuard_1 = this; }
|
|
56
|
+
static access = createParamDecorator((_data, context) => AccessGuard_1.accessOf(context.switchToHttp().getRequest()) ?? AccessGuard_1.notGuarded());
|
|
57
|
+
reflector;
|
|
58
|
+
dependencies;
|
|
59
|
+
/**
|
|
60
|
+
* @param injected The `Reflector`, then the `inject` providers
|
|
61
|
+
*/
|
|
62
|
+
constructor(...injected) {
|
|
63
|
+
const [reflector, ...dependencies] = injected;
|
|
64
|
+
this.reflector = reflector;
|
|
65
|
+
this.dependencies = dependencies;
|
|
66
|
+
}
|
|
67
|
+
static require(...requirements) {
|
|
68
|
+
return applyDecorators(SetMetadata(requirementsKey, requirements), UseGuards(this));
|
|
69
|
+
}
|
|
70
|
+
static accessOf(request) {
|
|
71
|
+
return request[accessKey];
|
|
72
|
+
}
|
|
73
|
+
static notGuarded() {
|
|
74
|
+
throw new Error(`The access of :${param} is read on a route without its access guard; add \`.require()\` to the route or controller`);
|
|
75
|
+
}
|
|
76
|
+
canActivate(context) {
|
|
77
|
+
const request = context.switchToHttp().getRequest();
|
|
78
|
+
const value = request.params?.[param];
|
|
79
|
+
const required = this.reflector.getAllAndOverride(requirementsKey, [context.getHandler(), context.getClass()]) ?? [];
|
|
80
|
+
return Promise.resolve(typeof value === 'string' && validate(value) ? resolve(value, request, ...this.dependencies) : null).then(access => {
|
|
81
|
+
if (access === null || access === undefined) {
|
|
82
|
+
throw new NotFoundException(notFound);
|
|
83
|
+
}
|
|
84
|
+
if (!required.every(requirement => allows(access, requirement))) {
|
|
85
|
+
throw new ForbiddenException(forbidden);
|
|
86
|
+
}
|
|
87
|
+
Object.assign(request, { [accessKey]: access });
|
|
88
|
+
return true;
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
AccessGuard = AccessGuard_1 = __decorate([
|
|
93
|
+
Injectable(),
|
|
94
|
+
__metadata("design:paramtypes", [Object])
|
|
95
|
+
], AccessGuard);
|
|
96
|
+
[Reflector, ...inject].forEach((token, index) => Inject(token)(AccessGuard, undefined, index));
|
|
97
|
+
return AccessGuard;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export { createAccessGuard };
|
|
101
|
+
//# sourceMappingURL=nest-pack.mjs.map
|
|
@@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,WAAW,CAkDhB;IAED,CAAC,SAAS,EAAE,GAAG,MAAM,CAAC,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,KAAK,KAAK,MAAM,CAAC,KAAK,CAAC,CAAC,WAAW,EAAE,SAAS,EAAE,KAAK,CAAC,CAAC;AAE9F,IAAA,OAAO,WAAW;AACpB;;;;"}
|
package/index.cjs
ADDED
package/index.cjs.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;"}
|
package/index.d.cts
ADDED
package/index.d.ts
ADDED
package/index.js
ADDED
package/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":""}
|
package/package.json
CHANGED
|
@@ -1,6 +1,140 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@telperion/nest-pack",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "NestJS utilities: access guards and validation helpers",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"nestjs",
|
|
7
|
+
"nest",
|
|
8
|
+
"guard",
|
|
9
|
+
"access control",
|
|
10
|
+
"authorization",
|
|
11
|
+
"permissions",
|
|
12
|
+
"decorator",
|
|
13
|
+
"class-transformer",
|
|
14
|
+
"class-validator",
|
|
15
|
+
"validation",
|
|
16
|
+
"trim"
|
|
17
|
+
],
|
|
18
|
+
"homepage": "https://github.com/telperiontech/telperion/tree/main/libs/nest-pack#readme",
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "https://github.com/telperiontech/telperion.git",
|
|
22
|
+
"directory": "libs/nest-pack"
|
|
23
|
+
},
|
|
24
|
+
"type": "module",
|
|
25
|
+
"module": "./index.js",
|
|
26
|
+
"main": "./index.cjs",
|
|
27
|
+
"typings": "./index.d.ts",
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"tslib": "^2.3.0"
|
|
30
|
+
},
|
|
31
|
+
"peerDependencies": {
|
|
32
|
+
"@nestjs/common": "^11.0.0 || ^12.0.0",
|
|
33
|
+
"@nestjs/core": "^11.0.0 || ^12.0.0",
|
|
34
|
+
"class-transformer": "^0.5.0",
|
|
35
|
+
"class-validator": "^0.14.0 || ^0.15.0"
|
|
36
|
+
},
|
|
37
|
+
"peerDependenciesMeta": {
|
|
38
|
+
"class-transformer": {
|
|
39
|
+
"optional": true
|
|
40
|
+
},
|
|
41
|
+
"class-validator": {
|
|
42
|
+
"optional": true
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
"bugs": "https://github.com/telperiontech/telperion/issues",
|
|
46
|
+
"license": "MIT",
|
|
47
|
+
"author": {
|
|
48
|
+
"name": "Ali Şahin Özçelik",
|
|
49
|
+
"email": "alisahinozcelik@gmail.com",
|
|
50
|
+
"url": "https://alisah.in"
|
|
51
|
+
},
|
|
52
|
+
"funding": {
|
|
53
|
+
"type": "patreon",
|
|
54
|
+
"url": "https://www.patreon.com/alisahin"
|
|
55
|
+
},
|
|
56
|
+
"sideEffects": false,
|
|
57
|
+
"exports": {
|
|
58
|
+
".": {
|
|
59
|
+
"types": {
|
|
60
|
+
"import": "./index.d.ts",
|
|
61
|
+
"require": "./index.d.cts"
|
|
62
|
+
},
|
|
63
|
+
"import": "./index.js",
|
|
64
|
+
"require": "./index.cjs",
|
|
65
|
+
"node": "./index.cjs",
|
|
66
|
+
"default": "./index.js"
|
|
67
|
+
},
|
|
68
|
+
"./access/access-guard-options": {
|
|
69
|
+
"types": {
|
|
70
|
+
"import": "./access/access-guard-options.d.ts",
|
|
71
|
+
"require": "./access/access-guard-options.d.cts"
|
|
72
|
+
},
|
|
73
|
+
"import": "./access/access-guard-options.js",
|
|
74
|
+
"require": "./access/access-guard-options.cjs",
|
|
75
|
+
"node": "./access/access-guard-options.cjs",
|
|
76
|
+
"default": "./access/access-guard-options.js"
|
|
77
|
+
},
|
|
78
|
+
"./access/create-access-guard": {
|
|
79
|
+
"types": {
|
|
80
|
+
"import": "./access/create-access-guard.d.ts",
|
|
81
|
+
"require": "./access/create-access-guard.d.cts"
|
|
82
|
+
},
|
|
83
|
+
"import": "./access/create-access-guard.js",
|
|
84
|
+
"require": "./access/create-access-guard.cjs",
|
|
85
|
+
"node": "./access/create-access-guard.cjs",
|
|
86
|
+
"default": "./access/create-access-guard.js"
|
|
87
|
+
},
|
|
88
|
+
"./access/index": {
|
|
89
|
+
"types": {
|
|
90
|
+
"import": "./access/index.d.ts",
|
|
91
|
+
"require": "./access/index.d.cts"
|
|
92
|
+
},
|
|
93
|
+
"import": "./access/index.js",
|
|
94
|
+
"require": "./access/index.cjs",
|
|
95
|
+
"node": "./access/index.cjs",
|
|
96
|
+
"default": "./access/index.js"
|
|
97
|
+
},
|
|
98
|
+
"./access": {
|
|
99
|
+
"types": {
|
|
100
|
+
"import": "./access/index.d.ts",
|
|
101
|
+
"require": "./access/index.d.cts"
|
|
102
|
+
},
|
|
103
|
+
"import": "./access/index.js",
|
|
104
|
+
"require": "./access/index.cjs",
|
|
105
|
+
"node": "./access/index.cjs",
|
|
106
|
+
"default": "./access/index.js"
|
|
107
|
+
},
|
|
108
|
+
"./validation/index": {
|
|
109
|
+
"types": {
|
|
110
|
+
"import": "./validation/index.d.ts",
|
|
111
|
+
"require": "./validation/index.d.cts"
|
|
112
|
+
},
|
|
113
|
+
"import": "./validation/index.js",
|
|
114
|
+
"require": "./validation/index.cjs",
|
|
115
|
+
"node": "./validation/index.cjs",
|
|
116
|
+
"default": "./validation/index.js"
|
|
117
|
+
},
|
|
118
|
+
"./validation": {
|
|
119
|
+
"types": {
|
|
120
|
+
"import": "./validation/index.d.ts",
|
|
121
|
+
"require": "./validation/index.d.cts"
|
|
122
|
+
},
|
|
123
|
+
"import": "./validation/index.js",
|
|
124
|
+
"require": "./validation/index.cjs",
|
|
125
|
+
"node": "./validation/index.cjs",
|
|
126
|
+
"default": "./validation/index.js"
|
|
127
|
+
},
|
|
128
|
+
"./validation/trim": {
|
|
129
|
+
"types": {
|
|
130
|
+
"import": "./validation/trim.d.ts",
|
|
131
|
+
"require": "./validation/trim.d.cts"
|
|
132
|
+
},
|
|
133
|
+
"import": "./validation/trim.js",
|
|
134
|
+
"require": "./validation/trim.cjs",
|
|
135
|
+
"node": "./validation/trim.cjs",
|
|
136
|
+
"default": "./validation/trim.js"
|
|
137
|
+
}
|
|
138
|
+
},
|
|
139
|
+
"types": "./index.d.ts"
|
|
140
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { Trim } from './trim.cjs';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { Trim } from './trim.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":""}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var classTransformer = require('class-transformer');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* #### Trim
|
|
7
|
+
*
|
|
8
|
+
* A class-transformer decorator that trims string values when a plain object becomes a DTO
|
|
9
|
+
* (`plainToInstance`, which `ValidationPipe` runs before validating), so `' '` fails `@IsNotEmpty()`
|
|
10
|
+
* and `' a@example.com '` passes `@IsEmail()`. Other values (numbers, `null`, arrays, …) pass unchanged.
|
|
11
|
+
*
|
|
12
|
+
* `ValidationPipe` validates the transformed DTO either way; turn on its `transform` option
|
|
13
|
+
* so the handler receives the trimmed values too.
|
|
14
|
+
*
|
|
15
|
+
* Requires `class-transformer` in your project.
|
|
16
|
+
*
|
|
17
|
+
* * * *
|
|
18
|
+
* Example:
|
|
19
|
+
* ```typescript
|
|
20
|
+
* import { Trim } from '@telperion/nest-pack/validation';
|
|
21
|
+
* import { IsNotEmpty, MaxLength } from 'class-validator';
|
|
22
|
+
*
|
|
23
|
+
* export class CreateProjectDto {
|
|
24
|
+
* @Trim()
|
|
25
|
+
* @IsNotEmpty()
|
|
26
|
+
* @MaxLength(80)
|
|
27
|
+
* name!: string;
|
|
28
|
+
* }
|
|
29
|
+
*
|
|
30
|
+
* app.useGlobalPipes(new ValidationPipe({ transform: true }));
|
|
31
|
+
* ```
|
|
32
|
+
* * * *
|
|
33
|
+
* @returns A property decorator
|
|
34
|
+
*/
|
|
35
|
+
function Trim() {
|
|
36
|
+
return classTransformer.Transform(({ value }) => (typeof value === 'string' ? value.trim() : value));
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
exports.Trim = Trim;
|
|
40
|
+
//# sourceMappingURL=trim.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"trim.cjs","sources":["../../../../libs/nest-pack/src/validation/trim.ts"],"sourcesContent":[null],"names":["Transform"],"mappings":";;;;AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6BG;SACa,IAAI,GAAA;IAClB,OAAOA,0BAAS,CAAC,CAAC,EAAE,KAAK,EAAsB,MAAM,OAAO,KAAK,KAAK,QAAQ,GAAG,KAAK,CAAC,IAAI,EAAE,GAAG,KAAK,CAAC,CAAC;AACzG;;;;"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* #### Trim
|
|
3
|
+
*
|
|
4
|
+
* A class-transformer decorator that trims string values when a plain object becomes a DTO
|
|
5
|
+
* (`plainToInstance`, which `ValidationPipe` runs before validating), so `' '` fails `@IsNotEmpty()`
|
|
6
|
+
* and `' a@example.com '` passes `@IsEmail()`. Other values (numbers, `null`, arrays, …) pass unchanged.
|
|
7
|
+
*
|
|
8
|
+
* `ValidationPipe` validates the transformed DTO either way; turn on its `transform` option
|
|
9
|
+
* so the handler receives the trimmed values too.
|
|
10
|
+
*
|
|
11
|
+
* Requires `class-transformer` in your project.
|
|
12
|
+
*
|
|
13
|
+
* * * *
|
|
14
|
+
* Example:
|
|
15
|
+
* ```typescript
|
|
16
|
+
* import { Trim } from '@telperion/nest-pack/validation';
|
|
17
|
+
* import { IsNotEmpty, MaxLength } from 'class-validator';
|
|
18
|
+
*
|
|
19
|
+
* export class CreateProjectDto {
|
|
20
|
+
* @Trim()
|
|
21
|
+
* @IsNotEmpty()
|
|
22
|
+
* @MaxLength(80)
|
|
23
|
+
* name!: string;
|
|
24
|
+
* }
|
|
25
|
+
*
|
|
26
|
+
* app.useGlobalPipes(new ValidationPipe({ transform: true }));
|
|
27
|
+
* ```
|
|
28
|
+
* * * *
|
|
29
|
+
* @returns A property decorator
|
|
30
|
+
*/
|
|
31
|
+
declare function Trim(): PropertyDecorator;
|
|
32
|
+
|
|
33
|
+
export { Trim };
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* #### Trim
|
|
3
|
+
*
|
|
4
|
+
* A class-transformer decorator that trims string values when a plain object becomes a DTO
|
|
5
|
+
* (`plainToInstance`, which `ValidationPipe` runs before validating), so `' '` fails `@IsNotEmpty()`
|
|
6
|
+
* and `' a@example.com '` passes `@IsEmail()`. Other values (numbers, `null`, arrays, …) pass unchanged.
|
|
7
|
+
*
|
|
8
|
+
* `ValidationPipe` validates the transformed DTO either way; turn on its `transform` option
|
|
9
|
+
* so the handler receives the trimmed values too.
|
|
10
|
+
*
|
|
11
|
+
* Requires `class-transformer` in your project.
|
|
12
|
+
*
|
|
13
|
+
* * * *
|
|
14
|
+
* Example:
|
|
15
|
+
* ```typescript
|
|
16
|
+
* import { Trim } from '@telperion/nest-pack/validation';
|
|
17
|
+
* import { IsNotEmpty, MaxLength } from 'class-validator';
|
|
18
|
+
*
|
|
19
|
+
* export class CreateProjectDto {
|
|
20
|
+
* @Trim()
|
|
21
|
+
* @IsNotEmpty()
|
|
22
|
+
* @MaxLength(80)
|
|
23
|
+
* name!: string;
|
|
24
|
+
* }
|
|
25
|
+
*
|
|
26
|
+
* app.useGlobalPipes(new ValidationPipe({ transform: true }));
|
|
27
|
+
* ```
|
|
28
|
+
* * * *
|
|
29
|
+
* @returns A property decorator
|
|
30
|
+
*/
|
|
31
|
+
declare function Trim(): PropertyDecorator;
|
|
32
|
+
|
|
33
|
+
export { Trim };
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { Transform } from 'class-transformer';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* #### Trim
|
|
5
|
+
*
|
|
6
|
+
* A class-transformer decorator that trims string values when a plain object becomes a DTO
|
|
7
|
+
* (`plainToInstance`, which `ValidationPipe` runs before validating), so `' '` fails `@IsNotEmpty()`
|
|
8
|
+
* and `' a@example.com '` passes `@IsEmail()`. Other values (numbers, `null`, arrays, …) pass unchanged.
|
|
9
|
+
*
|
|
10
|
+
* `ValidationPipe` validates the transformed DTO either way; turn on its `transform` option
|
|
11
|
+
* so the handler receives the trimmed values too.
|
|
12
|
+
*
|
|
13
|
+
* Requires `class-transformer` in your project.
|
|
14
|
+
*
|
|
15
|
+
* * * *
|
|
16
|
+
* Example:
|
|
17
|
+
* ```typescript
|
|
18
|
+
* import { Trim } from '@telperion/nest-pack/validation';
|
|
19
|
+
* import { IsNotEmpty, MaxLength } from 'class-validator';
|
|
20
|
+
*
|
|
21
|
+
* export class CreateProjectDto {
|
|
22
|
+
* @Trim()
|
|
23
|
+
* @IsNotEmpty()
|
|
24
|
+
* @MaxLength(80)
|
|
25
|
+
* name!: string;
|
|
26
|
+
* }
|
|
27
|
+
*
|
|
28
|
+
* app.useGlobalPipes(new ValidationPipe({ transform: true }));
|
|
29
|
+
* ```
|
|
30
|
+
* * * *
|
|
31
|
+
* @returns A property decorator
|
|
32
|
+
*/
|
|
33
|
+
function Trim() {
|
|
34
|
+
return Transform(({ value }) => (typeof value === 'string' ? value.trim() : value));
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export { Trim };
|
|
38
|
+
//# sourceMappingURL=trim.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"trim.js","sources":["../../../../libs/nest-pack/src/validation/trim.ts"],"sourcesContent":[null],"names":[],"mappings":";;AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6BG;SACa,IAAI,GAAA;IAClB,OAAO,SAAS,CAAC,CAAC,EAAE,KAAK,EAAsB,MAAM,OAAO,KAAK,KAAK,QAAQ,GAAG,KAAK,CAAC,IAAI,EAAE,GAAG,KAAK,CAAC,CAAC;AACzG;;;;"}
|