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.
@@ -1 +1 @@
1
- export declare function sanitizeDto<T extends Record<string, unknown>>(dto: T, userRoles: string[]): T;
1
+ export declare function sanitizeDto<T extends object>(dto: T, userRoles: string[]): T;
@@ -7,9 +7,10 @@ export function sanitizeDto(dto, userRoles) {
7
7
  if (!fieldRolesMap)
8
8
  return dto;
9
9
  const hasSuperAdmin = userRoles.some((r) => r === 'super_admin' || r === 'super-admin');
10
+ const dtoRecord = dto;
10
11
  for (const [field, allowedRoles] of Object.entries(fieldRolesMap)) {
11
- if (field in dto && !hasSuperAdmin && !userRoles.some((r) => allowedRoles.includes(r))) {
12
- delete dto[field];
12
+ if (field in dtoRecord && !hasSuperAdmin && !userRoles.some((r) => allowedRoles.includes(r))) {
13
+ delete dtoRecord[field];
13
14
  }
14
15
  }
15
16
  return dto;
@@ -1 +1 @@
1
- {"version":3,"file":"dto-sanitizer.js","sourceRoot":"","sources":["../src/dto-sanitizer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAuB9C,MAAM,UAAU,WAAW,CAAoC,GAAM,EAAE,SAAmB;IACxF,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,cAAc,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,KAAK,MAAM,CAAC,KAAK,EAAE,YAAY,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,aAAa,CAAC,EAAE,CAAC;QAClE,IAAI,KAAK,IAAI,GAAG,IAAI,CAAC,aAAa,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,YAAY,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;YACvF,OAAO,GAAG,CAAC,KAAgB,CAAC,CAAC;QAC/B,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 Record<string, unknown>>(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 for (const [field, allowedRoles] of Object.entries(fieldRolesMap)) {\n if (field in dto && !hasSuperAdmin && !userRoles.some((r) => allowedRoles.includes(r))) {\n delete dto[field as keyof T];\n }\n }\n\n return dto;\n}\n"]}
1
+ {"version":3,"file":"dto-sanitizer.js","sourceRoot":"","sources":["../src/dto-sanitizer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAuB9C,MAAM,UAAU,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,cAAc,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"]}