@heliosjs/middlewares 9.0.11 → 9.0.15

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Naumov Oleh
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.
@@ -0,0 +1,15 @@
1
+ import type { FingerprintComponent } from '@heliosjs/core/types';
2
+ export interface UseFingerprintOptions {
3
+ components?: FingerprintComponent[];
4
+ }
5
+ /**
6
+ * Controller/method decorator that computes the request fingerprint and attaches
7
+ * it to request state (`getState('fingerprint')`) so downstream guards and
8
+ * interceptors can read it. Non-blocking. Optional `components` overrides the
9
+ * configured/default component set for this scope.
10
+ *
11
+ * @example
12
+ * @UseFingerprint()
13
+ * class MyController {}
14
+ */
15
+ export declare function UseFingerprint(options?: UseFingerprintOptions): (target: any, propertyKey?: string, _descriptor?: PropertyDescriptor) => any;
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.UseFingerprint = UseFingerprint;
4
+ const utils_1 = require("@heliosjs/core/utils");
5
+ /**
6
+ * Controller/method decorator that computes the request fingerprint and attaches
7
+ * it to request state (`getState('fingerprint')`) so downstream guards and
8
+ * interceptors can read it. Non-blocking. Optional `components` overrides the
9
+ * configured/default component set for this scope.
10
+ *
11
+ * @example
12
+ * @UseFingerprint()
13
+ * class MyController {}
14
+ */
15
+ function UseFingerprint(options = {}) {
16
+ const middleware = (req, _res, next) => {
17
+ (0, utils_1.getOrComputeFingerprint)(req, options.components);
18
+ return next();
19
+ };
20
+ return function (target, propertyKey, _descriptor) {
21
+ const data = [{ middleware }];
22
+ if (propertyKey) {
23
+ (0, utils_1.defineMiddlewaresMeta)(data, target, propertyKey);
24
+ }
25
+ else {
26
+ (0, utils_1.defineMiddlewaresMeta)(data, target);
27
+ }
28
+ return target;
29
+ };
30
+ }
31
+ //# sourceMappingURL=fingerprint.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fingerprint.js","sourceRoot":"","sources":["../src/fingerprint.ts"],"names":[],"mappings":";;AAoBA,wCAeC;AA/BD,gDAAsF;AAMtF;;;;;;;;;GASG;AACH,SAAgB,cAAc,CAAC,UAAiC,EAAE;IAChE,MAAM,UAAU,GAAiB,CAAC,GAAG,EAAE,IAAI,EAAE,IAAI,EAAE,EAAE;QACnD,IAAA,+BAAuB,EAAC,GAAG,EAAE,OAAO,CAAC,UAAU,CAAC,CAAC;QACjD,OAAO,IAAI,EAAE,CAAC;IAChB,CAAC,CAAC;IAEF,OAAO,UAAU,MAAW,EAAE,WAAoB,EAAE,WAAgC;QAClF,MAAM,IAAI,GAAG,CAAC,EAAE,UAAU,EAAE,CAAC,CAAC;QAC9B,IAAI,WAAW,EAAE,CAAC;YAChB,IAAA,6BAAqB,EAAC,IAAI,EAAE,MAAM,EAAE,WAAW,CAAC,CAAC;QACnD,CAAC;aAAM,CAAC;YACN,IAAA,6BAAqB,EAAC,IAAI,EAAE,MAAM,CAAC,CAAC;QACtC,CAAC;QACD,OAAO,MAAM,CAAC;IAChB,CAAC,CAAC;AACJ,CAAC"}
package/dist/index.d.ts CHANGED
@@ -1,9 +1,11 @@
1
1
  import 'reflect-metadata';
2
2
  export * from './catch';
3
3
  export * from './cors';
4
+ export * from './fingerprint';
4
5
  export * from './guard';
5
6
  export * from './interceptor';
6
7
  export * from './pipe';
8
+ export * from './roles';
7
9
  export * from './sanitize';
8
10
  export * from './status';
9
11
  export * from './use';
package/dist/index.js CHANGED
@@ -17,9 +17,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
17
17
  require("reflect-metadata");
18
18
  __exportStar(require("./catch"), exports);
19
19
  __exportStar(require("./cors"), exports);
20
+ __exportStar(require("./fingerprint"), exports);
20
21
  __exportStar(require("./guard"), exports);
21
22
  __exportStar(require("./interceptor"), exports);
22
23
  __exportStar(require("./pipe"), exports);
24
+ __exportStar(require("./roles"), exports);
23
25
  __exportStar(require("./sanitize"), exports);
24
26
  __exportStar(require("./status"), exports);
