Skip to content

Commit

Permalink
Add env package, configure web API urls in .env
Browse files Browse the repository at this point in the history
  • Loading branch information
solomonhawk committed Oct 30, 2024
1 parent 50e04f3 commit 35fccc7
Show file tree
Hide file tree
Showing 14 changed files with 88 additions and 22 deletions.
5 changes: 2 additions & 3 deletions apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@
"@hono/node-server": "^1.13.2",
"@hono/trpc-server": "^0.3.2",
"@manifold/auth": "*",
"@manifold/env": "^0.0.0",
"@manifold/router": "*",
"@t3-oss/env-core": "^0.11.1",
"dotenv": "^16.4.5",
"hono": "^4.6.5",
"zod": "^3.23.8"
"hono": "^4.6.5"
},
"devDependencies": {
"@manifold/eslint-config": "*",
Expand Down
3 changes: 1 addition & 2 deletions apps/api/src/env.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import "dotenv/config";

import { createEnv } from "@t3-oss/env-core";
import { createEnv, z } from "@manifold/env";
import type { Context } from "hono";
import { env } from "hono/adapter";
import { z } from "zod";

export const getEnv = (c?: Context) =>
createEnv({
Expand Down
8 changes: 8 additions & 0 deletions apps/web/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# custom browser to use when vite opens the app
BROWSER=

# vite proxies the API in local development
API_PROXY_URL=http://localhost:3000

# so the client should make api requests to the vite proxy
PUBLIC_API_BASE_URL=http://localhost:5173
2 changes: 2 additions & 0 deletions apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"@hookform/resolvers": "^3.9.0",
"@manifold/auth": "*",
"@manifold/engine": "*",
"@manifold/env": "^0.0.0",
"@manifold/lib": "*",
"@manifold/router": "*",
"@manifold/tailwind-config": "*",
Expand Down Expand Up @@ -53,6 +54,7 @@
"eslint": "^8.57.0",
"postcss": "^8.4.47",
"tailwindcss": "^3.4.13",
"tsx": "4.19.2",
"typescript": "^5.6.3",
"vite": "^5.1.4",
"vite-plugin-comlink": "^5.0.1",
Expand Down
15 changes: 15 additions & 0 deletions apps/web/src/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { createEnv, z } from "@manifold/env";

console.log(import.meta.env);
export const env = createEnv({
server: {
API_PROXY_URL: z.string(),
},
client: {
PUBLIC_API_BASE_URL: z.string(),
},
clientPrefix: "PUBLIC_",
runtimeEnv: import.meta.env,
});

export type Env = typeof env;
4 changes: 3 additions & 1 deletion apps/web/src/utils/trpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { httpBatchLink } from "@trpc/client";
import { createTRPCReact } from "@trpc/react-query";
import superjson from "superjson";

import { env } from "~env";

/**
* This type annotation feels unnecessary, but `tsc` chokes without it.
*
Expand All @@ -16,7 +18,7 @@ export const trpcClient = trpc.createClient({
transformer: superjson,
links: [
httpBatchLink({
url: "http://localhost:5173/api/trpc",
url: new URL("/api/trpc", env.PUBLIC_API_BASE_URL).toString(),
}),
],
});
Expand Down
17 changes: 16 additions & 1 deletion apps/web/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
import "dotenv/config";

import react from "@vitejs/plugin-react";
import jotaiDebugLabel from "jotai/babel/plugin-debug-label";
import jotaiReactRefresh from "jotai/babel/plugin-react-refresh";
import { tsImport } from "tsx/esm/api";
import { defineConfig } from "vite";
import { comlink } from "vite-plugin-comlink";
import tsconfigPaths from "vite-tsconfig-paths";

import type { Env } from "./src/env";

/**
* Cannot import TS ESM modules directly in Vite config, so we rely on `tsx`'s
* `tsImport` helper to load the `env` module, which we then have to cast to the
* correct type manually, :sadge:
*/
const { env } = (await tsImport("./src/env.ts", {
parentURL: import.meta.url,
})) as { env: Env };

export default defineConfig({
envPrefix: "PUBLIC_",
plugins: [
comlink(),
react({ babel: { plugins: [jotaiDebugLabel, jotaiReactRefresh] } }),
Expand All @@ -17,7 +32,7 @@ export default defineConfig({
server: {
proxy: {
"/api": {
target: "http://localhost:3000",
target: env.API_PROXY_URL,
changeOrigin: true,
},
},
Expand Down
27 changes: 19 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions packages/db/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@
},
"dependencies": {
"@auth/core": "^0.37.2",
"@t3-oss/env-core": "^0.11.1",
"@manifold/env": "^0.0.0",
"dotenv": "^16.4.5",
"drizzle-orm": "^0.35.3",
"pg": "^8.13.0",
"zod": "^3.23.8"
"pg": "^8.13.0"
}
}
3 changes: 1 addition & 2 deletions packages/db/src/env.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import "dotenv/config";

import { createEnv } from "@t3-oss/env-core";
import { z } from "zod";
import { createEnv, z } from "@manifold/env";

export const env = createEnv({
server: {
Expand Down
1 change: 1 addition & 0 deletions packages/env/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# `@manifold/env`
13 changes: 13 additions & 0 deletions packages/env/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "@manifold/env",
"version": "0.0.0",
"private": true,
"type": "module",
"exports": {
".": "./src/index.ts"
},
"dependencies": {
"@t3-oss/env-core": "^0.11.1",
"zod": "^3.23.8"
}
}
4 changes: 4 additions & 0 deletions packages/env/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { createEnv } from "@t3-oss/env-core";
import { z } from "zod";

export { createEnv, z };
3 changes: 1 addition & 2 deletions packages/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@
"react-resizable-panels": "^2.1.4",
"sonner": "^1.5.0",
"tailwind-merge": "^2.5.3",
"tailwindcss-animate": "^1.0.7",
"zod": "^3.23.8"
"tailwindcss-animate": "^1.0.7"
},
"lint-staged": {
"**/*.{js,ts,tsx}": [
Expand Down

0 comments on commit 35fccc7

Please sign in to comment.