create-lx2-app 0.11.4-beta.8b33b2b → 0.11.5-beta.1933d30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/dist/index.js +27 -25
  2. package/package.json +7 -1
  3. package/template/base/_gitignore +1 -0
  4. package/template/packages/config/prisma.config.ts +14 -0
  5. package/template/packages/prisma/schema/base.prisma +1 -1
  6. package/template/packages/prisma/schema/with-authjs.prisma +1 -1
  7. package/template/packages/prisma/schema/with-better-auth.prisma +1 -1
  8. package/template/packages/src/app/api/trpc/[trpc]/route.ts +34 -0
  9. package/template/packages/src/app/layout/with-trpc.tsx +37 -0
  10. package/template/packages/src/app/page/with-authjs-prisma.tsx +6 -0
  11. package/template/packages/src/app/page/with-better-auth-prisma.tsx +6 -0
  12. package/template/packages/src/app/page/with-prisma.tsx +6 -0
  13. package/template/packages/src/app/page/with-trpc.tsx +115 -0
  14. package/template/packages/src/components/greeting.tsx +21 -0
  15. package/template/packages/src/env/with-trpc-authjs-db.js +55 -0
  16. package/template/packages/src/env/with-trpc-authjs.js +53 -0
  17. package/template/packages/src/env/with-trpc-better-auth-db.js +54 -0
  18. package/template/packages/src/env/with-trpc-better-auth.js +52 -0
  19. package/template/packages/src/env/with-trpc-db.js +46 -0
  20. package/template/packages/src/env/with-trpc.js +44 -0
  21. package/template/packages/src/lib/api/client.tsx +85 -0
  22. package/template/packages/src/lib/api/query-client.ts +22 -0
  23. package/template/packages/src/lib/api/server.ts +31 -0
  24. package/template/packages/src/lib/utils.ts +7 -0
  25. package/template/packages/src/server/api/init/base.ts +103 -0
  26. package/template/packages/src/server/api/init/with-authjs-db.ts +132 -0
  27. package/template/packages/src/server/api/init/with-authjs.ts +130 -0
  28. package/template/packages/src/server/api/init/with-betterauth-db.ts +134 -0
  29. package/template/packages/src/server/api/init/with-betterauth.ts +132 -0
  30. package/template/packages/src/server/api/init/with-db.ts +106 -0
  31. package/template/packages/src/server/api/root.ts +23 -0
  32. package/template/packages/src/server/api/routers/post/base.ts +46 -0
  33. package/template/packages/src/server/api/routers/post/with-auth-drizzle.ts +44 -0
  34. package/template/packages/src/server/api/routers/post/with-auth-prisma.ts +47 -0
  35. package/template/packages/src/server/api/routers/post/with-auth.ts +43 -0
  36. package/template/packages/src/server/api/routers/post/with-drizzle.ts +36 -0
  37. package/template/packages/src/server/api/routers/post/with-prisma.ts +37 -0
  38. package/template/packages/src/server/auth/better-auth.ts +1 -0
  39. package/template/packages/src/server/auth/config/authjs-with-drizzle.ts +1 -1
  40. package/template/packages/src/server/auth/config/authjs-with-prisma.ts +1 -1
  41. package/template/packages/src/server/auth/config/authjs.ts +1 -1
  42. package/template/packages/src/server/db/{db-prisma.ts → prisma/with-mysql.ts} +5 -1
  43. package/template/packages/src/server/db/prisma/with-postgresql.ts +29 -0
  44. package/template/packages/src/server/db/prisma/with-sqlite-bun.ts +29 -0
  45. package/template/packages/src/server/db/prisma/with-sqlite.ts +29 -0
