forked from ehacke/ts-di-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.ts
120 lines (94 loc) · 2.2 KB
/
config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import { ValidatedBase } from 'validated-base';
import { IsBoolean, IsInstance, IsNumber, IsOptional, IsString, ValidateNested } from 'class-validator';
/* eslint-disable require-jsdoc */
interface GcpConfigInterface {
serviceAccountPath: string;
projectId: string;
pubsubEmulator: boolean;
databaseURL?: string;
}
/**
* @class
*/
class GcpConfig extends ValidatedBase implements GcpConfigInterface {
constructor(params: GcpConfigInterface, validate = true) {
super();
this.serviceAccountPath = params.serviceAccountPath;
this.projectId = params.projectId;
this.databaseURL = params.databaseURL;
this.pubsubEmulator = params.pubsubEmulator;
if (validate) {
this.validate();
}
}
@IsString()
serviceAccountPath: string;
@IsString()
projectId: string;
@IsOptional()
@IsString()
databaseURL?: string;
@IsBoolean()
pubsubEmulator: boolean;
}
interface RedisConfigInterface {
host: string;
port: number;
ttlSec: number;
}
/**
* @class
*/
class RedisConfig extends ValidatedBase implements RedisConfigInterface {
constructor(params: RedisConfigInterface, validate = true) {
super();
this.host = params.host;
this.port = params.port;
this.ttlSec = params.ttlSec;
if (validate) {
this.validate();
}
}
@IsString()
host: string;
@IsNumber()
port: number;
@IsNumber()
ttlSec: number;
}
export interface ConfigInterface {
port: number;
name: string;
version: string;
gcp: GcpConfigInterface;
redis: RedisConfigInterface;
}
/**
* @class
*/
export class Config extends ValidatedBase implements ConfigInterface {
constructor(params: ConfigInterface, validate = true) {
super();
this.gcp = new GcpConfig(params.gcp, false);
this.port = params.port;
this.name = params.name;
this.version = params.version;
this.redis = new RedisConfig(params.redis, false);
if (validate) {
this.validate();
}
}
@IsNumber()
port: number;
@IsString()
name: string;
@IsString()
version: string;
@IsInstance(RedisConfig)
@ValidateNested()
redis: RedisConfigInterface;
@IsInstance(GcpConfig)
@ValidateNested()
gcp: GcpConfigInterface;
}
/* eslint-enable require-jsdoc */