-
Notifications
You must be signed in to change notification settings - Fork 75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix: Resolve Type Warnings for ConfigService.get() #3350
base: main
Are you sure you want to change the base?
Changes from all commits
df2f57f
a6285b0
0f0377e
f62379c
c7e6a08
7f3c824
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
* | ||
* Hedera JSON RPC Relay | ||
* | ||
* Copyright (C) 2024 Hedera Hashgraph, LLC | ||
* Copyright (C) 2024 Hedera Hashgraph, LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
|
@@ -18,15 +18,30 @@ | |
* | ||
*/ | ||
|
||
type TypeStrToType<Tstr extends string> = Tstr extends 'string' | ||
? string | ||
: Tstr extends 'boolean' | ||
? boolean | ||
: Tstr extends 'number' | ||
? number | ||
: Tstr extends 'array' | ||
? unknown[] | ||
: never; | ||
|
||
type GetTypeStrOfKey<K extends string> = K extends keyof typeof _CONFIG | ||
? typeof _CONFIG[K]['type'] | ||
: never; | ||
|
||
export type TypeOfKey<K extends string> = TypeStrToType<GetTypeStrOfKey<K>>; | ||
|
||
export interface ConfigProperty { | ||
envName: string; | ||
type: string; | ||
required: boolean; | ||
defaultValue: string | number | boolean | null; | ||
} | ||
|
||
export class GlobalConfig { | ||
public static readonly ENTRIES: Record<string, ConfigProperty> = { | ||
const _CONFIG = { | ||
BATCH_REQUESTS_ENABLED: { | ||
envName: 'BATCH_REQUESTS_ENABLED', | ||
type: 'boolean', | ||
|
@@ -568,6 +583,12 @@ export class GlobalConfig { | |
required: false, | ||
defaultValue: null, | ||
}, | ||
SERVER_HOST: { | ||
envName: 'SERVER_HOST', | ||
type: 'string', | ||
required: false, | ||
defaultValue: null, | ||
}, | ||
SERVER_PORT: { | ||
envName: 'SERVER_PORT', | ||
type: 'number', | ||
|
@@ -742,5 +763,44 @@ export class GlobalConfig { | |
required: false, | ||
defaultValue: null, | ||
}, | ||
}; | ||
} satisfies { [key: string]: ConfigProperty }; | ||
|
||
export type ConfigKey = keyof typeof _CONFIG; | ||
|
||
export class ConfigService { | ||
private static config: typeof _CONFIG = _CONFIG; | ||
|
||
public static get<K extends ConfigKey>(name: K): TypeOfKey<K> | undefined { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This signature should be placed in the other |
||
const configItem = this.config[name]; | ||
if (!configItem) { | ||
return undefined; | ||
} | ||
|
||
const value = process.env[configItem.envName]; | ||
|
||
if (value === undefined) { | ||
return configItem.defaultValue as TypeOfKey<K>; | ||
} | ||
|
||
switch (configItem.type) { | ||
case 'boolean': | ||
return (value.toLowerCase() === 'true') as TypeOfKey<K>; | ||
case 'number': | ||
return Number(value) as TypeOfKey<K>; | ||
case 'string': | ||
return value as TypeOfKey<K>; | ||
case 'array': | ||
try { | ||
return JSON.parse(value) as TypeOfKey<K>; | ||
} catch { | ||
return undefined; | ||
} | ||
default: | ||
return undefined; | ||
} | ||
} | ||
} | ||
|
||
export class GlobalConfig { | ||
public static readonly ENTRIES: Record<ConfigKey, ConfigProperty> = _CONFIG; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ import findConfig from 'find-config'; | |
import pino from 'pino'; | ||
import { LoggerService } from './loggerService'; | ||
import { ValidationService } from './validationService'; | ||
import type { ConfigKey } from './globalConfig'; | ||
|
||
const mainLogger = pino({ | ||
name: 'hedera-json-rpc-relay', | ||
|
@@ -97,7 +98,7 @@ export class ConfigService { | |
* @param name string | ||
* @returns string | undefined | ||
*/ | ||
public static get(name: string): string | number | boolean | null | undefined { | ||
public static get(name: ConfigKey): string | number | boolean | null | undefined { | ||
return this.getInstance().envs[name]; | ||
} | ||
Comment on lines
+101
to
103
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, this still doesn’t resolve the primary issue with the .get() method. There are still instances across the codebase where the error “Argument of type ‘string | number | boolean’ is not assignable to parameter of type…” persists, as shown in the example below. This remains an unresolved task that the ticket is meant to address. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Luis added a very good solution which can fix the issue, please refer to his solution here #3350 (comment) |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
* | ||
* Hedera JSON RPC Relay | ||
* | ||
* Copyright (C) 2024 Hedera Hashgraph, LLC | ||
* Copyright (C) 2022-2024 Hedera Hashgraph, LLC | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's revert back to original |
||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
/* | ||
/*- | ||
* | ||
* Hedera JSON RPC Relay | ||
* | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for this extra class. The runtime conversion is already done by
ValidationService.typeCasting
.