Passed
Push — master ( 24a438...ac8bb2 )
by Sergey
04:17
created

RBAcModule.useCache   A

Complexity

Conditions 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 11
rs 9.95
c 0
b 0
f 0
cc 1
1
import { DynamicModule, Global, Module } from '@nestjs/common';
2
import { RbacService } from './services/rbac.service';
3
import { ModuleRef, Reflector } from '@nestjs/core';
4
import { StorageRbacService } from './services/storage.rbac.service';
5
import { IStorageRbac } from './interfaces/storage.rbac.interface';
6
import { IDynamicStorageRbac } from './interfaces/dynamic.storage.rbac.interface';
7
import { ICacheRBAC } from './interfaces/cache.rbac.interface';
8
9
@Global()
10
@Module({
11
  providers: [
12
    RbacService,
13
    StorageRbacService,
14
    Reflector,
15
  ],
16
  imports: [],
17
  exports: [
18
    RbacService,
19
  ],
20
})
21
export class RBAcModule {
22
  private static cache?: any | ICacheRBAC;
23
  private static cacheOptions?: { KEY?: string, TTL?: number };
24
25
  static useCache(
26
    cache: any | ICacheRBAC,
27
    options?: {
28
      KEY?: string,
29
      TTL?: number
30
    },
31
  ) {
32
    RBAcModule.cache = cache;
33
    RBAcModule.cacheOptions = options;
34
    return RBAcModule;
35
  }
36
37
  static forRoot(
38
    rbac: IStorageRbac,
39
    providers?: any[],
40
    imports?: any[],
41
  ): DynamicModule {
42
43
    return RBAcModule.forDynamic(
44
      /* tslint:disable */
45
      class {
46
        async getRbac(): Promise<IStorageRbac> {
47
          return rbac;
48
        };
49
      },
50
      providers,
51
      imports,
52
    );
53
  }
54
55
  static forDynamic(
56
    rbac: new () => IDynamicStorageRbac,
57
    providers?: any[],
58
    imports?: any[],
59
  ): DynamicModule {
60
    const inject = [ModuleRef, rbac];
61
    const commonProviders = [];
62
    if (RBAcModule.cache) {
63
      commonProviders.push(RBAcModule.cache,{
64
        provide: 'ICacheRBAC',
65
        useFactory: (cache: ICacheRBAC): ICacheRBAC => {
66
          return RBAcModule.setCacheOptions(cache);
67
        },
68
        inject: [RBAcModule.cache],
69
      });
70
      inject.push(RBAcModule.cache);
71
    }
72
73
    commonProviders.push(...[
74
      ...(providers || []),
75
      rbac,
76
      {
77
        provide: StorageRbacService,
78
        useFactory: async (moduleRef: ModuleRef, rbacService: IDynamicStorageRbac, cache?: ICacheRBAC) => {
79
          return new StorageRbacService(moduleRef, rbacService, RBAcModule.setCacheOptions(cache));
80
        },
81
        inject,
82
      },
83
    ]);
84
85
    return {
86
      module: RBAcModule,
87
      providers: commonProviders,
88
      imports: [
89
        ...(imports || []),
90
      ],
91
    };
92
  }
93
94
  private static setCacheOptions(cache?: ICacheRBAC) {
95
    if (!cache || RBAcModule.cacheOptions) {
96
      return cache;
97
    }
98
    if (!RBAcModule.cacheOptions) {
99
      return cache;
100
    }
101
    if (RBAcModule.cacheOptions.KEY) {
102
      cache.KEY = RBAcModule.cacheOptions.KEY;
103
    }
104
105
    if (RBAcModule.cacheOptions.TTL) {
106
      cache.TTL = RBAcModule.cacheOptions.TTL;
107
    }
108
109
    return cache;
110
  }
111
}
112