Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

consistent.tsx

121 lines3.9 KB

1import { createFileRoute } from '@tanstack/solid-router'
2import * as Solid from 'solid-js'
3import { createServerFn } from '@tanstack/solid-start'
4​
5/**
6 * This checks whether the returned payloads from a
7 * server function are the same, regardless of whether the server function is
8 * called directly from the client or from within the server function.
9 * @link https://github.com/TanStack/router/issues/1866
10 * @link https://github.com/TanStack/router/issues/2481
11 */
12​
13export const Route = createFileRoute('/consistent')({
14 component: ConsistentServerFnCalls,
15 loader: async () => {
16 const data = await cons_serverGetFn1({ data: { username: 'TEST' } })
17 console.log('cons_serverGetFn1', data)
18 return { data }
19 },
20})
21​
22const cons_getFn1 = createServerFn()
23 .validator((d: { username: string }) => d)
24 .handler(({ data }) => {
25 return { payload: data }
26 })
27​
28const cons_serverGetFn1 = createServerFn()
29 .validator((d: { username: string }) => d)
30 .handler(async ({ data }) => {
31 return cons_getFn1({ data })
32 })
33​
34const cons_postFn1 = createServerFn({ method: 'POST' })
35 .validator((d: { username: string }) => d)
36 .handler(({ data }) => {
37 return { payload: data }
38 })
39​
40const cons_serverPostFn1 = createServerFn({ method: 'POST' })
41 .validator((d: { username: string }) => d)
42 .handler(({ data }) => {
43 return cons_postFn1({ data })
44 })
45​
46function ConsistentServerFnCalls() {
47 const [getServerResult, setGetServerResult] = Solid.createSignal({})
48 const [getDirectResult, setGetDirectResult] = Solid.createSignal({})
49​
50 const [postServerResult, setPostServerResult] = Solid.createSignal({})
51 const [postDirectResult, setPostDirectResult] = Solid.createSignal({})
52​
53 return (
54 <div class="p-2 m-2 grid gap-2">
55 <h3>Consistent Server Fn GET Calls</h3>
56 <p>
57 This component checks whether the returned payloads from server function
58 are the same, regardless of whether the server function is called
59 directly from the client or from within the server function.
60 </p>
61 <div>
62 It should return{' '}
63 <code>
64 <pre data-testid="expected-consistent-server-fns-result">
65 {JSON.stringify({ payload: { username: 'TEST' } })}
66 </pre>
67 </code>
68 </div>
69 <p>
70 {`GET: cons_getFn1 called from server cons_serverGetFn1 returns`}
71 <br />
72 <span data-testid="cons_serverGetFn1-response">
73 {JSON.stringify(getServerResult())}
74 </span>
75 </p>
76 <p>
77 {`GET: cons_getFn1 called directly returns`}
78 <br />
79 <span data-testid="cons_getFn1-response">
80 {JSON.stringify(getDirectResult())}
81 </span>
82 </p>
83 <p>
84 {`POST: cons_postFn1 called from cons_serverPostFn1 returns`}
85 <br />
86 <span data-testid="cons_serverPostFn1-response">
87 {JSON.stringify(postServerResult())}
88 </span>
89 </p>
90 <p>
91 {`POST: cons_postFn1 called directly returns`}
92 <br />
93 <span data-testid="cons_postFn1-response">
94 {JSON.stringify(postDirectResult())}
95 </span>
96 </p>
97 <button
98 data-testid="test-consistent-server-fn-calls-btn"
99 type="button"
100 class="rounded-md bg-white px-2.5 py-1.5 text-sm font-semibold text-gray-900 shadow-xs ring-1 ring-inset ring-gray-300 hover:bg-gray-50"
101 onClick={() => {
102 // GET calls
103 cons_serverGetFn1({ data: { username: 'TEST' } }).then(
104 setGetServerResult,
105 )
106 cons_getFn1({ data: { username: 'TEST' } }).then(setGetDirectResult)
107​
108 // POST calls
109 cons_serverPostFn1({ data: { username: 'TEST' } }).then(
110 setPostServerResult,
111 )
112 cons_postFn1({ data: { username: 'TEST' } }).then(setPostDirectResult)
113​
114 cons_postFn1({ data: { username: 'TEST' } }).then(setPostDirectResult)
115 }}
116 >
117 Test Consistent server function responses
118 </button>
119 </div>
120 )