Skip to content

Commit 549fc11

Browse files
committed
Add env vars for API urls
1 parent 98a2020 commit 549fc11

File tree

3 files changed

+38
-18
lines changed

3 files changed

+38
-18
lines changed

apps/web/.env.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# Database
22
DATABASE_URL=postgresql://user:password@localhost:5432/nextwiki
33

4+
# API
5+
NEXT_PUBLIC_API_URL=http://localhost:3000/api/trcp
6+
NEXT_PUBLIC_WS_URL=
7+
48
# NextAuth
59
NEXTAUTH_URL=http://localhost:3000
610
NEXTAUTH_SECRET=your-32-character-secret-key-goes-here

apps/web/src/env.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ export const env = createEnv({
3030
NEXT_PUBLIC_NODE_ENV: z
3131
.enum(["development", "production"])
3232
.default("development"),
33-
// NEXT_PUBLIC_CLIENTVAR: z.string(),
33+
NEXT_PUBLIC_API_URL: z.string().url(),
34+
NEXT_PUBLIC_WS_URL: z.string().url().optional(),
3435
},
3536

3637
/**
@@ -50,6 +51,8 @@ export const env = createEnv({
5051
GOOGLE_CLIENT_SECRET: process.env.GOOGLE_CLIENT_SECRET,
5152
OVERRIDE_MAX_LOG_LEVEL: process.env.OVERRIDE_MAX_LOG_LEVEL,
5253
PROCESS_ORIGIN: process.env.PROCESS_ORIGIN,
54+
NEXT_PUBLIC_API_URL: process.env.NEXT_PUBLIC_API_URL,
55+
NEXT_PUBLIC_WS_URL: process.env.NEXT_PUBLIC_WS_URL,
5356
// Client-side env vars
5457
// NEXT_PUBLIC_CLIENTVAR: process.env.NEXT_PUBLIC_CLIENTVAR,
5558
},

apps/web/src/server/providers.tsx

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { ReactQueryDevtools } from "@tanstack/react-query-devtools";
77
import { createWSClient, httpBatchLink, wsLink, splitLink } from "@trpc/client";
88
import { createTRPCClient } from "@trpc/client";
99
import { AppRouter } from "./routers";
10+
import { env } from "~/env";
1011

1112
function makeQueryClient() {
1213
return new QueryClient({
@@ -39,27 +40,39 @@ function getQueryClient() {
3940
}
4041

4142
export function TRPCClientProvider({ children }: PropsWithChildren) {
42-
const wsClient = createWSClient({
43-
url: "ws://localhost:3001",
44-
});
4543
const queryClient = getQueryClient();
46-
const [trpcClient] = useState(() =>
47-
createTRPCClient<AppRouter>({
48-
links: [
49-
splitLink({
50-
condition(op) {
51-
return op.type === "subscription";
52-
},
53-
true: wsLink({
54-
client: wsClient,
55-
}),
56-
false: httpBatchLink({
57-
url: "http://localhost:3000/api/trpc",
44+
45+
const [trpcClient] = useState(() => {
46+
if (env.NEXT_PUBLIC_WS_URL) {
47+
const wsClient = createWSClient({
48+
url: `ws://${env.NEXT_PUBLIC_WS_URL}`,
49+
});
50+
51+
return createTRPCClient<AppRouter>({
52+
links: [
53+
splitLink({
54+
condition(op) {
55+
return op.type === "subscription";
56+
},
57+
true: wsLink({
58+
client: wsClient,
59+
}),
60+
false: httpBatchLink({
61+
url: `${env.NEXT_PUBLIC_API_URL}/api/trpc`,
62+
}),
5863
}),
64+
],
65+
});
66+
}
67+
68+
return createTRPCClient<AppRouter>({
69+
links: [
70+
httpBatchLink({
71+
url: `${env.NEXT_PUBLIC_API_URL}/api/trpc`,
5972
}),
6073
],
61-
})
62-
);
74+
});
75+
});
6376

6477
return (
6578
<QueryClientProvider client={queryClient}>

0 commit comments

Comments
 (0)