Skip to content

Commit

Permalink
Eslint cleanup, simplify parseEnvJSON typing
Browse files Browse the repository at this point in the history
  • Loading branch information
robertherber committed Mar 30, 2024
1 parent 6a7fef4 commit a1df31e
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 12 deletions.
1 change: 1 addition & 0 deletions packages/utils/node/SuperDataLoader.performance1.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-console */
import DataLoader from 'dataloader'

import createSuperDataLoader from './SuperDataLoader'
Expand Down
1 change: 1 addition & 0 deletions packages/utils/node/SuperDataLoader.performance2.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-console */
import DataLoader from 'dataloader'

import createSuperDataLoader from './SuperDataLoader'
Expand Down
1 change: 1 addition & 0 deletions packages/utils/node/SuperDataLoader.performance3.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-console */
import DataLoader from 'dataloader'

import createSuperDataLoader from './SuperDataLoader'
Expand Down
1 change: 1 addition & 0 deletions packages/utils/node/debugPipeline.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-console */
import type { Collection, Document } from 'mongodb'

// Be aware of issues
Expand Down
6 changes: 6 additions & 0 deletions packages/utils/node/parseEnv.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,9 @@ test('parseEnvJSON return defaultValue when null', () => {

expect(result).toEqual({ defaultish: true })
})

test('parseEnvJSON return actual value', () => {
const result = parseEnvJSON<{readonly isValid: boolean}>('envVariableWithJson', { isValid: false }, { envVariableWithJson: '{ "isValid": true }' })

expect(result).toEqual({ isValid: true })
})
22 changes: 11 additions & 11 deletions packages/utils/node/parseEnv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,16 @@ export function parseEnvNumber<
try {
return parseFloat(rawValue) as T
} catch (e) {
console.error(`Failed to parse environment variable "${prop}", expected number got "${rawValue}"`)
return undefined as TDefault
throw new Error(`Failed to parse environment variable "${prop}", expected number got "${rawValue}"`)
}
}

// todo [2024-03-01]: allow to only specify first type argument
export function parseEnvJSON<
T,
TDefault extends T | undefined
TDefault extends T | undefined = T | undefined
>(
prop: string,
defaultValue?: TDefault,
defaultValue: TDefault,
env = process.env,
): T | TDefault {
const rawValue = env[prop]
Expand All @@ -44,8 +42,7 @@ export function parseEnvJSON<
try {
return JSON.parse(rawValue) as T
} catch (e) {
console.error(`Failed to parse environment variable "${prop}", expected JSON got "${rawValue}"`)
return undefined as TDefault
throw Error(`Failed to parse environment variable "${prop}", expected JSON got "${rawValue}"`)
}
}

Expand Down Expand Up @@ -77,8 +74,7 @@ export function parseEnvBoolean<T extends boolean>(

return defaultValue as T
} catch (e) {
console.error(`Failed to parse environment variable "${prop}", expected boolean got "${rawValue}"`)
return defaultValue ?? false as T
throw new Error(`Failed to parse environment variable "${prop}", expected boolean got "${rawValue}"`)
}
}

Expand All @@ -103,8 +99,12 @@ export function parseEnvEnum<
return rawValue as T & TDefault
}

if (isNotNullOrUndefined(defaultValue) && enumOrArray.includes(defaultValue)) {
return defaultValue
if (isNotNullOrUndefined(defaultValue)) {
if (enumOrArray.includes(defaultValue)) {
return defaultValue
}

throw new Error(`Failed to parse environment variable "${prop}", expected one of ${JSON.stringify(enumOrArray)} got "${rawValue}"`)
}

return undefined as unknown as T & TDefault
Expand Down
3 changes: 2 additions & 1 deletion packages/utils/node/projectionFromGraphQLInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export type Paths<T, D extends number = 5> = readonly [D] extends readonly [neve
: never
}[keyof T] : ''

// export type Projection<DBType extends Record<string, unknown>, DBPath extends Paths<DBType> = Paths<DBType>> = Record<DBPath, number>
// export type Projection<DBType extends Record<string, unknown>, DBPath extends Paths<DBType> = Paths<DBType>> =
// Record<DBPath, number>

export type DbType = Document & { readonly _id: ObjectId };

Expand Down

0 comments on commit a1df31e

Please sign in to comment.