Skip to content

Commit

Permalink
fix: missing shared envs in transform function
Browse files Browse the repository at this point in the history
  • Loading branch information
goszczynskip committed Oct 11, 2023
1 parent 4a5c7ef commit ef1b130
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 13 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/npm-publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ jobs:
run: pnpm install
- name: Run tests
run: pnpm test
- name: Type check
run: pnpm type-check

publish-npm:
needs: build
Expand Down
31 changes: 18 additions & 13 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,17 @@ type ServerOptions<
};

type TransformOptions<
TShared extends Record<string, ZodType>,
TClient extends Record<string, ZodType>,
TServer extends Record<string, ZodType>,
TTransformOutput extends Record<string, unknown>
> = {
transform: (
envs: Simplify<z.infer<ZodObject<TClient>> & z.infer<ZodObject<TServer>>>
envs: Simplify<
z.infer<ZodObject<TShared>> &
z.infer<ZodObject<TClient>> &
z.infer<ZodObject<TServer>>
>
) => TTransformOutput;
};

Expand Down Expand Up @@ -282,7 +287,7 @@ type EnvOptions<
> = (
| (ClientOptions<TPrefix, TClient> &
ServerOptions<TPrefix, TServer> &
TransformOptions<TClient, TServer, TTransformOutput> &
TransformOptions<TShared, TClient, TServer, TTransformOutput> &
FeatureFlagsOptions<
TShared,
TClient,
Expand All @@ -292,11 +297,11 @@ type EnvOptions<
>)
| (ClientOptions<TPrefix, TClient> &
ServerOptions<TPrefix, TServer> &
TransformOptions<TClient, TServer, TTransformOutput> &
TransformOptions<TShared, TClient, TServer, TTransformOutput> &
Impossible<FeatureFlagsOptions<never, never, never, never, never>>)
| (ClientOptions<TPrefix, TClient> &
ServerOptions<TPrefix, TServer> &
Impossible<TransformOptions<never, never, never>> &
Impossible<TransformOptions<never, never, never, never>> &
FeatureFlagsOptions<
TShared,
TClient,
Expand All @@ -306,7 +311,7 @@ type EnvOptions<
>)
| (ClientOptions<TPrefix, TClient> &
Impossible<ServerOptions<never, never>> &
TransformOptions<TClient, TServer, TTransformOutput> &
TransformOptions<TShared, TClient, TServer, TTransformOutput> &
FeatureFlagsOptions<
TShared,
TClient,
Expand All @@ -316,7 +321,7 @@ type EnvOptions<
>)
| (Impossible<ClientOptions<never, never>> &
ServerOptions<TPrefix, TServer> &
TransformOptions<TClient, TServer, TTransformOutput> &
TransformOptions<TShared, TClient, TServer, TTransformOutput> &
FeatureFlagsOptions<
TShared,
TClient,
Expand All @@ -326,19 +331,19 @@ type EnvOptions<
>)
| (ClientOptions<TPrefix, TClient> &
ServerOptions<TPrefix, TServer> &
Impossible<TransformOptions<never, never, never>> &
Impossible<TransformOptions<never, never, never, never>> &
Impossible<FeatureFlagsOptions<never, never, never, never, never>>)
| (ClientOptions<TPrefix, TClient> &
Impossible<ServerOptions<never, never>> &
TransformOptions<TClient, TServer, TTransformOutput> &
TransformOptions<TShared, TClient, TServer, TTransformOutput> &
Impossible<FeatureFlagsOptions<never, never, never, never, never>>)
| (Impossible<ClientOptions<never, never>> &
ServerOptions<TPrefix, TServer> &
TransformOptions<TClient, TServer, TTransformOutput> &
TransformOptions<TShared, TClient, TServer, TTransformOutput> &
Impossible<FeatureFlagsOptions<never, never, never, never, never>>)
| (ClientOptions<TPrefix, TClient> &
Impossible<ServerOptions<never, never>> &
Impossible<TransformOptions<never, never, never>> &
Impossible<TransformOptions<never, never, never, never>> &
FeatureFlagsOptions<
TShared,
TClient,
Expand All @@ -348,7 +353,7 @@ type EnvOptions<
>)
| (Impossible<ClientOptions<never, never>> &
ServerOptions<TPrefix, TServer> &
Impossible<TransformOptions<never, never, never>> &
Impossible<TransformOptions<never, never, never, never>> &
FeatureFlagsOptions<
TShared,
TClient,
Expand All @@ -358,11 +363,11 @@ type EnvOptions<
>)
| (ClientOptions<TPrefix, TClient> &
Impossible<ServerOptions<never, never>> &
Impossible<TransformOptions<never, never, never>> &
Impossible<TransformOptions<never, never, never, never>> &
Impossible<FeatureFlagsOptions<never, never, never, never, never>>)
| (ServerOptions<TPrefix, TServer> &
Impossible<ClientOptions<never, never>> &
Impossible<TransformOptions<never, never, never>> &
Impossible<TransformOptions<never, never, never, never>> &
Impossible<FeatureFlagsOptions<never, never, never, never, never>>)
) &
RuntimeOptions<TPrefix, TShared, TClient, TServer> &
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"scripts": {
"build": "tsup index.ts",
"test": "vitest",
"type-check": "tsc --noEmit",
"release": "release-it"
},
"peerDependencies": {
Expand Down
28 changes: 28 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,34 @@ describe("createEnv", () => {
}
});

it("should pass all envs to transform function", () => {
const result = createEnv({
isServer: true,
server: {
VALUE: z.string(),
},
client: {
PREFIX_VALUE: z.string(),
},
shared: {
SHARED_VALUE: z.string(),
},
transform: (env) => {
return {
TRANSFORM_VALUE: env.VALUE + env.PREFIX_VALUE + env.SHARED_VALUE,
};
},
clientPrefix: "PREFIX_" as const,
runtimeEnv: {
VALUE: "1",
PREFIX_VALUE: "2",
SHARED_VALUE: "3",
},
});

expect(result.TRANSFORM_VALUE).toEqual("123");
});

describe.skip("types", () => {
it("should fail when runtimeEnv doesn't contain shared keys", () => {
createEnv({
Expand Down

0 comments on commit ef1b130

Please sign in to comment.