From 61abee01867f1cc505b34fd7b9445e41c2023626 Mon Sep 17 00:00:00 2001 From: Fahmi Idris Date: Wed, 10 Jan 2024 10:47:56 +0700 Subject: [PATCH] feat: add t3 env for type-safe env variable (#7) --- next.config.mjs | 2 ++ package.json | 4 +++- pnpm-lock.yaml | 38 +++++++++++++++++++++++++++++++++++++- src/env.mjs | 16 ++++++++++++++++ 4 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 src/env.mjs diff --git a/next.config.mjs b/next.config.mjs index ae97155..3ee6da2 100644 --- a/next.config.mjs +++ b/next.config.mjs @@ -1,3 +1,5 @@ +import './src/env.mjs'; + import bundleAnalyzer from '@next/bundle-analyzer'; const withBundleAnalyzer = bundleAnalyzer({ diff --git a/package.json b/package.json index 1dfd920..df6e628 100644 --- a/package.json +++ b/package.json @@ -26,11 +26,13 @@ }, "dependencies": { "@next/bundle-analyzer": "^14.0.2", + "@t3-oss/env-nextjs": "^0.7.1", "clsx": "^2.0.0", "next": "^14.0.2", "react": "^18.2.0", "react-dom": "^18.2.0", - "tailwind-merge": "^1.14.0" + "tailwind-merge": "^1.14.0", + "zod": "^3.22.4" }, "devDependencies": { "@commitlint/cli": "^17.8.1", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index b5d905e..c90525e 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -8,6 +8,9 @@ dependencies: '@next/bundle-analyzer': specifier: ^14.0.2 version: 14.0.3 + '@t3-oss/env-nextjs': + specifier: ^0.7.1 + version: 0.7.1(typescript@5.3.2)(zod@3.22.4) clsx: specifier: ^2.0.0 version: 2.0.0 @@ -23,6 +26,9 @@ dependencies: tailwind-merge: specifier: ^1.14.0 version: 1.14.0 + zod: + specifier: ^3.22.4 + version: 3.22.4 devDependencies: '@commitlint/cli': @@ -548,6 +554,33 @@ packages: tslib: 2.6.2 dev: false + /@t3-oss/env-core@0.7.1(typescript@5.3.2)(zod@3.22.4): + resolution: { integrity: sha512-3+SQt39OlmSaRLqYVFv8uRm1BpFepM5TIiMytRqO9cjH+wB77o6BIJdeyM5h5U4qLBMEzOJWCY4MBaU/rLwbYw== } + peerDependencies: + typescript: '>=4.7.2' + zod: ^3.0.0 + peerDependenciesMeta: + typescript: + optional: true + dependencies: + typescript: 5.3.2 + zod: 3.22.4 + dev: false + + /@t3-oss/env-nextjs@0.7.1(typescript@5.3.2)(zod@3.22.4): + resolution: { integrity: sha512-tQDbNLGCOvKGi+JoGuJ/CJInJI7/kLWJqtgGppAKS7ZFLdVOqZYR/uRjxlXOWPnxmUKF8VswOAsq7fXUpNZDhA== } + peerDependencies: + typescript: '>=4.7.2' + zod: ^3.0.0 + peerDependenciesMeta: + typescript: + optional: true + dependencies: + '@t3-oss/env-core': 0.7.1(typescript@5.3.2)(zod@3.22.4) + typescript: 5.3.2 + zod: 3.22.4 + dev: false + /@tsconfig/node10@1.0.9: resolution: { integrity: sha512-jNsYVVxU8v5g43Erja32laIDHXeoNvFEpX33OK4d6hljo3jDhCBDhx5dhCCTMWUojscpAagGiRkBKxpdl9fxqA== } dev: true @@ -4085,7 +4118,6 @@ packages: resolution: { integrity: sha512-6l+RyNy7oAHDfxC4FzSJcz9vnjTKxrLpDG5M2Vu4SHRVNg6xzqZp6LYSR9zjqQTu8DU/f5xwxUdADOkbrIX2gQ== } engines: { node: '>=14.17' } hasBin: true - dev: true /unbox-primitive@1.0.2: resolution: { integrity: sha512-61pPlCD9h51VoreyJ0BReideM3MDKMKnh6+V9L08331ipq6Q8OFXZYiqP6n/tbHx4s5I9uRhcye6BrbkizkBDw== } @@ -4311,3 +4343,7 @@ packages: resolution: { integrity: sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q== } engines: { node: '>=10' } dev: true + + /zod@3.22.4: + resolution: { integrity: sha512-iC+8Io04lddc+mVqQ9AZ7OQ2MrUKGN+oIQyq1vemgt46jwCwLfhq7/pwnBnNXXXZb8VTVLKwp9EDkx+ryxIWmg== } + dev: false diff --git a/src/env.mjs b/src/env.mjs new file mode 100644 index 0000000..9c4e167 --- /dev/null +++ b/src/env.mjs @@ -0,0 +1,16 @@ +import { z } from 'zod'; +import { createEnv } from '@t3-oss/env-nextjs'; + +const env = createEnv({ + client: { + NEXT_PUBLIC_FE_URL: z.string().url(), + NEXT_PUBLIC_BE_URL: z.string().url(), + }, + server: {}, + runtimeEnv: { + NEXT_PUBLIC_FE_URL: process.env.NEXT_PUBLIC_FE_URL, + NEXT_PUBLIC_BE_URL: process.env.NEXT_PUBLIC_BE_URL, + }, +}); + +export default env;