Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,7 @@ type VariableEnvVarEntry<T extends ValidSettingValue> = {

export interface Getter<T extends SettingValueType | undefined> {
get(variable: string): T
getEnvVarName(variable: string): string
entries(): VariableEnvVarEntry<Exclude<T, undefined>>[]
}

Expand Down Expand Up @@ -593,8 +594,12 @@ export class EnvGetter<
)
}

private getCanonicalVariable(variable: string): string {
return variable.replace(/\W/g, '_').toUpperCase()
}

get(variable: string): SettingType<T> {
const canonicalVariable = variable.replace(/\W/g, '_').toUpperCase()
const canonicalVariable = this.getCanonicalVariable(variable)
if (canonicalVariable in this.variableMap) {
return this.variableMap[canonicalVariable].value as SettingType<T>
}
Expand All @@ -604,16 +609,23 @@ export class EnvGetter<
if (!this.settingsDefinition.required) {
return undefined as SettingType<T>
}
const envName = getEnvName(
this.name.replace(this.settingsDefinition.variablePlaceholder, canonicalVariable),
this.prefix,
)
const envName = this.getEnvVarName(variable)
throw new AdapterError({
statusCode: 500,
message: `Missing required environment variable: ${envName}`,
})
}

getEnvVarName(variable: string): string {
return getEnvName(
this.name.replace(
this.settingsDefinition.variablePlaceholder,
this.getCanonicalVariable(variable),
),
this.prefix,
)
}

entries(): VariableEnvVarEntry<SettingTypeWhenPresent<T>>[] {
return Object.values(this.variableMap)
}
Expand Down
21 changes: 21 additions & 0 deletions test/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,27 @@ test.serial('Get variable env var entries', async (t) => {
])
})

test.serial('Get variable env var name', async (t) => {
const envVarsPrefix = 'PREFIX'
const customSettings = {
NETWORK_NETWORK_TYPE: {
description: 'Network type for the given blockchain',
type: 'enum',
options: ['mainnet', 'testnet'],
variablePlaceholder: 'NETWORK',
},
} as const

const config = new AdapterConfig(customSettings, { envVarsPrefix })
config.initialize()
config.validate()
t.is(
config.settings.NETWORK_NETWORK_TYPE.getEnvVarName('ethereum'),
'PREFIX_ETHEREUM_NETWORK_TYPE',
)
t.is(config.settings.NETWORK_NETWORK_TYPE.getEnvVarName('a.b$c'), 'PREFIX_A_B_C_NETWORK_TYPE')
})

test.serial('Setting name must not have invalid characters', async (t) => {
const customSettings = {
'NETWORK-RPC-URL': {
Expand Down
Loading