|
2 | 2 | import { get } from 'env-var'; |
3 | 3 | import { displayCLIWarning } from './cli'; |
4 | 4 | import { isNativeSupported } from './platform'; |
| 5 | +import type { LogLevel } from './logger'; |
| 6 | +import type { ApiScopes, ExportBucketType } from './shared-types'; |
5 | 7 |
|
6 | 8 | export class InvalidConfiguration extends Error { |
7 | 9 | public constructor(key: string, value: any, description: string) { |
@@ -163,7 +165,24 @@ const variables = { |
163 | 165 | devMode: () => get('CUBEJS_DEV_MODE') |
164 | 166 | .default('false') |
165 | 167 | .asBoolStrict(), |
166 | | - logLevel: () => get('CUBEJS_LOG_LEVEL').asString(), |
| 168 | + logLevel: (): LogLevel | undefined => { |
| 169 | + const value = get('CUBEJS_LOG_LEVEL').asString(); |
| 170 | + if (value) { |
| 171 | + switch (value.toLowerCase()) { |
| 172 | + case 'trace': |
| 173 | + case 'info': |
| 174 | + case 'warn': |
| 175 | + case 'error': |
| 176 | + break; |
| 177 | + // not used, but let's allow |
| 178 | + case 'debug': |
| 179 | + break; |
| 180 | + default: |
| 181 | + throw new InvalidConfiguration('CUBEJS_LOG_LEVEL', value, 'Must be one of: trace, debug, info, warn, error'); |
| 182 | + } |
| 183 | + } |
| 184 | + return value as LogLevel | undefined; |
| 185 | + }, |
167 | 186 | port: () => asPortOrSocket(process.env.PORT || '4000', 'PORT'), |
168 | 187 | tls: () => get('CUBEJS_ENABLE_TLS') |
169 | 188 | .default('false') |
@@ -847,27 +866,28 @@ const variables = { |
847 | 866 | /** |
848 | 867 | * Export bucket storage type. |
849 | 868 | */ |
850 | | - dbExportBucketType: ({ |
| 869 | + dbExportBucketType: <T extends ExportBucketType>({ |
851 | 870 | supported, |
852 | 871 | dataSource, |
853 | 872 | }: { |
854 | | - supported: ('s3' | 'gcp' | 'azure')[], |
| 873 | + supported: readonly T[], |
855 | 874 | dataSource: string, |
856 | | - }) => { |
| 875 | + }): T | undefined => { |
857 | 876 | const val = process.env[ |
858 | 877 | keyByDataSource('CUBEJS_DB_EXPORT_BUCKET_TYPE', dataSource) |
859 | | - ]; |
| 878 | + ] as T | undefined; |
860 | 879 | if ( |
861 | 880 | val && |
862 | 881 | supported && |
863 | | - supported.indexOf(<'s3' | 'gcp' | 'azure'>val) === -1 |
| 882 | + supported.indexOf(val) === -1 |
864 | 883 | ) { |
865 | 884 | throw new TypeError( |
866 | 885 | `The ${ |
867 | 886 | keyByDataSource('CUBEJS_DB_EXPORT_BUCKET_TYPE', dataSource) |
868 | 887 | } must be one of the [${supported.join(', ')}].` |
869 | 888 | ); |
870 | 889 | } |
| 890 | + |
871 | 891 | return val; |
872 | 892 | }, |
873 | 893 |
|
@@ -2186,7 +2206,7 @@ const variables = { |
2186 | 2206 | cacheAndQueueDriver: () => get('CUBEJS_CACHE_AND_QUEUE_DRIVER') |
2187 | 2207 | .asString(), |
2188 | 2208 | defaultApiScope: () => get('CUBEJS_DEFAULT_API_SCOPES') |
2189 | | - .asArray(','), |
| 2209 | + .asArray(',') as ApiScopes[] | undefined, |
2190 | 2210 | jwkUrl: () => get('CUBEJS_JWK_URL') |
2191 | 2211 | .asString(), |
2192 | 2212 | jwtKey: () => get('CUBEJS_JWT_KEY') |
@@ -2333,6 +2353,20 @@ const variables = { |
2333 | 2353 |
|
2334 | 2354 | type Vars = typeof variables; |
2335 | 2355 |
|
| 2356 | +export function getEnvFn<T extends keyof Vars>(key: T): Vars[T] { |
| 2357 | + if (key in variables) { |
| 2358 | + return variables[key] as Vars[T]; |
| 2359 | + } |
| 2360 | + |
| 2361 | + throw new Error( |
| 2362 | + `Unsupported env variable: "${key}"`, |
| 2363 | + ); |
| 2364 | +} |
| 2365 | + |
| 2366 | +/** |
| 2367 | + * @deprecated Use getEnvFn instead. TypeScript cannot infer return types correctly |
| 2368 | + * for generic env functions through this wrapper, as ReturnType<> erases generics. |
| 2369 | + */ |
2336 | 2370 | export function getEnv<T extends keyof Vars>(key: T, ...args: Parameters<Vars[T]>): ReturnType<Vars[T]> { |
2337 | 2371 | if (key in variables) { |
2338 | 2372 | return (variables[key] as any)(...args); |
|
0 commit comments