Skip to content

Commit

Permalink
Allow support for extra inspectables (#206)
Browse files Browse the repository at this point in the history
  • Loading branch information
simenandre authored Apr 5, 2023
1 parent 237c2ad commit ce5c0e5
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
15 changes: 12 additions & 3 deletions src/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { CleanedEnvAccessors } from './types'
import { CleanedEnvAccessors, StrictProxyMiddlewareOptions } from './types'

export const strictProxyMiddleware = <T extends object>(envObj: T, rawEnv: unknown) => {
export const strictProxyMiddleware = <T extends object>(
envObj: T,
rawEnv: unknown,
options: StrictProxyMiddlewareOptions = {},
) => {
const { extraInspectables = [] } = options
const inspectables = [
'length',
'inspect',
Expand Down Expand Up @@ -29,7 +34,11 @@ export const strictProxyMiddleware = <T extends object>(envObj: T, rawEnv: unkno
// proxy that throws crashes the entire process. This permits access on
// the necessary properties for `console.log(envObj)`, `envObj.length`,
// `envObj.hasOwnProperty('string')` to work.
if (inspectables.includes(name) || inspectSymbolStrings.includes(name.toString())) {
if (
inspectables.includes(name) ||
inspectSymbolStrings.includes(name.toString()) ||
extraInspectables.includes(name)
) {
// @ts-expect-error TS doesn't like symbol types as indexers
return target[name]
}
Expand Down
9 changes: 9 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,12 @@ export interface CleanOptions<T> {
*/
reporter?: ((opts: ReporterOptions<T>) => void) | null
}

export interface StrictProxyMiddlewareOptions {
/**
* A list of extra inspectable properties to add to the middleware.
*
* This is useful if you want to add support for framework-specific values.
*/
extraInspectables?: string[]
}
13 changes: 12 additions & 1 deletion tests/middleware.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { cleanEnv, customCleanEnv, str } from '../src'
import { accessorMiddleware } from '../src/middleware'
import { accessorMiddleware, strictProxyMiddleware } from '../src/middleware'

describe('customCleanEnv middleware type inference', () => {
test('allows access to properties on the output object', () => {
Expand Down Expand Up @@ -158,3 +158,14 @@ describe('proxy middleware', () => {
expect(JSON.stringify(env)).toEqual('{"FOO":"foo"}')
})
})

describe('strictProxyMiddleware', () => {
test('proxy allows extra inspectables applied through options', () => {
const env = customCleanEnv({ FOO: 'bar' }, { FOO: str() }, (cleaned, raw) =>
strictProxyMiddleware(cleaned, raw, { extraInspectables: ['hello'] }),
)

// @ts-expect-error This invalid usage should trigger a type error
expect(() => env.hello).not.toThrow()
})
})

0 comments on commit ce5c0e5

Please sign in to comment.