-
Notifications
You must be signed in to change notification settings - Fork 101
/
config.ts
94 lines (90 loc) · 2.61 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
import { CorsOptions } from 'apollo-server-express'
import { IntrospectionNotPermitted, MissingConfig, TracingRequired } from './errors'
export type Config = {
allowIntrospection?: boolean
allowedOrigins: CorsOptions['origin']
allowListPath: string
apiPort: number
cacheEnabled: boolean
genesisFileByron: string
genesisFileShelley: string
hasuraUri: string
poolMetadataProxy: string
prometheusMetrics: boolean
queryDepthLimit: number
tracing: boolean
}
export async function getConfig (): Promise<Config> {
const {
allowIntrospection,
allowedOrigins,
allowListPath,
apiPort,
cacheEnabled,
genesisFileByron,
genesisFileShelley,
hasuraUri,
poolMetadataProxy,
prometheusMetrics,
queryDepthLimit,
tracing
} = filterAndTypecastEnvs(process.env)
if (!hasuraUri && !genesisFileShelley) {
throw new MissingConfig('You have not provided configuration to load an API segment. Either set HASURA_URI or GENESIS_FILE_SHELLEY')
}
if (prometheusMetrics && process.env.TRACING === 'false') {
throw new TracingRequired('Prometheus')
}
if (allowListPath && allowIntrospection) {
throw new IntrospectionNotPermitted('allowList')
}
return {
allowIntrospection,
allowedOrigins: allowedOrigins || true,
allowListPath,
apiPort: apiPort || 3100,
cacheEnabled: cacheEnabled || false,
genesisFileByron,
genesisFileShelley,
hasuraUri,
poolMetadataProxy,
prometheusMetrics,
queryDepthLimit: queryDepthLimit || 10,
tracing
}
}
function filterAndTypecastEnvs (env: any) {
const {
ALLOW_INTROSPECTION,
ALLOWED_ORIGINS,
ALLOW_LIST_PATH,
API_PORT,
CACHE_ENABLED,
GENESIS_FILE_BYRON,
GENESIS_FILE_SHELLEY,
HASURA_URI,
POOL_METADATA_PROXY,
PROMETHEUS_METRICS,
QUERY_DEPTH_LIMIT,
TRACING,
WHITELIST_PATH
} = env
if (WHITELIST_PATH) {
console.log(`WHITELIST_PATH is deprecated and will be removed in 3.0.0.
Please update your configuration to use ALLOW_LIST_PATH instead.`)
}
return {
allowIntrospection: ALLOW_INTROSPECTION === 'true' ? true : undefined,
allowedOrigins: ALLOWED_ORIGINS,
allowListPath: ALLOW_LIST_PATH || WHITELIST_PATH,
apiPort: Number(API_PORT),
cacheEnabled: CACHE_ENABLED === 'true' ? true : undefined,
genesisFileByron: GENESIS_FILE_BYRON,
genesisFileShelley: GENESIS_FILE_SHELLEY,
hasuraUri: HASURA_URI,
poolMetadataProxy: POOL_METADATA_PROXY,
prometheusMetrics: PROMETHEUS_METRICS === 'true' ? true : undefined,
queryDepthLimit: Number(QUERY_DEPTH_LIMIT),
tracing: TRACING === 'true' ? true : undefined
}
}