@tanstack/react-router 1.15.2 → 1.15.4
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/dist/cjs/createServerFn.cjs +32 -0
- package/dist/cjs/createServerFn.cjs.map +1 -0
- package/dist/cjs/createServerFn.d.cts +36 -0
- package/dist/cjs/index.cjs +3 -0
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +1 -0
- package/dist/cjs/link.cjs +1 -1
- package/dist/cjs/link.cjs.map +1 -1
- package/dist/cjs/router.cjs +5 -2
- package/dist/cjs/router.cjs.map +1 -1
- package/dist/esm/createServerFn.d.ts +36 -0
- package/dist/esm/createServerFn.js +32 -0
- package/dist/esm/createServerFn.js.map +1 -0
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/index.js +3 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/link.js +1 -1
- package/dist/esm/link.js.map +1 -1
- package/dist/esm/router.js +5 -2
- package/dist/esm/router.js.map +1 -1
- package/package.json +1 -1
- package/src/createServerFn.ts +105 -0
- package/src/index.tsx +1 -0
- package/src/link.tsx +1 -1
- package/src/router.ts +5 -2
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import invariant from 'tiny-invariant'
|
|
2
|
+
|
|
3
|
+
export const XTSROrigin = 'x-tsr-origin'
|
|
4
|
+
|
|
5
|
+
export interface JsonResponse<TData> extends Response {
|
|
6
|
+
json(): Promise<TData>
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export type FetcherOptionsBase = {
|
|
10
|
+
method?: 'GET' | 'POST'
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export type FetcherOptions = FetcherOptionsBase & {
|
|
14
|
+
requestInit?: RequestInit
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export type FetchFnCtx = {
|
|
18
|
+
method: 'GET' | 'POST'
|
|
19
|
+
request: Request
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export type FetchFn<TPayload, TResponse> = {
|
|
23
|
+
(payload: TPayload, ctx: FetchFnCtx): TResponse
|
|
24
|
+
url?: string
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// export type AnyFetchFn = FetchFn<any, any>
|
|
28
|
+
|
|
29
|
+
// export type FetchFnReturn<T extends AnyFetchFn> =
|
|
30
|
+
// Awaited<ReturnType<T>> extends JsonResponse<infer R> ? R : ReturnType<T>
|
|
31
|
+
|
|
32
|
+
// export type FetcherFn<T extends AnyFetchFn> = Parameters<T>[0] extends undefined
|
|
33
|
+
// ? (
|
|
34
|
+
// payload?: Parameters<T>['0'],
|
|
35
|
+
// opts?: FetchFnCtx,
|
|
36
|
+
// ) => Promise<Awaited<FetchFnReturn<T>>>
|
|
37
|
+
// : (
|
|
38
|
+
// payload: Parameters<T>['0'],
|
|
39
|
+
// opts?: FetchFnCtx,
|
|
40
|
+
// ) => Promise<Awaited<FetchFnReturn<T>>>
|
|
41
|
+
|
|
42
|
+
// export type FetcherMethods<T extends AnyFetchFn> = {
|
|
43
|
+
// url: string
|
|
44
|
+
// fetch: (
|
|
45
|
+
// init: RequestInit,
|
|
46
|
+
// opts?: FetcherOptions,
|
|
47
|
+
// ) => Promise<Awaited<FetchFnReturn<T>>>
|
|
48
|
+
// }
|
|
49
|
+
|
|
50
|
+
// export type Fetcher<T extends AnyFetchFn> = FetcherFn<T> & FetcherMethods<T>
|
|
51
|
+
|
|
52
|
+
export type CompiledFetcherFnOptions<TPayload> = {
|
|
53
|
+
method: 'GET' | 'POST'
|
|
54
|
+
type: 'request' | 'payload'
|
|
55
|
+
payload: TPayload
|
|
56
|
+
requestInit?: RequestInit
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export type CompiledFetcherFn<TPayload, TResponse> = {
|
|
60
|
+
(opts: CompiledFetcherFnOptions<TPayload>): Promise<TResponse>
|
|
61
|
+
url: string
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export type Fetcher<TPayload, TResponse> = (TPayload extends undefined
|
|
65
|
+
? {
|
|
66
|
+
(payload?: TPayload, opts?: FetcherOptions): Promise<TResponse>
|
|
67
|
+
}
|
|
68
|
+
: {
|
|
69
|
+
(payload: TPayload, opts?: FetcherOptions): Promise<TResponse>
|
|
70
|
+
}) & {
|
|
71
|
+
url: string
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function createServerFn<TPayload, TResponse>(
|
|
75
|
+
method: 'GET' | 'POST',
|
|
76
|
+
fn: FetchFn<TPayload, TResponse>,
|
|
77
|
+
): Fetcher<TPayload, TResponse> {
|
|
78
|
+
// Cast the compiled function that will be injected by vinxi
|
|
79
|
+
const compiledFn = fn as unknown as CompiledFetcherFn<TPayload, TResponse>
|
|
80
|
+
|
|
81
|
+
console.log(
|
|
82
|
+
compiledFn,
|
|
83
|
+
compiledFn.toString(),
|
|
84
|
+
JSON.stringify(compiledFn, null, 2),
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
invariant(
|
|
88
|
+
compiledFn.url,
|
|
89
|
+
`createServerFn must be called with a function that is marked with the 'use server' pragma.`,
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
return Object.assign(
|
|
93
|
+
async (payload: TPayload, opts?: FetcherOptions) => {
|
|
94
|
+
return compiledFn({
|
|
95
|
+
method,
|
|
96
|
+
type: payload instanceof Request ? 'request' : 'payload',
|
|
97
|
+
payload,
|
|
98
|
+
requestInit: opts?.requestInit,
|
|
99
|
+
})
|
|
100
|
+
},
|
|
101
|
+
{
|
|
102
|
+
url: fn.url!,
|
|
103
|
+
},
|
|
104
|
+
) as Fetcher<TPayload, TResponse>
|
|
105
|
+
}
|
package/src/index.tsx
CHANGED
package/src/link.tsx
CHANGED
|
@@ -571,7 +571,7 @@ export const Link: LinkComponent = React.forwardRef((props: any, ref) => {
|
|
|
571
571
|
const children =
|
|
572
572
|
typeof props.children === 'function'
|
|
573
573
|
? props.children({
|
|
574
|
-
isActive: (
|
|
574
|
+
isActive: (linkProps as any)['data-status'] === 'active',
|
|
575
575
|
})
|
|
576
576
|
: props.children
|
|
577
577
|
|
package/src/router.ts
CHANGED
|
@@ -1243,7 +1243,10 @@ export class Router<
|
|
|
1243
1243
|
}
|
|
1244
1244
|
|
|
1245
1245
|
if (isNotFound(err)) {
|
|
1246
|
-
|
|
1246
|
+
if (!preload) {
|
|
1247
|
+
this.updateMatchesWithNotFound(matches, match, err)
|
|
1248
|
+
}
|
|
1249
|
+
return true
|
|
1247
1250
|
}
|
|
1248
1251
|
|
|
1249
1252
|
return false
|
|
@@ -1339,7 +1342,7 @@ export class Router<
|
|
|
1339
1342
|
const loaderData = await loadPromise
|
|
1340
1343
|
if ((latestPromise = checkLatest())) return await latestPromise
|
|
1341
1344
|
|
|
1342
|
-
if (isRedirect(loaderData)) {
|
|
1345
|
+
if (isRedirect(loaderData) || isNotFound(loaderData)) {
|
|
1343
1346
|
if (handleErrorAndRedirect(loaderData)) return
|
|
1344
1347
|
}
|
|
1345
1348
|
|