Skip to content

Commit 6d2d2ab

Browse files
authored
add option to write back to original environment object (#26)
1 parent 1ac29b9 commit 6d2d2ab

3 files changed

Lines changed: 12 additions & 0 deletions

File tree

.changeset/lemon-actors-sneeze.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@arundo/typed-env": minor
3+
---
4+
5+
add option to write back to original environment object

src/contracts.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export type Options<TTransform, TPrefixRemoval> = {
77
transform?: TTransform;
88
constructErrorFn?: (issues: ZodIssue[]) => Error;
99
excludePrefix?: TPrefixRemoval;
10+
writeBackToEnv?: boolean;
1011
};
1112

1213
export type ConditionalType<TTransform extends NamingConvention | undefined, TSchema> = 'default' extends TTransform

src/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,16 @@ export const typeEnvironment = <
8080
transform = 'default',
8181
constructErrorFn: constructErrorFn = constructError,
8282
excludePrefix = '' as TPrefixRemoval,
83+
writeBackToEnv = false,
8384
} = options;
8485

8586
try {
8687
const parsed = schema.parse(overrideEnv);
88+
if (writeBackToEnv) {
89+
for (const key of Object.keys(parsed)) {
90+
overrideEnv[key] = typeof parsed[key] === 'string' ? parsed[key] : JSON.stringify(parsed[key]);
91+
}
92+
}
8793
type TSchemaOutput = TSchema['_output'];
8894
const prefixRemoved = removePrefix(parsed, excludePrefix) as PrefixRemoved<TSchemaOutput, TPrefixRemoval>;
8995
return changeCase(transform, prefixRemoved) as ConditionalType<

0 commit comments

Comments
 (0)