@flusys/nestjs-shared 9.1.1 → 9.2.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.
Files changed (46) hide show
  1. package/README.md +119 -18
  2. package/classes/api-controller.class.d.ts +1 -0
  3. package/classes/api-service.class.d.ts +7 -2
  4. package/classes/hybrid-cache.class.d.ts +17 -4
  5. package/constants/index.d.ts +1 -0
  6. package/constants/permissions.d.ts +9 -0
  7. package/fesm/183.js +7 -7
  8. package/fesm/47.js +1520 -0
  9. package/fesm/470.js +333 -382
  10. package/fesm/667.js +17 -17
  11. package/fesm/681.js +326 -129
  12. package/fesm/{948.js → 997.js} +45 -23
  13. package/fesm/chunks/123.js +17 -9
  14. package/fesm/chunks/37.js +5 -5
  15. package/fesm/classes/index.js +4 -3
  16. package/fesm/constants/index.js +18 -7
  17. package/fesm/decorators/index.js +120 -39
  18. package/fesm/dtos/index.js +1 -1
  19. package/fesm/entities/index.js +8 -8
  20. package/fesm/enums/index.js +2 -2
  21. package/fesm/exceptions/index.js +12 -12
  22. package/fesm/guards/index.js +201 -68
  23. package/fesm/index.js +95 -42
  24. package/fesm/interceptors/index.js +290 -590
  25. package/fesm/interfaces/index.js +97 -8
  26. package/fesm/middlewares/index.js +4 -4
  27. package/fesm/modules/index.js +2 -2
  28. package/fesm/pipes/index.js +8 -8
  29. package/fesm/utils/index.js +150 -478
  30. package/interceptors/idempotency.interceptor.d.ts +14 -2
  31. package/interceptors/slug.interceptor.d.ts +1 -0
  32. package/interfaces/expression.interface.d.ts +45 -0
  33. package/interfaces/index.d.ts +1 -0
  34. package/interfaces/permission-action-registry.interface.d.ts +1 -0
  35. package/interfaces/permission.interface.d.ts +1 -0
  36. package/modules/shared-permission-cache/shared-permission-cache.service.d.ts +3 -1
  37. package/modules/utils/utils.service.d.ts +6 -6
  38. package/package.json +3 -2
  39. package/utils/date-time.util.d.ts +0 -1
  40. package/utils/event-pattern.util.d.ts +1 -0
  41. package/utils/expression.util.d.ts +35 -0
  42. package/utils/index.d.ts +2 -0
  43. package/utils/permission-cache-key.util.d.ts +3 -0
  44. package/utils/query-helpers.util.d.ts +2 -2
  45. package/utils/safe-regex.util.d.ts +10 -0
  46. package/fesm/270.js +0 -659
@@ -1,10 +1,22 @@
1
1
  import { type CallHandler, type ExecutionContext, type NestInterceptor } from '@nestjs/common';
2
2
  import { Observable } from 'rxjs';
3
3
  import { HybridCache } from '../classes/hybrid-cache.class.js';
4
+ interface IIdempotentRequest {
5
+ headers: Record<string, string | string[] | undefined>;
6
+ method?: string;
7
+ originalUrl?: string;
8
+ url?: string;
9
+ route?: {
10
+ path?: string;
11
+ };
12
+ user?: {
13
+ id?: string;
14
+ } | null;
15
+ }
16
+ export declare function buildIdempotencyCacheKey(request: IIdempotentRequest, idempotencyKey: string): string;
4
17
  export declare class IdempotencyInterceptor implements NestInterceptor {
5
18
  private readonly cache?;
6
- private readonly CACHE_TTL;
7
- private readonly PROCESSING_MARKER;
8
19
  constructor(cache?: HybridCache | undefined);
9
20
  intercept<T = any>(context: ExecutionContext, next: CallHandler<T>): Promise<Observable<T>>;
10
21
  }
22
+ export {};
@@ -5,4 +5,5 @@ export declare class Slug implements NestInterceptor {
5
5
  private readonly utilsService;
6
6
  constructor(utilsService: UtilsService);
7
7
  intercept<T = any>(context: ExecutionContext, next: CallHandler<T>): Observable<T>;
8
+ private withSlug;
8
9
  }