@@ -0,0 +1,115 @@
1
+ import { fileURLToPath } from "url"
2
+
3
+ import { Greeting } from "@/components/greeting"
4
+ import { api, HydrateClient } from "@/lib/api/server"
5
+
6
+ export const dynamic = "force-dynamic"
7
+
8
+ export default async function HomePage() {
9
+ const fileURL = `vscode://file/${fileURLToPath(import.meta.url)}`
10
+
11
+ void api.post.greeting({ text: "from tRPC!" })
12
+
13
+ return (
14
+ <HydrateClient>
15
+ <main className="mx-auto flex h-screen max-w-5xl flex-col items-center justify-between overflow-hidden p-6 sm:p-[45px]">
16
+ <div className="flex grow flex-col items-center justify-center">
17
+ {/* Logo */}
18
+ <picture className="relative">
19
+ <div className="absolute inset-0 animate-pulse bg-linear-to-r from-[oklch(0.7468_0.1455_302.21)] via-[oklch(0.7345_0.0464_270.71)] to-[oklch(0.7563_0.1807_347.17)] opacity-20 blur-lg dark:via-[oklch(0.5567_0.0816_269.53)]" />
20
+
21
+ <source srcSet="https://github.com/SlickYeet/create-lx2-app/blob/f1209465d59e03e284702d9f492f1bc1cfa49c32/docs/v2/public/android-chrome-192x192.png?raw=true" />
22
+ <img
23
+ src="https://github.com/SlickYeet/create-lx2-app/blob/f1209465d59e03e284702d9f492f1bc1cfa49c32/docs/v2/public/android-chrome-192x192.png?raw=true"
24
+ alt="Logo"
25
+ width={65}
26
+ height={65}
27
+ className="block h-auto max-w-full"
28
+ />
29
+ </picture>
30
+
31
+ {/* Title & Description */}
32
+ <h1 className="mt-6 text-5xl font-bold tracking-tight text-balance md:text-6xl lg:text-7xl">
33
+ Create{" "}
34
+ <span className="text-[oklch(0.7468_0.1455_302.21)]">Lx2</span> App
35
+ </h1>
36
+ <p className="text-center text-lg text-neutral-700 md:text-xl lg:mt-6 dark:text-neutral-300">
37
+ The Most Opinionated Way to Build Next.js Apps
38
+ </p>
39
+
40
+ {/* Links */}
41
+ <div className="mt-12 flex items-center gap-3">
42
+ <a
43
+ href="https://create.lx2.dev/docs"
44
+ target="_blank"
45
+ rel="noopener noreferrer"
46
+ className="flex items-center rounded-md border border-white/25 px-2 py-1 outline-none hover:opacity-80 focus:opacity-80 active:opacity-70"
47
+ >
48
+ Docs
49
+ <svg
50
+ xmlns="http://www.w3.org/2000/svg"
51
+ viewBox="0 0 24 24"
52
+ strokeLinecap="round"
53
+ strokeLinejoin="round"
54
+ className="mb-1.5 size-4 fill-none stroke-current stroke-2"
55
+ >
56
+ <path d="M7 7h10v10" />
57
+ <path d="M7 17 17 7" />
58
+ </svg>
59
+ </a>
60
+ <a
61
+ href="https://link.lx2.dev/discord"
62
+ target="_blank"
63
+ rel="noopener noreferrer"
64
+ className="flex items-center rounded-md border border-white/25 px-2 py-1 outline-none hover:opacity-80 focus:opacity-80 active:opacity-70"
65
+ >
66
+ Discord
67
+ <svg
68
+ xmlns="http://www.w3.org/2000/svg"
69
+ viewBox="0 0 24 24"
70
+ strokeLinecap="round"
71
+ strokeLinejoin="round"
72
+ className="mb-1.5 size-4 fill-none stroke-current stroke-2"
73
+ >
74
+ <path d="M7 7h10v10" />
75
+ <path d="M7 17 17 7" />
76
+ </svg>
77
+ </a>
78
+ <a
79
+ href="https://github.com/SlickYeet/create-lx2-app"
80
+ target="_blank"
81
+ rel="noopener noreferrer"
82
+ className="flex items-center rounded-md border border-white/25 px-2 py-1 outline-none hover:opacity-80 focus:opacity-80 active:opacity-70"
83
+ >
84
+ GitHub
85
+ <svg
86
+ xmlns="http://www.w3.org/2000/svg"
87
+ viewBox="0 0 24 24"
88
+ strokeLinecap="round"
89
+ strokeLinejoin="round"
90
+ className="mb-1.5 size-4 fill-none stroke-current stroke-2"
91
+ >
92
+ <path d="M7 7h10v10" />
93
+ <path d="M7 17 17 7" />
94
+ </svg>
95
+ </a>
96
+ </div>
97
+
98
+ {/* tRPC Greeting */}
99
+ <Greeting />
100
+ </div>
101
+
102
+ {/* Footer */}
103
+ <div className="flex flex-col items-center gap-1 text-sm text-neutral-600 lg:flex-row lg:gap-2 dark:text-neutral-400">
104
+ <p className="m-0">Get started by editing </p>
105
+ <a
106
+ href={fileURL}
107
+ className="rounded-md bg-neutral-200 px-2 py-1 dark:bg-neutral-800"
108
+ >
109
+ <code>src/app/page.tsx</code>
110
+ </a>
111
+ </div>
112
+ </main>
113
+ </HydrateClient>
114
+ )
115
+ }
@@ -0,0 +1,21 @@
1
+ "use client"
2
+
3
+ import { api } from "@/lib/api/client"
4
+
5
+ export function Greeting() {
6
+ const [greeting] = api.post.greeting.useSuspenseQuery({
7
+ text: "from tRPC!",
8
+ })
9
+
10
+ return (
11
+ <div className="mt-12 flex flex-col items-center gap-3">
12
+ <div className="mb-4">
13
+ <h2 className="mb-4 text-center">
14
+ <span className="text-2xl text-neutral-700 dark:text-neutral-300">
15
+ {greeting}
16
+ </span>
17
+ </h2>
18
+ </div>
19
+ </div>
20
+ )
21
+ }
@@ -0,0 +1,55 @@
1
+ import { createEnv } from "@t3-oss/env-nextjs"
2
+ import { z } from "zod"
3
+
4
+ export const env = createEnv({
5
+ /**
6
+ * Specify your server-side environment variables schema here. This way you can ensure the app
7
+ * isn't built with invalid env vars.
8
+ */
9
+ server: {
10
+ DATABASE_URL: z.url(),
11
+ AUTH_SECRET:
12
+ process.env.NODE_ENV === "production"
13
+ ? z.string()
14
+ : z.string().optional(),
15
+ DISCORD_CLIENT_ID: z.string(),
16
+ DISCORD_CLIENT_SECRET: z.string(),
17
+ NODE_ENV: z
18
+ .enum(["development", "test", "production"])
19
+ .default("development"),
20
+ },
21
+
22
+ /**
23
+ * Specify your client-side environment variables schema here. This way you can ensure the app
24
+ * isn't built with invalid env vars. To expose them to the client, prefix them with
25
+ * `NEXT_PUBLIC_`.
26
+ */
27
+ client: {
28
+ // NEXT_PUBLIC_CLIENTVAR: z.string(),
29
+ NEXT_PUBLIC_URL: z.url(),
30
+ },
31
+
32
+ /**
33
+ * You can't destruct `process.env` as a regular object in the Next.js edge runtimes (e.g.
34
+ * middlewares) or client-side so we need to destruct manually.
35
+ */
36
+ runtimeEnv: {
37
+ DATABASE_URL: process.env.DATABASE_URL,
38
+ AUTH_SECRET: process.env.AUTH_SECRET,
39
+ DISCORD_CLIENT_ID: process.env.DISCORD_CLIENT_ID,
40
+ DISCORD_CLIENT_SECRET: process.env.DISCORD_CLIENT_SECRET,
41
+ NODE_ENV: process.env.NODE_ENV,
42
+ // NEXT_PUBLIC_CLIENTVAR: process.env.NEXT_PUBLIC_CLIENTVAR,
43
+ NEXT_PUBLIC_URL: process.env.NEXT_PUBLIC_URL,
44
+ },
45
+ /**
46
+ * Run `build` or `dev` with `SKIP_ENV_VALIDATION` to skip env validation. This is especially
47
+ * useful for Docker builds.
48
+ */
49
+ skipValidation: !!process.env.SKIP_ENV_VALIDATION,
50
+ /**
51
+ * Makes it so that empty strings are treated as undefined. `SOME_VAR: z.string()` and
52
+ * `SOME_VAR=''` will throw an error.
53
+ */
54
+ emptyStringAsUndefined: true,
55
+ })
@@ -0,0 +1,53 @@
1
+ import { createEnv } from "@t3-oss/env-nextjs"
2
+ import { z } from "zod"
3
+
4
+ export const env = createEnv({
5
+ /**
6
+ * Specify your server-side environment variables schema here. This way you can ensure the app
7
+ * isn't built with invalid env vars.
8
+ */
9
+ server: {
10
+ AUTH_SECRET:
11
+ process.env.NODE_ENV === "production"
12
+ ? z.string()
13
+ : z.string().optional(),
14
+ DISCORD_CLIENT_ID: z.string(),
15
+ DISCORD_CLIENT_SECRET: z.string(),
16
+ NODE_ENV: z
17
+ .enum(["development", "test", "production"])
18
+ .default("development"),
19
+ },
20
+
21
+ /**
22
+ * Specify your client-side environment variables schema here. This way you can ensure the app
23
+ * isn't built with invalid env vars. To expose them to the client, prefix them with
24
+ * `NEXT_PUBLIC_`.
25
+ */
26
+ client: {
27
+ // NEXT_PUBLIC_CLIENTVAR: z.string(),
28
+ NEXT_PUBLIC_URL: z.url(),
29
+ },
30
+
31
+ /**
32
+ * You can't destruct `process.env` as a regular object in the Next.js edge runtimes (e.g.
33
+ * middlewares) or client-side so we need to destruct manually.
34
+ */
35
+ runtimeEnv: {
36
+ AUTH_SECRET: process.env.AUTH_SECRET,
37
+ DISCORD_CLIENT_ID: process.env.DISCORD_CLIENT_ID,
38
+ DISCORD_CLIENT_SECRET: process.env.DISCORD_CLIENT_SECRET,
39
+ NODE_ENV: process.env.NODE_ENV,
40
+ // NEXT_PUBLIC_CLIENTVAR: process.env.NEXT_PUBLIC_CLIENTVAR,
41
+ NEXT_PUBLIC_URL: process.env.NEXT_PUBLIC_URL,
42
+ },
43
+ /**
44
+ * Run `build` or `dev` with `SKIP_ENV_VALIDATION` to skip env validation. This is especially
45
+ * useful for Docker builds.
46
+ */
47
+ skipValidation: !!process.env.SKIP_ENV_VALIDATION,
48
+ /**
49
+ * Makes it so that empty strings are treated as undefined. `SOME_VAR: z.string()` and
50
+ * `SOME_VAR=''` will throw an error.
51
+ */
52
+ emptyStringAsUndefined: true,
53
+ })
@@ -0,0 +1,54 @@
1
+ import { createEnv } from "@t3-oss/env-nextjs"
2
+ import { z } from "zod"
3
+
4
+ export const env = createEnv({
5
+ /**
6
+ * Specify your server-side environment variables schema here. This way you can ensure the app
7
+ * isn't built with invalid env vars.
8
+ */
9
+ server: {
10
+ DATABASE_URL: z.url(),
11
+ NODE_ENV: z
12
+ .enum(["development", "test", "production"])
13
+ .default("development"),
14
+ BETTER_AUTH_SECRET: z.string(),
15
+ DISCORD_CLIENT_ID: z.string(),
16
+ DISCORD_CLIENT_SECRET: z.string(),
17
+ },
18
+
19
+ /**
20
+ * Specify your client-side environment variables schema here. This way you can ensure the app
21
+ * isn't built with invalid env vars. To expose them to the client, prefix them with
22
+ * `NEXT_PUBLIC_`.
23
+ */
24
+ client: {
25
+ // NEXT_PUBLIC_CLIENTVAR: z.string(),
26
+ NEXT_PUBLIC_URL: z.url(),
27
+ NEXT_PUBLIC_BETTER_AUTH_URL: z.url(),
28
+ },
29
+
30
+ /**
31
+ * You can't destruct `process.env` as a regular object in the Next.js edge runtimes (e.g.
32
+ * middlewares) or client-side so we need to destruct manually.
33
+ */
34
+ runtimeEnv: {
35
+ DATABASE_URL: process.env.DATABASE_URL,
36
+ BETTER_AUTH_SECRET: process.env.BETTER_AUTH_SECRET,
37
+ DISCORD_CLIENT_ID: process.env.DISCORD_CLIENT_ID,
38
+ DISCORD_CLIENT_SECRET: process.env.DISCORD_CLIENT_SECRET,
39
+ NODE_ENV: process.env.NODE_ENV,
40
+ // NEXT_PUBLIC_CLIENTVAR: process.env.NEXT_PUBLIC_CLIENTVAR,
41
+ NEXT_PUBLIC_URL: process.env.NEXT_PUBLIC_URL,
42
+ NEXT_PUBLIC_BETTER_AUTH_URL: process.env.NEXT_PUBLIC_BETTER_AUTH_URL,
43
+ },
44
+ /**
45
+ * Run `build` or `dev` with `SKIP_ENV_VALIDATION` to skip env validation. This is especially
46
+ * useful for Docker builds.
47
+ */
48
+ skipValidation: !!process.env.SKIP_ENV_VALIDATION,
49
+ /**
50
+ * Makes it so that empty strings are treated as undefined. `SOME_VAR: z.string()` and
51
+ * `SOME_VAR=''` will throw an error.
52
+ */
53
+ emptyStringAsUndefined: true,
54
+ })
@@ -0,0 +1,52 @@
1
+ import { createEnv } from "@t3-oss/env-nextjs"
2
+ import { z } from "zod"
3
+
4
+ export const env = createEnv({
5
+ /**
6
+ * Specify your server-side environment variables schema here. This way you can ensure the app
7
+ * isn't built with invalid env vars.
8
+ */
9
+ server: {
10
+ NODE_ENV: z
11
+ .enum(["development", "test", "production"])
12
+ .default("development"),
13
+ BETTER_AUTH_SECRET: z.string(),
14
+ DISCORD_CLIENT_ID: z.string(),
15
+ DISCORD_CLIENT_SECRET: z.string(),
16
+ },
17
+
18
+ /**
19
+ * Specify your client-side environment variables schema here. This way you can ensure the app
20
+ * isn't built with invalid env vars. To expose them to the client, prefix them with
21
+ * `NEXT_PUBLIC_`.
22
+ */
23
+ client: {
24
+ // NEXT_PUBLIC_CLIENTVAR: z.string(),
25
+ NEXT_PUBLIC_URL: z.url(),
26
+ NEXT_PUBLIC_BETTER_AUTH_URL: z.url(),
27
+ },
28
+
29
+ /**
30
+ * You can't destruct `process.env` as a regular object in the Next.js edge runtimes (e.g.
31
+ * middlewares) or client-side so we need to destruct manually.
32
+ */
33
+ runtimeEnv: {
34
+ BETTER_AUTH_SECRET: process.env.BETTER_AUTH_SECRET,
35
+ DISCORD_CLIENT_ID: process.env.DISCORD_CLIENT_ID,
36
+ DISCORD_CLIENT_SECRET: process.env.DISCORD_CLIENT_SECRET,
37
+ NODE_ENV: process.env.NODE_ENV,
38
+ // NEXT_PUBLIC_CLIENTVAR: process.env.NEXT_PUBLIC_CLIENTVAR,
39
+ NEXT_PUBLIC_URL: process.env.NEXT_PUBLIC_URL,
40
+ NEXT_PUBLIC_BETTER_AUTH_URL: process.env.NEXT_PUBLIC_BETTER_AUTH_URL,
41
+ },
42
+ /**
43
+ * Run `build` or `dev` with `SKIP_ENV_VALIDATION` to skip env validation. This is especially
44
+ * useful for Docker builds.
45
+ */
46
+ skipValidation: !!process.env.SKIP_ENV_VALIDATION,
47
+ /**
48
+ * Makes it so that empty strings are treated as undefined. `SOME_VAR: z.string()` and
49
+ * `SOME_VAR=''` will throw an error.
50
+ */
51
+ emptyStringAsUndefined: true,
52
+ })
@@ -0,0 +1,46 @@
1
+ import { createEnv } from "@t3-oss/env-nextjs"
2
+ import { z } from "zod"
3
+
4
+ export const env = createEnv({
5
+ /**
6
+ * Specify your server-side environment variables schema here. This way you can ensure the app
7
+ * isn't built with invalid env vars.
8
+ */
9
+ server: {
10
+ DATABASE_URL: z.url(),
11
+ NODE_ENV: z
12
+ .enum(["development", "test", "production"])
13
+ .default("development"),
14
+ },
15
+
16
+ /**
17
+ * Specify your client-side environment variables schema here. This way you can ensure the app
18
+ * isn't built with invalid env vars. To expose them to the client, prefix them with
19
+ * `NEXT_PUBLIC_`.
20
+ */
21
+ client: {
22
+ // NEXT_PUBLIC_CLIENTVAR: z.string(),
23
+ NEXT_PUBLIC_URL: z.url(),
24
+ },
25
+
26
+ /**
27
+ * You can't destruct `process.env` as a regular object in the Next.js edge runtimes (e.g.
28
+ * middlewares) or client-side so we need to destruct manually.
29
+ */
30
+ runtimeEnv: {
31
+ DATABASE_URL: process.env.DATABASE_URL,
32
+ NODE_ENV: process.env.NODE_ENV,
33
+ // NEXT_PUBLIC_CLIENTVAR: process.env.NEXT_PUBLIC_CLIENTVAR,
34
+ NEXT_PUBLIC_URL: process.env.NEXT_PUBLIC_URL,
35
+ },
36
+ /**
37
+ * Run `build` or `dev` with `SKIP_ENV_VALIDATION` to skip env validation. This is especially
38
+ * useful for Docker builds.
39
+ */
40
+ skipValidation: !!process.env.SKIP_ENV_VALIDATION,
41
+ /**
42
+ * Makes it so that empty strings are treated as undefined. `SOME_VAR: z.string()` and
43
+ * `SOME_VAR=''` will throw an error.
44
+ */
45
+ emptyStringAsUndefined: true,
46
+ })
@@ -0,0 +1,44 @@
1
+ import { createEnv } from "@t3-oss/env-nextjs"
2
+ import { z } from "zod"
3
+
4
+ export const env = createEnv({
5
+ /**
6
+ * Specify your server-side environment variables schema here. This way you can ensure the app
7
+ * isn't built with invalid env vars.
8
+ */
9
+ server: {
10
+ NODE_ENV: z
11
+ .enum(["development", "test", "production"])
12
+ .default("development"),
13
+ },
14
+
15
+ /**
16
+ * Specify your client-side environment variables schema here. This way you can ensure the app
17
+ * isn't built with invalid env vars. To expose them to the client, prefix them with
18
+ * `NEXT_PUBLIC_`.
19
+ */
20
+ client: {
21
+ // NEXT_PUBLIC_CLIENTVAR: z.string(),
22
+ NEXT_PUBLIC_URL: z.url(),
23
+ },
24
+
25
+ /**
26
+ * You can't destruct `process.env` as a regular object in the Next.js edge runtimes (e.g.
27
+ * middlewares) or client-side so we need to destruct manually.
28
+ */
29
+ runtimeEnv: {
30
+ NODE_ENV: process.env.NODE_ENV,
31
+ // NEXT_PUBLIC_CLIENTVAR: process.env.NEXT_PUBLIC_CLIENTVAR,
32
+ NEXT_PUBLIC_URL: process.env.NEXT_PUBLIC_URL,
33
+ },
34
+ /**
35
+ * Run `build` or `dev` with `SKIP_ENV_VALIDATION` to skip env validation. This is especially
36
+ * useful for Docker builds.
37
+ */
38
+ skipValidation: !!process.env.SKIP_ENV_VALIDATION,
39
+ /**
40
+ * Makes it so that empty strings are treated as undefined. `SOME_VAR: z.string()` and
41
+ * `SOME_VAR=''` will throw an error.
42
+ */
43
+ emptyStringAsUndefined: true,
44
+ })
@@ -0,0 +1,85 @@
1
+ "use client"
2
+
3
+ import { QueryClientProvider, type QueryClient } from "@tanstack/react-query"
4
+ import { httpBatchStreamLink, loggerLink } from "@trpc/client"
5
+ import { createTRPCReact } from "@trpc/react-query"
6
+ import { type inferRouterInputs, type inferRouterOutputs } from "@trpc/server"
7
+ import React from "react"
8
+ import superjson from "superjson"
9
+
10
+ import { getBaseUrl } from "@/lib/utils"
11
+ import { type AppRouter } from "@/server/api/root"
12
+
13
+ import { createQueryClient } from "./query-client"
14
+
15
+ let clientQueryClientSingleton: QueryClient | undefined = undefined
16
+ function getQueryClient() {
17
+ if (typeof window === "undefined") {
18
+ // Server: always make a new query client
19
+ return createQueryClient()
20
+ }
21
+
22
+ // Browser: use singleton pattern to keep the same query client
23
+ clientQueryClientSingleton ??= createQueryClient()
24
+ return clientQueryClientSingleton
25
+ }
26
+
27
+ export const api = createTRPCReact<AppRouter>()
28
+
29
+ /**
30
+ * Inference helper for inputs.
31
+ *
32
+ * @example type HelloInput = RouterInputs["helloWorld"]["hello"]
33
+ */
34
+ export type RouterInputs = inferRouterInputs<AppRouter>
35
+
36
+ /**
37
+ * Inference helper for outputs.
38
+ *
39
+ * @example type HelloOutput = RouterOutputs["helloWorld"]["hello"]
40
+ */
41
+ export type RouterOutputs = inferRouterOutputs<AppRouter>
42
+
43
+ export function TRPCReactProvider({ children }: React.PropsWithChildren) {
44
+ const queryClient = getQueryClient()
45
+
46
+ const [trpcClient] = React.useState(() =>
47
+ api.createClient({
48
+ links: [
49
+ /**
50
+ * The loggerLink is useful for debugging, but can be very noisy.
51
+ * You can disable Query logging by commenting out the process.env.NODE_ENV check.
52
+ *
53
+ * @example
54
+ * loggerLink({
55
+ * enabled: (opts) =>
56
+ * // process.env.NODE_ENV === "development" ||
57
+ * opts.direction === "down" && opts.result instanceof Error,
58
+ * }),
59
+ */
60
+ loggerLink({
61
+ enabled: (opts) =>
62
+ process.env.NODE_ENV === "development" ||
63
+ (opts.direction === "down" && opts.result instanceof Error),
64
+ }),
65
+ httpBatchStreamLink({
66
+ transformer: superjson,
67
+ url: `${getBaseUrl()}/api/trpc`,
68
+ headers() {
69
+ const headers = new Headers()
70
+ headers.set("x-trpc-source", "nextjs-react")
71
+ return headers
72
+ },
73
+ }),
74
+ ],
75
+ })
76
+ )
77
+
78
+ return (
79
+ <QueryClientProvider client={queryClient}>
80
+ <api.Provider client={trpcClient} queryClient={queryClient}>
81
+ {children}
82
+ </api.Provider>
83
+ </QueryClientProvider>
84
+ )
85
+ }
@@ -0,0 +1,22 @@
1
+ import { defaultShouldDehydrateQuery, QueryClient } from "@tanstack/react-query"
2
+ import superjson from "superjson"
3
+
4
+ export const createQueryClient = () =>
5
+ new QueryClient({
6
+ defaultOptions: {
7
+ queries: {
8
+ // With SSR, we usually want to set some default staleTime
9
+ // above 0 to avoid refetching immediately on the client
10
+ staleTime: 1000 * 30,
11
+ },
12
+ dehydrate: {
13
+ serializeData: superjson.serialize,
14
+ shouldDehydrateQuery: (query) =>
15
+ defaultShouldDehydrateQuery(query) ||
16
+ query.state.status === "pending",
17
+ },
18
+ hydrate: {
19
+ deserializeData: superjson.deserialize,
20
+ },
21
+ },
22
+ })
@@ -0,0 +1,31 @@
1
+ import "server-only"
2
+
3
+ import { createHydrationHelpers } from "@trpc/react-query/rsc"
4
+ import { headers } from "next/headers"
5
+ import React from "react"
6
+
7
+ import { createTRPCContext } from "@/server/api/init"
8
+ import { createCaller, type AppRouter } from "@/server/api/root"
9
+
10
+ import { createQueryClient } from "./query-client"
11
+
12
+ /**
13
+ * This wraps the `createTRPCContext` helper and provides the required context for the tRPC API when
14
+ * handling a tRPC call from a React Server Component.
15
+ */
16
+ const createContext = React.cache(async () => {
17
+ const heads = new Headers(await headers())
18
+ heads.set("x-trpc-source", "rsc")
19
+
20
+ return createTRPCContext({
21
+ headers: heads,
22
+ })
23
+ })
24
+
25
+ const caller = createCaller(createContext)
26
+ const getQueryClient = React.cache(createQueryClient)
27
+
28
+ export const { trpc: api, HydrateClient } = createHydrationHelpers<AppRouter>(
29
+ caller,
30
+ getQueryClient
31
+ )
@@ -0,0 +1,7 @@
1
+ import { env } from "@/env"
2
+
3
+ export function getBaseUrl() {
4
+ if (typeof window !== "undefined") return window.location.origin
5
+ if (process.env.VERCEL_URL) return `https://${process.env.VERCEL_URL}`
6
+ return env.NEXT_PUBLIC_URL
7
+ }