Skip to content

Commit

Permalink
Refactor ConfigService.get to enhance type safety and remove manual c…
Browse files Browse the repository at this point in the history
…asting

Signed-off-by: belloibrahv <belloibrahv@gmail.com>
  • Loading branch information
belloibrahv committed Jan 8, 2025
1 parent c7e6a08 commit 7f3c824
Show file tree
Hide file tree
Showing 46 changed files with 62 additions and 50 deletions.
2 changes: 1 addition & 1 deletion packages/config-service/src/commands/printEnvs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
*
* Hedera JSON RPC Relay
*
* Copyright (C) 2022-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.
Expand Down
52 changes: 51 additions & 1 deletion packages/config-service/src/services/globalConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
*
* Hedera JSON RPC Relay
*
* Copyright (C) 2022-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.
Expand All @@ -18,6 +18,22 @@
*
*/

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;
Expand Down Expand Up @@ -751,6 +767,40 @@ const _CONFIG = {

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 {
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;
}
4 changes: 2 additions & 2 deletions packages/config-service/src/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
*
* Hedera JSON RPC Relay
*
* Copyright (C) 2022-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.
Expand All @@ -23,7 +23,7 @@ import findConfig from 'find-config';
import pino from 'pino';
import { LoggerService } from './loggerService';
import { ValidationService } from './validationService';
import { ConfigKey } from './globalConfig';
import type { ConfigKey } from './globalConfig';

const mainLogger = pino({
name: 'hedera-json-rpc-relay',
Expand Down
2 changes: 1 addition & 1 deletion packages/config-service/src/services/validationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
*
* Hedera JSON RPC Relay
*
* Copyright (C) 2022-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.
Expand Down
2 changes: 1 addition & 1 deletion packages/config-service/tests/configServiceTestHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
*
* Hedera JSON RPC Relay
*
* Copyright (C) 2022-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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
*
* Hedera JSON RPC Relay
*
* Copyright (C) 2022-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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
*
* Hedera JSON RPC Relay
*
* Copyright (C) 2022-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.
Expand All @@ -24,7 +24,6 @@ import crypto from 'crypto';
import { ConfigService } from '../../../src/services';
import { LoggerService } from '../../../src/services/loggerService';
import { GlobalConfig } from '../../../dist/services/globalConfig';
import { ConfigKey } from '../../../src/services/globalConfig';

chai.use(chaiAsPromised);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
*
* Hedera JSON RPC Relay
*
* Copyright (C) 2022-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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ import {
verifyResult,
withOverriddenEnvsInMochaTest,
} from '../../helpers';
import { ConfigKey } from '../../../../config-service/src/services/globalConfig';

chai.use(chaiAsPromised);

Expand Down
1 change: 0 additions & 1 deletion packages/relay/tests/lib/eth/eth-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import {
mockData,
toHex,
} from '../../helpers';
import { ConfigKey } from '../../../../config-service/src/services/globalConfig';

export const BLOCK_TRANSACTION_COUNT = 77;
export const GAS_USED_1 = 200000;
Expand Down
1 change: 0 additions & 1 deletion packages/relay/tests/lib/eth/eth-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import { EthImpl } from '../../../src/lib/eth';
import { CacheService } from '../../../src/lib/services/cacheService/cacheService';
import HAPIService from '../../../src/lib/services/hapiService/hapiService';
import { HbarLimitService } from '../../../src/lib/services/hbarLimitService';
import { ConfigKey } from '../../../../config-service/src/services/globalConfig';

export function contractResultsByNumberByIndexURL(number: number, index: number): string {
return `contracts/results?block.number=${number}&transaction.index=${index}&limit=100&order=asc`;
Expand Down
1 change: 0 additions & 1 deletion packages/relay/tests/lib/eth/eth_common.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import { Registry } from 'prom-client';

import { RelayImpl } from '../../../src';
import { RequestDetails } from '../../../src/lib/types';
import { ConfigKey } from '../../../../config-service/src/services/globalConfig';

use(chaiAsPromised);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ import {
} from '../../helpers';
import { ACCOUNT_ADDRESS_1, DEFAULT_NETWORK_FEES, MAX_GAS_LIMIT_HEX, NO_TRANSACTIONS } from './eth-config';
import { generateEthTestEnv } from './eth-helpers';
import { ConfigKey } from '../../../../config-service/src/services/globalConfig';

use(chaiAsPromised);

Expand Down
3 changes: 1 addition & 2 deletions packages/relay/tests/lib/ethGetBlockBy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import HAPIService from '../../src/lib/services/hapiService/hapiService';
import { HbarLimitService } from '../../src/lib/services/hbarLimitService';
import { RequestDetails } from '../../src/lib/types';
import { defaultDetailedContractResults, overrideEnvsInMochaDescribe, useInMemoryRedisServer } from '../helpers';
import { ConfigKey } from '../../../config-service/src/services/globalConfig';

use(chaiAsPromised);

Expand Down Expand Up @@ -129,7 +128,7 @@ describe('eth_getBlockBy', async function () {

// @ts-ignore
mirrorNodeInstance = new MirrorNodeClient(
(ConfigService.get('MIRROR_NODE_URL')) ?? '',
ConfigService.get('MIRROR_NODE_URL') ?? '',
logger.child({ name: `mirror-node` }),
registry,
cacheService,
Expand Down
1 change: 0 additions & 1 deletion packages/relay/tests/lib/hapiService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import HAPIService from '../../src/lib/services/hapiService/hapiService';
import { HbarLimitService } from '../../src/lib/services/hbarLimitService';
import { RequestDetails } from '../../src/lib/types';
import { overrideEnvsInMochaDescribe, withOverriddenEnvsInMochaTest } from '../helpers';
import { ConfigKey } from '../../../config-service/src/services/globalConfig';

const registry = new Registry();
const logger = pino();
Expand Down
1 change: 0 additions & 1 deletion packages/relay/tests/lib/mirrorNodeClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ const registry = new Registry();
import { MirrorNodeTransactionRecord, RequestDetails } from '../../src/lib/types';
import { SDKClientError } from '../../src/lib/errors/SDKClientError';
import { BigNumber } from 'bignumber.js';
import { ConfigKey } from '../../../config-service/src/services/globalConfig';

const logger = pino();
const noTransactions = '?transactions=false';
Expand Down
1 change: 0 additions & 1 deletion packages/relay/tests/lib/net.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import { Registry } from 'prom-client';
import { RelayImpl } from '../../src/lib/relay';
import constants from '../../src/lib/constants';
import { withOverriddenEnvsInMochaTest } from '../helpers';
import { ConfigKey } from '../../../config-service/src/services/globalConfig';

const logger = pino();
let Relay;
Expand Down
2 changes: 1 addition & 1 deletion packages/relay/tests/lib/openrpc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ import {
signedTransactionHash,
} from '../helpers';
import { CONTRACT_RESULT_MOCK, NOT_FOUND_RES } from './eth/eth-config';
import { ConfigKey } from '../../../config-service/src/services/globalConfig';

const logger = pino();
const registry = new Registry();
const Relay = new RelayImpl(logger, registry);
Expand Down
1 change: 0 additions & 1 deletion packages/relay/tests/lib/poller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import sinon from 'sinon';

import { EthImpl } from '../../src/lib/eth';
import { Poller } from '../../src/lib/poller';
import { ConfigKey } from '../../../config-service/src/services/globalConfig';

const logger = pino({ level: 'trace' });

Expand Down
1 change: 0 additions & 1 deletion packages/relay/tests/lib/precheck.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ import { ONE_TINYBAR_IN_WEI_HEX } from './eth/eth-config';

const registry = new Registry();
import { RequestDetails } from '../../src/lib/types';
import { ConfigKey } from '../../../config-service/src/services/globalConfig';

const logger = pino();
const limitOrderPostFix = '?order=desc&limit=1';
Expand Down
1 change: 0 additions & 1 deletion packages/relay/tests/lib/sdkClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ import {
random20BytesAddress,
withOverriddenEnvsInMochaTest,
} from '../helpers';
import { ConfigKey } from '../../../config-service/src/services/globalConfig';

const registry = new Registry();
const logger = pino();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import { CommonService } from '../../../../src/lib/services/ethService';
import { RequestDetails } from '../../../../src/lib/types';
import RelayAssertions from '../../../assertions';
import { getQueryParams, withOverriddenEnvsInMochaTest } from '../../../helpers';
import { ConfigKey } from '../../../../../config-service/src/services/globalConfig';

chai.use(chaiAsPromised);

Expand Down
1 change: 0 additions & 1 deletion packages/relay/tests/lib/services/eth/filter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import {
toHex,
withOverriddenEnvsInMochaTest,
} from '../../../helpers';
import { ConfigKey } from '../../../../../config-service/src/services/globalConfig';

const logger = pino();
const registry = new Registry();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ import { SubscriptionTier } from '../../../../src/lib/db/types/hbarLimiter/subsc
import { CacheService } from '../../../../src/lib/services/cacheService/cacheService';
import { HbarLimitService } from '../../../../src/lib/services/hbarLimitService';
import { RequestDetails } from '../../../../src/lib/types';
import { ConfigKey } from '../../../../../config-service/src/services/globalConfig';

chai.use(chaiAsPromised);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ import {
overrideEnvsInMochaDescribe,
withOverriddenEnvsInMochaTest,
} from '../../../helpers';
import { ConfigKey } from '../../../../../config-service/src/services/globalConfig';

const registry = new Registry();
const logger = pino();
Expand Down
1 change: 0 additions & 1 deletion packages/relay/tests/lib/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import { ConfigService } from '@hashgraph/json-rpc-config-service/dist/services'
import { expect } from 'chai';
import createHash from 'keccak';

import { ConfigKey } from '../../../config-service/src/services/globalConfig';
import { ASCIIToHex, prepend0x } from '../../src/formatters';
import constants from '../../src/lib/constants';
import { Utils } from '../../src/utils';
Expand Down
1 change: 0 additions & 1 deletion packages/relay/tests/lib/web3.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import { expect } from 'chai';
import pino from 'pino';
import { Registry } from 'prom-client';

import { ConfigKey } from '../../../config-service/src/services/globalConfig';
import { RelayImpl } from '../../src';
import { withOverriddenEnvsInMochaTest } from '../helpers';

Expand Down
1 change: 0 additions & 1 deletion packages/server/tests/acceptance/conformityTests.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import LogsContract from '../contracts/Logs.json';
import CallerContract from '../contracts/Caller.json';
import { ConfigService } from '@hashgraph/json-rpc-config-service/dist/services';
import { parseOpenRPCDocument } from '@open-rpc/schema-utils-js';
import { ConfigKey } from '../../../config-service/src/services/globalConfig';

const directoryPath = path.resolve(__dirname, '../../../../node_modules/execution-apis/tests');

Expand Down
1 change: 0 additions & 1 deletion packages/server/tests/acceptance/hbarLimiter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ import testConstants from '../helpers/constants';
// Local resources
import { Utils } from '../helpers/utils';
import { AliasAccount } from '../types/AliasAccount';
import { ConfigKey } from '../../../config-service/src/services/globalConfig';

config({ path: resolve(__dirname, '../localAcceptance.env') });
const DOT_ENV = dotenv.parse(fs.readFileSync(resolve(__dirname, '../localAcceptance.env')));
Expand Down
1 change: 0 additions & 1 deletion packages/server/tests/acceptance/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ import ServicesClient from '../clients/servicesClient';
// Utils and types
import { Utils } from '../helpers/utils';
import { AliasAccount } from '../types/AliasAccount';
import { ConfigKey } from '../../../config-service/src/services/globalConfig';

chai.use(chaiAsPromised);
dotenv.config({ path: path.resolve(__dirname, '../../../../.env') });
Expand Down
1 change: 0 additions & 1 deletion packages/server/tests/acceptance/rateLimiter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import relayConstants from '@hashgraph/json-rpc-relay/dist/lib/constants';
import testConstants from '../../tests/helpers/constants';
import RelayClient from '../clients/relayClient';
import Assertions from '../helpers/assertions';
import { ConfigKey } from '../../../config-service/src/services/globalConfig';

describe('@ratelimiter Rate Limiters Acceptance Tests', function () {
this.timeout(480 * 1000); // 480 seconds
Expand Down
1 change: 0 additions & 1 deletion packages/server/tests/acceptance/rpc_batch1.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ import parentContractJson from '../contracts/Parent.json';
import Assertions from '../helpers/assertions';
import { Utils } from '../helpers/utils';
import { AliasAccount } from '../types/AliasAccount';
import { ConfigKey } from '../../../config-service/src/services/globalConfig';

const Address = RelayCalls;

Expand Down
1 change: 0 additions & 1 deletion packages/server/tests/acceptance/rpc_batch2.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ import TokenCreateJson from '../contracts/TokenCreateContract.json';
import Assertions from '../helpers/assertions';
import { Utils } from '../helpers/utils';
import { AliasAccount } from '../types/AliasAccount';
import { ConfigKey } from '../../../config-service/src/services/globalConfig';

describe('@api-batch-2 RPC Server Acceptance Tests', function () {
this.timeout(240 * 1000); // 240 seconds
Expand Down
1 change: 0 additions & 1 deletion packages/server/tests/acceptance/rpc_batch3.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ import Assertions from '../helpers/assertions';
import RelayCalls from '../helpers/constants';
import { Utils } from '../helpers/utils';
import { AliasAccount } from '../types/AliasAccount';
import { ConfigKey } from '../../../config-service/src/services/globalConfig';

chai.use(chaiExclude);

Expand Down
1 change: 0 additions & 1 deletion packages/server/tests/acceptance/serverConfig.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import { ConfigService } from '@hashgraph/json-rpc-config-service/dist/services'
import { expect } from 'chai';

import { Utils } from '../helpers/utils';
import { ConfigKey } from '../../../config-service/src/services/globalConfig';

describe('@server-config Server Configuration Options Coverage', function () {
describe('Koa Server Timeout', () => {
Expand Down
1 change: 0 additions & 1 deletion packages/server/tests/integration/server.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ import * as Constants from '../../src/validator/constants';
import RelayCalls from '../../tests/helpers/constants';
import Assertions from '../helpers/assertions';
import { Utils } from '../helpers/utils';
import { ConfigKey } from '../../../config-service/src/services/globalConfig';

const MISSING_PARAM_ERROR = 'Missing value for required parameter';

Expand Down
1 change: 0 additions & 1 deletion packages/ws-server/tests/acceptance/batchRequest.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import { expect } from 'chai';
import { ethers, WebSocketProvider } from 'ethers';

import { WsTestConstant, WsTestHelper } from '../helper';
import { ConfigKey } from '../../../config-service/src/services/globalConfig';

describe('@web-socket-batch-request Batch Requests', async function () {
const METHOD_NAME = 'batch_request';
Expand Down
Loading

0 comments on commit 7f3c824

Please sign in to comment.