@@ -0,0 +1,45 @@
1
+ export declare const EXPRESSION_ARITHMETIC_OPS: readonly ["sum", "subtract", "multiply", "divide", "modulo", "power", "average", "min", "max"];
2
+ export type ExpressionArithmeticOp = (typeof EXPRESSION_ARITHMETIC_OPS)[number];
3
+ export declare const EXPRESSION_COMPARISONS: readonly ["equals", "not_equals", "greater_than", "less_than", "greater_or_equal", "less_or_equal", "contains", "not_contains", "starts_with", "ends_with", "in", "not_in", "is_empty", "is_not_empty", "is_true", "is_false", "matches", "not_matches"];
4
+ export type ExpressionComparison = (typeof EXPRESSION_COMPARISONS)[number];
5
+ export declare const UNARY_EXPRESSION_COMPARISONS: readonly ExpressionComparison[];
6
+ export declare const EXPRESSION_DURATION_UNITS: readonly ["second", "minute", "hour", "day", "week", "month", "year"];
7
+ export type ExpressionDurationUnit = (typeof EXPRESSION_DURATION_UNITS)[number];
8
+ export declare const EXPRESSION_DATE_PARTS: readonly ["year", "month", "day", "weekday", "hour", "minute"];
9
+ export type ExpressionDatePart = (typeof EXPRESSION_DATE_PARTS)[number];
10
+ export interface IExpressionCondition {
11
+ left: Expression;
12
+ comparison: ExpressionComparison;
13
+ right?: Expression;
14
+ }
15
+ export interface IExpressionConditionGroup {
16
+ operator: 'AND' | 'OR';
17
+ conditions: Array<IExpressionCondition | IExpressionConditionGroup>;
18
+ }
19
+ export type Expression = {
20
+ type: 'literal';
21
+ value: string | number | boolean | null;
22
+ } | {
23
+ type: 'field_ref';
24
+ ref: string;
25
+ } | {
26
+ type: 'arithmetic';
27
+ op: ExpressionArithmeticOp;
28
+ operands: Expression[];
29
+ } | {
30
+ type: 'concat';
31
+ parts: Expression[];
32
+ separator?: string;
33
+ } | {
34
+ type: 'template';
35
+ template: string;
36
+ } | {
37
+ type: 'function';
38
+ name: string;
39
+ args: Expression[];
40
+ } | {
41
+ type: 'if';
42
+ condition: IExpressionConditionGroup;
43
+ whenTrue: Expression;
44
+ whenFalse?: Expression;
45
+ };
@@ -6,6 +6,7 @@ export * from './datasource.interface.js';
6
6
  export * from './email-adapter.interface.js';
7
7
  export * from './event-bus.interface.js';
8
8
  export * from './event-manager-adapter.interface.js';
9
+ export * from './expression.interface.js';
9
10
  export * from './identity.interface.js';
10
11
  export * from './logged-user-info.interface.js';
11
12
  export * from './message-ref.interface.js';