25
27
  __exportStar(require("./use"), exports);
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,4BAA0B;AAE1B,0CAAwB;AACxB,yCAAuB;AACvB,0CAAwB;AACxB,gDAA8B;AAC9B,yCAAuB;AACvB,6CAA2B;AAC3B,2CAAyB;AACzB,wCAAsB"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,4BAA0B;AAE1B,0CAAwB;AACxB,yCAAuB;AACvB,gDAA8B;AAC9B,0CAAwB;AACxB,gDAA8B;AAC9B,yCAAuB;AACvB,0CAAwB;AACxB,6CAA2B;AAC3B,2CAAyB;AACzB,wCAAsB"}
@@ -0,0 +1,30 @@
1
+ import type { GuardFunction } from '@heliosjs/core/types';
2
+ export type RoleMode = 'any' | 'all';
3
+ export interface RolesOptions {
4
+ mode?: RoleMode;
5
+ message?: string;
6
+ }
7
+ type RolesArg = string | string[];
8
+ export declare function matchRoles(required: string[], userRoles: string[], mode: RoleMode): boolean;
9
+ export declare function normalizeArgs(args: (RolesArg | RolesOptions)[]): {
10
+ roles: string[];
11
+ options: RolesOptions;
12
+ };
13
+ export declare function createRolesGuard(required: string[], options: RolesOptions): GuardFunction;
14
+ /**
15
+ * Restrict a controller or route to users holding the required role(s).
16
+ *
17
+ * Roles are read through the extractor configured via `@Server({ rbac })`.
18
+ * Default match mode is ANY (the user needs at least one listed role); pass
19
+ * `{ mode: 'all' }` to require every role. A failing check throws
20
+ * `ForbiddenError` with `options.message` (default `"Insufficient role"`).
21
+ *
22
+ * @example
23
+ * @Roles('admin')
24
+ * @example
25
+ * @Roles('admin', 'editor') // ANY
26
+ * @example
27
+ * @Roles(['admin', 'editor'], { mode: 'all' }) // ALL
28
+ */
29
+ export declare function Roles(...args: (RolesArg | RolesOptions)[]): (target: any, propertyKey?: string, descriptor?: PropertyDescriptor) => void;
30
+ export {};
package/dist/roles.js ADDED
@@ -0,0 +1,72 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.matchRoles = matchRoles;
4
+ exports.normalizeArgs = normalizeArgs;
5
+ exports.createRolesGuard = createRolesGuard;
6
+ exports.Roles = Roles;
7
+ const utils_1 = require("@heliosjs/core/utils");
8
+ function matchRoles(required, userRoles, mode) {
9
+ return mode === 'all'
10
+ ? required.every((role) => userRoles.includes(role))
11
+ : required.some((role) => userRoles.includes(role));
12
+ }
13
+ function normalizeArgs(args) {
14
+ let options = {};
15
+ let roleArgs = args;
16
+ const last = args[args.length - 1];
17
+ const isOptions = typeof last === 'object' && last !== null && !Array.isArray(last);
18
+ if (isOptions) {
19
+ options = last;
20
+ roleArgs = args.slice(0, -1);
21
+ }
22
+ const roles = roleArgs.flatMap((arg) => {
23
+ if (Array.isArray(arg))
24
+ return arg;
25
+ if (typeof arg === 'string')
26
+ return [arg];
27
+ return [];
28
+ });
29
+ return { roles, options };
30
+ }
31
+ function createRolesGuard(required, options) {
32
+ const mode = options.mode ?? 'any';
33
+ const message = options.message ?? 'Insufficient role';
34
+ return async (req) => {
35
+ const extractor = (0, utils_1.getRolesExtractor)();
36
+ if (!extractor) {
37
+ throw new utils_1.InvalidStateError('RBAC extractor not set; configure rbac.getRoles in @Server');
38
+ }
39
+ const raw = await extractor(req);
40
+ const userRoles = raw == null ? [] : Array.isArray(raw) ? raw : [raw];
41
+ return matchRoles(required, userRoles, mode) ? true : message;
42
+ };
43
+ }
44
+ /**
45
+ * Restrict a controller or route to users holding the required role(s).
46
+ *
47
+ * Roles are read through the extractor configured via `@Server({ rbac })`.
48
+ * Default match mode is ANY (the user needs at least one listed role); pass
49
+ * `{ mode: 'all' }` to require every role. A failing check throws
50
+ * `ForbiddenError` with `options.message` (default `"Insufficient role"`).
51
+ *
52
+ * @example
53
+ * @Roles('admin')
54
+ * @example
55
+ * @Roles('admin', 'editor') // ANY
56
+ * @example
57
+ * @Roles(['admin', 'editor'], { mode: 'all' }) // ALL
58
+ */
59
+ function Roles(...args) {
60
+ const { roles, options } = normalizeArgs(args);
61
+ const guard = createRolesGuard(roles, options);
62
+ return function (target, propertyKey, descriptor) {
63
+ const data = [{ guard }];
64
+ if (descriptor) {
65
+ (0, utils_1.defineMiddlewaresMeta)(data, target, propertyKey);
66
+ }
67
+ else {
68
+ (0, utils_1.defineMiddlewaresMeta)(data, target);
69
+ }
70
+ };
71
+ }
72
+ //# sourceMappingURL=roles.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"roles.js","sourceRoot":"","sources":["../src/roles.ts"],"names":[],"mappings":";;AAYA,gCAQC;AAED,sCAsBC;AAED,4CAoBC;AAiBD,sBAaC;AA/FD,gDAAmG;AAWnG,SAAgB,UAAU,CACxB,QAAkB,EAClB,SAAmB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3
- "version": "9.0.11",
3
+ "version": "9.0.15",
4
4
  "description": "Middlewares for @heliosjs",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -15,7 +15,8 @@
15
15
  },
16
16
  "files": [
17
17
  "dist/*",
18
- "README.md"
18
+ "README.md",
19
+ "LICENSE"
19
20
  ],
20
21
  "scripts": {
21
22
  "build": "yarn clean && tsc -p tsconfig.build.json",
@@ -29,9 +30,10 @@
29
30
  "reflect-metadata": "^0.2.2"
30
31
  },
31
32
  "peerDependencies": {
32
- "@heliosjs/core": "^3.1.12",
33
- "typescript": ">=4.0.0",
33
+ "@heliosjs/core": "^3.1.16",
34
+ "@types/node": ">=20.0.0",
34
35
  "reflect-metadata": "^0.2.2",
35
- "@types/node": ">=18.0.0"
36
- }
36
+ "typescript": ">=5.0.0"
37
+ },
38
+ "devDependencies": {}
37
39
  }