-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathconfig.ts
More file actions
118 lines (98 loc) · 2.96 KB
/
config.ts
File metadata and controls
118 lines (98 loc) · 2.96 KB
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
import Conf from 'conf'
const dataSchema = {
accountId: { type: 'string' },
accountName: { type: 'string' },
}
const authSchema = {
apiKey: { type: 'string' },
}
const projectSuffix = process.env.CHECKLY_ENV ?? ''
// eslint-disable-next-line no-restricted-syntax
enum Env {
production = 'production',
staging = 'staging',
development = 'development',
local = 'local',
}
class ChecklyConfig {
private _auth?: Conf<{ apiKey: unknown }>
private _data?: Conf<{ accountId: unknown, accountName: unknown }>
// Accessing auth or data will cause a config file to be created.
// We should avoid doing this unless absolutely necessary, since this operation can fail due to file permissions.
get auth () {
// Create this._auth lazily
return this._auth ?? (this._auth = new Conf({
projectName: '@checkly/cli',
configName: 'auth',
projectSuffix,
// @ts-ignore
schema: authSchema,
}))
}
get data () {
// Create this._data lazily
return this._data ?? (this._data = new Conf({
projectName: '@checkly/cli',
configName: 'config',
projectSuffix,
// @ts-ignore
schema: dataSchema,
}))
}
clear () {
this.auth.clear()
this.data.clear()
}
getEnv (): Env {
const environments = ['production', 'development', 'staging', 'local']
const env = process.env.CHECKLY_ENV as string || environments[0] as string
if (!(env in Env)) {
throw new Error('Invalid CHECKLY_ENV')
}
return env as Env
}
getApiKey (): string {
return process.env.CHECKLY_API_KEY || this.auth.get<string>('apiKey') as string || ''
}
getAccountId (): string {
return process.env.CHECKLY_ACCOUNT_ID || this.data.get<string>('accountId') as string || ''
}
hasEnvVarsConfigured (): boolean {
const apiKey = process.env.CHECKLY_API_KEY || ''
const accoundId = process.env.CHECKLY_ACCOUNT_ID || ''
return apiKey !== '' || accoundId !== ''
}
getApiUrl (): string {
// Allow overriding the API URL via CHECKLY_BASE_URL environment variable
if (process.env.CHECKLY_BASE_URL) {
return process.env.CHECKLY_BASE_URL
}
const environments = {
local: 'http://127.0.0.1:3000',
development: 'https://api-dev.checklyhq.com',
staging: 'https://api-test.checklyhq.com',
production: 'https://api.checklyhq.com',
}
return environments[this.getEnv()]!
}
getMqttUrl (): string {
if (process.env.CHECKLY_MQTT_URL) {
return process.env.CHECKLY_MQTT_URL
}
const environments = {
local: 'wss://events-local.checklyhq.com',
development: 'wss://events-dev.checklyhq.com',
staging: 'wss://events-test.checklyhq.com',
production: 'wss://events.checklyhq.com',
}
return environments[this.getEnv()]!
}
hasValidCredentials (): boolean {
if (this.getApiKey() !== '' && this.getAccountId() !== '') {
return true
}
return false
}
}
const config = new ChecklyConfig()
export default config