@@ -9,4 +9,5 @@ export interface IPermissionActionInput {
9
9
  export interface IPermissionActionRegistry {
10
10
  registerActions(actions: IPermissionActionInput[]): Promise<void>;
11
11
  removeActionsByCodes(codes: string[]): Promise<void>;
12
+ findMissingCodes?(codes: string[]): Promise<string[]>;
12
13
  }
@@ -18,4 +18,5 @@ export interface PermissionGuardConfig {
18
18
  enableCompanyFeature?: boolean;
19
19
  userPermissionKeyFormat?: string;
20
20
  companyPermissionKeyFormat?: string;
21
+ multiTenant?: boolean;
21
22
  }
@@ -1,10 +1,12 @@
1
1
  import { HybridCache } from '../../classes/hybrid-cache.class.js';
2
2
  import type { ILogicNode, PermissionGuardConfig } from '../../interfaces/index.js';
3
+ import { type IPermissionResolver } from '../../interfaces/permission-resolver.interface.js';
3
4
  import { type PermissionCacheKeyOptions } from '../../utils/permission-cache-key.util.js';
4
5
  export declare class SharedPermissionCacheService {
5
6
  private readonly cache?;
7
+ private readonly resolver?;
6
8
  private readonly config;
7
- constructor(cache?: HybridCache | undefined, config?: PermissionGuardConfig);
9
+ constructor(cache?: HybridCache | undefined, config?: PermissionGuardConfig, resolver?: IPermissionResolver | undefined);
8
10
  getPermissions(options: PermissionCacheKeyOptions): Promise<string[]>;
9
11
  setPermissions(options: PermissionCacheKeyOptions, permissions: string[], ttl?: number): Promise<void>;
10
12
  clearPermissions(options: PermissionCacheKeyOptions): Promise<void>;
@@ -1,10 +1,10 @@
1
1
  import { HybridCache } from '../../classes/hybrid-cache.class.js';
2
2
  export declare class UtilsService {
3
- getCacheKey(entityName: string, params: any, entityId?: string, tenantId?: string): string;
4
- trackCacheKey(cacheKey: string, entityName: string, cacheManager: HybridCache, entityId?: string, tenantId?: string): Promise<void>;
5
- clearCache(entityName: string, cacheManager: HybridCache, entityId?: string, tenantId?: string): Promise<void>;
6
- transformToSlug(value: string, salt?: boolean): string;
7
- getRandomInt(min: number, max: number): number;
3
+ private readonly logger;
4
+ getCacheStamp(cacheManager: HybridCache, entityNames: string[], tenantId?: string): Promise<string>;
5
+ getCacheKey(entityName: string, params: unknown, entityId: string | undefined, tenantId: string | undefined, stamp: string): string;
6
+ clearCache(entityName: string, cacheManager: HybridCache, tenantId?: string): Promise<void>;
7
+ getStampName(entityName: string, tenantId?: string): string;
8
+ transformToSlug(value: string): string;
8
9
  private buildTenantPrefix;
9
- private buildTrackingKey;
10
10
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flusys/nestjs-shared",
3
- "version": "9.1.1",
3
+ "version": "9.2.0",
4
4
  "description": "Common shared utilities for Flusys NestJS applications",
5
5
  "type": "module",
6
6
  "main": "./fesm/index.js",
@@ -104,6 +104,7 @@
104
104
  "rxjs": "^7.8.0"
105
105
  },
106
106
  "dependencies": {
107
- "@flusys/nestjs-core": "9.1.1"
107
+ "@flusys/nestjs-core": "9.2.0",
108
+ "re2js": "^2.8.6"
108
109
  }
109
110
  }
@@ -5,4 +5,3 @@ export declare function utcToLocal(utcDate: Date | string, timezone: string): {
5
5
  display: string;
6
6
  };
7
7
  export declare function formatInTimezone(utcDate: Date | string, timezone: string, options?: Intl.DateTimeFormatOptions): string;
8
- export declare function getTodayInTimezone(timezone: string): string;
@@ -1,3 +1,4 @@
1
1
  export declare function matchEventPattern(pattern: string, eventName: string): boolean;
2
2
  export declare function isMultiWildcard(segment: string): boolean;
3
3
  export declare function sanitizeEventPayload<T>(value: T): T;
4
+ export declare function isSensitiveKey(key: string): boolean;
@@ -0,0 +1,35 @@
1
+ import { type Expression, type IExpressionConditionGroup } from '../interfaces/expression.interface.js';
2
+ export declare const EXPRESSION_LIMITS: {
3
+ readonly MAX_DEPTH: 16;
4
+ readonly MAX_NODES: 500;
5
+ readonly MAX_TEMPLATE_LENGTH: 4000;
6
+ readonly MAX_TEXT_LENGTH: 100000;
7
+ readonly MAX_PAD_LENGTH: 1000;
8
+ };
9
+ export type ExpressionErrorCode = 'invalid_node' | 'unknown_function' | 'wrong_arg_count' | 'unknown_comparison' | 'unknown_ref' | 'too_deep' | 'too_many_nodes' | 'template_too_long' | 'text_too_long' | 'expected_number' | 'expected_date' | 'division_by_zero' | 'invalid_unit' | 'invalid_date_part' | 'regex_too_long' | 'regex_invalid' | 'regex_too_complex' | 'regex_input_too_long';
10
+ export declare class ExpressionError extends Error {
11
+ readonly code: ExpressionErrorCode;
12
+ readonly variables: Record<string, string | number>;
13
+ constructor(code: ExpressionErrorCode, variables?: Record<string, string | number>);
14
+ }
15
+ export declare function isExpressionError(error: unknown): error is ExpressionError;
16
+ export interface IExpressionFunction {
17
+ minArgs: number;
18
+ maxArgs?: number;
19
+ regexArgs?: readonly number[];
20
+ call(args: unknown[], ctx: IExpressionContext): unknown;
21
+ }
22
+ export interface IExpressionContext {
23
+ resolveRef(ref: string): unknown;
24
+ now?: () => Date;
25
+ }
26
+ export interface IExpressionValidationOptions {
27
+ isKnownRef?: (ref: string) => boolean;
28
+ }
29
+ export declare function expressionToText(value: unknown): string | null;
30
+ export declare function expressionToNumber(value: unknown): number | null;
31
+ export declare const EXPRESSION_FUNCTIONS: Readonly<Record<string, IExpressionFunction>>;
32
+ export declare function evaluateExpression(expression: Expression, ctx: IExpressionContext): unknown;
33
+ export declare function evaluateConditionGroup(group: IExpressionConditionGroup, ctx: IExpressionContext): boolean;
34
+ export declare function assertValidExpression(expression: unknown, options?: IExpressionValidationOptions): void;
35
+ export declare function assertValidConditionGroup(group: unknown, options?: IExpressionValidationOptions): void;
package/utils/index.d.ts CHANGED
@@ -2,6 +2,7 @@ export * from './branch-hierarchy.util.js';
2
2
  export * from './cache-tenant.util.js';
3
3
  export * from './date-time.util.js';
4
4
  export * from './event-pattern.util.js';
5
+ export * from './expression.util.js';
5
6
  export * from './html-sanitizer.util.js';
6
7
  export * from './message-ref.util.js';
7
8
  export * from './optional-module.util.js';
@@ -9,5 +10,6 @@ export * from './permission-cache-key.util.js';
9
10
  export * from './permission-match.util.js';
10
11
  export * from './query-helpers.util.js';
11
12
  export * from './request.util.js';
13
+ export * from './safe-regex.util.js';
12
14
  export * from './string.util.js';
13
15
  export * from './token.util.js';
@@ -1,3 +1,4 @@
1
+ import { type HybridCache } from '../classes/hybrid-cache.class.js';
1
2
  import { type PermissionGuardConfig } from '../interfaces/permission.interface.js';
2
3
  export interface PermissionCacheKeyOptions {
3
4
  userId: string;
@@ -7,3 +8,5 @@ export interface PermissionCacheKeyOptions {
7
8
  export declare const DEFAULT_USER_PERMISSION_KEY_FORMAT = "my-permissions:user:{userId}";
8
9
  export declare const DEFAULT_COMPANY_PERMISSION_KEY_FORMAT = "my-permissions:company:{companyId}:branch:{branchId}:user:{userId}";
9
10
  export declare function buildPermissionCacheKey(options: PermissionCacheKeyOptions, config?: PermissionGuardConfig): string;
11
+ export declare function resolvePermissionCacheKey(cache: Pick<HybridCache, 'getStamp'>, options: PermissionCacheKeyOptions, config?: PermissionGuardConfig): Promise<string>;
12
+ export declare function invalidatePermissionCache(cache: Pick<HybridCache, 'bumpStamp'>, userId: string, config?: PermissionGuardConfig): Promise<void>;
@@ -1,4 +1,4 @@
1
- import { type ObjectLiteral, type QueryRunner, SelectQueryBuilder } from 'typeorm';
1
+ import { type FindOperator, type ObjectLiteral, type QueryRunner, SelectQueryBuilder } from 'typeorm';
2
2
  import { type ILoggedUserInfo } from '../interfaces/index.js';
3
3
  export interface ICompanyFilterConfig {
4
4
  isCompanyFeatureEnabled: boolean;
@@ -13,7 +13,7 @@ export interface IBranchFilterConfig {
13
13
  }
14
14
  export declare function applyBranchFilter<T extends ObjectLiteral>(query: SelectQueryBuilder<T>, config: IBranchFilterConfig, branchIds: ReadonlyArray<string | null | undefined>): SelectQueryBuilder<T>;
15
15
  export declare function buildCompanyWhereCondition<T extends Record<string, unknown>>(baseWhere: T, isCompanyFeatureEnabled: boolean, user: ILoggedUserInfo | null | undefined): T & {
16
- companyId?: string;
16
+ companyId?: string | FindOperator<string>;
17
17
  };
18
18
  export interface ICompanyEnabled {
19
19
  companyId?: string | null;
@@ -0,0 +1,10 @@
1
+ import { RE2JS } from 're2js';
2
+ export declare const SAFE_REGEX_LIMITS: {
3
+ readonly MAX_PATTERN_LENGTH: 500;
4
+ readonly MAX_INPUT_LENGTH: 10000;
5
+ readonly MAX_REPEAT: 100;
6
+ readonly MAX_REPEAT_EXPANSION: 300;
7
+ };
8
+ export type SafeRegexProblem = 'too_long' | 'invalid' | 'too_complex';
9
+ export declare function safeRegexProblem(pattern: string): SafeRegexProblem | null;
10
+ export declare function compileSafeRegex(pattern: string): RE2JS;