Skip to content

Commit

Permalink
add option to write back to original environment object (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
bompi88 authored Apr 10, 2024
1 parent 1ac29b9 commit 6d2d2ab
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/lemon-actors-sneeze.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@arundo/typed-env": minor
---

add option to write back to original environment object
1 change: 1 addition & 0 deletions src/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type Options<TTransform, TPrefixRemoval> = {
transform?: TTransform;
constructErrorFn?: (issues: ZodIssue[]) => Error;
excludePrefix?: TPrefixRemoval;
writeBackToEnv?: boolean;
};

export type ConditionalType<TTransform extends NamingConvention | undefined, TSchema> = 'default' extends TTransform
Expand Down
6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,16 @@ export const typeEnvironment = <
transform = 'default',
constructErrorFn: constructErrorFn = constructError,
excludePrefix = '' as TPrefixRemoval,
writeBackToEnv = false,
} = options;

try {
const parsed = schema.parse(overrideEnv);
if (writeBackToEnv) {
for (const key of Object.keys(parsed)) {
overrideEnv[key] = typeof parsed[key] === 'string' ? parsed[key] : JSON.stringify(parsed[key]);
}
}
type TSchemaOutput = TSchema['_output'];
const prefixRemoved = removePrefix(parsed, excludePrefix) as PrefixRemoved<TSchemaOutput, TPrefixRemoval>;
return changeCase(transform, prefixRemoved) as ConditionalType<
Expand Down

0 comments on commit 6d2d2ab

Please sign in to comment.