@tanstack/react-router 1.20.5 → 1.21.0
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/index.cjs +0 -5
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +0 -1
- package/dist/cjs/not-found.cjs.map +1 -1
- package/dist/cjs/not-found.d.cts +1 -0
- package/dist/cjs/redirects.cjs +2 -1
- package/dist/cjs/redirects.cjs.map +1 -1
- package/dist/cjs/redirects.d.cts +2 -1
- package/dist/cjs/routeInfo.d.cts +1 -1
- package/dist/cjs/router.cjs +2 -0
- package/dist/cjs/router.cjs.map +1 -1
- package/dist/esm/index.d.ts +0 -1
- package/dist/esm/index.js +0 -5
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/not-found.d.ts +1 -0
- package/dist/esm/not-found.js.map +1 -1
- package/dist/esm/redirects.d.ts +2 -1
- package/dist/esm/redirects.js +2 -1
- package/dist/esm/redirects.js.map +1 -1
- package/dist/esm/routeInfo.d.ts +1 -1
- package/dist/esm/router.js +2 -0
- package/dist/esm/router.js.map +1 -1
- package/package.json +2 -2
- package/src/index.tsx +0 -1
- package/src/not-found.tsx +1 -0
- package/src/redirects.ts +4 -2
- package/src/routeInfo.ts +1 -2
- package/src/router.ts +3 -1
- package/dist/cjs/createServerFn.cjs +0 -40
- package/dist/cjs/createServerFn.cjs.map +0 -1
- package/dist/cjs/createServerFn.d.cts +0 -44
- package/dist/esm/createServerFn.d.ts +0 -44
- package/dist/esm/createServerFn.js +0 -40
- package/dist/esm/createServerFn.js.map +0 -1
- package/src/createServerFn.ts +0 -107
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
-
const invariant = require("tiny-invariant");
|
|
4
|
-
const serverFnReturnTypeHeader = "server-fn-return-type";
|
|
5
|
-
const serverFnPayloadTypeHeader = "server-fn-payload-type";
|
|
6
|
-
function createServerFn(method, fn) {
|
|
7
|
-
const compiledFn = fn;
|
|
8
|
-
invariant(
|
|
9
|
-
compiledFn.url,
|
|
10
|
-
`createServerFn must be called with a function that is marked with the 'use server' pragma.`
|
|
11
|
-
);
|
|
12
|
-
return Object.assign(
|
|
13
|
-
async (payload, opts) => {
|
|
14
|
-
return compiledFn({
|
|
15
|
-
method,
|
|
16
|
-
payload: payload || void 0,
|
|
17
|
-
requestInit: opts == null ? void 0 : opts.requestInit
|
|
18
|
-
});
|
|
19
|
-
},
|
|
20
|
-
{
|
|
21
|
-
url: fn.url
|
|
22
|
-
}
|
|
23
|
-
);
|
|
24
|
-
}
|
|
25
|
-
function json(payload, opts) {
|
|
26
|
-
return new Response(JSON.stringify(payload), {
|
|
27
|
-
status: (opts == null ? void 0 : opts.status) || 200,
|
|
28
|
-
statusText: (opts == null ? void 0 : opts.statusText) || (opts == null ? void 0 : opts.status) === 200 ? "OK" : "Error",
|
|
29
|
-
headers: {
|
|
30
|
-
"Content-Type": "application/json",
|
|
31
|
-
[serverFnReturnTypeHeader]: "json",
|
|
32
|
-
...opts == null ? void 0 : opts.headers
|
|
33
|
-
}
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
exports.createServerFn = createServerFn;
|
|
37
|
-
exports.json = json;
|
|
38
|
-
exports.serverFnPayloadTypeHeader = serverFnPayloadTypeHeader;
|
|
39
|
-
exports.serverFnReturnTypeHeader = serverFnReturnTypeHeader;
|
|
40
|
-
//# sourceMappingURL=createServerFn.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"createServerFn.cjs","sources":["../../src/createServerFn.ts"],"sourcesContent":["import invariant from 'tiny-invariant'\n\nexport const serverFnReturnTypeHeader = 'server-fn-return-type'\nexport const serverFnPayloadTypeHeader = 'server-fn-payload-type'\n\nexport interface JsonResponse<TData> extends Response {\n json(): Promise<TData>\n}\n\nexport type FetcherOptionsBase = {\n method?: 'GET' | 'POST'\n}\n\nexport type FetcherOptions = FetcherOptionsBase & {\n requestInit?: RequestInit\n}\n\nexport type FetchFnCtx = {\n method: 'GET' | 'POST'\n request: Request\n}\n\nexport type FetchFn<TPayload, TResponse> = {\n (payload: TPayload, ctx: FetchFnCtx): Promise<TResponse> | TResponse\n url?: string\n}\n\nexport type CompiledFetcherFnOptions<TPayload> = {\n method: 'GET' | 'POST'\n payload: TPayload | undefined\n requestInit?: RequestInit\n}\n\nexport type CompiledFetcherFn<TPayload, TResponse> = {\n (opts: CompiledFetcherFnOptions<TPayload>): Promise<TResponse>\n url: string\n}\n\ntype IsPayloadOptional<T> = [T] extends [undefined] ? true : false\n\nexport type Fetcher<TPayload, TResponse> =\n (IsPayloadOptional<TPayload> extends true\n ? {\n (\n payload?: TPayload,\n opts?: FetcherOptions,\n ): Promise<JsonResponseOrPayload<TResponse>>\n }\n : {\n (\n payload: TPayload,\n opts?: FetcherOptions,\n ): Promise<JsonResponseOrPayload<TResponse>>\n }) & {\n url: string\n }\n\nexport type JsonResponseOrPayload<TResponse> =\n TResponse extends JsonResponse<infer TData> ? TData : TResponse\n\nexport function createServerFn<\n TPayload extends any = undefined,\n TResponse = unknown,\n>(\n method: 'GET' | 'POST',\n fn: FetchFn<TPayload, TResponse>,\n): Fetcher<TPayload, TResponse> {\n // Cast the compiled function that will be injected by vinxi\n const compiledFn = fn as unknown as CompiledFetcherFn<TPayload, TResponse>\n\n invariant(\n compiledFn.url,\n `createServerFn must be called with a function that is marked with the 'use server' pragma.`,\n )\n\n return Object.assign(\n async (payload: TPayload, opts?: FetcherOptions) => {\n return compiledFn({\n method,\n payload: payload || undefined,\n requestInit: opts?.requestInit,\n })\n },\n {\n url: fn.url!,\n },\n ) as Fetcher<TPayload, TResponse>\n}\n\nexport function json<TData>(\n payload: TData,\n opts?: {\n status?: number\n statusText?: string\n headers?: HeadersInit\n },\n): JsonResponse<TData> {\n return new Response(JSON.stringify(payload), {\n status: opts?.status || 200,\n statusText: opts?.statusText || opts?.status === 200 ? 'OK' : 'Error',\n headers: {\n 'Content-Type': 'application/json',\n [serverFnReturnTypeHeader]: 'json',\n ...opts?.headers,\n },\n })\n}\n"],"names":[],"mappings":";;;AAEO,MAAM,2BAA2B;AACjC,MAAM,4BAA4B;AAyDzB,SAAA,eAId,QACA,IAC8B;AAE9B,QAAM,aAAa;AAEnB;AAAA,IACE,WAAW;AAAA,IACX;AAAA,EAAA;AAGF,SAAO,OAAO;AAAA,IACZ,OAAO,SAAmB,SAA0B;AAClD,aAAO,WAAW;AAAA,QAChB;AAAA,QACA,SAAS,WAAW;AAAA,QACpB,aAAa,6BAAM;AAAA,MAAA,CACpB;AAAA,IACH;AAAA,IACA;AAAA,MACE,KAAK,GAAG;AAAA,IACV;AAAA,EAAA;AAEJ;AAEgB,SAAA,KACd,SACA,MAKqB;AACrB,SAAO,IAAI,SAAS,KAAK,UAAU,OAAO,GAAG;AAAA,IAC3C,SAAQ,6BAAM,WAAU;AAAA,IACxB,aAAY,6BAAM,gBAAc,6BAAM,YAAW,MAAM,OAAO;AAAA,IAC9D,SAAS;AAAA,MACP,gBAAgB;AAAA,MAChB,CAAC,wBAAwB,GAAG;AAAA,MAC5B,GAAG,6BAAM;AAAA,IACX;AAAA,EAAA,CACD;AACH;;;;;"}
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
export declare const serverFnReturnTypeHeader = "server-fn-return-type";
|
|
2
|
-
export declare const serverFnPayloadTypeHeader = "server-fn-payload-type";
|
|
3
|
-
export interface JsonResponse<TData> extends Response {
|
|
4
|
-
json(): Promise<TData>;
|
|
5
|
-
}
|
|
6
|
-
export type FetcherOptionsBase = {
|
|
7
|
-
method?: 'GET' | 'POST';
|
|
8
|
-
};
|
|
9
|
-
export type FetcherOptions = FetcherOptionsBase & {
|
|
10
|
-
requestInit?: RequestInit;
|
|
11
|
-
};
|
|
12
|
-
export type FetchFnCtx = {
|
|
13
|
-
method: 'GET' | 'POST';
|
|
14
|
-
request: Request;
|
|
15
|
-
};
|
|
16
|
-
export type FetchFn<TPayload, TResponse> = {
|
|
17
|
-
(payload: TPayload, ctx: FetchFnCtx): Promise<TResponse> | TResponse;
|
|
18
|
-
url?: string;
|
|
19
|
-
};
|
|
20
|
-
export type CompiledFetcherFnOptions<TPayload> = {
|
|
21
|
-
method: 'GET' | 'POST';
|
|
22
|
-
payload: TPayload | undefined;
|
|
23
|
-
requestInit?: RequestInit;
|
|
24
|
-
};
|
|
25
|
-
export type CompiledFetcherFn<TPayload, TResponse> = {
|
|
26
|
-
(opts: CompiledFetcherFnOptions<TPayload>): Promise<TResponse>;
|
|
27
|
-
url: string;
|
|
28
|
-
};
|
|
29
|
-
type IsPayloadOptional<T> = [T] extends [undefined] ? true : false;
|
|
30
|
-
export type Fetcher<TPayload, TResponse> = (IsPayloadOptional<TPayload> extends true ? {
|
|
31
|
-
(payload?: TPayload, opts?: FetcherOptions): Promise<JsonResponseOrPayload<TResponse>>;
|
|
32
|
-
} : {
|
|
33
|
-
(payload: TPayload, opts?: FetcherOptions): Promise<JsonResponseOrPayload<TResponse>>;
|
|
34
|
-
}) & {
|
|
35
|
-
url: string;
|
|
36
|
-
};
|
|
37
|
-
export type JsonResponseOrPayload<TResponse> = TResponse extends JsonResponse<infer TData> ? TData : TResponse;
|
|
38
|
-
export declare function createServerFn<TPayload extends any = undefined, TResponse = unknown>(method: 'GET' | 'POST', fn: FetchFn<TPayload, TResponse>): Fetcher<TPayload, TResponse>;
|
|
39
|
-
export declare function json<TData>(payload: TData, opts?: {
|
|
40
|
-
status?: number;
|
|
41
|
-
statusText?: string;
|
|
42
|
-
headers?: HeadersInit;
|
|
43
|
-
}): JsonResponse<TData>;
|
|
44
|
-
export {};
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
export declare const serverFnReturnTypeHeader = "server-fn-return-type";
|
|
2
|
-
export declare const serverFnPayloadTypeHeader = "server-fn-payload-type";
|
|
3
|
-
export interface JsonResponse<TData> extends Response {
|
|
4
|
-
json(): Promise<TData>;
|
|
5
|
-
}
|
|
6
|
-
export type FetcherOptionsBase = {
|
|
7
|
-
method?: 'GET' | 'POST';
|
|
8
|
-
};
|
|
9
|
-
export type FetcherOptions = FetcherOptionsBase & {
|
|
10
|
-
requestInit?: RequestInit;
|
|
11
|
-
};
|
|
12
|
-
export type FetchFnCtx = {
|
|
13
|
-
method: 'GET' | 'POST';
|
|
14
|
-
request: Request;
|
|
15
|
-
};
|
|
16
|
-
export type FetchFn<TPayload, TResponse> = {
|
|
17
|
-
(payload: TPayload, ctx: FetchFnCtx): Promise<TResponse> | TResponse;
|
|
18
|
-
url?: string;
|
|
19
|
-
};
|
|
20
|
-
export type CompiledFetcherFnOptions<TPayload> = {
|
|
21
|
-
method: 'GET' | 'POST';
|
|
22
|
-
payload: TPayload | undefined;
|
|
23
|
-
requestInit?: RequestInit;
|
|
24
|
-
};
|
|
25
|
-
export type CompiledFetcherFn<TPayload, TResponse> = {
|
|
26
|
-
(opts: CompiledFetcherFnOptions<TPayload>): Promise<TResponse>;
|
|
27
|
-
url: string;
|
|
28
|
-
};
|
|
29
|
-
type IsPayloadOptional<T> = [T] extends [undefined] ? true : false;
|
|
30
|
-
export type Fetcher<TPayload, TResponse> = (IsPayloadOptional<TPayload> extends true ? {
|
|
31
|
-
(payload?: TPayload, opts?: FetcherOptions): Promise<JsonResponseOrPayload<TResponse>>;
|
|
32
|
-
} : {
|
|
33
|
-
(payload: TPayload, opts?: FetcherOptions): Promise<JsonResponseOrPayload<TResponse>>;
|
|
34
|
-
}) & {
|
|
35
|
-
url: string;
|
|
36
|
-
};
|
|
37
|
-
export type JsonResponseOrPayload<TResponse> = TResponse extends JsonResponse<infer TData> ? TData : TResponse;
|
|
38
|
-
export declare function createServerFn<TPayload extends any = undefined, TResponse = unknown>(method: 'GET' | 'POST', fn: FetchFn<TPayload, TResponse>): Fetcher<TPayload, TResponse>;
|
|
39
|
-
export declare function json<TData>(payload: TData, opts?: {
|
|
40
|
-
status?: number;
|
|
41
|
-
statusText?: string;
|
|
42
|
-
headers?: HeadersInit;
|
|
43
|
-
}): JsonResponse<TData>;
|
|
44
|
-
export {};
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import invariant from "tiny-invariant";
|
|
2
|
-
const serverFnReturnTypeHeader = "server-fn-return-type";
|
|
3
|
-
const serverFnPayloadTypeHeader = "server-fn-payload-type";
|
|
4
|
-
function createServerFn(method, fn) {
|
|
5
|
-
const compiledFn = fn;
|
|
6
|
-
invariant(
|
|
7
|
-
compiledFn.url,
|
|
8
|
-
`createServerFn must be called with a function that is marked with the 'use server' pragma.`
|
|
9
|
-
);
|
|
10
|
-
return Object.assign(
|
|
11
|
-
async (payload, opts) => {
|
|
12
|
-
return compiledFn({
|
|
13
|
-
method,
|
|
14
|
-
payload: payload || void 0,
|
|
15
|
-
requestInit: opts == null ? void 0 : opts.requestInit
|
|
16
|
-
});
|
|
17
|
-
},
|
|
18
|
-
{
|
|
19
|
-
url: fn.url
|
|
20
|
-
}
|
|
21
|
-
);
|
|
22
|
-
}
|
|
23
|
-
function json(payload, opts) {
|
|
24
|
-
return new Response(JSON.stringify(payload), {
|
|
25
|
-
status: (opts == null ? void 0 : opts.status) || 200,
|
|
26
|
-
statusText: (opts == null ? void 0 : opts.statusText) || (opts == null ? void 0 : opts.status) === 200 ? "OK" : "Error",
|
|
27
|
-
headers: {
|
|
28
|
-
"Content-Type": "application/json",
|
|
29
|
-
[serverFnReturnTypeHeader]: "json",
|
|
30
|
-
...opts == null ? void 0 : opts.headers
|
|
31
|
-
}
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
export {
|
|
35
|
-
createServerFn,
|
|
36
|
-
json,
|
|
37
|
-
serverFnPayloadTypeHeader,
|
|
38
|
-
serverFnReturnTypeHeader
|
|
39
|
-
};
|
|
40
|
-
//# sourceMappingURL=createServerFn.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"createServerFn.js","sources":["../../src/createServerFn.ts"],"sourcesContent":["import invariant from 'tiny-invariant'\n\nexport const serverFnReturnTypeHeader = 'server-fn-return-type'\nexport const serverFnPayloadTypeHeader = 'server-fn-payload-type'\n\nexport interface JsonResponse<TData> extends Response {\n json(): Promise<TData>\n}\n\nexport type FetcherOptionsBase = {\n method?: 'GET' | 'POST'\n}\n\nexport type FetcherOptions = FetcherOptionsBase & {\n requestInit?: RequestInit\n}\n\nexport type FetchFnCtx = {\n method: 'GET' | 'POST'\n request: Request\n}\n\nexport type FetchFn<TPayload, TResponse> = {\n (payload: TPayload, ctx: FetchFnCtx): Promise<TResponse> | TResponse\n url?: string\n}\n\nexport type CompiledFetcherFnOptions<TPayload> = {\n method: 'GET' | 'POST'\n payload: TPayload | undefined\n requestInit?: RequestInit\n}\n\nexport type CompiledFetcherFn<TPayload, TResponse> = {\n (opts: CompiledFetcherFnOptions<TPayload>): Promise<TResponse>\n url: string\n}\n\ntype IsPayloadOptional<T> = [T] extends [undefined] ? true : false\n\nexport type Fetcher<TPayload, TResponse> =\n (IsPayloadOptional<TPayload> extends true\n ? {\n (\n payload?: TPayload,\n opts?: FetcherOptions,\n ): Promise<JsonResponseOrPayload<TResponse>>\n }\n : {\n (\n payload: TPayload,\n opts?: FetcherOptions,\n ): Promise<JsonResponseOrPayload<TResponse>>\n }) & {\n url: string\n }\n\nexport type JsonResponseOrPayload<TResponse> =\n TResponse extends JsonResponse<infer TData> ? TData : TResponse\n\nexport function createServerFn<\n TPayload extends any = undefined,\n TResponse = unknown,\n>(\n method: 'GET' | 'POST',\n fn: FetchFn<TPayload, TResponse>,\n): Fetcher<TPayload, TResponse> {\n // Cast the compiled function that will be injected by vinxi\n const compiledFn = fn as unknown as CompiledFetcherFn<TPayload, TResponse>\n\n invariant(\n compiledFn.url,\n `createServerFn must be called with a function that is marked with the 'use server' pragma.`,\n )\n\n return Object.assign(\n async (payload: TPayload, opts?: FetcherOptions) => {\n return compiledFn({\n method,\n payload: payload || undefined,\n requestInit: opts?.requestInit,\n })\n },\n {\n url: fn.url!,\n },\n ) as Fetcher<TPayload, TResponse>\n}\n\nexport function json<TData>(\n payload: TData,\n opts?: {\n status?: number\n statusText?: string\n headers?: HeadersInit\n },\n): JsonResponse<TData> {\n return new Response(JSON.stringify(payload), {\n status: opts?.status || 200,\n statusText: opts?.statusText || opts?.status === 200 ? 'OK' : 'Error',\n headers: {\n 'Content-Type': 'application/json',\n [serverFnReturnTypeHeader]: 'json',\n ...opts?.headers,\n },\n })\n}\n"],"names":[],"mappings":";AAEO,MAAM,2BAA2B;AACjC,MAAM,4BAA4B;AAyDzB,SAAA,eAId,QACA,IAC8B;AAE9B,QAAM,aAAa;AAEnB;AAAA,IACE,WAAW;AAAA,IACX;AAAA,EAAA;AAGF,SAAO,OAAO;AAAA,IACZ,OAAO,SAAmB,SAA0B;AAClD,aAAO,WAAW;AAAA,QAChB;AAAA,QACA,SAAS,WAAW;AAAA,QACpB,aAAa,6BAAM;AAAA,MAAA,CACpB;AAAA,IACH;AAAA,IACA;AAAA,MACE,KAAK,GAAG;AAAA,IACV;AAAA,EAAA;AAEJ;AAEgB,SAAA,KACd,SACA,MAKqB;AACrB,SAAO,IAAI,SAAS,KAAK,UAAU,OAAO,GAAG;AAAA,IAC3C,SAAQ,6BAAM,WAAU;AAAA,IACxB,aAAY,6BAAM,gBAAc,6BAAM,YAAW,MAAM,OAAO;AAAA,IAC9D,SAAS;AAAA,MACP,gBAAgB;AAAA,MAChB,CAAC,wBAAwB,GAAG;AAAA,MAC5B,GAAG,6BAAM;AAAA,IACX;AAAA,EAAA,CACD;AACH;"}
|
package/src/createServerFn.ts
DELETED
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
import invariant from 'tiny-invariant'
|
|
2
|
-
|
|
3
|
-
export const serverFnReturnTypeHeader = 'server-fn-return-type'
|
|
4
|
-
export const serverFnPayloadTypeHeader = 'server-fn-payload-type'
|
|
5
|
-
|
|
6
|
-
export interface JsonResponse<TData> extends Response {
|
|
7
|
-
json(): Promise<TData>
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export type FetcherOptionsBase = {
|
|
11
|
-
method?: 'GET' | 'POST'
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export type FetcherOptions = FetcherOptionsBase & {
|
|
15
|
-
requestInit?: RequestInit
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export type FetchFnCtx = {
|
|
19
|
-
method: 'GET' | 'POST'
|
|
20
|
-
request: Request
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export type FetchFn<TPayload, TResponse> = {
|
|
24
|
-
(payload: TPayload, ctx: FetchFnCtx): Promise<TResponse> | TResponse
|
|
25
|
-
url?: string
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export type CompiledFetcherFnOptions<TPayload> = {
|
|
29
|
-
method: 'GET' | 'POST'
|
|
30
|
-
payload: TPayload | undefined
|
|
31
|
-
requestInit?: RequestInit
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export type CompiledFetcherFn<TPayload, TResponse> = {
|
|
35
|
-
(opts: CompiledFetcherFnOptions<TPayload>): Promise<TResponse>
|
|
36
|
-
url: string
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
type IsPayloadOptional<T> = [T] extends [undefined] ? true : false
|
|
40
|
-
|
|
41
|
-
export type Fetcher<TPayload, TResponse> =
|
|
42
|
-
(IsPayloadOptional<TPayload> extends true
|
|
43
|
-
? {
|
|
44
|
-
(
|
|
45
|
-
payload?: TPayload,
|
|
46
|
-
opts?: FetcherOptions,
|
|
47
|
-
): Promise<JsonResponseOrPayload<TResponse>>
|
|
48
|
-
}
|
|
49
|
-
: {
|
|
50
|
-
(
|
|
51
|
-
payload: TPayload,
|
|
52
|
-
opts?: FetcherOptions,
|
|
53
|
-
): Promise<JsonResponseOrPayload<TResponse>>
|
|
54
|
-
}) & {
|
|
55
|
-
url: string
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
export type JsonResponseOrPayload<TResponse> =
|
|
59
|
-
TResponse extends JsonResponse<infer TData> ? TData : TResponse
|
|
60
|
-
|
|
61
|
-
export function createServerFn<
|
|
62
|
-
TPayload extends any = undefined,
|
|
63
|
-
TResponse = unknown,
|
|
64
|
-
>(
|
|
65
|
-
method: 'GET' | 'POST',
|
|
66
|
-
fn: FetchFn<TPayload, TResponse>,
|
|
67
|
-
): Fetcher<TPayload, TResponse> {
|
|
68
|
-
// Cast the compiled function that will be injected by vinxi
|
|
69
|
-
const compiledFn = fn as unknown as CompiledFetcherFn<TPayload, TResponse>
|
|
70
|
-
|
|
71
|
-
invariant(
|
|
72
|
-
compiledFn.url,
|
|
73
|
-
`createServerFn must be called with a function that is marked with the 'use server' pragma.`,
|
|
74
|
-
)
|
|
75
|
-
|
|
76
|
-
return Object.assign(
|
|
77
|
-
async (payload: TPayload, opts?: FetcherOptions) => {
|
|
78
|
-
return compiledFn({
|
|
79
|
-
method,
|
|
80
|
-
payload: payload || undefined,
|
|
81
|
-
requestInit: opts?.requestInit,
|
|
82
|
-
})
|
|
83
|
-
},
|
|
84
|
-
{
|
|
85
|
-
url: fn.url!,
|
|
86
|
-
},
|
|
87
|
-
) as Fetcher<TPayload, TResponse>
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
export function json<TData>(
|
|
91
|
-
payload: TData,
|
|
92
|
-
opts?: {
|
|
93
|
-
status?: number
|
|
94
|
-
statusText?: string
|
|
95
|
-
headers?: HeadersInit
|
|
96
|
-
},
|
|
97
|
-
): JsonResponse<TData> {
|
|
98
|
-
return new Response(JSON.stringify(payload), {
|
|
99
|
-
status: opts?.status || 200,
|
|
100
|
-
statusText: opts?.statusText || opts?.status === 200 ? 'OK' : 'Error',
|
|
101
|
-
headers: {
|
|
102
|
-
'Content-Type': 'application/json',
|
|
103
|
-
[serverFnReturnTypeHeader]: 'json',
|
|
104
|
-
...opts?.headers,
|
|
105
|
-
},
|
|
106
|
-
})
|
|
107
|
-
}
|