denwa-nest-shared 1.2.3 → 1.2.4
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 +2 -0
- package/cjs/dto-sanitizer.d.ts +1 -1
- package/cjs/dto-sanitizer.js +3 -2
- package/cjs/dto-sanitizer.js.map +1 -1
- package/cjs/tsconfig.cjs.tsbuildinfo +1 -1
- package/dist/dto-sanitizer.d.ts +1 -1
- package/dist/dto-sanitizer.js +3 -2
- package/dist/dto-sanitizer.js.map +1 -1
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
|
4
4
|
|
|
5
|
+
### [1.2.4](https://github.com/Denwa799/nest-shared/compare/v1.2.3...v1.2.4) (2026-07-14)
|
|
6
|
+
|
|
5
7
|
### [1.2.3](https://github.com/Denwa799/nest-shared/compare/v1.2.2...v1.2.3) (2026-07-14)
|
|
6
8
|
|
|
7
9
|
### [1.2.2](https://github.com/Denwa799/nest-shared/compare/v1.2.1...v1.2.2) (2026-07-09)
|
package/cjs/dto-sanitizer.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare function sanitizeDto<T extends
|
|
1
|
+
export declare function sanitizeDto<T extends object>(dto: T, userRoles: string[]): T;
|
package/cjs/dto-sanitizer.js
CHANGED
|
@@ -10,9 +10,10 @@ function sanitizeDto(dto, userRoles) {
|
|
|
10
10
|
if (!fieldRolesMap)
|
|
11
11
|
return dto;
|
|
12
12
|
const hasSuperAdmin = userRoles.some((r) => r === 'super_admin' || r === 'super-admin');
|
|
13
|
+
const dtoRecord = dto;
|
|
13
14
|
for (const [field, allowedRoles] of Object.entries(fieldRolesMap)) {
|
|
14
|
-
if (field in
|
|
15
|
-
delete
|
|
15
|
+
if (field in dtoRecord && !hasSuperAdmin && !userRoles.some((r) => allowedRoles.includes(r))) {
|
|
16
|
+
delete dtoRecord[field];
|
|
16
17
|
}
|
|
17
18
|
}
|
|
18
19
|
return dto;
|
package/cjs/dto-sanitizer.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dto-sanitizer.js","sourceRoot":"","sources":["../src/dto-sanitizer.ts"],"names":[],"mappings":";;AAuBA,
|
|
1
|
+
{"version":3,"file":"dto-sanitizer.js","sourceRoot":"","sources":["../src/dto-sanitizer.ts"],"names":[],"mappings":";;AAuBA,kCAsBC;AA7CD,6CAA8C;AAuB9C,SAAgB,WAAW,CAAmB,GAAM,EAAE,SAAmB;IACvE,MAAM,SAAS,GAAG,MAAM,CAAC,cAAc,CAAC,GAAG,CAAkB,CAAC;IAC9D,IAAI,CAAC,SAAS,IAAI,SAAS,KAAK,MAAM,CAAC,SAAS;QAAE,OAAO,GAAG,CAAC;IAE7D,MAAM,aAAa,GAAyC,OAAO,CAAC,WAAW,CAC7E,2BAAc,EACd,SAAS,CAC8B,CAAC;IAE1C,IAAI,CAAC,aAAa;QAAE,OAAO,GAAG,CAAC;IAE/B,MAAM,aAAa,GAAG,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,KAAK,aAAa,IAAI,CAAC,KAAK,aAAa,CAAC,CAAC;IAExF,MAAM,SAAS,GAAG,GAA8B,CAAC;IAEjD,KAAK,MAAM,CAAC,KAAK,EAAE,YAAY,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,aAAa,CAAC,EAAE,CAAC;QAClE,IAAI,KAAK,IAAI,SAAS,IAAI,CAAC,aAAa,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,YAAY,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;YAC7F,OAAO,SAAS,CAAC,KAAK,CAAC,CAAC;QAC1B,CAAC;IACH,CAAC;IAED,OAAO,GAAG,CAAC;AACb,CAAC","sourcesContent":["import { MUTABLE_BY_KEY } from './decorators';\n\n/**\n * Strips DTO fields that the current user is not allowed to edit,\n * based on `@MutableBy()` decorators on the DTO class.\n *\n * Fields without `@MutableBy()` are considered public — no restriction.\n * If the user has `super_admin` role, all fields pass through.\n *\n * @example\n * ```ts\n * // DTO\n * class UpdateProductDto {\n * \\@MutableBy([ROLES.admin])\n * priority?: number;\n *\n * nameRU: string;\n * }\n *\n * // Resolver\n * sanitizeDto(data, ctx.req?.user?.roles ?? []);\n * ```\n */\nexport function sanitizeDto<T extends object>(dto: T, userRoles: string[]): T {\n const prototype = Object.getPrototypeOf(dto) as object | null;\n if (!prototype || prototype === Object.prototype) return dto;\n\n const fieldRolesMap: Record<string, string[]> | undefined = Reflect.getMetadata(\n MUTABLE_BY_KEY,\n prototype,\n ) as Record<string, string[]> | undefined;\n\n if (!fieldRolesMap) return dto;\n\n const hasSuperAdmin = userRoles.some((r) => r === 'super_admin' || r === 'super-admin');\n\n const dtoRecord = dto as Record<string, unknown>;\n\n for (const [field, allowedRoles] of Object.entries(fieldRolesMap)) {\n if (field in dtoRecord && !hasSuperAdmin && !userRoles.some((r) => allowedRoles.includes(r))) {\n delete dtoRecord[field];\n }\n }\n\n return dto;\n}\n"]}
|