@tanstack/cta-framework-react-cra 0.16.6 → 0.16.8
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.
- package/add-ons/oRPC/assets/src/orpc/client.ts +29 -0
- package/add-ons/oRPC/assets/src/orpc/router/index.ts +6 -0
- package/add-ons/oRPC/assets/src/orpc/router/todos.ts +20 -0
- package/add-ons/oRPC/assets/src/orpc/schema.ts +6 -0
- package/add-ons/oRPC/assets/src/polyfill.ts +21 -0
- package/add-ons/oRPC/assets/src/routes/api.$.ts +73 -0
- package/add-ons/oRPC/assets/src/routes/api.rpc.$.ts +25 -0
- package/add-ons/oRPC/assets/src/routes/demo.orpc-todo.tsx +82 -0
- package/add-ons/oRPC/info.json +17 -0
- package/add-ons/oRPC/package.json +11 -0
- package/add-ons/oRPC/small-logo.svg +1 -0
- package/add-ons/start/assets/src/routes/api.demo-names.ts +3 -1
- package/add-ons/start/assets/vite.config.ts.ejs +6 -2
- package/add-ons/tRPC/assets/src/integrations/trpc/router.ts +19 -13
- package/add-ons/tRPC/assets/src/routes/demo.trpc-todo.tsx +76 -0
- package/add-ons/tRPC/info.json +9 -1
- package/add-ons/tanstack-query/assets/src/integrations/tanstack-query/root-provider.tsx.ejs +2 -2
- package/add-ons/tanstack-query/assets/src/routes/demo.tanstack-query.tsx.ejs +24 -25
- package/package.json +2 -2
- package/project/base/src/routes/__root.tsx.ejs +3 -2
- package/tests/snapshots/react-cra/cr-ts-start-npm.json +3 -3
- package/tests/snapshots/react-cra/cr-ts-start-tanstack-query-npm.json +4 -4
- package/toolchains/eslint/assets/eslint.config.js +2 -2
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { createRouterClient } from '@orpc/server'
|
|
2
|
+
import { createORPCClient } from '@orpc/client'
|
|
3
|
+
import { RPCLink } from '@orpc/client/fetch'
|
|
4
|
+
import { createTanstackQueryUtils } from '@orpc/tanstack-query'
|
|
5
|
+
import { getHeaders } from '@tanstack/react-start/server'
|
|
6
|
+
import { createIsomorphicFn } from '@tanstack/react-start'
|
|
7
|
+
|
|
8
|
+
import type { RouterClient } from '@orpc/server'
|
|
9
|
+
|
|
10
|
+
import router from '@/orpc/router'
|
|
11
|
+
|
|
12
|
+
const getORPCClient = createIsomorphicFn()
|
|
13
|
+
.server(() =>
|
|
14
|
+
createRouterClient(router, {
|
|
15
|
+
context: () => ({
|
|
16
|
+
headers: getHeaders(),
|
|
17
|
+
}),
|
|
18
|
+
}),
|
|
19
|
+
)
|
|
20
|
+
.client((): RouterClient<typeof router> => {
|
|
21
|
+
const link = new RPCLink({
|
|
22
|
+
url: `${window.location.origin}/api/rpc`,
|
|
23
|
+
})
|
|
24
|
+
return createORPCClient(link)
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
export const client: RouterClient<typeof router> = getORPCClient()
|
|
28
|
+
|
|
29
|
+
export const orpc = createTanstackQueryUtils(client)
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { os } from '@orpc/server'
|
|
2
|
+
import * as z from 'zod'
|
|
3
|
+
|
|
4
|
+
const todos = [
|
|
5
|
+
{ id: 1, name: 'Get groceries' },
|
|
6
|
+
{ id: 2, name: 'Buy a new phone' },
|
|
7
|
+
{ id: 3, name: 'Finish the project' },
|
|
8
|
+
]
|
|
9
|
+
|
|
10
|
+
export const listTodos = os.input(z.object({})).handler(() => {
|
|
11
|
+
return todos
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
export const addTodo = os
|
|
15
|
+
.input(z.object({ name: z.string() }))
|
|
16
|
+
.handler(({ input }) => {
|
|
17
|
+
const newTodo = { id: todos.length + 1, name: input.name }
|
|
18
|
+
todos.push(newTodo)
|
|
19
|
+
return newTodo
|
|
20
|
+
})
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { File } from "node:buffer";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* This file aims to polyfill missing APIs in Node.js 18 that oRPC depends on.
|
|
5
|
+
*
|
|
6
|
+
* Since Stackblitz runs on Node.js 18, these polyfills ensure oRPC works in that environment.
|
|
7
|
+
* If you're running oRPC locally, please use Node.js 20 or later for full compatibility.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Note: Stackblitz provides an emulated Node.js environment with inherent limitations.
|
|
12
|
+
* If you encounter issues, please test on a local setup with Node.js 20 or later before reporting them.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The `oz.file()` schema depends on the `File` API.
|
|
17
|
+
* If you're not using `oz.file()`, you can safely remove this polyfill.
|
|
18
|
+
*/
|
|
19
|
+
if (typeof globalThis.File === "undefined") {
|
|
20
|
+
globalThis.File = File as any;
|
|
21
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import '@/polyfill'
|
|
2
|
+
|
|
3
|
+
import { OpenAPIHandler } from '@orpc/openapi/fetch'
|
|
4
|
+
import { ZodToJsonSchemaConverter } from '@orpc/zod/zod4'
|
|
5
|
+
import { experimental_SmartCoercionPlugin as SmartCoercionPlugin } from '@orpc/json-schema'
|
|
6
|
+
import { createServerFileRoute } from '@tanstack/react-start/server'
|
|
7
|
+
import { onError } from '@orpc/server'
|
|
8
|
+
import { OpenAPIReferencePlugin } from '@orpc/openapi/plugins'
|
|
9
|
+
|
|
10
|
+
import { TodoSchema } from '@/orpc/schema'
|
|
11
|
+
import router from '@/orpc/router'
|
|
12
|
+
|
|
13
|
+
const handler = new OpenAPIHandler(router, {
|
|
14
|
+
interceptors: [
|
|
15
|
+
onError((error) => {
|
|
16
|
+
console.error(error)
|
|
17
|
+
}),
|
|
18
|
+
],
|
|
19
|
+
plugins: [
|
|
20
|
+
new SmartCoercionPlugin({
|
|
21
|
+
schemaConverters: [new ZodToJsonSchemaConverter()],
|
|
22
|
+
}),
|
|
23
|
+
new OpenAPIReferencePlugin({
|
|
24
|
+
schemaConverters: [new ZodToJsonSchemaConverter()],
|
|
25
|
+
specGenerateOptions: {
|
|
26
|
+
info: {
|
|
27
|
+
title: 'TanStack ORPC Playground',
|
|
28
|
+
version: '1.0.0',
|
|
29
|
+
},
|
|
30
|
+
commonSchemas: {
|
|
31
|
+
Todo: { schema: TodoSchema },
|
|
32
|
+
UndefinedError: { error: 'UndefinedError' },
|
|
33
|
+
},
|
|
34
|
+
security: [{ bearerAuth: [] }],
|
|
35
|
+
components: {
|
|
36
|
+
securitySchemes: {
|
|
37
|
+
bearerAuth: {
|
|
38
|
+
type: 'http',
|
|
39
|
+
scheme: 'bearer',
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
docsConfig: {
|
|
45
|
+
authentication: {
|
|
46
|
+
securitySchemes: {
|
|
47
|
+
bearerAuth: {
|
|
48
|
+
token: 'default-token',
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
}),
|
|
54
|
+
],
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
async function handle({ request }: { request: Request }) {
|
|
58
|
+
const { response } = await handler.handle(request, {
|
|
59
|
+
prefix: '/api',
|
|
60
|
+
context: {},
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
return response ?? new Response('Not Found', { status: 404 })
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export const ServerRoute = createServerFileRoute('/api/$').methods({
|
|
67
|
+
HEAD: handle,
|
|
68
|
+
GET: handle,
|
|
69
|
+
POST: handle,
|
|
70
|
+
PUT: handle,
|
|
71
|
+
PATCH: handle,
|
|
72
|
+
DELETE: handle,
|
|
73
|
+
})
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import '@/polyfill'
|
|
2
|
+
|
|
3
|
+
import { RPCHandler } from '@orpc/server/fetch'
|
|
4
|
+
import { createServerFileRoute } from '@tanstack/react-start/server'
|
|
5
|
+
import router from '@/orpc/router'
|
|
6
|
+
|
|
7
|
+
const handler = new RPCHandler(router)
|
|
8
|
+
|
|
9
|
+
async function handle({ request }: { request: Request }) {
|
|
10
|
+
const { response } = await handler.handle(request, {
|
|
11
|
+
prefix: '/api/rpc',
|
|
12
|
+
context: {},
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
return response ?? new Response('Not Found', { status: 404 })
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export const ServerRoute = createServerFileRoute('/api/rpc/$').methods({
|
|
19
|
+
HEAD: handle,
|
|
20
|
+
GET: handle,
|
|
21
|
+
POST: handle,
|
|
22
|
+
PUT: handle,
|
|
23
|
+
PATCH: handle,
|
|
24
|
+
DELETE: handle,
|
|
25
|
+
})
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { useCallback, useState } from 'react'
|
|
2
|
+
import { createFileRoute } from '@tanstack/react-router'
|
|
3
|
+
import { useMutation, useQuery } from '@tanstack/react-query'
|
|
4
|
+
|
|
5
|
+
import { orpc } from '@/orpc/client'
|
|
6
|
+
|
|
7
|
+
export const Route = createFileRoute('/demo/orpc-todo')({
|
|
8
|
+
component: ORPCTodos,
|
|
9
|
+
loader: async ({ context }) => {
|
|
10
|
+
await context.queryClient.prefetchQuery(
|
|
11
|
+
orpc.listTodos.queryOptions({
|
|
12
|
+
input: {},
|
|
13
|
+
}),
|
|
14
|
+
)
|
|
15
|
+
},
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
function ORPCTodos() {
|
|
19
|
+
const { data, refetch } = useQuery(
|
|
20
|
+
orpc.listTodos.queryOptions({
|
|
21
|
+
input: {},
|
|
22
|
+
}),
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
const [todo, setTodo] = useState('')
|
|
26
|
+
const { mutate: addTodo } = useMutation({
|
|
27
|
+
mutationFn: orpc.addTodo.call,
|
|
28
|
+
onSuccess: () => {
|
|
29
|
+
refetch()
|
|
30
|
+
setTodo('')
|
|
31
|
+
},
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
const submitTodo = useCallback(() => {
|
|
35
|
+
addTodo({ name: todo })
|
|
36
|
+
}, [addTodo, todo])
|
|
37
|
+
|
|
38
|
+
return (
|
|
39
|
+
<div
|
|
40
|
+
className="flex items-center justify-center min-h-screen bg-gradient-to-br from-purple-100 to-blue-100 p-4 text-white"
|
|
41
|
+
style={{
|
|
42
|
+
backgroundImage:
|
|
43
|
+
'radial-gradient(50% 50% at 50% 50%, #D2149D 0%, #8E1066 50%, #2D0A1F 100%)',
|
|
44
|
+
}}
|
|
45
|
+
>
|
|
46
|
+
<div className="w-full max-w-2xl p-8 rounded-xl backdrop-blur-md bg-black/50 shadow-xl border-8 border-black/10">
|
|
47
|
+
<h1 className="text-2xl mb-4">Todos list</h1>
|
|
48
|
+
<ul className="mb-4 space-y-2">
|
|
49
|
+
{data?.map((t) => (
|
|
50
|
+
<li
|
|
51
|
+
key={t.id}
|
|
52
|
+
className="bg-white/10 border border-white/20 rounded-lg p-3 backdrop-blur-sm shadow-md"
|
|
53
|
+
>
|
|
54
|
+
<span className="text-lg text-white">{t.name}</span>
|
|
55
|
+
</li>
|
|
56
|
+
))}
|
|
57
|
+
</ul>
|
|
58
|
+
<div className="flex flex-col gap-2">
|
|
59
|
+
<input
|
|
60
|
+
type="text"
|
|
61
|
+
value={todo}
|
|
62
|
+
onChange={(e) => setTodo(e.target.value)}
|
|
63
|
+
onKeyDown={(e) => {
|
|
64
|
+
if (e.key === 'Enter') {
|
|
65
|
+
submitTodo()
|
|
66
|
+
}
|
|
67
|
+
}}
|
|
68
|
+
placeholder="Enter a new todo..."
|
|
69
|
+
className="w-full px-4 py-3 rounded-lg border border-white/20 bg-white/10 backdrop-blur-sm text-white placeholder-white/60 focus:outline-none focus:ring-2 focus:ring-blue-400 focus:border-transparent"
|
|
70
|
+
/>
|
|
71
|
+
<button
|
|
72
|
+
disabled={todo.trim().length === 0}
|
|
73
|
+
onClick={submitTodo}
|
|
74
|
+
className="bg-blue-500 hover:bg-blue-600 disabled:bg-blue-500/50 disabled:cursor-not-allowed text-white font-bold py-3 px-4 rounded-lg transition-colors"
|
|
75
|
+
>
|
|
76
|
+
Add todo
|
|
77
|
+
</button>
|
|
78
|
+
</div>
|
|
79
|
+
</div>
|
|
80
|
+
</div>
|
|
81
|
+
)
|
|
82
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "oRPC",
|
|
3
|
+
"description": "Integrate oRPC into your application.",
|
|
4
|
+
"phase": "add-on",
|
|
5
|
+
"modes": ["file-router"],
|
|
6
|
+
"link": "https://orpc.unnoq.com/",
|
|
7
|
+
"dependsOn": ["tanstack-query", "start"],
|
|
8
|
+
"type": "add-on",
|
|
9
|
+
"routes": [
|
|
10
|
+
{
|
|
11
|
+
"url": "/demo/orpc-todo",
|
|
12
|
+
"name": "oRPC Todo",
|
|
13
|
+
"path": "src/routes/demo.orpc-todo.tsx",
|
|
14
|
+
"jsName": "ORPCTodos"
|
|
15
|
+
}
|
|
16
|
+
]
|
|
17
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" width="512" height="512" fill="none" viewBox="0 0 512 512"><rect width="512" height="512" fill="#398CCB" rx="150"/><path fill="#fff" fill-rule="evenodd" d="m255.446 75 71.077 41.008v22.548l86.031 49.682v84.986l23.077 13.322v82.062L364.6 409.615l-31.535-18.237-76.673 44.268-76.214-44.012-31.093 17.981-71.031-41.077v-81.992l22.177-12.803v-85.505l84.184-48.6.047-.002v-23.628L255.446 75Zm71.077 84.879v38.144l-71.031 41.008-71.03-41.008v-37.087l-.047.002-65.723 37.962v64.184l30.393-17.546 71.03 41.008v81.992l-21.489 12.427 57.766 33.358 58.226-33.611-21.049-12.174v-81.992l71.031-41.008 29.492 17.027V198.9l-67.569-39.021Zm-14.492 198.09v-50.054l43.338 25.016v50.054l-43.338-25.016Zm105.138-50.123-43.338 25.016v50.123l43.338-25.085v-50.054ZM96.515 357.9v-50.054l43.339 25.016v50.053L96.515 357.9Zm105.139-50.054-43.339 25.016v50.053l43.339-25.015v-50.054Zm119.608-15.923 43.338-25.015 43.338 25.015-43.338 25.039-43.338-25.039Zm-172.177-25.085-43.339 25.085 43.339 24.969 43.338-24.969-43.338-25.085Zm53.838-79.476v-50.054l43.292 25.038v50.031l-43.292-25.015Zm105.092-50.054-43.292 25.038v50.008l43.292-24.992v-50.054Zm-95.861-15.97 43.292-25.015 43.339 25.015-43.339 25.016-43.292-25.016Z" clip-rule="evenodd"/></svg>
|
|
@@ -1,5 +1,7 @@
|
|
|
1
|
+
import { createServerFileRoute } from '@tanstack/react-start/server'
|
|
2
|
+
|
|
1
3
|
export const ServerRoute = createServerFileRoute().methods({
|
|
2
|
-
GET:
|
|
4
|
+
GET: ({ request }) => {
|
|
3
5
|
return new Response(JSON.stringify(['Alice', 'Bob', 'Charlie']), {
|
|
4
6
|
headers: {
|
|
5
7
|
'Content-Type': 'application/json',
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { defineConfig } from 'vite'
|
|
2
|
-
import { tanstackStart } from
|
|
2
|
+
import { tanstackStart } from '@tanstack/react-start/plugin/vite';
|
|
3
|
+
import viteReact from '@vitejs/plugin-react'
|
|
3
4
|
import viteTsConfigPaths from 'vite-tsconfig-paths'<% if (tailwind) { %>
|
|
4
5
|
import tailwindcss from "@tailwindcss/vite"
|
|
5
6
|
<% } %><% if (addOnEnabled.sentry) { %>
|
|
@@ -23,7 +24,10 @@ const config = defineConfig({
|
|
|
23
24
|
projects: ['./tsconfig.json'],
|
|
24
25
|
}),
|
|
25
26
|
<% if (tailwind) { %>tailwindcss(),<% } %>
|
|
26
|
-
tanstackStart(
|
|
27
|
+
tanstackStart({
|
|
28
|
+
customViteReactPlugin: true,
|
|
29
|
+
}),
|
|
30
|
+
viteReact(),
|
|
27
31
|
],
|
|
28
32
|
})
|
|
29
33
|
|
|
@@ -1,21 +1,27 @@
|
|
|
1
|
-
|
|
2
|
-
import type { TRPCRouterRecord } from '@trpc/server'
|
|
3
|
-
// import { z } from 'zod'
|
|
1
|
+
import { z } from 'zod'
|
|
4
2
|
|
|
5
3
|
import { createTRPCRouter, publicProcedure } from './init'
|
|
6
4
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
5
|
+
import type { TRPCRouterRecord } from '@trpc/server'
|
|
6
|
+
|
|
7
|
+
const todos = [
|
|
8
|
+
{ id: 1, name: 'Get groceries' },
|
|
9
|
+
{ id: 2, name: 'Buy a new phone' },
|
|
10
|
+
{ id: 3, name: 'Finish the project' },
|
|
11
|
+
]
|
|
12
|
+
|
|
13
|
+
const todosRouter = {
|
|
14
|
+
list: publicProcedure.query(() => todos),
|
|
15
|
+
add: publicProcedure
|
|
16
|
+
.input(z.object({ name: z.string() }))
|
|
17
|
+
.mutation(({ input }) => {
|
|
18
|
+
const newTodo = { id: todos.length + 1, name: input.name }
|
|
19
|
+
todos.push(newTodo)
|
|
20
|
+
return newTodo
|
|
21
|
+
}),
|
|
16
22
|
} satisfies TRPCRouterRecord
|
|
17
23
|
|
|
18
24
|
export const trpcRouter = createTRPCRouter({
|
|
19
|
-
|
|
25
|
+
todos: todosRouter,
|
|
20
26
|
})
|
|
21
27
|
export type TRPCRouter = typeof trpcRouter
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { useCallback, useState } from 'react'
|
|
2
|
+
import { createFileRoute } from '@tanstack/react-router'
|
|
3
|
+
import { useMutation, useQuery } from '@tanstack/react-query'
|
|
4
|
+
import { useTRPC } from '@/integrations/trpc/react'
|
|
5
|
+
|
|
6
|
+
export const Route = createFileRoute('/demo/trpc-todo')({
|
|
7
|
+
component: TRPCTodos,
|
|
8
|
+
loader: async ({ context }) => {
|
|
9
|
+
await context.queryClient.prefetchQuery(
|
|
10
|
+
context.trpc.todos.list.queryOptions(),
|
|
11
|
+
)
|
|
12
|
+
},
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
function TRPCTodos() {
|
|
16
|
+
const trpc = useTRPC()
|
|
17
|
+
const { data, refetch } = useQuery(trpc.todos.list.queryOptions())
|
|
18
|
+
|
|
19
|
+
const [todo, setTodo] = useState('')
|
|
20
|
+
const { mutate: addTodo } = useMutation({
|
|
21
|
+
...trpc.todos.add.mutationOptions(),
|
|
22
|
+
onSuccess: () => {
|
|
23
|
+
refetch()
|
|
24
|
+
setTodo('')
|
|
25
|
+
},
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
const submitTodo = useCallback(() => {
|
|
29
|
+
addTodo({ name: todo })
|
|
30
|
+
}, [addTodo, todo])
|
|
31
|
+
|
|
32
|
+
return (
|
|
33
|
+
<div
|
|
34
|
+
className="flex items-center justify-center min-h-screen bg-gradient-to-br from-purple-100 to-blue-100 p-4 text-white"
|
|
35
|
+
style={{
|
|
36
|
+
backgroundImage:
|
|
37
|
+
'radial-gradient(50% 50% at 95% 5%, #4a90c2 0%, #317eb9 50%, #1e4d72 100%)',
|
|
38
|
+
}}
|
|
39
|
+
>
|
|
40
|
+
<div className="w-full max-w-2xl p-8 rounded-xl backdrop-blur-md bg-black/50 shadow-xl border-8 border-black/10">
|
|
41
|
+
<h1 className="text-2xl mb-4">tRPC Todos list</h1>
|
|
42
|
+
<ul className="mb-4 space-y-2">
|
|
43
|
+
{data?.map((t) => (
|
|
44
|
+
<li
|
|
45
|
+
key={t.id}
|
|
46
|
+
className="bg-white/10 border border-white/20 rounded-lg p-3 backdrop-blur-sm shadow-md"
|
|
47
|
+
>
|
|
48
|
+
<span className="text-lg text-white">{t.name}</span>
|
|
49
|
+
</li>
|
|
50
|
+
))}
|
|
51
|
+
</ul>
|
|
52
|
+
<div className="flex flex-col gap-2">
|
|
53
|
+
<input
|
|
54
|
+
type="text"
|
|
55
|
+
value={todo}
|
|
56
|
+
onChange={(e) => setTodo(e.target.value)}
|
|
57
|
+
onKeyDown={(e) => {
|
|
58
|
+
if (e.key === 'Enter') {
|
|
59
|
+
submitTodo()
|
|
60
|
+
}
|
|
61
|
+
}}
|
|
62
|
+
placeholder="Enter a new todo..."
|
|
63
|
+
className="w-full px-4 py-3 rounded-lg border border-white/20 bg-white/10 backdrop-blur-sm text-white placeholder-white/60 focus:outline-none focus:ring-2 focus:ring-blue-400 focus:border-transparent"
|
|
64
|
+
/>
|
|
65
|
+
<button
|
|
66
|
+
disabled={todo.trim().length === 0}
|
|
67
|
+
onClick={submitTodo}
|
|
68
|
+
className="bg-blue-500 hover:bg-blue-600 disabled:bg-blue-500/50 disabled:cursor-not-allowed text-white font-bold py-3 px-4 rounded-lg transition-colors"
|
|
69
|
+
>
|
|
70
|
+
Add todo
|
|
71
|
+
</button>
|
|
72
|
+
</div>
|
|
73
|
+
</div>
|
|
74
|
+
</div>
|
|
75
|
+
)
|
|
76
|
+
}
|
package/add-ons/tRPC/info.json
CHANGED
|
@@ -5,5 +5,13 @@
|
|
|
5
5
|
"modes": ["file-router"],
|
|
6
6
|
"link": "https://trpc.io/",
|
|
7
7
|
"dependsOn": ["tanstack-query", "start"],
|
|
8
|
-
"type": "add-on"
|
|
8
|
+
"type": "add-on",
|
|
9
|
+
"routes": [
|
|
10
|
+
{
|
|
11
|
+
"url": "/demo/trpc-todo",
|
|
12
|
+
"name": "tRPC Todo",
|
|
13
|
+
"path": "src/routes/demo.trpc-todo.tsx",
|
|
14
|
+
"jsName": "TRPCTodos"
|
|
15
|
+
}
|
|
16
|
+
]
|
|
9
17
|
}
|
|
@@ -4,10 +4,10 @@ import superjson from "superjson";
|
|
|
4
4
|
import { createTRPCClient, httpBatchStreamLink } from "@trpc/client";
|
|
5
5
|
import { createTRPCOptionsProxy } from "@trpc/tanstack-react-query";
|
|
6
6
|
|
|
7
|
-
import { TRPCProvider } from "@/integrations/trpc/react";
|
|
8
|
-
|
|
9
7
|
import type { TRPCRouter } from "@/integrations/trpc/router";
|
|
10
8
|
|
|
9
|
+
import { TRPCProvider } from "@/integrations/trpc/react";
|
|
10
|
+
|
|
11
11
|
function getUrl() {
|
|
12
12
|
const base = (() => {
|
|
13
13
|
if (typeof window !== "undefined") return "";
|
|
@@ -1,45 +1,44 @@
|
|
|
1
1
|
import { <% if (fileRouter) { %>createFileRoute<% } else { %>createRoute<% } %> } from '@tanstack/react-router'
|
|
2
2
|
import { useQuery } from '@tanstack/react-query'
|
|
3
|
-
<% if (addOnEnabled.tRPC) { %>
|
|
4
|
-
import { useTRPC } from "@/integrations/trpc/react";
|
|
5
|
-
<% } %>
|
|
6
3
|
<% if (codeRouter) { %>
|
|
7
4
|
import type { RootRoute } from '@tanstack/react-router'
|
|
8
5
|
<% } else { %>
|
|
9
6
|
export const Route = createFileRoute('/demo/tanstack-query')({
|
|
10
|
-
<% if (addOnEnabled.tRPC) { %>
|
|
11
|
-
loader: async ({ context }) => {
|
|
12
|
-
await context.queryClient.prefetchQuery(
|
|
13
|
-
context.trpc.people.list.queryOptions()
|
|
14
|
-
);
|
|
15
|
-
},
|
|
16
|
-
<% } %>
|
|
17
7
|
component: TanStackQueryDemo,
|
|
18
8
|
})
|
|
19
9
|
<% } %>
|
|
20
10
|
function TanStackQueryDemo() {
|
|
21
|
-
<% if (addOnEnabled.tRPC) { %>
|
|
22
|
-
const trpc = useTRPC();
|
|
23
|
-
const { data } = useQuery(trpc.people.list.queryOptions());
|
|
24
|
-
<% } else { %>
|
|
25
11
|
const { data } = useQuery({
|
|
26
|
-
queryKey: ['
|
|
12
|
+
queryKey: ['todos'],
|
|
27
13
|
queryFn: () => Promise.resolve([
|
|
28
|
-
{ name: '
|
|
29
|
-
{ name: '
|
|
14
|
+
{ id: 1, name: 'Get groceries' },
|
|
15
|
+
{ id: 2, name: 'Buy a new phone' },
|
|
16
|
+
{ id: 3, name: 'Finish the project' },
|
|
30
17
|
]),
|
|
31
18
|
initialData: [],
|
|
32
19
|
})
|
|
33
|
-
<% } %>
|
|
34
20
|
|
|
35
21
|
return (
|
|
36
|
-
<div
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
22
|
+
<div
|
|
23
|
+
className="flex items-center justify-center min-h-screen bg-gradient-to-br from-purple-100 to-blue-100 p-4 text-white"
|
|
24
|
+
style={{
|
|
25
|
+
backgroundImage:
|
|
26
|
+
'radial-gradient(50% 50% at 95% 5%, #f4a460 0%, #8b4513 70%, #1a0f0a 100%)',
|
|
27
|
+
}}
|
|
28
|
+
>
|
|
29
|
+
<div className="w-full max-w-2xl p-8 rounded-xl backdrop-blur-md bg-black/50 shadow-xl border-8 border-black/10">
|
|
30
|
+
<h1 className="text-2xl mb-4">TanStack Query Todos list</h1>
|
|
31
|
+
<ul className="mb-4 space-y-2">
|
|
32
|
+
{data.map((todo) => (
|
|
33
|
+
<li
|
|
34
|
+
key={todo.id}
|
|
35
|
+
className="bg-white/10 border border-white/20 rounded-lg p-3 backdrop-blur-sm shadow-md"
|
|
36
|
+
>
|
|
37
|
+
<span className="text-lg text-white">{todo.name}</span>
|
|
38
|
+
</li>
|
|
39
|
+
))}
|
|
40
|
+
</ul>
|
|
41
|
+
</div>
|
|
43
42
|
</div>
|
|
44
43
|
)
|
|
45
44
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/cta-framework-react-cra",
|
|
3
|
-
"version": "0.16.
|
|
3
|
+
"version": "0.16.8",
|
|
4
4
|
"description": "CTA Framework for React (Create React App)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"author": "Jack Herrington <jherr@pobox.com>",
|
|
24
24
|
"license": "MIT",
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@tanstack/cta-engine": "0.16.
|
|
26
|
+
"@tanstack/cta-engine": "0.16.8"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"@types/node": "^22.13.4",
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
<% if (!fileRouter) { ignoreFile() } %>import {
|
|
2
|
-
|
|
1
|
+
<% if (!fileRouter) { ignoreFile() } %>import { <% if (addOnEnabled.start) { %>
|
|
2
|
+
HeadContent, <% } %>Outlet<% if (addOnEnabled.start) { %>
|
|
3
|
+
, Scripts<% } %>, <% if (addOnEnabled["tanstack-query"]) { %>createRootRouteWithContext<% } else { %>createRootRoute<% } %> } from '@tanstack/react-router'
|
|
3
4
|
import { TanStackRouterDevtools } from '@tanstack/react-router-devtools'
|
|
4
5
|
<% if (addOns.length) { %>
|
|
5
6
|
import Header from '../components/Header'
|
|
@@ -7,8 +7,8 @@
|
|
|
7
7
|
"/public/robots.txt": "# https://www.robotstxt.org/robotstxt.html\nUser-agent: *\nDisallow:\n",
|
|
8
8
|
"/src/components/Header.tsx": "import { Link } from '@tanstack/react-router'\n\nexport default function Header() {\n return (\n <header className=\"p-2 flex gap-2 bg-white text-black justify-between\">\n <nav className=\"flex flex-row\">\n <div className=\"px-2 font-bold\">\n <Link to=\"/\">Home</Link>\n </div>\n\n <div className=\"px-2 font-bold\">\n <Link to=\"/demo/start/server-funcs\">Start - Server Functions</Link>\n </div>\n\n <div className=\"px-2 font-bold\">\n <Link to=\"/demo/start/api-request\">Start - API Request</Link>\n </div>\n </nav>\n </header>\n )\n}\n",
|
|
9
9
|
"/src/router.tsx": "import { createRouter as createTanstackRouter } from '@tanstack/react-router'\n\n// Import the generated route tree\nimport { routeTree } from './routeTree.gen'\n\n// Create a new router instance\nexport const createRouter = () => {\n return createTanstackRouter({\n routeTree,\n scrollRestoration: true,\n defaultPreloadStaleTime: 0,\n })\n}\n\n// Register the router instance for type safety\ndeclare module '@tanstack/react-router' {\n interface Register {\n router: ReturnType<typeof createRouter>\n }\n}\n",
|
|
10
|
-
"/src/routes/__root.tsx": "import {\n
|
|
11
|
-
"/src/routes/api.demo-names.ts": "
|
|
10
|
+
"/src/routes/__root.tsx": "import {\n HeadContent,\n Outlet,\n Scripts,\n createRootRoute,\n} from '@tanstack/react-router'\nimport { TanStackRouterDevtools } from '@tanstack/react-router-devtools'\n\nimport Header from '../components/Header'\n\nimport appCss from '../styles.css?url'\n\nexport const Route = createRootRoute({\n head: () => ({\n meta: [\n {\n charSet: 'utf-8',\n },\n {\n name: 'viewport',\n content: 'width=device-width, initial-scale=1',\n },\n {\n title: 'TanStack Start Starter',\n },\n ],\n links: [\n {\n rel: 'stylesheet',\n href: appCss,\n },\n ],\n }),\n\n component: () => (\n <RootDocument>\n <Header />\n\n <Outlet />\n <TanStackRouterDevtools />\n </RootDocument>\n ),\n})\n\nfunction RootDocument({ children }: { children: React.ReactNode }) {\n return (\n <html lang=\"en\">\n <head>\n <HeadContent />\n </head>\n <body>\n {children}\n <Scripts />\n </body>\n </html>\n )\n}\n",
|
|
11
|
+
"/src/routes/api.demo-names.ts": "import { createServerFileRoute } from '@tanstack/react-start/server'\n\nexport const ServerRoute = createServerFileRoute().methods({\n GET: ({ request }) => {\n return new Response(JSON.stringify(['Alice', 'Bob', 'Charlie']), {\n headers: {\n 'Content-Type': 'application/json',\n },\n })\n },\n})\n",
|
|
12
12
|
"/src/routes/demo.start.api-request.tsx": "import { useEffect, useState } from 'react'\n\nimport { createFileRoute } from '@tanstack/react-router'\n\nfunction getNames() {\n return fetch('/api/demo-names').then((res) => res.json())\n}\n\nexport const Route = createFileRoute('/demo/start/api-request')({\n component: Home,\n})\n\nfunction Home() {\n const [names, setNames] = useState<Array<string>>([])\n useEffect(() => {\n getNames().then(setNames)\n }, [])\n\n return (\n <div className=\"p-4\">\n <div>{names.join(', ')}</div>\n </div>\n )\n}\n",
|
|
13
13
|
"/src/routes/demo.start.server-funcs.tsx": "import * as fs from 'node:fs'\nimport { createFileRoute, useRouter } from '@tanstack/react-router'\nimport { createServerFn } from '@tanstack/react-start'\n\nconst filePath = 'count.txt'\n\nasync function readCount() {\n return parseInt(\n await fs.promises.readFile(filePath, 'utf-8').catch(() => '0'),\n )\n}\n\nconst getCount = createServerFn({\n method: 'GET',\n}).handler(() => {\n return readCount()\n})\n\nconst updateCount = createServerFn({ method: 'POST' })\n .validator((d: number) => d)\n .handler(async ({ data }) => {\n const count = await readCount()\n await fs.promises.writeFile(filePath, `${count + data}`)\n })\n\nexport const Route = createFileRoute('/demo/start/server-funcs')({\n component: Home,\n loader: async () => await getCount(),\n})\n\nfunction Home() {\n const router = useRouter()\n const state = Route.useLoaderData()\n\n return (\n <div className=\"p-4\">\n <button\n type=\"button\"\n onClick={() => {\n updateCount({ data: 1 }).then(() => {\n router.invalidate()\n })\n }}\n className=\"bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded\"\n >\n Add 1 to {state}?\n </button>\n </div>\n )\n}\n",
|
|
14
14
|
"/src/routes/index.tsx": "import { createFileRoute } from '@tanstack/react-router'\nimport logo from '../logo.svg'\n\nexport const Route = createFileRoute('/')({\n component: App,\n})\n\nfunction App() {\n return (\n <div className=\"text-center\">\n <header className=\"min-h-screen flex flex-col items-center justify-center bg-[#282c34] text-white text-[calc(10px+2vmin)]\">\n <img\n src={logo}\n className=\"h-[40vmin] pointer-events-none animate-[spin_20s_linear_infinite]\"\n alt=\"logo\"\n />\n <p>\n Edit <code>src/routes/index.tsx</code> and save to reload.\n </p>\n <a\n className=\"text-[#61dafb] hover:underline\"\n href=\"https://reactjs.org\"\n target=\"_blank\"\n rel=\"noopener noreferrer\"\n >\n Learn React\n </a>\n <a\n className=\"text-[#61dafb] hover:underline\"\n href=\"https://tanstack.com\"\n target=\"_blank\"\n rel=\"noopener noreferrer\"\n >\n Learn TanStack\n </a>\n </header>\n </div>\n )\n}\n",
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
"README.md": "Welcome to your new TanStack app! \n\n# Getting Started\n\nTo run this application:\n\n```bash\nnpm install\nnpm run start \n```\n\n# Building For Production\n\nTo build this application for production:\n\n```bash\nnpm run build\n```\n\n## Testing\n\nThis project uses [Vitest](https://vitest.dev/) for testing. You can run the tests with:\n\n```bash\nnpm run test\n```\n\n## Styling\n\nThis project uses [Tailwind CSS](https://tailwindcss.com/) for styling.\n\n\n\n\n## Routing\nThis project uses [TanStack Router](https://tanstack.com/router). The initial setup is a file based router. Which means that the routes are managed as files in `src/routes`.\n\n### Adding A Route\n\nTo add a new route to your application just add another a new file in the `./src/routes` directory.\n\nTanStack will automatically generate the content of the route file for you.\n\nNow that you have two routes you can use a `Link` component to navigate between them.\n\n### Adding Links\n\nTo use SPA (Single Page Application) navigation you will need to import the `Link` component from `@tanstack/react-router`.\n\n```tsx\nimport { Link } from \"@tanstack/react-router\";\n```\n\nThen anywhere in your JSX you can use it like so:\n\n```tsx\n<Link to=\"/about\">About</Link>\n```\n\nThis will create a link that will navigate to the `/about` route.\n\nMore information on the `Link` component can be found in the [Link documentation](https://tanstack.com/router/v1/docs/framework/react/api/router/linkComponent).\n\n### Using A Layout\n\nIn the File Based Routing setup the layout is located in `src/routes/__root.tsx`. Anything you add to the root route will appear in all the routes. The route content will appear in the JSX where you use the `<Outlet />` component.\n\nHere is an example layout that includes a header:\n\n```tsx\nimport { Outlet, createRootRoute } from '@tanstack/react-router'\nimport { TanStackRouterDevtools } from '@tanstack/react-router-devtools'\n\nimport { Link } from \"@tanstack/react-router\";\n\nexport const Route = createRootRoute({\n component: () => (\n <>\n <header>\n <nav>\n <Link to=\"/\">Home</Link>\n <Link to=\"/about\">About</Link>\n </nav>\n </header>\n <Outlet />\n <TanStackRouterDevtools />\n </>\n ),\n})\n```\n\nThe `<TanStackRouterDevtools />` component is not required so you can remove it if you don't want it in your layout.\n\nMore information on layouts can be found in the [Layouts documentation](https://tanstack.com/router/latest/docs/framework/react/guide/routing-concepts#layouts).\n\n\n## Data Fetching\n\nThere are multiple ways to fetch data in your application. You can use TanStack Query to fetch data from a server. But you can also use the `loader` functionality built into TanStack Router to load the data for a route before it's rendered.\n\nFor example:\n\n```tsx\nconst peopleRoute = createRoute({\n getParentRoute: () => rootRoute,\n path: \"/people\",\n loader: async () => {\n const response = await fetch(\"https://swapi.dev/api/people\");\n return response.json() as Promise<{\n results: {\n name: string;\n }[];\n }>;\n },\n component: () => {\n const data = peopleRoute.useLoaderData();\n return (\n <ul>\n {data.results.map((person) => (\n <li key={person.name}>{person.name}</li>\n ))}\n </ul>\n );\n },\n});\n```\n\nLoaders simplify your data fetching logic dramatically. Check out more information in the [Loader documentation](https://tanstack.com/router/latest/docs/framework/react/guide/data-loading#loader-parameters).\n\n### React-Query\n\nReact-Query is an excellent addition or alternative to route loading and integrating it into you application is a breeze.\n\nFirst add your dependencies:\n\n```bash\nnpm install @tanstack/react-query @tanstack/react-query-devtools\n```\n\nNext we'll need to create a query client and provider. We recommend putting those in `main.tsx`.\n\n```tsx\nimport { QueryClient, QueryClientProvider } from \"@tanstack/react-query\";\n\n// ...\n\nconst queryClient = new QueryClient();\n\n// ...\n\nif (!rootElement.innerHTML) {\n const root = ReactDOM.createRoot(rootElement);\n\n root.render(\n <QueryClientProvider client={queryClient}>\n <RouterProvider router={router} />\n </QueryClientProvider>\n );\n}\n```\n\nYou can also add TanStack Query Devtools to the root route (optional).\n\n```tsx\nimport { ReactQueryDevtools } from \"@tanstack/react-query-devtools\";\n\nconst rootRoute = createRootRoute({\n component: () => (\n <>\n <Outlet />\n <ReactQueryDevtools buttonPosition=\"top-right\" />\n <TanStackRouterDevtools />\n </>\n ),\n});\n```\n\nNow you can use `useQuery` to fetch your data.\n\n```tsx\nimport { useQuery } from \"@tanstack/react-query\";\n\nimport \"./App.css\";\n\nfunction App() {\n const { data } = useQuery({\n queryKey: [\"people\"],\n queryFn: () =>\n fetch(\"https://swapi.dev/api/people\")\n .then((res) => res.json())\n .then((data) => data.results as { name: string }[]),\n initialData: [],\n });\n\n return (\n <div>\n <ul>\n {data.map((person) => (\n <li key={person.name}>{person.name}</li>\n ))}\n </ul>\n </div>\n );\n}\n\nexport default App;\n```\n\nYou can find out everything you need to know on how to use React-Query in the [React-Query documentation](https://tanstack.com/query/latest/docs/framework/react/overview).\n\n## State Management\n\nAnother common requirement for React applications is state management. There are many options for state management in React. TanStack Store provides a great starting point for your project.\n\nFirst you need to add TanStack Store as a dependency:\n\n```bash\nnpm install @tanstack/store\n```\n\nNow let's create a simple counter in the `src/App.tsx` file as a demonstration.\n\n```tsx\nimport { useStore } from \"@tanstack/react-store\";\nimport { Store } from \"@tanstack/store\";\nimport \"./App.css\";\n\nconst countStore = new Store(0);\n\nfunction App() {\n const count = useStore(countStore);\n return (\n <div>\n <button onClick={() => countStore.setState((n) => n + 1)}>\n Increment - {count}\n </button>\n </div>\n );\n}\n\nexport default App;\n```\n\nOne of the many nice features of TanStack Store is the ability to derive state from other state. That derived state will update when the base state updates.\n\nLet's check this out by doubling the count using derived state.\n\n```tsx\nimport { useStore } from \"@tanstack/react-store\";\nimport { Store, Derived } from \"@tanstack/store\";\nimport \"./App.css\";\n\nconst countStore = new Store(0);\n\nconst doubledStore = new Derived({\n fn: () => countStore.state * 2,\n deps: [countStore],\n});\ndoubledStore.mount();\n\nfunction App() {\n const count = useStore(countStore);\n const doubledCount = useStore(doubledStore);\n\n return (\n <div>\n <button onClick={() => countStore.setState((n) => n + 1)}>\n Increment - {count}\n </button>\n <div>Doubled - {doubledCount}</div>\n </div>\n );\n}\n\nexport default App;\n```\n\nWe use the `Derived` class to create a new store that is derived from another store. The `Derived` class has a `mount` method that will start the derived store updating.\n\nOnce we've created the derived store we can use it in the `App` component just like we would any other store using the `useStore` hook.\n\nYou can find out everything you need to know on how to use TanStack Store in the [TanStack Store documentation](https://tanstack.com/store/latest).\n\n# Demo files\n\nFiles prefixed with `demo` can be safely deleted. They are there to provide a starting point for you to play around with the features you've installed.\n\n# Learn More\n\nYou can learn more about all of the offerings from TanStack in the [TanStack documentation](https://tanstack.com).\n",
|
|
17
17
|
"package.json": "{\n \"name\": \"TEST\",\n \"private\": true,\n \"type\": \"module\",\n \"scripts\": {\n \"dev\": \"vite dev --port 3000\",\n \"start\": \"node .output/server/index.mjs\",\n \"build\": \"vite build\",\n \"serve\": \"vite preview\",\n \"test\": \"vitest run\"\n },\n \"dependencies\": {\n \"@tailwindcss/vite\": \"^4.0.6\",\n \"@tanstack/react-router\": \"^1.130.2\",\n \"@tanstack/react-router-devtools\": \"^1.130.2\",\n \"@tanstack/react-router-with-query\": \"^1.130.2\",\n \"@tanstack/react-start\": \"^1.130.2\",\n \"@tanstack/router-plugin\": \"^1.121.2\",\n \"react\": \"^19.0.0\",\n \"react-dom\": \"^19.0.0\",\n \"tailwindcss\": \"^4.0.6\",\n \"vite-tsconfig-paths\": \"^5.1.4\"\n },\n \"devDependencies\": {\n \"@testing-library/dom\": \"^10.4.0\",\n \"@testing-library/react\": \"^16.2.0\",\n \"@types/react\": \"^19.0.8\",\n \"@types/react-dom\": \"^19.0.3\",\n \"@vitejs/plugin-react\": \"^4.3.4\",\n \"jsdom\": \"^26.0.0\",\n \"typescript\": \"^5.7.2\",\n \"vite\": \"^6.3.5\",\n \"vitest\": \"^3.0.5\",\n \"web-vitals\": \"^4.2.4\"\n }\n}",
|
|
18
18
|
"tsconfig.json": "{\n \"include\": [\"**/*.ts\", \"**/*.tsx\"],\n \"compilerOptions\": {\n \"target\": \"ES2022\",\n \"jsx\": \"react-jsx\",\n \"module\": \"ESNext\",\n \"lib\": [\"ES2022\", \"DOM\", \"DOM.Iterable\"],\n \"types\": [\"vite/client\"],\n\n /* Bundler mode */\n \"moduleResolution\": \"bundler\",\n \"allowImportingTsExtensions\": true,\n \"verbatimModuleSyntax\": true,\n \"noEmit\": true,\n\n /* Linting */\n \"skipLibCheck\": true,\n \"strict\": true,\n \"noUnusedLocals\": true,\n \"noUnusedParameters\": true,\n \"noFallthroughCasesInSwitch\": true,\n \"noUncheckedSideEffectImports\": true,\n \"baseUrl\": \".\",\n \"paths\": {\n \"@/*\": [\"./src/*\"],\n }\n }\n}\n",
|
|
19
|
-
"vite.config.ts": "import { defineConfig } from 'vite'\nimport { tanstackStart } from '@tanstack/react-start/plugin/vite'\nimport viteTsConfigPaths from 'vite-tsconfig-paths'\nimport tailwindcss from '@tailwindcss/vite'\n\nconst config = defineConfig({\n plugins: [\n // this is the plugin that enables path aliases\n viteTsConfigPaths({\n projects: ['./tsconfig.json'],\n }),\n tailwindcss(),\n tanstackStart(),\n ],\n})\n\nexport default config\n"
|
|
19
|
+
"vite.config.ts": "import { defineConfig } from 'vite'\nimport { tanstackStart } from '@tanstack/react-start/plugin/vite'\nimport viteReact from '@vitejs/plugin-react'\nimport viteTsConfigPaths from 'vite-tsconfig-paths'\nimport tailwindcss from '@tailwindcss/vite'\n\nconst config = defineConfig({\n plugins: [\n // this is the plugin that enables path aliases\n viteTsConfigPaths({\n projects: ['./tsconfig.json'],\n }),\n tailwindcss(),\n tanstackStart({\n customViteReactPlugin: true,\n }),\n viteReact(),\n ],\n})\n\nexport default config\n"
|
|
20
20
|
},
|
|
21
21
|
"commands": [
|
|
22
22
|
"git init",
|
|
@@ -9,17 +9,17 @@
|
|
|
9
9
|
"/src/integrations/tanstack-query/layout.tsx": "import { ReactQueryDevtools } from '@tanstack/react-query-devtools'\n\nexport default function LayoutAddition() {\n return <ReactQueryDevtools buttonPosition=\"bottom-right\" />\n}\n",
|
|
10
10
|
"/src/integrations/tanstack-query/root-provider.tsx": "import { QueryClient, QueryClientProvider } from '@tanstack/react-query'\n\nexport function getContext() {\n const queryClient = new QueryClient()\n return {\n queryClient,\n }\n}\n\nexport function Provider({\n children,\n queryClient,\n}: {\n children: React.ReactNode\n queryClient: QueryClient\n}) {\n return (\n <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>\n )\n}\n",
|
|
11
11
|
"/src/router.tsx": "import { createRouter as createTanstackRouter } from '@tanstack/react-router'\nimport { routerWithQueryClient } from '@tanstack/react-router-with-query'\nimport * as TanstackQuery from './integrations/tanstack-query/root-provider'\n\n// Import the generated route tree\nimport { routeTree } from './routeTree.gen'\n\n// Create a new router instance\nexport const createRouter = () => {\n const rqContext = TanstackQuery.getContext()\n\n return routerWithQueryClient(\n createTanstackRouter({\n routeTree,\n context: { ...rqContext },\n defaultPreload: 'intent',\n Wrap: (props: { children: React.ReactNode }) => {\n return (\n <TanstackQuery.Provider {...rqContext}>\n {props.children}\n </TanstackQuery.Provider>\n )\n },\n }),\n rqContext.queryClient,\n )\n}\n\n// Register the router instance for type safety\ndeclare module '@tanstack/react-router' {\n interface Register {\n router: ReturnType<typeof createRouter>\n }\n}\n",
|
|
12
|
-
"/src/routes/__root.tsx": "import {\n
|
|
13
|
-
"/src/routes/api.demo-names.ts": "
|
|
12
|
+
"/src/routes/__root.tsx": "import {\n HeadContent,\n Outlet,\n Scripts,\n createRootRouteWithContext,\n} from '@tanstack/react-router'\nimport { TanStackRouterDevtools } from '@tanstack/react-router-devtools'\n\nimport Header from '../components/Header'\n\nimport TanStackQueryLayout from '../integrations/tanstack-query/layout.tsx'\n\nimport appCss from '../styles.css?url'\n\nimport type { QueryClient } from '@tanstack/react-query'\n\ninterface MyRouterContext {\n queryClient: QueryClient\n}\n\nexport const Route = createRootRouteWithContext<MyRouterContext>()({\n head: () => ({\n meta: [\n {\n charSet: 'utf-8',\n },\n {\n name: 'viewport',\n content: 'width=device-width, initial-scale=1',\n },\n {\n title: 'TanStack Start Starter',\n },\n ],\n links: [\n {\n rel: 'stylesheet',\n href: appCss,\n },\n ],\n }),\n\n component: () => (\n <RootDocument>\n <Header />\n\n <Outlet />\n <TanStackRouterDevtools />\n\n <TanStackQueryLayout />\n </RootDocument>\n ),\n})\n\nfunction RootDocument({ children }: { children: React.ReactNode }) {\n return (\n <html lang=\"en\">\n <head>\n <HeadContent />\n </head>\n <body>\n {children}\n <Scripts />\n </body>\n </html>\n )\n}\n",
|
|
13
|
+
"/src/routes/api.demo-names.ts": "import { createServerFileRoute } from '@tanstack/react-start/server'\n\nexport const ServerRoute = createServerFileRoute().methods({\n GET: ({ request }) => {\n return new Response(JSON.stringify(['Alice', 'Bob', 'Charlie']), {\n headers: {\n 'Content-Type': 'application/json',\n },\n })\n },\n})\n",
|
|
14
14
|
"/src/routes/demo.start.api-request.tsx": "import { useEffect, useState } from 'react'\n\nimport { createFileRoute } from '@tanstack/react-router'\n\nfunction getNames() {\n return fetch('/api/demo-names').then((res) => res.json())\n}\n\nexport const Route = createFileRoute('/demo/start/api-request')({\n component: Home,\n})\n\nfunction Home() {\n const [names, setNames] = useState<Array<string>>([])\n useEffect(() => {\n getNames().then(setNames)\n }, [])\n\n return (\n <div className=\"p-4\">\n <div>{names.join(', ')}</div>\n </div>\n )\n}\n",
|
|
15
15
|
"/src/routes/demo.start.server-funcs.tsx": "import * as fs from 'node:fs'\nimport { createFileRoute, useRouter } from '@tanstack/react-router'\nimport { createServerFn } from '@tanstack/react-start'\n\nconst filePath = 'count.txt'\n\nasync function readCount() {\n return parseInt(\n await fs.promises.readFile(filePath, 'utf-8').catch(() => '0'),\n )\n}\n\nconst getCount = createServerFn({\n method: 'GET',\n}).handler(() => {\n return readCount()\n})\n\nconst updateCount = createServerFn({ method: 'POST' })\n .validator((d: number) => d)\n .handler(async ({ data }) => {\n const count = await readCount()\n await fs.promises.writeFile(filePath, `${count + data}`)\n })\n\nexport const Route = createFileRoute('/demo/start/server-funcs')({\n component: Home,\n loader: async () => await getCount(),\n})\n\nfunction Home() {\n const router = useRouter()\n const state = Route.useLoaderData()\n\n return (\n <div className=\"p-4\">\n <button\n type=\"button\"\n onClick={() => {\n updateCount({ data: 1 }).then(() => {\n router.invalidate()\n })\n }}\n className=\"bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded\"\n >\n Add 1 to {state}?\n </button>\n </div>\n )\n}\n",
|
|
16
|
-
"/src/routes/demo.tanstack-query.tsx": "import { createFileRoute } from '@tanstack/react-router'\nimport { useQuery } from '@tanstack/react-query'\n\nexport const Route = createFileRoute('/demo/tanstack-query')({\n component: TanStackQueryDemo,\n})\n\nfunction TanStackQueryDemo() {\n const { data } = useQuery({\n queryKey: ['
|
|
16
|
+
"/src/routes/demo.tanstack-query.tsx": "import { createFileRoute } from '@tanstack/react-router'\nimport { useQuery } from '@tanstack/react-query'\n\nexport const Route = createFileRoute('/demo/tanstack-query')({\n component: TanStackQueryDemo,\n})\n\nfunction TanStackQueryDemo() {\n const { data } = useQuery({\n queryKey: ['todos'],\n queryFn: () =>\n Promise.resolve([\n { id: 1, name: 'Get groceries' },\n { id: 2, name: 'Buy a new phone' },\n { id: 3, name: 'Finish the project' },\n ]),\n initialData: [],\n })\n\n return (\n <div\n className=\"flex items-center justify-center min-h-screen bg-gradient-to-br from-purple-100 to-blue-100 p-4 text-white\"\n style={{\n backgroundImage:\n 'radial-gradient(50% 50% at 95% 5%, #f4a460 0%, #8b4513 70%, #1a0f0a 100%)',\n }}\n >\n <div className=\"w-full max-w-2xl p-8 rounded-xl backdrop-blur-md bg-black/50 shadow-xl border-8 border-black/10\">\n <h1 className=\"text-2xl mb-4\">TanStack Query Todos list</h1>\n <ul className=\"mb-4 space-y-2\">\n {data.map((todo) => (\n <li\n key={todo.id}\n className=\"bg-white/10 border border-white/20 rounded-lg p-3 backdrop-blur-sm shadow-md\"\n >\n <span className=\"text-lg text-white\">{todo.name}</span>\n </li>\n ))}\n </ul>\n </div>\n </div>\n )\n}\n",
|
|
17
17
|
"/src/routes/index.tsx": "import { createFileRoute } from '@tanstack/react-router'\nimport logo from '../logo.svg'\n\nexport const Route = createFileRoute('/')({\n component: App,\n})\n\nfunction App() {\n return (\n <div className=\"text-center\">\n <header className=\"min-h-screen flex flex-col items-center justify-center bg-[#282c34] text-white text-[calc(10px+2vmin)]\">\n <img\n src={logo}\n className=\"h-[40vmin] pointer-events-none animate-[spin_20s_linear_infinite]\"\n alt=\"logo\"\n />\n <p>\n Edit <code>src/routes/index.tsx</code> and save to reload.\n </p>\n <a\n className=\"text-[#61dafb] hover:underline\"\n href=\"https://reactjs.org\"\n target=\"_blank\"\n rel=\"noopener noreferrer\"\n >\n Learn React\n </a>\n <a\n className=\"text-[#61dafb] hover:underline\"\n href=\"https://tanstack.com\"\n target=\"_blank\"\n rel=\"noopener noreferrer\"\n >\n Learn TanStack\n </a>\n </header>\n </div>\n )\n}\n",
|
|
18
18
|
"/src/styles.css": "@import \"tailwindcss\";\n\nbody {\n @apply m-0;\n font-family: -apple-system, BlinkMacSystemFont, \"Segoe UI\", \"Roboto\", \"Oxygen\",\n \"Ubuntu\", \"Cantarell\", \"Fira Sans\", \"Droid Sans\", \"Helvetica Neue\",\n sans-serif;\n -webkit-font-smoothing: antialiased;\n -moz-osx-font-smoothing: grayscale;\n}\n\ncode {\n font-family: source-code-pro, Menlo, Monaco, Consolas, \"Courier New\",\n monospace;\n}\n",
|
|
19
19
|
"README.md": "Welcome to your new TanStack app! \n\n# Getting Started\n\nTo run this application:\n\n```bash\nnpm install\nnpm run start \n```\n\n# Building For Production\n\nTo build this application for production:\n\n```bash\nnpm run build\n```\n\n## Testing\n\nThis project uses [Vitest](https://vitest.dev/) for testing. You can run the tests with:\n\n```bash\nnpm run test\n```\n\n## Styling\n\nThis project uses [Tailwind CSS](https://tailwindcss.com/) for styling.\n\n\n\n\n## Routing\nThis project uses [TanStack Router](https://tanstack.com/router). The initial setup is a file based router. Which means that the routes are managed as files in `src/routes`.\n\n### Adding A Route\n\nTo add a new route to your application just add another a new file in the `./src/routes` directory.\n\nTanStack will automatically generate the content of the route file for you.\n\nNow that you have two routes you can use a `Link` component to navigate between them.\n\n### Adding Links\n\nTo use SPA (Single Page Application) navigation you will need to import the `Link` component from `@tanstack/react-router`.\n\n```tsx\nimport { Link } from \"@tanstack/react-router\";\n```\n\nThen anywhere in your JSX you can use it like so:\n\n```tsx\n<Link to=\"/about\">About</Link>\n```\n\nThis will create a link that will navigate to the `/about` route.\n\nMore information on the `Link` component can be found in the [Link documentation](https://tanstack.com/router/v1/docs/framework/react/api/router/linkComponent).\n\n### Using A Layout\n\nIn the File Based Routing setup the layout is located in `src/routes/__root.tsx`. Anything you add to the root route will appear in all the routes. The route content will appear in the JSX where you use the `<Outlet />` component.\n\nHere is an example layout that includes a header:\n\n```tsx\nimport { Outlet, createRootRoute } from '@tanstack/react-router'\nimport { TanStackRouterDevtools } from '@tanstack/react-router-devtools'\n\nimport { Link } from \"@tanstack/react-router\";\n\nexport const Route = createRootRoute({\n component: () => (\n <>\n <header>\n <nav>\n <Link to=\"/\">Home</Link>\n <Link to=\"/about\">About</Link>\n </nav>\n </header>\n <Outlet />\n <TanStackRouterDevtools />\n </>\n ),\n})\n```\n\nThe `<TanStackRouterDevtools />` component is not required so you can remove it if you don't want it in your layout.\n\nMore information on layouts can be found in the [Layouts documentation](https://tanstack.com/router/latest/docs/framework/react/guide/routing-concepts#layouts).\n\n\n## Data Fetching\n\nThere are multiple ways to fetch data in your application. You can use TanStack Query to fetch data from a server. But you can also use the `loader` functionality built into TanStack Router to load the data for a route before it's rendered.\n\nFor example:\n\n```tsx\nconst peopleRoute = createRoute({\n getParentRoute: () => rootRoute,\n path: \"/people\",\n loader: async () => {\n const response = await fetch(\"https://swapi.dev/api/people\");\n return response.json() as Promise<{\n results: {\n name: string;\n }[];\n }>;\n },\n component: () => {\n const data = peopleRoute.useLoaderData();\n return (\n <ul>\n {data.results.map((person) => (\n <li key={person.name}>{person.name}</li>\n ))}\n </ul>\n );\n },\n});\n```\n\nLoaders simplify your data fetching logic dramatically. Check out more information in the [Loader documentation](https://tanstack.com/router/latest/docs/framework/react/guide/data-loading#loader-parameters).\n\n### React-Query\n\nReact-Query is an excellent addition or alternative to route loading and integrating it into you application is a breeze.\n\nFirst add your dependencies:\n\n```bash\nnpm install @tanstack/react-query @tanstack/react-query-devtools\n```\n\nNext we'll need to create a query client and provider. We recommend putting those in `main.tsx`.\n\n```tsx\nimport { QueryClient, QueryClientProvider } from \"@tanstack/react-query\";\n\n// ...\n\nconst queryClient = new QueryClient();\n\n// ...\n\nif (!rootElement.innerHTML) {\n const root = ReactDOM.createRoot(rootElement);\n\n root.render(\n <QueryClientProvider client={queryClient}>\n <RouterProvider router={router} />\n </QueryClientProvider>\n );\n}\n```\n\nYou can also add TanStack Query Devtools to the root route (optional).\n\n```tsx\nimport { ReactQueryDevtools } from \"@tanstack/react-query-devtools\";\n\nconst rootRoute = createRootRoute({\n component: () => (\n <>\n <Outlet />\n <ReactQueryDevtools buttonPosition=\"top-right\" />\n <TanStackRouterDevtools />\n </>\n ),\n});\n```\n\nNow you can use `useQuery` to fetch your data.\n\n```tsx\nimport { useQuery } from \"@tanstack/react-query\";\n\nimport \"./App.css\";\n\nfunction App() {\n const { data } = useQuery({\n queryKey: [\"people\"],\n queryFn: () =>\n fetch(\"https://swapi.dev/api/people\")\n .then((res) => res.json())\n .then((data) => data.results as { name: string }[]),\n initialData: [],\n });\n\n return (\n <div>\n <ul>\n {data.map((person) => (\n <li key={person.name}>{person.name}</li>\n ))}\n </ul>\n </div>\n );\n}\n\nexport default App;\n```\n\nYou can find out everything you need to know on how to use React-Query in the [React-Query documentation](https://tanstack.com/query/latest/docs/framework/react/overview).\n\n## State Management\n\nAnother common requirement for React applications is state management. There are many options for state management in React. TanStack Store provides a great starting point for your project.\n\nFirst you need to add TanStack Store as a dependency:\n\n```bash\nnpm install @tanstack/store\n```\n\nNow let's create a simple counter in the `src/App.tsx` file as a demonstration.\n\n```tsx\nimport { useStore } from \"@tanstack/react-store\";\nimport { Store } from \"@tanstack/store\";\nimport \"./App.css\";\n\nconst countStore = new Store(0);\n\nfunction App() {\n const count = useStore(countStore);\n return (\n <div>\n <button onClick={() => countStore.setState((n) => n + 1)}>\n Increment - {count}\n </button>\n </div>\n );\n}\n\nexport default App;\n```\n\nOne of the many nice features of TanStack Store is the ability to derive state from other state. That derived state will update when the base state updates.\n\nLet's check this out by doubling the count using derived state.\n\n```tsx\nimport { useStore } from \"@tanstack/react-store\";\nimport { Store, Derived } from \"@tanstack/store\";\nimport \"./App.css\";\n\nconst countStore = new Store(0);\n\nconst doubledStore = new Derived({\n fn: () => countStore.state * 2,\n deps: [countStore],\n});\ndoubledStore.mount();\n\nfunction App() {\n const count = useStore(countStore);\n const doubledCount = useStore(doubledStore);\n\n return (\n <div>\n <button onClick={() => countStore.setState((n) => n + 1)}>\n Increment - {count}\n </button>\n <div>Doubled - {doubledCount}</div>\n </div>\n );\n}\n\nexport default App;\n```\n\nWe use the `Derived` class to create a new store that is derived from another store. The `Derived` class has a `mount` method that will start the derived store updating.\n\nOnce we've created the derived store we can use it in the `App` component just like we would any other store using the `useStore` hook.\n\nYou can find out everything you need to know on how to use TanStack Store in the [TanStack Store documentation](https://tanstack.com/store/latest).\n\n# Demo files\n\nFiles prefixed with `demo` can be safely deleted. They are there to provide a starting point for you to play around with the features you've installed.\n\n# Learn More\n\nYou can learn more about all of the offerings from TanStack in the [TanStack documentation](https://tanstack.com).\n",
|
|
20
20
|
"package.json": "{\n \"name\": \"TEST\",\n \"private\": true,\n \"type\": \"module\",\n \"scripts\": {\n \"dev\": \"vite dev --port 3000\",\n \"start\": \"node .output/server/index.mjs\",\n \"build\": \"vite build\",\n \"serve\": \"vite preview\",\n \"test\": \"vitest run\"\n },\n \"dependencies\": {\n \"@tailwindcss/vite\": \"^4.0.6\",\n \"@tanstack/react-query\": \"^5.66.5\",\n \"@tanstack/react-query-devtools\": \"^5.66.5\",\n \"@tanstack/react-router\": \"^1.130.2\",\n \"@tanstack/react-router-devtools\": \"^1.130.2\",\n \"@tanstack/react-router-with-query\": \"^1.130.2\",\n \"@tanstack/react-start\": \"^1.130.2\",\n \"@tanstack/router-plugin\": \"^1.121.2\",\n \"react\": \"^19.0.0\",\n \"react-dom\": \"^19.0.0\",\n \"tailwindcss\": \"^4.0.6\",\n \"vite-tsconfig-paths\": \"^5.1.4\"\n },\n \"devDependencies\": {\n \"@testing-library/dom\": \"^10.4.0\",\n \"@testing-library/react\": \"^16.2.0\",\n \"@types/react\": \"^19.0.8\",\n \"@types/react-dom\": \"^19.0.3\",\n \"@vitejs/plugin-react\": \"^4.3.4\",\n \"jsdom\": \"^26.0.0\",\n \"typescript\": \"^5.7.2\",\n \"vite\": \"^6.3.5\",\n \"vitest\": \"^3.0.5\",\n \"web-vitals\": \"^4.2.4\"\n }\n}",
|
|
21
21
|
"tsconfig.json": "{\n \"include\": [\"**/*.ts\", \"**/*.tsx\"],\n \"compilerOptions\": {\n \"target\": \"ES2022\",\n \"jsx\": \"react-jsx\",\n \"module\": \"ESNext\",\n \"lib\": [\"ES2022\", \"DOM\", \"DOM.Iterable\"],\n \"types\": [\"vite/client\"],\n\n /* Bundler mode */\n \"moduleResolution\": \"bundler\",\n \"allowImportingTsExtensions\": true,\n \"verbatimModuleSyntax\": true,\n \"noEmit\": true,\n\n /* Linting */\n \"skipLibCheck\": true,\n \"strict\": true,\n \"noUnusedLocals\": true,\n \"noUnusedParameters\": true,\n \"noFallthroughCasesInSwitch\": true,\n \"noUncheckedSideEffectImports\": true,\n \"baseUrl\": \".\",\n \"paths\": {\n \"@/*\": [\"./src/*\"],\n }\n }\n}\n",
|
|
22
|
-
"vite.config.ts": "import { defineConfig } from 'vite'\nimport { tanstackStart } from '@tanstack/react-start/plugin/vite'\nimport viteTsConfigPaths from 'vite-tsconfig-paths'\nimport tailwindcss from '@tailwindcss/vite'\n\nconst config = defineConfig({\n plugins: [\n // this is the plugin that enables path aliases\n viteTsConfigPaths({\n projects: ['./tsconfig.json'],\n }),\n tailwindcss(),\n tanstackStart(),\n ],\n})\n\nexport default config\n"
|
|
22
|
+
"vite.config.ts": "import { defineConfig } from 'vite'\nimport { tanstackStart } from '@tanstack/react-start/plugin/vite'\nimport viteReact from '@vitejs/plugin-react'\nimport viteTsConfigPaths from 'vite-tsconfig-paths'\nimport tailwindcss from '@tailwindcss/vite'\n\nconst config = defineConfig({\n plugins: [\n // this is the plugin that enables path aliases\n viteTsConfigPaths({\n projects: ['./tsconfig.json'],\n }),\n tailwindcss(),\n tanstackStart({\n customViteReactPlugin: true,\n }),\n viteReact(),\n ],\n})\n\nexport default config\n"
|
|
23
23
|
},
|
|
24
24
|
"commands": [
|
|
25
25
|
"git init",
|