From ef1b130f821263cc04959cefeb7eb5a4f018735d Mon Sep 17 00:00:00 2001 From: Piotr Goszczynski Date: Wed, 11 Oct 2023 14:21:32 +0200 Subject: [PATCH] fix: missing shared envs in transform function --- .github/workflows/npm-publish.yaml | 2 ++ index.ts | 31 +++++++++++++++++------------- package.json | 1 + test/index.test.ts | 28 +++++++++++++++++++++++++++ 4 files changed, 49 insertions(+), 13 deletions(-) diff --git a/.github/workflows/npm-publish.yaml b/.github/workflows/npm-publish.yaml index d1c3edf..1bb0a72 100644 --- a/.github/workflows/npm-publish.yaml +++ b/.github/workflows/npm-publish.yaml @@ -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 diff --git a/index.ts b/index.ts index 0e157b9..5b11e77 100644 --- a/index.ts +++ b/index.ts @@ -40,12 +40,17 @@ type ServerOptions< }; type TransformOptions< + TShared extends Record, TClient extends Record, TServer extends Record, TTransformOutput extends Record > = { transform: ( - envs: Simplify> & z.infer>> + envs: Simplify< + z.infer> & + z.infer> & + z.infer> + > ) => TTransformOutput; }; @@ -282,7 +287,7 @@ type EnvOptions< > = ( | (ClientOptions & ServerOptions & - TransformOptions & + TransformOptions & FeatureFlagsOptions< TShared, TClient, @@ -292,11 +297,11 @@ type EnvOptions< >) | (ClientOptions & ServerOptions & - TransformOptions & + TransformOptions & Impossible>) | (ClientOptions & ServerOptions & - Impossible> & + Impossible> & FeatureFlagsOptions< TShared, TClient, @@ -306,7 +311,7 @@ type EnvOptions< >) | (ClientOptions & Impossible> & - TransformOptions & + TransformOptions & FeatureFlagsOptions< TShared, TClient, @@ -316,7 +321,7 @@ type EnvOptions< >) | (Impossible> & ServerOptions & - TransformOptions & + TransformOptions & FeatureFlagsOptions< TShared, TClient, @@ -326,19 +331,19 @@ type EnvOptions< >) | (ClientOptions & ServerOptions & - Impossible> & + Impossible> & Impossible>) | (ClientOptions & Impossible> & - TransformOptions & + TransformOptions & Impossible>) | (Impossible> & ServerOptions & - TransformOptions & + TransformOptions & Impossible>) | (ClientOptions & Impossible> & - Impossible> & + Impossible> & FeatureFlagsOptions< TShared, TClient, @@ -348,7 +353,7 @@ type EnvOptions< >) | (Impossible> & ServerOptions & - Impossible> & + Impossible> & FeatureFlagsOptions< TShared, TClient, @@ -358,11 +363,11 @@ type EnvOptions< >) | (ClientOptions & Impossible> & - Impossible> & + Impossible> & Impossible>) | (ServerOptions & Impossible> & - Impossible> & + Impossible> & Impossible>) ) & RuntimeOptions & diff --git a/package.json b/package.json index e5ed456..4658e5c 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,7 @@ "scripts": { "build": "tsup index.ts", "test": "vitest", + "type-check": "tsc --noEmit", "release": "release-it" }, "peerDependencies": { diff --git a/test/index.test.ts b/test/index.test.ts index 9a401e1..9e17061 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -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({