@tanstack/react-start-client 1.111.11 → 1.112.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.
@@ -40,24 +40,16 @@ function hydrate(router) {
40
40
  })
41
41
  );
42
42
  matches.forEach((match) => {
43
- var _a2, _b2, _c2, _d, _e;
44
- const route = router.looseRoutesById[match.routeId];
43
+ var _a2;
45
44
  const dehydratedMatch = window.__TSR_SSR__.matches.find(
46
45
  (d) => d.id === match.id
47
46
  );
48
47
  if (dehydratedMatch) {
49
48
  Object.assign(match, dehydratedMatch);
50
- const parentMatch = matches[match.index - 1];
51
- const parentContext = (parentMatch == null ? void 0 : parentMatch.context) ?? router.options.context ?? {};
52
49
  if (dehydratedMatch.__beforeLoadContext) {
53
50
  match.__beforeLoadContext = router.ssr.serializer.parse(
54
51
  dehydratedMatch.__beforeLoadContext
55
52
  );
56
- match.context = {
57
- ...parentContext,
58
- ...match.__routeContext,
59
- ...match.__beforeLoadContext
60
- };
61
53
  }
62
54
  if (dehydratedMatch.loaderData) {
63
55
  match.loaderData = router.ssr.serializer.parse(
@@ -76,27 +68,51 @@ function hydrate(router) {
76
68
  updatedAt: Date.now()
77
69
  });
78
70
  }
71
+ return match;
72
+ });
73
+ router.__store.setState((s) => {
74
+ return {
75
+ ...s,
76
+ matches
77
+ };
78
+ });
79
+ (_c = (_b = router.options).hydrate) == null ? void 0 : _c.call(_b, dehydratedData);
80
+ router.state.matches.forEach((match) => {
81
+ var _a2, _b2, _c2, _d, _e, _f;
82
+ const route = router.looseRoutesById[match.routeId];
83
+ const parentMatch = router.state.matches[match.index - 1];
84
+ const parentContext = (parentMatch == null ? void 0 : parentMatch.context) ?? router.options.context ?? {};
85
+ const contextFnContext = {
86
+ deps: match.loaderDeps,
87
+ params: match.params,
88
+ context: parentContext,
89
+ location: router.state.location,
90
+ navigate: (opts) => router.navigate({ ...opts, _fromLocation: router.state.location }),
91
+ buildLocation: router.buildLocation,
92
+ cause: match.cause,
93
+ abortController: match.abortController,
94
+ preload: false,
95
+ matches
96
+ };
97
+ match.__routeContext = ((_b2 = (_a2 = route.options).context) == null ? void 0 : _b2.call(_a2, contextFnContext)) ?? {};
98
+ match.context = {
99
+ ...parentContext,
100
+ ...match.__routeContext,
101
+ ...match.__beforeLoadContext
102
+ };
79
103
  const assetContext = {
80
104
  matches: router.state.matches,
81
105
  match,
82
106
  params: match.params,
83
107
  loaderData: match.loaderData
84
108
  };
85
- const headFnContent = (_c2 = (_b2 = route.options).head) == null ? void 0 : _c2.call(_b2, assetContext);
86
- const scripts = (_e = (_d = route.options).scripts) == null ? void 0 : _e.call(_d, assetContext);
109
+ const headFnContent = (_d = (_c2 = route.options).head) == null ? void 0 : _d.call(_c2, assetContext);
110
+ const scripts = (_f = (_e = route.options).scripts) == null ? void 0 : _f.call(_e, assetContext);
87
111
  match.meta = headFnContent == null ? void 0 : headFnContent.meta;
88
112
  match.links = headFnContent == null ? void 0 : headFnContent.links;
89
113
  match.headScripts = headFnContent == null ? void 0 : headFnContent.scripts;
90
114
  match.scripts = scripts;
91
- return match;
92
115
  });
93
- router.__store.setState((s) => {
94
- return {
95
- ...s,
96
- matches
97
- };
98
- });
99
- (_c = (_b = router.options).hydrate) == null ? void 0 : _c.call(_b, dehydratedData);
100
116
  return routeChunkPromise;
101
117
  }
102
118
  function deepMutableSetByPath(obj, path, value) {
@@ -1 +1 @@
1
- {"version":3,"file":"ssr-client.cjs","sources":["../../src/ssr-client.tsx"],"sourcesContent":["import { isPlainObject } from '@tanstack/router-core'\n\nimport invariant from 'tiny-invariant'\n\nimport { startSerializer } from './serializer'\nimport type {\n AnyRouter,\n ControllablePromise,\n MakeRouteMatch,\n} from '@tanstack/react-router'\n\nimport type { DeferredPromiseState, Manifest } from '@tanstack/router-core'\n\ndeclare global {\n interface Window {\n __TSR_SSR__?: StartSsrGlobal\n }\n}\n\nexport interface StartSsrGlobal {\n matches: Array<SsrMatch>\n streamedValues: Record<\n string,\n {\n value: any\n parsed: any\n }\n >\n cleanScripts: () => void\n dehydrated?: any\n initMatch: (match: SsrMatch) => void\n resolvePromise: (opts: {\n matchId: string\n id: number\n promiseState: DeferredPromiseState<any>\n }) => void\n injectChunk: (opts: { matchId: string; id: number; chunk: string }) => void\n closeStream: (opts: { matchId: string; id: number }) => void\n}\n\nexport interface SsrMatch {\n id: string\n __beforeLoadContext: string\n loaderData?: string\n error?: string\n extracted?: Array<ClientExtractedEntry>\n updatedAt: MakeRouteMatch['updatedAt']\n status: MakeRouteMatch['status']\n}\n\nexport type ClientExtractedEntry =\n | ClientExtractedStream\n | ClientExtractedPromise\n\nexport interface ClientExtractedPromise extends ClientExtractedBaseEntry {\n type: 'promise'\n value?: ControllablePromise<any>\n}\n\nexport interface ClientExtractedStream extends ClientExtractedBaseEntry {\n type: 'stream'\n value?: ReadableStream & { controller?: ReadableStreamDefaultController }\n}\n\nexport interface ClientExtractedBaseEntry {\n type: string\n path: Array<string>\n}\n\nexport interface ResolvePromiseState {\n matchId: string\n id: number\n promiseState: DeferredPromiseState<any>\n}\n\nexport interface DehydratedRouter {\n manifest: Manifest | undefined\n dehydratedData: any\n}\n\nexport function hydrate(router: AnyRouter) {\n invariant(\n window.__TSR_SSR__?.dehydrated,\n 'Expected to find a dehydrated data on window.__TSR_SSR__.dehydrated... but we did not. Please file an issue!',\n )\n\n const { manifest, dehydratedData } = startSerializer.parse(\n window.__TSR_SSR__.dehydrated,\n ) as DehydratedRouter\n\n router.ssr = {\n manifest,\n serializer: startSerializer,\n }\n\n router.clientSsr = {\n getStreamedValue: <T,>(key: string): T | undefined => {\n if (router.isServer) {\n return undefined\n }\n\n const streamedValue = window.__TSR_SSR__?.streamedValues[key]\n\n if (!streamedValue) {\n return\n }\n\n if (!streamedValue.parsed) {\n streamedValue.parsed = router.ssr!.serializer.parse(streamedValue.value)\n }\n\n return streamedValue.parsed\n },\n }\n\n // Hydrate the router state\n const matches = router.matchRoutes(router.state.location)\n // kick off loading the route chunks\n const routeChunkPromise = Promise.all(\n matches.map((match) => {\n const route = router.looseRoutesById[match.routeId]!\n return router.loadRouteChunk(route)\n }),\n )\n matches.forEach((match) => {\n const route = router.looseRoutesById[match.routeId]!\n\n // Right after hydration and before the first render, we need to rehydrate each match\n // This includes rehydrating the loaderData and also using the beforeLoadContext\n // to reconstruct any context that was serialized on the server\n\n const dehydratedMatch = window.__TSR_SSR__!.matches.find(\n (d) => d.id === match.id,\n )\n\n if (dehydratedMatch) {\n Object.assign(match, dehydratedMatch)\n\n const parentMatch = matches[match.index - 1]\n const parentContext = parentMatch?.context ?? router.options.context ?? {}\n\n // Handle beforeLoadContext\n if (dehydratedMatch.__beforeLoadContext) {\n match.__beforeLoadContext = router.ssr!.serializer.parse(\n dehydratedMatch.__beforeLoadContext,\n ) as any\n\n match.context = {\n ...parentContext,\n ...match.__routeContext,\n ...match.__beforeLoadContext,\n }\n }\n\n // Handle loaderData\n if (dehydratedMatch.loaderData) {\n match.loaderData = router.ssr!.serializer.parse(\n dehydratedMatch.loaderData,\n )\n }\n\n // Handle error\n if (dehydratedMatch.error) {\n match.error = router.ssr!.serializer.parse(dehydratedMatch.error)\n }\n\n // Handle extracted\n ;(match as unknown as SsrMatch).extracted?.forEach((ex) => {\n deepMutableSetByPath(match, ['loaderData', ...ex.path], ex.value)\n })\n } else {\n Object.assign(match, {\n status: 'success',\n updatedAt: Date.now(),\n })\n }\n\n const assetContext = {\n matches: router.state.matches,\n match,\n params: match.params,\n loaderData: match.loaderData,\n }\n const headFnContent = route.options.head?.(assetContext)\n\n const scripts = route.options.scripts?.(assetContext)\n\n match.meta = headFnContent?.meta\n match.links = headFnContent?.links\n match.headScripts = headFnContent?.scripts\n match.scripts = scripts\n\n return match\n })\n\n router.__store.setState((s) => {\n return {\n ...s,\n matches,\n }\n })\n\n // Allow the user to handle custom hydration data\n router.options.hydrate?.(dehydratedData)\n return routeChunkPromise\n}\n\nfunction deepMutableSetByPath<T>(obj: T, path: Array<string>, value: any) {\n // mutable set by path retaining array and object references\n if (path.length === 1) {\n ;(obj as any)[path[0]!] = value\n }\n\n const [key, ...rest] = path\n\n if (Array.isArray(obj)) {\n deepMutableSetByPath(obj[Number(key)], rest, value)\n } else if (isPlainObject(obj)) {\n deepMutableSetByPath((obj as any)[key!], rest, value)\n }\n}\n"],"names":["startSerializer","_a","_c","_b","isPlainObject"],"mappings":"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"}
1
+ {"version":3,"file":"ssr-client.cjs","sources":["../../src/ssr-client.tsx"],"sourcesContent":["import { isPlainObject } from '@tanstack/router-core'\n\nimport invariant from 'tiny-invariant'\n\nimport { startSerializer } from './serializer'\nimport type {\n AnyRouter,\n ControllablePromise,\n MakeRouteMatch,\n} from '@tanstack/react-router'\n\nimport type {\n DeferredPromiseState,\n Manifest,\n RouteContextOptions,\n} from '@tanstack/router-core'\n\ndeclare global {\n interface Window {\n __TSR_SSR__?: StartSsrGlobal\n }\n}\n\nexport interface StartSsrGlobal {\n matches: Array<SsrMatch>\n streamedValues: Record<\n string,\n {\n value: any\n parsed: any\n }\n >\n cleanScripts: () => void\n dehydrated?: any\n initMatch: (match: SsrMatch) => void\n resolvePromise: (opts: {\n matchId: string\n id: number\n promiseState: DeferredPromiseState<any>\n }) => void\n injectChunk: (opts: { matchId: string; id: number; chunk: string }) => void\n closeStream: (opts: { matchId: string; id: number }) => void\n}\n\nexport interface SsrMatch {\n id: string\n __beforeLoadContext: string\n loaderData?: string\n error?: string\n extracted?: Array<ClientExtractedEntry>\n updatedAt: MakeRouteMatch['updatedAt']\n status: MakeRouteMatch['status']\n}\n\nexport type ClientExtractedEntry =\n | ClientExtractedStream\n | ClientExtractedPromise\n\nexport interface ClientExtractedPromise extends ClientExtractedBaseEntry {\n type: 'promise'\n value?: ControllablePromise<any>\n}\n\nexport interface ClientExtractedStream extends ClientExtractedBaseEntry {\n type: 'stream'\n value?: ReadableStream & { controller?: ReadableStreamDefaultController }\n}\n\nexport interface ClientExtractedBaseEntry {\n type: string\n path: Array<string>\n}\n\nexport interface ResolvePromiseState {\n matchId: string\n id: number\n promiseState: DeferredPromiseState<any>\n}\n\nexport interface DehydratedRouter {\n manifest: Manifest | undefined\n dehydratedData: any\n}\n\nexport function hydrate(router: AnyRouter) {\n invariant(\n window.__TSR_SSR__?.dehydrated,\n 'Expected to find a dehydrated data on window.__TSR_SSR__.dehydrated... but we did not. Please file an issue!',\n )\n\n const { manifest, dehydratedData } = startSerializer.parse(\n window.__TSR_SSR__.dehydrated,\n ) as DehydratedRouter\n\n router.ssr = {\n manifest,\n serializer: startSerializer,\n }\n\n router.clientSsr = {\n getStreamedValue: <T,>(key: string): T | undefined => {\n if (router.isServer) {\n return undefined\n }\n\n const streamedValue = window.__TSR_SSR__?.streamedValues[key]\n\n if (!streamedValue) {\n return\n }\n\n if (!streamedValue.parsed) {\n streamedValue.parsed = router.ssr!.serializer.parse(streamedValue.value)\n }\n\n return streamedValue.parsed\n },\n }\n\n // Hydrate the router state\n const matches = router.matchRoutes(router.state.location)\n // kick off loading the route chunks\n const routeChunkPromise = Promise.all(\n matches.map((match) => {\n const route = router.looseRoutesById[match.routeId]!\n return router.loadRouteChunk(route)\n }),\n )\n // Right after hydration and before the first render, we need to rehydrate each match\n // First step is to reyhdrate loaderData and __beforeLoadContext\n matches.forEach((match) => {\n const dehydratedMatch = window.__TSR_SSR__!.matches.find(\n (d) => d.id === match.id,\n )\n\n if (dehydratedMatch) {\n Object.assign(match, dehydratedMatch)\n\n // Handle beforeLoadContext\n if (dehydratedMatch.__beforeLoadContext) {\n match.__beforeLoadContext = router.ssr!.serializer.parse(\n dehydratedMatch.__beforeLoadContext,\n ) as any\n }\n\n // Handle loaderData\n if (dehydratedMatch.loaderData) {\n match.loaderData = router.ssr!.serializer.parse(\n dehydratedMatch.loaderData,\n )\n }\n\n // Handle error\n if (dehydratedMatch.error) {\n match.error = router.ssr!.serializer.parse(dehydratedMatch.error)\n }\n\n // Handle extracted\n ;(match as unknown as SsrMatch).extracted?.forEach((ex) => {\n deepMutableSetByPath(match, ['loaderData', ...ex.path], ex.value)\n })\n } else {\n Object.assign(match, {\n status: 'success',\n updatedAt: Date.now(),\n })\n }\n\n return match\n })\n\n router.__store.setState((s) => {\n return {\n ...s,\n matches,\n }\n })\n\n // Allow the user to handle custom hydration data\n router.options.hydrate?.(dehydratedData)\n\n // now that all necessary data is hydrated:\n // 1) fully reconstruct the route context\n // 2) execute `head()` and `scripts()` for each match\n router.state.matches.forEach((match) => {\n const route = router.looseRoutesById[match.routeId]!\n\n const parentMatch = router.state.matches[match.index - 1]\n const parentContext = parentMatch?.context ?? router.options.context ?? {}\n\n // `context()` was already executed by `matchRoutes`, however route context was not yet fully reconstructed\n // so run it again and merge route context\n const contextFnContext: RouteContextOptions<any, any, any, any> = {\n deps: match.loaderDeps,\n params: match.params,\n context: parentContext,\n location: router.state.location,\n navigate: (opts: any) =>\n router.navigate({ ...opts, _fromLocation: router.state.location }),\n buildLocation: router.buildLocation,\n cause: match.cause,\n abortController: match.abortController,\n preload: false,\n matches,\n }\n match.__routeContext = route.options.context?.(contextFnContext) ?? {}\n\n match.context = {\n ...parentContext,\n ...match.__routeContext,\n ...match.__beforeLoadContext,\n }\n\n const assetContext = {\n matches: router.state.matches,\n match,\n params: match.params,\n loaderData: match.loaderData,\n }\n const headFnContent = route.options.head?.(assetContext)\n\n const scripts = route.options.scripts?.(assetContext)\n\n match.meta = headFnContent?.meta\n match.links = headFnContent?.links\n match.headScripts = headFnContent?.scripts\n match.scripts = scripts\n })\n\n return routeChunkPromise\n}\n\nfunction deepMutableSetByPath<T>(obj: T, path: Array<string>, value: any) {\n // mutable set by path retaining array and object references\n if (path.length === 1) {\n ;(obj as any)[path[0]!] = value\n }\n\n const [key, ...rest] = path\n\n if (Array.isArray(obj)) {\n deepMutableSetByPath(obj[Number(key)], rest, value)\n } else if (isPlainObject(obj)) {\n deepMutableSetByPath((obj as any)[key!], rest, value)\n }\n}\n"],"names":["startSerializer","_a","_b","_c","isPlainObject"],"mappings":"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"}
@@ -1,4 +1,4 @@
1
- import { ConstrainValidator, Method, ServerFnTypeOrTypeFn } from './createServerFn.js';
1
+ import { ConstrainValidator, Method, ServerFnResponseType, ServerFnTypeOrTypeFn } from './createServerFn.js';
2
2
  import { Assign, Constrain, Expand, IntersectAssign, ResolveValidatorInput, ResolveValidatorOutput, SerializerStringify } from '@tanstack/router-core';
3
3
  export type AssignAllMiddleware<TMiddlewares, TType extends keyof AnyMiddleware['_types'], TAcc = undefined> = TMiddlewares extends readonly [
4
4
  infer TMiddleware extends AnyMiddleware,
@@ -27,45 +27,47 @@ export type IntersectAllValidatorInputs<TMiddlewares, TValidator> = unknown exte
27
27
  * Recursively merge the output type produced by a sequence of middleware
28
28
  */
29
29
  export type IntersectAllValidatorOutputs<TMiddlewares, TValidator> = unknown extends TValidator ? TValidator : IntersectAssign<IntersectAllMiddleware<TMiddlewares, 'allOutput'>, TValidator extends undefined ? undefined : ResolveValidatorOutput<TValidator>>;
30
- export interface MiddlewareOptions<in out TMiddlewares, in out TValidator, in out TServerContext, in out TClientContext> {
30
+ export interface MiddlewareOptions<in out TMiddlewares, in out TValidator, in out TServerContext, in out TClientContext, in out TServerFnResponseType extends ServerFnResponseType> {
31
31
  validateClient?: boolean;
32
32
  middleware?: TMiddlewares;
33
33
  validator?: ConstrainValidator<TValidator>;
34
- client?: MiddlewareClientFn<TMiddlewares, TValidator, TServerContext, TClientContext>;
35
- server?: MiddlewareServerFn<TMiddlewares, TValidator, TServerContext, unknown, unknown>;
34
+ client?: MiddlewareClientFn<TMiddlewares, TValidator, TServerContext, TClientContext, TServerFnResponseType>;
35
+ server?: MiddlewareServerFn<TMiddlewares, TValidator, TServerContext, unknown, unknown, TServerFnResponseType>;
36
36
  }
37
37
  export type MiddlewareServerNextFn<TMiddlewares, TServerSendContext> = <TNewServerContext = undefined, TSendContext = undefined>(ctx?: {
38
38
  context?: TNewServerContext;
39
39
  sendContext?: SerializerStringify<TSendContext>;
40
40
  }) => Promise<ServerResultWithContext<TMiddlewares, TServerSendContext, TNewServerContext, TSendContext>>;
41
- export interface MiddlewareServerFnOptions<in out TMiddlewares, in out TValidator, in out TServerSendContext> {
41
+ export interface MiddlewareServerFnOptions<in out TMiddlewares, in out TValidator, in out TServerSendContext, in out TServerFnResponseType> {
42
42
  data: Expand<IntersectAllValidatorOutputs<TMiddlewares, TValidator>>;
43
43
  context: Expand<AssignAllServerContext<TMiddlewares, TServerSendContext>>;
44
44
  next: MiddlewareServerNextFn<TMiddlewares, TServerSendContext>;
45
+ response: TServerFnResponseType;
45
46
  method: Method;
46
47
  filename: string;
47
48
  functionId: string;
48
49
  signal: AbortSignal;
49
50
  }
50
- export type MiddlewareServerFn<TMiddlewares, TValidator, TServerSendContext, TNewServerContext, TSendContext> = (options: MiddlewareServerFnOptions<TMiddlewares, TValidator, TServerSendContext>) => MiddlewareServerFnResult<TMiddlewares, TServerSendContext, TNewServerContext, TSendContext>;
51
+ export type MiddlewareServerFn<TMiddlewares, TValidator, TServerSendContext, TNewServerContext, TSendContext, TServerFnResponseType extends ServerFnResponseType> = (options: MiddlewareServerFnOptions<TMiddlewares, TValidator, TServerSendContext, TServerFnResponseType>) => MiddlewareServerFnResult<TMiddlewares, TServerSendContext, TNewServerContext, TSendContext>;
51
52
  export type MiddlewareServerFnResult<TMiddlewares, TServerSendContext, TServerContext, TSendContext> = Promise<ServerResultWithContext<TMiddlewares, TServerSendContext, TServerContext, TSendContext>> | ServerResultWithContext<TMiddlewares, TServerSendContext, TServerContext, TSendContext>;
52
53
  export type MiddlewareClientNextFn<TMiddlewares> = <TSendContext = undefined, TNewClientContext = undefined>(ctx?: {
53
54
  context?: TNewClientContext;
54
55
  sendContext?: SerializerStringify<TSendContext>;
55
56
  headers?: HeadersInit;
56
57
  }) => Promise<ClientResultWithContext<TMiddlewares, TSendContext, TNewClientContext>>;
57
- export interface MiddlewareClientFnOptions<in out TMiddlewares, in out TValidator> {
58
+ export interface MiddlewareClientFnOptions<in out TMiddlewares, in out TValidator, in out TServerFnResponseType extends ServerFnResponseType> {
58
59
  data: Expand<IntersectAllValidatorInputs<TMiddlewares, TValidator>>;
59
60
  context: Expand<AssignAllClientContextBeforeNext<TMiddlewares>>;
60
61
  sendContext: Expand<AssignAllServerSendContext<TMiddlewares>>;
61
62
  method: Method;
63
+ response: TServerFnResponseType;
62
64
  signal: AbortSignal;
63
65
  next: MiddlewareClientNextFn<TMiddlewares>;
64
66
  filename: string;
65
67
  functionId: string;
66
- type: ServerFnTypeOrTypeFn<Method, TMiddlewares, TValidator>;
68
+ type: ServerFnTypeOrTypeFn<Method, TServerFnResponseType, TMiddlewares, TValidator>;
67
69
  }
68
- export type MiddlewareClientFn<TMiddlewares, TValidator, TSendContext, TClientContext> = (options: MiddlewareClientFnOptions<TMiddlewares, TValidator>) => MiddlewareClientFnResult<TMiddlewares, TSendContext, TClientContext>;
70
+ export type MiddlewareClientFn<TMiddlewares, TValidator, TSendContext, TClientContext, TServerFnResponseType extends ServerFnResponseType> = (options: MiddlewareClientFnOptions<TMiddlewares, TValidator, TServerFnResponseType>) => MiddlewareClientFnResult<TMiddlewares, TSendContext, TClientContext>;
69
71
  export type MiddlewareClientFnResult<TMiddlewares, TSendContext, TClientContext> = Promise<ClientResultWithContext<TMiddlewares, TSendContext, TClientContext>> | ClientResultWithContext<TMiddlewares, TSendContext, TClientContext>;
70
72
  export type ServerResultWithContext<in out TMiddlewares, in out TServerSendContext, in out TServerContext, in out TSendContext> = {
71
73
  'use functions must return the result of next()': true;
@@ -82,7 +84,7 @@ export type ClientResultWithContext<in out TMiddlewares, in out TSendContext, in
82
84
  sendContext: Expand<AssignAllServerSendContext<TMiddlewares, TSendContext>>;
83
85
  headers: HeadersInit;
84
86
  };
85
- export type AnyMiddleware = MiddlewareWithTypes<any, any, any, any, any, any>;
87
+ export type AnyMiddleware = MiddlewareWithTypes<any, any, any, any, any, any, any>;
86
88
  export interface MiddlewareTypes<in out TMiddlewares, in out TValidator, in out TServerContext, in out TServerSendContext, in out TClientContext, in out TClientSendContext> {
87
89
  middlewares: TMiddlewares;
88
90
  input: ResolveValidatorInput<TValidator>;
@@ -100,30 +102,30 @@ export interface MiddlewareTypes<in out TMiddlewares, in out TValidator, in out
100
102
  allClientSendContext: AssignAllClientSendContext<TMiddlewares, TClientSendContext>;
101
103
  validator: TValidator;
102
104
  }
103
- export interface MiddlewareWithTypes<TMiddlewares, TValidator, TServerContext, TServerSendContext, TClientContext, TClientSendContext> {
105
+ export interface MiddlewareWithTypes<TMiddlewares, TValidator, TServerContext, TServerSendContext, TClientContext, TClientSendContext, TServerFnResponseType extends ServerFnResponseType> {
104
106
  _types: MiddlewareTypes<TMiddlewares, TValidator, TServerContext, TServerSendContext, TClientContext, TClientSendContext>;
105
- options: MiddlewareOptions<TMiddlewares, TValidator, TServerContext, TClientContext>;
107
+ options: MiddlewareOptions<TMiddlewares, TValidator, TServerContext, TClientContext, TServerFnResponseType>;
106
108
  }
107
- export interface MiddlewareAfterValidator<TMiddlewares, TValidator> extends MiddlewareWithTypes<TMiddlewares, TValidator, undefined, undefined, undefined, undefined>, MiddlewareServer<TMiddlewares, TValidator, undefined, undefined>, MiddlewareClient<TMiddlewares, TValidator> {
109
+ export interface MiddlewareAfterValidator<TMiddlewares, TValidator, TServerFnResponseType extends ServerFnResponseType> extends MiddlewareWithTypes<TMiddlewares, TValidator, undefined, undefined, undefined, undefined, ServerFnResponseType>, MiddlewareServer<TMiddlewares, TValidator, undefined, undefined, TServerFnResponseType>, MiddlewareClient<TMiddlewares, TValidator, ServerFnResponseType> {
108
110
  }
109
- export interface MiddlewareValidator<TMiddlewares> {
110
- validator: <TNewValidator>(input: ConstrainValidator<TNewValidator>) => MiddlewareAfterValidator<TMiddlewares, TNewValidator>;
111
+ export interface MiddlewareValidator<TMiddlewares, TServerFnResponseType extends ServerFnResponseType> {
112
+ validator: <TNewValidator>(input: ConstrainValidator<TNewValidator>) => MiddlewareAfterValidator<TMiddlewares, TNewValidator, TServerFnResponseType>;
111
113
  }
112
- export interface MiddlewareAfterServer<TMiddlewares, TValidator, TServerContext, TServerSendContext, TClientContext, TClientSendContext> extends MiddlewareWithTypes<TMiddlewares, TValidator, TServerContext, TServerSendContext, TClientContext, TClientSendContext> {
114
+ export interface MiddlewareAfterServer<TMiddlewares, TValidator, TServerContext, TServerSendContext, TClientContext, TClientSendContext, TServerFnResponseType extends ServerFnResponseType> extends MiddlewareWithTypes<TMiddlewares, TValidator, TServerContext, TServerSendContext, TClientContext, TClientSendContext, TServerFnResponseType> {
113
115
  }
114
- export interface MiddlewareServer<TMiddlewares, TValidator, TServerSendContext, TClientContext> {
115
- server: <TNewServerContext = undefined, TSendContext = undefined>(server: MiddlewareServerFn<TMiddlewares, TValidator, TServerSendContext, TNewServerContext, TSendContext>) => MiddlewareAfterServer<TMiddlewares, TValidator, TNewServerContext, TServerSendContext, TClientContext, TSendContext>;
116
+ export interface MiddlewareServer<TMiddlewares, TValidator, TServerSendContext, TClientContext, TServerFnResponseType extends ServerFnResponseType> {
117
+ server: <TNewServerContext = undefined, TSendContext = undefined>(server: MiddlewareServerFn<TMiddlewares, TValidator, TServerSendContext, TNewServerContext, TSendContext, TServerFnResponseType>) => MiddlewareAfterServer<TMiddlewares, TValidator, TNewServerContext, TServerSendContext, TClientContext, TSendContext, ServerFnResponseType>;
116
118
  }
117
- export interface MiddlewareAfterClient<TMiddlewares, TValidator, TServerSendContext, TClientContext> extends MiddlewareWithTypes<TMiddlewares, TValidator, undefined, TServerSendContext, TClientContext, undefined>, MiddlewareServer<TMiddlewares, TValidator, TServerSendContext, TClientContext> {
119
+ export interface MiddlewareAfterClient<TMiddlewares, TValidator, TServerSendContext, TClientContext, TServerFnResponseType extends ServerFnResponseType> extends MiddlewareWithTypes<TMiddlewares, TValidator, undefined, TServerSendContext, TClientContext, undefined, TServerFnResponseType>, MiddlewareServer<TMiddlewares, TValidator, TServerSendContext, TClientContext, TServerFnResponseType> {
118
120
  }
119
- export interface MiddlewareClient<TMiddlewares, TValidator> {
120
- client: <TSendServerContext = undefined, TNewClientContext = undefined>(client: MiddlewareClientFn<TMiddlewares, TValidator, TSendServerContext, TNewClientContext>) => MiddlewareAfterClient<TMiddlewares, TValidator, TSendServerContext, TNewClientContext>;
121
+ export interface MiddlewareClient<TMiddlewares, TValidator, TServerFnResponseType extends ServerFnResponseType> {
122
+ client: <TSendServerContext = undefined, TNewClientContext = undefined>(client: MiddlewareClientFn<TMiddlewares, TValidator, TSendServerContext, TNewClientContext, TServerFnResponseType>) => MiddlewareAfterClient<TMiddlewares, TValidator, TSendServerContext, TNewClientContext, ServerFnResponseType>;
121
123
  }
122
- export interface MiddlewareAfterMiddleware<TMiddlewares> extends MiddlewareWithTypes<TMiddlewares, undefined, undefined, undefined, undefined, undefined>, MiddlewareServer<TMiddlewares, undefined, undefined, undefined>, MiddlewareClient<TMiddlewares, undefined>, MiddlewareValidator<TMiddlewares> {
124
+ export interface MiddlewareAfterMiddleware<TMiddlewares, TServerFnResponseType extends ServerFnResponseType> extends MiddlewareWithTypes<TMiddlewares, undefined, undefined, undefined, undefined, undefined, TServerFnResponseType>, MiddlewareServer<TMiddlewares, undefined, undefined, undefined, TServerFnResponseType>, MiddlewareClient<TMiddlewares, undefined, TServerFnResponseType>, MiddlewareValidator<TMiddlewares, TServerFnResponseType> {
123
125
  }
124
- export interface Middleware extends MiddlewareAfterMiddleware<unknown> {
125
- middleware: <const TNewMiddlewares = undefined>(middlewares: Constrain<TNewMiddlewares, ReadonlyArray<AnyMiddleware>>) => MiddlewareAfterMiddleware<TNewMiddlewares>;
126
+ export interface Middleware<TServerFnResponseType extends ServerFnResponseType> extends MiddlewareAfterMiddleware<unknown, TServerFnResponseType> {
127
+ middleware: <const TNewMiddlewares = undefined>(middlewares: Constrain<TNewMiddlewares, ReadonlyArray<AnyMiddleware>>) => MiddlewareAfterMiddleware<TNewMiddlewares, TServerFnResponseType>;
126
128
  }
127
129
  export declare function createMiddleware(options?: {
128
130
  validateClient?: boolean;
129
- }, __opts?: MiddlewareOptions<unknown, undefined, undefined, undefined>): Middleware;
131
+ }, __opts?: MiddlewareOptions<unknown, undefined, undefined, undefined, ServerFnResponseType>): Middleware<ServerFnResponseType>;
@@ -1 +1 @@
1
- {"version":3,"file":"createMiddleware.js","sources":["../../src/createMiddleware.ts"],"sourcesContent":["import type {\n ConstrainValidator,\n Method,\n ServerFnTypeOrTypeFn,\n} from './createServerFn'\nimport type {\n Assign,\n Constrain,\n Expand,\n IntersectAssign,\n ResolveValidatorInput,\n ResolveValidatorOutput,\n SerializerStringify,\n} from '@tanstack/router-core'\n\nexport type AssignAllMiddleware<\n TMiddlewares,\n TType extends keyof AnyMiddleware['_types'],\n TAcc = undefined,\n> = TMiddlewares extends readonly [\n infer TMiddleware extends AnyMiddleware,\n ...infer TRest,\n]\n ? AssignAllMiddleware<\n TRest,\n TType,\n Assign<TAcc, TMiddleware['_types'][TType]>\n >\n : TAcc\n\n/**\n * Recursively resolve the client context type produced by a sequence of middleware\n */\nexport type AssignAllClientContextBeforeNext<\n TMiddlewares,\n TClientContext = undefined,\n> = unknown extends TClientContext\n ? TClientContext\n : Assign<\n AssignAllMiddleware<TMiddlewares, 'allClientContextBeforeNext'>,\n TClientContext\n >\n\nexport type AssignAllClientSendContext<\n TMiddlewares,\n TSendContext = undefined,\n> = unknown extends TSendContext\n ? TSendContext\n : Assign<\n AssignAllMiddleware<TMiddlewares, 'allClientSendContext'>,\n TSendContext\n >\n\nexport type AssignAllClientContextAfterNext<\n TMiddlewares,\n TClientContext = undefined,\n TSendContext = undefined,\n> = unknown extends TClientContext\n ? Assign<TClientContext, TSendContext>\n : Assign<\n AssignAllMiddleware<TMiddlewares, 'allClientContextAfterNext'>,\n Assign<TClientContext, TSendContext>\n >\n\n/**\n * Recursively resolve the server context type produced by a sequence of middleware\n */\nexport type AssignAllServerContext<\n TMiddlewares,\n TSendContext = undefined,\n TServerContext = undefined,\n> = unknown extends TSendContext\n ? Assign<TSendContext, TServerContext>\n : Assign<\n AssignAllMiddleware<TMiddlewares, 'allServerContext'>,\n Assign<TSendContext, TServerContext>\n >\n\nexport type AssignAllServerSendContext<\n TMiddlewares,\n TSendContext = undefined,\n> = unknown extends TSendContext\n ? TSendContext\n : Assign<\n AssignAllMiddleware<TMiddlewares, 'allServerSendContext'>,\n TSendContext\n >\n\nexport type IntersectAllMiddleware<\n TMiddlewares,\n TType extends keyof AnyMiddleware['_types'],\n TAcc = undefined,\n> = TMiddlewares extends readonly [\n infer TMiddleware extends AnyMiddleware,\n ...infer TRest,\n]\n ? IntersectAllMiddleware<\n TRest,\n TType,\n IntersectAssign<TAcc, TMiddleware['_types'][TType]>\n >\n : TAcc\n\n/**\n * Recursively resolve the input type produced by a sequence of middleware\n */\nexport type IntersectAllValidatorInputs<TMiddlewares, TValidator> =\n unknown extends TValidator\n ? TValidator\n : IntersectAssign<\n IntersectAllMiddleware<TMiddlewares, 'allInput'>,\n TValidator extends undefined\n ? undefined\n : ResolveValidatorInput<TValidator>\n >\n/**\n * Recursively merge the output type produced by a sequence of middleware\n */\nexport type IntersectAllValidatorOutputs<TMiddlewares, TValidator> =\n unknown extends TValidator\n ? TValidator\n : IntersectAssign<\n IntersectAllMiddleware<TMiddlewares, 'allOutput'>,\n TValidator extends undefined\n ? undefined\n : ResolveValidatorOutput<TValidator>\n >\n\nexport interface MiddlewareOptions<\n in out TMiddlewares,\n in out TValidator,\n in out TServerContext,\n in out TClientContext,\n> {\n validateClient?: boolean\n middleware?: TMiddlewares\n validator?: ConstrainValidator<TValidator>\n client?: MiddlewareClientFn<\n TMiddlewares,\n TValidator,\n TServerContext,\n TClientContext\n >\n server?: MiddlewareServerFn<\n TMiddlewares,\n TValidator,\n TServerContext,\n unknown,\n unknown\n >\n}\n\nexport type MiddlewareServerNextFn<TMiddlewares, TServerSendContext> = <\n TNewServerContext = undefined,\n TSendContext = undefined,\n>(ctx?: {\n context?: TNewServerContext\n sendContext?: SerializerStringify<TSendContext>\n}) => Promise<\n ServerResultWithContext<\n TMiddlewares,\n TServerSendContext,\n TNewServerContext,\n TSendContext\n >\n>\n\nexport interface MiddlewareServerFnOptions<\n in out TMiddlewares,\n in out TValidator,\n in out TServerSendContext,\n> {\n data: Expand<IntersectAllValidatorOutputs<TMiddlewares, TValidator>>\n context: Expand<AssignAllServerContext<TMiddlewares, TServerSendContext>>\n next: MiddlewareServerNextFn<TMiddlewares, TServerSendContext>\n method: Method\n filename: string\n functionId: string\n signal: AbortSignal\n}\n\nexport type MiddlewareServerFn<\n TMiddlewares,\n TValidator,\n TServerSendContext,\n TNewServerContext,\n TSendContext,\n> = (\n options: MiddlewareServerFnOptions<\n TMiddlewares,\n TValidator,\n TServerSendContext\n >,\n) => MiddlewareServerFnResult<\n TMiddlewares,\n TServerSendContext,\n TNewServerContext,\n TSendContext\n>\n\nexport type MiddlewareServerFnResult<\n TMiddlewares,\n TServerSendContext,\n TServerContext,\n TSendContext,\n> =\n | Promise<\n ServerResultWithContext<\n TMiddlewares,\n TServerSendContext,\n TServerContext,\n TSendContext\n >\n >\n | ServerResultWithContext<\n TMiddlewares,\n TServerSendContext,\n TServerContext,\n TSendContext\n >\n\nexport type MiddlewareClientNextFn<TMiddlewares> = <\n TSendContext = undefined,\n TNewClientContext = undefined,\n>(ctx?: {\n context?: TNewClientContext\n sendContext?: SerializerStringify<TSendContext>\n headers?: HeadersInit\n}) => Promise<\n ClientResultWithContext<TMiddlewares, TSendContext, TNewClientContext>\n>\n\nexport interface MiddlewareClientFnOptions<\n in out TMiddlewares,\n in out TValidator,\n> {\n data: Expand<IntersectAllValidatorInputs<TMiddlewares, TValidator>>\n context: Expand<AssignAllClientContextBeforeNext<TMiddlewares>>\n sendContext: Expand<AssignAllServerSendContext<TMiddlewares>>\n method: Method\n signal: AbortSignal\n next: MiddlewareClientNextFn<TMiddlewares>\n filename: string\n functionId: string\n type: ServerFnTypeOrTypeFn<Method, TMiddlewares, TValidator>\n}\n\nexport type MiddlewareClientFn<\n TMiddlewares,\n TValidator,\n TSendContext,\n TClientContext,\n> = (\n options: MiddlewareClientFnOptions<TMiddlewares, TValidator>,\n) => MiddlewareClientFnResult<TMiddlewares, TSendContext, TClientContext>\n\nexport type MiddlewareClientFnResult<\n TMiddlewares,\n TSendContext,\n TClientContext,\n> =\n | Promise<ClientResultWithContext<TMiddlewares, TSendContext, TClientContext>>\n | ClientResultWithContext<TMiddlewares, TSendContext, TClientContext>\n\nexport type ServerResultWithContext<\n in out TMiddlewares,\n in out TServerSendContext,\n in out TServerContext,\n in out TSendContext,\n> = {\n 'use functions must return the result of next()': true\n _types: {\n context: TServerContext\n sendContext: TSendContext\n }\n context: Expand<\n AssignAllServerContext<TMiddlewares, TServerSendContext, TServerContext>\n >\n sendContext: Expand<AssignAllClientSendContext<TMiddlewares, TSendContext>>\n}\n\nexport type ClientResultWithContext<\n in out TMiddlewares,\n in out TSendContext,\n in out TClientContext,\n> = {\n 'use functions must return the result of next()': true\n context: Expand<AssignAllClientContextAfterNext<TMiddlewares, TClientContext>>\n sendContext: Expand<AssignAllServerSendContext<TMiddlewares, TSendContext>>\n headers: HeadersInit\n}\n\nexport type AnyMiddleware = MiddlewareWithTypes<any, any, any, any, any, any>\n\nexport interface MiddlewareTypes<\n in out TMiddlewares,\n in out TValidator,\n in out TServerContext,\n in out TServerSendContext,\n in out TClientContext,\n in out TClientSendContext,\n> {\n middlewares: TMiddlewares\n input: ResolveValidatorInput<TValidator>\n allInput: IntersectAllValidatorInputs<TMiddlewares, TValidator>\n output: ResolveValidatorOutput<TValidator>\n allOutput: IntersectAllValidatorOutputs<TMiddlewares, TValidator>\n clientContext: TClientContext\n allClientContextBeforeNext: AssignAllClientContextBeforeNext<\n TMiddlewares,\n TClientContext\n >\n allClientContextAfterNext: AssignAllClientContextAfterNext<\n TMiddlewares,\n TClientContext,\n TClientSendContext\n >\n serverContext: TServerContext\n serverSendContext: TServerSendContext\n allServerSendContext: AssignAllServerSendContext<\n TMiddlewares,\n TServerSendContext\n >\n allServerContext: AssignAllServerContext<\n TMiddlewares,\n TServerSendContext,\n TServerContext\n >\n clientSendContext: TClientSendContext\n allClientSendContext: AssignAllClientSendContext<\n TMiddlewares,\n TClientSendContext\n >\n validator: TValidator\n}\n\nexport interface MiddlewareWithTypes<\n TMiddlewares,\n TValidator,\n TServerContext,\n TServerSendContext,\n TClientContext,\n TClientSendContext,\n> {\n _types: MiddlewareTypes<\n TMiddlewares,\n TValidator,\n TServerContext,\n TServerSendContext,\n TClientContext,\n TClientSendContext\n >\n options: MiddlewareOptions<\n TMiddlewares,\n TValidator,\n TServerContext,\n TClientContext\n >\n}\n\nexport interface MiddlewareAfterValidator<TMiddlewares, TValidator>\n extends MiddlewareWithTypes<\n TMiddlewares,\n TValidator,\n undefined,\n undefined,\n undefined,\n undefined\n >,\n MiddlewareServer<TMiddlewares, TValidator, undefined, undefined>,\n MiddlewareClient<TMiddlewares, TValidator> {}\n\nexport interface MiddlewareValidator<TMiddlewares> {\n validator: <TNewValidator>(\n input: ConstrainValidator<TNewValidator>,\n ) => MiddlewareAfterValidator<TMiddlewares, TNewValidator>\n}\n\nexport interface MiddlewareAfterServer<\n TMiddlewares,\n TValidator,\n TServerContext,\n TServerSendContext,\n TClientContext,\n TClientSendContext,\n> extends MiddlewareWithTypes<\n TMiddlewares,\n TValidator,\n TServerContext,\n TServerSendContext,\n TClientContext,\n TClientSendContext\n > {}\n\nexport interface MiddlewareServer<\n TMiddlewares,\n TValidator,\n TServerSendContext,\n TClientContext,\n> {\n server: <TNewServerContext = undefined, TSendContext = undefined>(\n server: MiddlewareServerFn<\n TMiddlewares,\n TValidator,\n TServerSendContext,\n TNewServerContext,\n TSendContext\n >,\n ) => MiddlewareAfterServer<\n TMiddlewares,\n TValidator,\n TNewServerContext,\n TServerSendContext,\n TClientContext,\n TSendContext\n >\n}\n\nexport interface MiddlewareAfterClient<\n TMiddlewares,\n TValidator,\n TServerSendContext,\n TClientContext,\n> extends MiddlewareWithTypes<\n TMiddlewares,\n TValidator,\n undefined,\n TServerSendContext,\n TClientContext,\n undefined\n >,\n MiddlewareServer<\n TMiddlewares,\n TValidator,\n TServerSendContext,\n TClientContext\n > {}\n\nexport interface MiddlewareClient<TMiddlewares, TValidator> {\n client: <TSendServerContext = undefined, TNewClientContext = undefined>(\n client: MiddlewareClientFn<\n TMiddlewares,\n TValidator,\n TSendServerContext,\n TNewClientContext\n >,\n ) => MiddlewareAfterClient<\n TMiddlewares,\n TValidator,\n TSendServerContext,\n TNewClientContext\n >\n}\n\nexport interface MiddlewareAfterMiddleware<TMiddlewares>\n extends MiddlewareWithTypes<\n TMiddlewares,\n undefined,\n undefined,\n undefined,\n undefined,\n undefined\n >,\n MiddlewareServer<TMiddlewares, undefined, undefined, undefined>,\n MiddlewareClient<TMiddlewares, undefined>,\n MiddlewareValidator<TMiddlewares> {}\n\nexport interface Middleware extends MiddlewareAfterMiddleware<unknown> {\n middleware: <const TNewMiddlewares = undefined>(\n middlewares: Constrain<TNewMiddlewares, ReadonlyArray<AnyMiddleware>>,\n ) => MiddlewareAfterMiddleware<TNewMiddlewares>\n}\n\nexport function createMiddleware(\n options?: {\n validateClient?: boolean\n },\n __opts?: MiddlewareOptions<unknown, undefined, undefined, undefined>,\n): Middleware {\n // const resolvedOptions = (__opts || options) as MiddlewareOptions<\n const resolvedOptions =\n __opts ||\n ((options || {}) as MiddlewareOptions<\n unknown,\n undefined,\n undefined,\n undefined\n >)\n\n return {\n options: resolvedOptions as any,\n middleware: (middleware: any) => {\n return createMiddleware(\n undefined,\n Object.assign(resolvedOptions, { middleware }),\n ) as any\n },\n validator: (validator: any) => {\n return createMiddleware(\n undefined,\n Object.assign(resolvedOptions, { validator }),\n ) as any\n },\n client: (client: any) => {\n return createMiddleware(\n undefined,\n Object.assign(resolvedOptions, { client }),\n ) as any\n },\n server: (server: any) => {\n return createMiddleware(\n undefined,\n Object.assign(resolvedOptions, { server }),\n ) as any\n },\n } as unknown as Middleware\n}\n"],"names":[],"mappings":"AAydgB,SAAA,iBACd,SAGA,QACY;AAEN,QAAA,kBACJ,WACE,WAAW;AAOR,SAAA;AAAA,IACL,SAAS;AAAA,IACT,YAAY,CAAC,eAAoB;AACxB,aAAA;AAAA,QACL;AAAA,QACA,OAAO,OAAO,iBAAiB,EAAE,WAAY,CAAA;AAAA,MAC/C;AAAA,IACF;AAAA,IACA,WAAW,CAAC,cAAmB;AACtB,aAAA;AAAA,QACL;AAAA,QACA,OAAO,OAAO,iBAAiB,EAAE,UAAW,CAAA;AAAA,MAC9C;AAAA,IACF;AAAA,IACA,QAAQ,CAAC,WAAgB;AAChB,aAAA;AAAA,QACL;AAAA,QACA,OAAO,OAAO,iBAAiB,EAAE,OAAQ,CAAA;AAAA,MAC3C;AAAA,IACF;AAAA,IACA,QAAQ,CAAC,WAAgB;AAChB,aAAA;AAAA,QACL;AAAA,QACA,OAAO,OAAO,iBAAiB,EAAE,OAAQ,CAAA;AAAA,MAC3C;AAAA,IAAA;AAAA,EAEJ;AACF;"}
1
+ {"version":3,"file":"createMiddleware.js","sources":["../../src/createMiddleware.ts"],"sourcesContent":["import type {\n ConstrainValidator,\n Method,\n ServerFnResponseType,\n ServerFnTypeOrTypeFn,\n} from './createServerFn'\nimport type {\n Assign,\n Constrain,\n Expand,\n IntersectAssign,\n ResolveValidatorInput,\n ResolveValidatorOutput,\n SerializerStringify,\n} from '@tanstack/router-core'\n\nexport type AssignAllMiddleware<\n TMiddlewares,\n TType extends keyof AnyMiddleware['_types'],\n TAcc = undefined,\n> = TMiddlewares extends readonly [\n infer TMiddleware extends AnyMiddleware,\n ...infer TRest,\n]\n ? AssignAllMiddleware<\n TRest,\n TType,\n Assign<TAcc, TMiddleware['_types'][TType]>\n >\n : TAcc\n\n/**\n * Recursively resolve the client context type produced by a sequence of middleware\n */\nexport type AssignAllClientContextBeforeNext<\n TMiddlewares,\n TClientContext = undefined,\n> = unknown extends TClientContext\n ? TClientContext\n : Assign<\n AssignAllMiddleware<TMiddlewares, 'allClientContextBeforeNext'>,\n TClientContext\n >\n\nexport type AssignAllClientSendContext<\n TMiddlewares,\n TSendContext = undefined,\n> = unknown extends TSendContext\n ? TSendContext\n : Assign<\n AssignAllMiddleware<TMiddlewares, 'allClientSendContext'>,\n TSendContext\n >\n\nexport type AssignAllClientContextAfterNext<\n TMiddlewares,\n TClientContext = undefined,\n TSendContext = undefined,\n> = unknown extends TClientContext\n ? Assign<TClientContext, TSendContext>\n : Assign<\n AssignAllMiddleware<TMiddlewares, 'allClientContextAfterNext'>,\n Assign<TClientContext, TSendContext>\n >\n\n/**\n * Recursively resolve the server context type produced by a sequence of middleware\n */\nexport type AssignAllServerContext<\n TMiddlewares,\n TSendContext = undefined,\n TServerContext = undefined,\n> = unknown extends TSendContext\n ? Assign<TSendContext, TServerContext>\n : Assign<\n AssignAllMiddleware<TMiddlewares, 'allServerContext'>,\n Assign<TSendContext, TServerContext>\n >\n\nexport type AssignAllServerSendContext<\n TMiddlewares,\n TSendContext = undefined,\n> = unknown extends TSendContext\n ? TSendContext\n : Assign<\n AssignAllMiddleware<TMiddlewares, 'allServerSendContext'>,\n TSendContext\n >\n\nexport type IntersectAllMiddleware<\n TMiddlewares,\n TType extends keyof AnyMiddleware['_types'],\n TAcc = undefined,\n> = TMiddlewares extends readonly [\n infer TMiddleware extends AnyMiddleware,\n ...infer TRest,\n]\n ? IntersectAllMiddleware<\n TRest,\n TType,\n IntersectAssign<TAcc, TMiddleware['_types'][TType]>\n >\n : TAcc\n\n/**\n * Recursively resolve the input type produced by a sequence of middleware\n */\nexport type IntersectAllValidatorInputs<TMiddlewares, TValidator> =\n unknown extends TValidator\n ? TValidator\n : IntersectAssign<\n IntersectAllMiddleware<TMiddlewares, 'allInput'>,\n TValidator extends undefined\n ? undefined\n : ResolveValidatorInput<TValidator>\n >\n/**\n * Recursively merge the output type produced by a sequence of middleware\n */\nexport type IntersectAllValidatorOutputs<TMiddlewares, TValidator> =\n unknown extends TValidator\n ? TValidator\n : IntersectAssign<\n IntersectAllMiddleware<TMiddlewares, 'allOutput'>,\n TValidator extends undefined\n ? undefined\n : ResolveValidatorOutput<TValidator>\n >\n\nexport interface MiddlewareOptions<\n in out TMiddlewares,\n in out TValidator,\n in out TServerContext,\n in out TClientContext,\n in out TServerFnResponseType extends ServerFnResponseType,\n> {\n validateClient?: boolean\n middleware?: TMiddlewares\n validator?: ConstrainValidator<TValidator>\n client?: MiddlewareClientFn<\n TMiddlewares,\n TValidator,\n TServerContext,\n TClientContext,\n TServerFnResponseType\n >\n server?: MiddlewareServerFn<\n TMiddlewares,\n TValidator,\n TServerContext,\n unknown,\n unknown,\n TServerFnResponseType\n >\n}\n\nexport type MiddlewareServerNextFn<TMiddlewares, TServerSendContext> = <\n TNewServerContext = undefined,\n TSendContext = undefined,\n>(ctx?: {\n context?: TNewServerContext\n sendContext?: SerializerStringify<TSendContext>\n}) => Promise<\n ServerResultWithContext<\n TMiddlewares,\n TServerSendContext,\n TNewServerContext,\n TSendContext\n >\n>\n\nexport interface MiddlewareServerFnOptions<\n in out TMiddlewares,\n in out TValidator,\n in out TServerSendContext,\n in out TServerFnResponseType,\n> {\n data: Expand<IntersectAllValidatorOutputs<TMiddlewares, TValidator>>\n context: Expand<AssignAllServerContext<TMiddlewares, TServerSendContext>>\n next: MiddlewareServerNextFn<TMiddlewares, TServerSendContext>\n response: TServerFnResponseType\n method: Method\n filename: string\n functionId: string\n signal: AbortSignal\n}\n\nexport type MiddlewareServerFn<\n TMiddlewares,\n TValidator,\n TServerSendContext,\n TNewServerContext,\n TSendContext,\n TServerFnResponseType extends ServerFnResponseType,\n> = (\n options: MiddlewareServerFnOptions<\n TMiddlewares,\n TValidator,\n TServerSendContext,\n TServerFnResponseType\n >,\n) => MiddlewareServerFnResult<\n TMiddlewares,\n TServerSendContext,\n TNewServerContext,\n TSendContext\n>\n\nexport type MiddlewareServerFnResult<\n TMiddlewares,\n TServerSendContext,\n TServerContext,\n TSendContext,\n> =\n | Promise<\n ServerResultWithContext<\n TMiddlewares,\n TServerSendContext,\n TServerContext,\n TSendContext\n >\n >\n | ServerResultWithContext<\n TMiddlewares,\n TServerSendContext,\n TServerContext,\n TSendContext\n >\n\nexport type MiddlewareClientNextFn<TMiddlewares> = <\n TSendContext = undefined,\n TNewClientContext = undefined,\n>(ctx?: {\n context?: TNewClientContext\n sendContext?: SerializerStringify<TSendContext>\n headers?: HeadersInit\n}) => Promise<\n ClientResultWithContext<TMiddlewares, TSendContext, TNewClientContext>\n>\n\nexport interface MiddlewareClientFnOptions<\n in out TMiddlewares,\n in out TValidator,\n in out TServerFnResponseType extends ServerFnResponseType,\n> {\n data: Expand<IntersectAllValidatorInputs<TMiddlewares, TValidator>>\n context: Expand<AssignAllClientContextBeforeNext<TMiddlewares>>\n sendContext: Expand<AssignAllServerSendContext<TMiddlewares>>\n method: Method\n response: TServerFnResponseType\n signal: AbortSignal\n next: MiddlewareClientNextFn<TMiddlewares>\n filename: string\n functionId: string\n type: ServerFnTypeOrTypeFn<\n Method,\n TServerFnResponseType,\n TMiddlewares,\n TValidator\n >\n}\n\nexport type MiddlewareClientFn<\n TMiddlewares,\n TValidator,\n TSendContext,\n TClientContext,\n TServerFnResponseType extends ServerFnResponseType,\n> = (\n options: MiddlewareClientFnOptions<\n TMiddlewares,\n TValidator,\n TServerFnResponseType\n >,\n) => MiddlewareClientFnResult<TMiddlewares, TSendContext, TClientContext>\n\nexport type MiddlewareClientFnResult<\n TMiddlewares,\n TSendContext,\n TClientContext,\n> =\n | Promise<ClientResultWithContext<TMiddlewares, TSendContext, TClientContext>>\n | ClientResultWithContext<TMiddlewares, TSendContext, TClientContext>\n\nexport type ServerResultWithContext<\n in out TMiddlewares,\n in out TServerSendContext,\n in out TServerContext,\n in out TSendContext,\n> = {\n 'use functions must return the result of next()': true\n _types: {\n context: TServerContext\n sendContext: TSendContext\n }\n context: Expand<\n AssignAllServerContext<TMiddlewares, TServerSendContext, TServerContext>\n >\n sendContext: Expand<AssignAllClientSendContext<TMiddlewares, TSendContext>>\n}\n\nexport type ClientResultWithContext<\n in out TMiddlewares,\n in out TSendContext,\n in out TClientContext,\n> = {\n 'use functions must return the result of next()': true\n context: Expand<AssignAllClientContextAfterNext<TMiddlewares, TClientContext>>\n sendContext: Expand<AssignAllServerSendContext<TMiddlewares, TSendContext>>\n headers: HeadersInit\n}\n\nexport type AnyMiddleware = MiddlewareWithTypes<\n any,\n any,\n any,\n any,\n any,\n any,\n any\n>\n\nexport interface MiddlewareTypes<\n in out TMiddlewares,\n in out TValidator,\n in out TServerContext,\n in out TServerSendContext,\n in out TClientContext,\n in out TClientSendContext,\n> {\n middlewares: TMiddlewares\n input: ResolveValidatorInput<TValidator>\n allInput: IntersectAllValidatorInputs<TMiddlewares, TValidator>\n output: ResolveValidatorOutput<TValidator>\n allOutput: IntersectAllValidatorOutputs<TMiddlewares, TValidator>\n clientContext: TClientContext\n allClientContextBeforeNext: AssignAllClientContextBeforeNext<\n TMiddlewares,\n TClientContext\n >\n allClientContextAfterNext: AssignAllClientContextAfterNext<\n TMiddlewares,\n TClientContext,\n TClientSendContext\n >\n serverContext: TServerContext\n serverSendContext: TServerSendContext\n allServerSendContext: AssignAllServerSendContext<\n TMiddlewares,\n TServerSendContext\n >\n allServerContext: AssignAllServerContext<\n TMiddlewares,\n TServerSendContext,\n TServerContext\n >\n clientSendContext: TClientSendContext\n allClientSendContext: AssignAllClientSendContext<\n TMiddlewares,\n TClientSendContext\n >\n validator: TValidator\n}\n\nexport interface MiddlewareWithTypes<\n TMiddlewares,\n TValidator,\n TServerContext,\n TServerSendContext,\n TClientContext,\n TClientSendContext,\n TServerFnResponseType extends ServerFnResponseType,\n> {\n _types: MiddlewareTypes<\n TMiddlewares,\n TValidator,\n TServerContext,\n TServerSendContext,\n TClientContext,\n TClientSendContext\n >\n options: MiddlewareOptions<\n TMiddlewares,\n TValidator,\n TServerContext,\n TClientContext,\n TServerFnResponseType\n >\n}\n\nexport interface MiddlewareAfterValidator<\n TMiddlewares,\n TValidator,\n TServerFnResponseType extends ServerFnResponseType,\n> extends MiddlewareWithTypes<\n TMiddlewares,\n TValidator,\n undefined,\n undefined,\n undefined,\n undefined,\n ServerFnResponseType\n >,\n MiddlewareServer<\n TMiddlewares,\n TValidator,\n undefined,\n undefined,\n TServerFnResponseType\n >,\n MiddlewareClient<TMiddlewares, TValidator, ServerFnResponseType> {}\n\nexport interface MiddlewareValidator<\n TMiddlewares,\n TServerFnResponseType extends ServerFnResponseType,\n> {\n validator: <TNewValidator>(\n input: ConstrainValidator<TNewValidator>,\n ) => MiddlewareAfterValidator<\n TMiddlewares,\n TNewValidator,\n TServerFnResponseType\n >\n}\n\nexport interface MiddlewareAfterServer<\n TMiddlewares,\n TValidator,\n TServerContext,\n TServerSendContext,\n TClientContext,\n TClientSendContext,\n TServerFnResponseType extends ServerFnResponseType,\n> extends MiddlewareWithTypes<\n TMiddlewares,\n TValidator,\n TServerContext,\n TServerSendContext,\n TClientContext,\n TClientSendContext,\n TServerFnResponseType\n > {}\n\nexport interface MiddlewareServer<\n TMiddlewares,\n TValidator,\n TServerSendContext,\n TClientContext,\n TServerFnResponseType extends ServerFnResponseType,\n> {\n server: <TNewServerContext = undefined, TSendContext = undefined>(\n server: MiddlewareServerFn<\n TMiddlewares,\n TValidator,\n TServerSendContext,\n TNewServerContext,\n TSendContext,\n TServerFnResponseType\n >,\n ) => MiddlewareAfterServer<\n TMiddlewares,\n TValidator,\n TNewServerContext,\n TServerSendContext,\n TClientContext,\n TSendContext,\n ServerFnResponseType\n >\n}\n\nexport interface MiddlewareAfterClient<\n TMiddlewares,\n TValidator,\n TServerSendContext,\n TClientContext,\n TServerFnResponseType extends ServerFnResponseType,\n> extends MiddlewareWithTypes<\n TMiddlewares,\n TValidator,\n undefined,\n TServerSendContext,\n TClientContext,\n undefined,\n TServerFnResponseType\n >,\n MiddlewareServer<\n TMiddlewares,\n TValidator,\n TServerSendContext,\n TClientContext,\n TServerFnResponseType\n > {}\n\nexport interface MiddlewareClient<\n TMiddlewares,\n TValidator,\n TServerFnResponseType extends ServerFnResponseType,\n> {\n client: <TSendServerContext = undefined, TNewClientContext = undefined>(\n client: MiddlewareClientFn<\n TMiddlewares,\n TValidator,\n TSendServerContext,\n TNewClientContext,\n TServerFnResponseType\n >,\n ) => MiddlewareAfterClient<\n TMiddlewares,\n TValidator,\n TSendServerContext,\n TNewClientContext,\n ServerFnResponseType\n >\n}\n\nexport interface MiddlewareAfterMiddleware<\n TMiddlewares,\n TServerFnResponseType extends ServerFnResponseType,\n> extends MiddlewareWithTypes<\n TMiddlewares,\n undefined,\n undefined,\n undefined,\n undefined,\n undefined,\n TServerFnResponseType\n >,\n MiddlewareServer<\n TMiddlewares,\n undefined,\n undefined,\n undefined,\n TServerFnResponseType\n >,\n MiddlewareClient<TMiddlewares, undefined, TServerFnResponseType>,\n MiddlewareValidator<TMiddlewares, TServerFnResponseType> {}\n\nexport interface Middleware<TServerFnResponseType extends ServerFnResponseType>\n extends MiddlewareAfterMiddleware<unknown, TServerFnResponseType> {\n middleware: <const TNewMiddlewares = undefined>(\n middlewares: Constrain<TNewMiddlewares, ReadonlyArray<AnyMiddleware>>,\n ) => MiddlewareAfterMiddleware<TNewMiddlewares, TServerFnResponseType>\n}\n\nexport function createMiddleware(\n options?: {\n validateClient?: boolean\n },\n __opts?: MiddlewareOptions<\n unknown,\n undefined,\n undefined,\n undefined,\n ServerFnResponseType\n >,\n): Middleware<ServerFnResponseType> {\n // const resolvedOptions = (__opts || options) as MiddlewareOptions<\n const resolvedOptions =\n __opts ||\n ((options || {}) as MiddlewareOptions<\n unknown,\n undefined,\n undefined,\n undefined,\n ServerFnResponseType\n >)\n\n return {\n options: resolvedOptions as any,\n middleware: (middleware: any) => {\n return createMiddleware(\n undefined,\n Object.assign(resolvedOptions, { middleware }),\n ) as any\n },\n validator: (validator: any) => {\n return createMiddleware(\n undefined,\n Object.assign(resolvedOptions, { validator }),\n ) as any\n },\n client: (client: any) => {\n return createMiddleware(\n undefined,\n Object.assign(resolvedOptions, { client }),\n ) as any\n },\n server: (server: any) => {\n return createMiddleware(\n undefined,\n Object.assign(resolvedOptions, { server }),\n ) as any\n },\n } as unknown as Middleware<ServerFnResponseType>\n}\n"],"names":[],"mappings":"AAgiBgB,SAAA,iBACd,SAGA,QAOkC;AAE5B,QAAA,kBACJ,WACE,WAAW;AAQR,SAAA;AAAA,IACL,SAAS;AAAA,IACT,YAAY,CAAC,eAAoB;AACxB,aAAA;AAAA,QACL;AAAA,QACA,OAAO,OAAO,iBAAiB,EAAE,WAAY,CAAA;AAAA,MAC/C;AAAA,IACF;AAAA,IACA,WAAW,CAAC,cAAmB;AACtB,aAAA;AAAA,QACL;AAAA,QACA,OAAO,OAAO,iBAAiB,EAAE,UAAW,CAAA;AAAA,MAC9C;AAAA,IACF;AAAA,IACA,QAAQ,CAAC,WAAgB;AAChB,aAAA;AAAA,QACL;AAAA,QACA,OAAO,OAAO,iBAAiB,EAAE,OAAQ,CAAA;AAAA,MAC3C;AAAA,IACF;AAAA,IACA,QAAQ,CAAC,WAAgB;AAChB,aAAA;AAAA,QACL;AAAA,QACA,OAAO,OAAO,iBAAiB,EAAE,OAAQ,CAAA;AAAA,MAC3C;AAAA,IAAA;AAAA,EAEJ;AACF;"}
@@ -1,3 +1,4 @@
1
+ import { Readable } from 'node:stream';
1
2
  import { AnyValidator, Constrain, Expand, ResolveValidatorInput, SerializerParse, SerializerStringify, SerializerStringifyBy, Validator } from '@tanstack/router-core';
2
3
  import { AnyMiddleware, AssignAllClientSendContext, AssignAllServerContext, IntersectAllValidatorInputs, IntersectAllValidatorOutputs } from './createMiddleware.js';
3
4
  export interface JsonResponse<TData> extends Response {
@@ -6,39 +7,40 @@ export interface JsonResponse<TData> extends Response {
6
7
  export type CompiledFetcherFnOptions = {
7
8
  method: Method;
8
9
  data: unknown;
10
+ response?: ServerFnResponseType;
9
11
  headers?: HeadersInit;
10
12
  signal?: AbortSignal;
11
13
  context?: any;
12
14
  };
13
- export type Fetcher<TMiddlewares, TValidator, TResponse> = undefined extends IntersectAllValidatorInputs<TMiddlewares, TValidator> ? OptionalFetcher<TMiddlewares, TValidator, TResponse> : RequiredFetcher<TMiddlewares, TValidator, TResponse>;
15
+ export type Fetcher<TMiddlewares, TValidator, TResponse, TServerFnResponseType extends ServerFnResponseType> = undefined extends IntersectAllValidatorInputs<TMiddlewares, TValidator> ? OptionalFetcher<TMiddlewares, TValidator, TResponse, TServerFnResponseType> : RequiredFetcher<TMiddlewares, TValidator, TResponse, TServerFnResponseType>;
14
16
  export interface FetcherBase {
15
17
  url: string;
16
18
  __executeServer: (opts: {
17
19
  method: Method;
20
+ response?: ServerFnResponseType;
18
21
  data: unknown;
19
22
  headers?: HeadersInit;
20
23
  context?: any;
21
24
  signal: AbortSignal;
22
25
  }) => Promise<unknown>;
23
26
  }
24
- export type FetchResult<TMiddlewares, TResponse, TFullResponse extends boolean> = false extends TFullResponse ? Promise<FetcherData<TResponse>> : Promise<FullFetcherData<TMiddlewares, TResponse>>;
25
- export interface OptionalFetcher<TMiddlewares, TValidator, TResponse> extends FetcherBase {
26
- <TFullResponse extends boolean>(options?: OptionalFetcherDataOptions<TMiddlewares, TValidator, TFullResponse>): FetchResult<TMiddlewares, TResponse, TFullResponse>;
27
+ export type FetchResult<TMiddlewares, TResponse, TServerFnResponseType extends ServerFnResponseType> = TServerFnResponseType extends 'raw' ? Promise<Response> : TServerFnResponseType extends 'full' ? Promise<FullFetcherData<TMiddlewares, TResponse>> : Promise<FetcherData<TResponse>>;
28
+ export interface OptionalFetcher<TMiddlewares, TValidator, TResponse, TServerFnResponseType extends ServerFnResponseType> extends FetcherBase {
29
+ (options?: OptionalFetcherDataOptions<TMiddlewares, TValidator>): FetchResult<TMiddlewares, TResponse, TServerFnResponseType>;
27
30
  }
28
- export interface RequiredFetcher<TMiddlewares, TValidator, TResponse> extends FetcherBase {
29
- <TFullResponse extends boolean>(opts: RequiredFetcherDataOptions<TMiddlewares, TValidator, TFullResponse>): FetchResult<TMiddlewares, TResponse, TFullResponse>;
31
+ export interface RequiredFetcher<TMiddlewares, TValidator, TResponse, TServerFnResponseType extends ServerFnResponseType> extends FetcherBase {
32
+ (opts: RequiredFetcherDataOptions<TMiddlewares, TValidator>): FetchResult<TMiddlewares, TResponse, TServerFnResponseType>;
30
33
  }
31
- export type FetcherBaseOptions<TFullResponse extends boolean = false> = {
34
+ export type FetcherBaseOptions = {
32
35
  headers?: HeadersInit;
33
36
  type?: ServerFnType;
34
37
  signal?: AbortSignal;
35
- fullResponse?: TFullResponse;
36
38
  };
37
39
  export type ServerFnType = 'static' | 'dynamic';
38
- export interface OptionalFetcherDataOptions<TMiddlewares, TValidator, TFullResponse extends boolean> extends FetcherBaseOptions<TFullResponse> {
40
+ export interface OptionalFetcherDataOptions<TMiddlewares, TValidator> extends FetcherBaseOptions {
39
41
  data?: Expand<IntersectAllValidatorInputs<TMiddlewares, TValidator>>;
40
42
  }
41
- export interface RequiredFetcherDataOptions<TMiddlewares, TValidator, TFullResponse extends boolean> extends FetcherBaseOptions<TFullResponse> {
43
+ export interface RequiredFetcherDataOptions<TMiddlewares, TValidator> extends FetcherBaseOptions {
42
44
  data: Expand<IntersectAllValidatorInputs<TMiddlewares, TValidator>>;
43
45
  }
44
46
  export interface FullFetcherData<TMiddlewares, TResponse> {
@@ -51,51 +53,56 @@ export type RscStream<T> = {
51
53
  __cacheState: T;
52
54
  };
53
55
  export type Method = 'GET' | 'POST';
54
- export type ServerFn<TMethod, TMiddlewares, TValidator, TResponse> = (ctx: ServerFnCtx<TMethod, TMiddlewares, TValidator>) => Promise<SerializerStringify<TResponse>> | SerializerStringify<TResponse>;
55
- export interface ServerFnCtx<TMethod, TMiddlewares, TValidator> {
56
+ export type ServerFnResponseType = 'data' | 'full' | 'raw';
57
+ export type RawResponse = Response | ReadableStream | Readable | null | string;
58
+ export type ServerFnReturnType<TServerFnResponseType extends ServerFnResponseType, TResponse> = TServerFnResponseType extends 'raw' ? RawResponse | Promise<RawResponse> : Promise<SerializerStringify<TResponse>> | SerializerStringify<TResponse>;
59
+ export type ServerFn<TMethod, TServerFnResponseType extends ServerFnResponseType, TMiddlewares, TValidator, TResponse> = (ctx: ServerFnCtx<TMethod, TServerFnResponseType, TMiddlewares, TValidator>) => ServerFnReturnType<TServerFnResponseType, TResponse>;
60
+ export interface ServerFnCtx<TMethod, TServerFnResponseType extends ServerFnResponseType, TMiddlewares, TValidator> {
56
61
  method: TMethod;
62
+ response: TServerFnResponseType;
57
63
  data: Expand<IntersectAllValidatorOutputs<TMiddlewares, TValidator>>;
58
64
  context: Expand<AssignAllServerContext<TMiddlewares>>;
59
65
  signal: AbortSignal;
60
66
  }
61
- export type CompiledFetcherFn<TResponse> = {
62
- (opts: CompiledFetcherFnOptions & ServerFnBaseOptions<Method>): Promise<TResponse>;
67
+ export type CompiledFetcherFn<TResponse, TServerFnResponseType extends ServerFnResponseType> = {
68
+ (opts: CompiledFetcherFnOptions & ServerFnBaseOptions<Method, TServerFnResponseType>): Promise<TResponse>;
63
69
  url: string;
64
70
  };
65
- type ServerFnBaseOptions<TMethod extends Method = 'GET', TResponse = unknown, TMiddlewares = unknown, TInput = unknown> = {
71
+ type ServerFnBaseOptions<TMethod extends Method = 'GET', TServerFnResponseType extends ServerFnResponseType = 'data', TResponse = unknown, TMiddlewares = unknown, TInput = unknown> = {
66
72
  method: TMethod;
73
+ response?: TServerFnResponseType;
67
74
  validateClient?: boolean;
68
75
  middleware?: Constrain<TMiddlewares, ReadonlyArray<AnyMiddleware>>;
69
76
  validator?: ConstrainValidator<TInput>;
70
- extractedFn?: CompiledFetcherFn<TResponse>;
71
- serverFn?: ServerFn<TMethod, TMiddlewares, TInput, TResponse>;
77
+ extractedFn?: CompiledFetcherFn<TResponse, TServerFnResponseType>;
78
+ serverFn?: ServerFn<TMethod, TServerFnResponseType, TMiddlewares, TInput, TResponse>;
72
79
  functionId: string;
73
- type: ServerFnTypeOrTypeFn<TMethod, TMiddlewares, AnyValidator>;
80
+ type: ServerFnTypeOrTypeFn<TMethod, TServerFnResponseType, TMiddlewares, AnyValidator>;
74
81
  };
75
82
  export type ValidatorSerializerStringify<TValidator> = Validator<SerializerStringifyBy<ResolveValidatorInput<TValidator>, Date | undefined | FormData>, any>;
76
83
  export type ConstrainValidator<TValidator> = unknown extends TValidator ? TValidator : Constrain<TValidator, ValidatorSerializerStringify<TValidator>>;
77
- export interface ServerFnMiddleware<TMethod extends Method, TValidator> {
78
- middleware: <const TNewMiddlewares = undefined>(middlewares: Constrain<TNewMiddlewares, ReadonlyArray<AnyMiddleware>>) => ServerFnAfterMiddleware<TMethod, TNewMiddlewares, TValidator>;
84
+ export interface ServerFnMiddleware<TMethod extends Method, TServerFnResponseType extends ServerFnResponseType, TValidator> {
85
+ middleware: <const TNewMiddlewares = undefined>(middlewares: Constrain<TNewMiddlewares, ReadonlyArray<AnyMiddleware>>) => ServerFnAfterMiddleware<TMethod, TServerFnResponseType, TNewMiddlewares, TValidator>;
79
86
  }
80
- export interface ServerFnAfterMiddleware<TMethod extends Method, TMiddlewares, TValidator> extends ServerFnValidator<TMethod, TMiddlewares>, ServerFnTyper<TMethod, TMiddlewares, TValidator>, ServerFnHandler<TMethod, TMiddlewares, TValidator> {
87
+ export interface ServerFnAfterMiddleware<TMethod extends Method, TServerFnResponseType extends ServerFnResponseType, TMiddlewares, TValidator> extends ServerFnValidator<TMethod, TServerFnResponseType, TMiddlewares>, ServerFnTyper<TMethod, TServerFnResponseType, TMiddlewares, TValidator>, ServerFnHandler<TMethod, TServerFnResponseType, TMiddlewares, TValidator> {
81
88
  }
82
- export type ValidatorFn<TMethod extends Method, TMiddlewares> = <TValidator>(validator: ConstrainValidator<TValidator>) => ServerFnAfterValidator<TMethod, TMiddlewares, TValidator>;
83
- export interface ServerFnValidator<TMethod extends Method, TMiddlewares> {
84
- validator: ValidatorFn<TMethod, TMiddlewares>;
89
+ export type ValidatorFn<TMethod extends Method, TServerFnResponseType extends ServerFnResponseType, TMiddlewares> = <TValidator>(validator: ConstrainValidator<TValidator>) => ServerFnAfterValidator<TMethod, TServerFnResponseType, TMiddlewares, TValidator>;
90
+ export interface ServerFnValidator<TMethod extends Method, TServerFnResponseType extends ServerFnResponseType, TMiddlewares> {
91
+ validator: ValidatorFn<TMethod, TServerFnResponseType, TMiddlewares>;
85
92
  }
86
- export interface ServerFnAfterValidator<TMethod extends Method, TMiddlewares, TValidator> extends ServerFnMiddleware<TMethod, TValidator>, ServerFnTyper<TMethod, TMiddlewares, TValidator>, ServerFnHandler<TMethod, TMiddlewares, TValidator> {
93
+ export interface ServerFnAfterValidator<TMethod extends Method, TServerFnResponseType extends ServerFnResponseType, TMiddlewares, TValidator> extends ServerFnMiddleware<TMethod, TServerFnResponseType, TValidator>, ServerFnTyper<TMethod, TServerFnResponseType, TMiddlewares, TValidator>, ServerFnHandler<TMethod, TServerFnResponseType, TMiddlewares, TValidator> {
87
94
  }
88
- export interface ServerFnTyper<TMethod extends Method, TMiddlewares, TValidator> {
89
- type: (typer: ServerFnTypeOrTypeFn<TMethod, TMiddlewares, TValidator>) => ServerFnAfterTyper<TMethod, TMiddlewares, TValidator>;
95
+ export interface ServerFnTyper<TMethod extends Method, TServerFnResponseType extends ServerFnResponseType, TMiddlewares, TValidator> {
96
+ type: (typer: ServerFnTypeOrTypeFn<TMethod, TServerFnResponseType, TMiddlewares, TValidator>) => ServerFnAfterTyper<TMethod, TServerFnResponseType, TMiddlewares, TValidator>;
90
97
  }
91
- export type ServerFnTypeOrTypeFn<TMethod extends Method, TMiddlewares, TValidator> = ServerFnType | ((ctx: ServerFnCtx<TMethod, TMiddlewares, TValidator>) => ServerFnType);
92
- export interface ServerFnAfterTyper<TMethod extends Method, TMiddlewares, TValidator> extends ServerFnHandler<TMethod, TMiddlewares, TValidator> {
98
+ export type ServerFnTypeOrTypeFn<TMethod extends Method, TServerFnResponseType extends ServerFnResponseType, TMiddlewares, TValidator> = ServerFnType | ((ctx: ServerFnCtx<TMethod, TServerFnResponseType, TMiddlewares, TValidator>) => ServerFnType);
99
+ export interface ServerFnAfterTyper<TMethod extends Method, TServerFnResponseType extends ServerFnResponseType, TMiddlewares, TValidator> extends ServerFnHandler<TMethod, TServerFnResponseType, TMiddlewares, TValidator> {
93
100
  }
94
- export interface ServerFnHandler<TMethod extends Method, TMiddlewares, TValidator> {
95
- handler: <TNewResponse>(fn?: ServerFn<TMethod, TMiddlewares, TValidator, TNewResponse>) => Fetcher<TMiddlewares, TValidator, TNewResponse>;
101
+ export interface ServerFnHandler<TMethod extends Method, TServerFnResponseType extends ServerFnResponseType, TMiddlewares, TValidator> {
102
+ handler: <TNewResponse>(fn?: ServerFn<TMethod, TServerFnResponseType, TMiddlewares, TValidator, TNewResponse>) => Fetcher<TMiddlewares, TValidator, TNewResponse, TServerFnResponseType>;
96
103
  }
97
- export interface ServerFnBuilder<TMethod extends Method = 'GET'> extends ServerFnMiddleware<TMethod, undefined>, ServerFnValidator<TMethod, undefined>, ServerFnTyper<TMethod, undefined, undefined>, ServerFnHandler<TMethod, undefined, undefined> {
98
- options: ServerFnBaseOptions<TMethod, unknown, undefined, undefined>;
104
+ export interface ServerFnBuilder<TMethod extends Method = 'GET', TServerFnResponseType extends ServerFnResponseType = 'data'> extends ServerFnMiddleware<TMethod, TServerFnResponseType, undefined>, ServerFnValidator<TMethod, TServerFnResponseType, undefined>, ServerFnTyper<TMethod, TServerFnResponseType, undefined, undefined>, ServerFnHandler<TMethod, TServerFnResponseType, undefined, undefined> {
105
+ options: ServerFnBaseOptions<TMethod, TServerFnResponseType, unknown, undefined, undefined>;
99
106
  }
100
107
  type StaticCachedResult = {
101
108
  ctx?: {
@@ -111,24 +118,26 @@ export type ServerFnStaticCache = {
111
118
  };
112
119
  export declare function setServerFnStaticCache(cache?: ServerFnStaticCache | (() => ServerFnStaticCache | undefined)): () => void;
113
120
  export declare function createServerFnStaticCache(serverFnStaticCache: ServerFnStaticCache): ServerFnStaticCache;
114
- export declare function createServerFn<TMethod extends Method, TResponse = unknown, TMiddlewares = undefined, TValidator = undefined>(options?: {
121
+ export declare function createServerFn<TMethod extends Method, TServerFnResponseType extends ServerFnResponseType = 'data', TResponse = unknown, TMiddlewares = undefined, TValidator = undefined>(options?: {
115
122
  method?: TMethod;
123
+ response?: TServerFnResponseType;
116
124
  type?: ServerFnType;
117
- }, __opts?: ServerFnBaseOptions<TMethod, TResponse, TMiddlewares, TValidator>): ServerFnBuilder<TMethod>;
125
+ }, __opts?: ServerFnBaseOptions<TMethod, TServerFnResponseType, TResponse, TMiddlewares, TValidator>): ServerFnBuilder<TMethod, TServerFnResponseType>;
118
126
  export type MiddlewareOptions = {
119
127
  method: Method;
128
+ response?: ServerFnResponseType;
120
129
  data: any;
121
130
  headers?: HeadersInit;
122
131
  signal?: AbortSignal;
123
132
  sendContext?: any;
124
133
  context?: any;
125
- type: ServerFnTypeOrTypeFn<any, any, any>;
134
+ type: ServerFnTypeOrTypeFn<any, any, any, any>;
126
135
  functionId: string;
127
136
  };
128
137
  export type MiddlewareResult = MiddlewareOptions & {
129
138
  result?: unknown;
130
139
  error?: unknown;
131
- type: ServerFnTypeOrTypeFn<any, any, any>;
140
+ type: ServerFnTypeOrTypeFn<any, any, any, any>;
132
141
  };
133
142
  export type NextFn = (ctx: MiddlewareResult) => Promise<MiddlewareResult>;
134
143
  export type MiddlewareFn = (ctx: MiddlewareOptions & {
@@ -80,22 +80,13 @@ function createServerFn(options, __opts) {
80
80
  return {
81
81
  options: resolvedOptions,
82
82
  middleware: (middleware) => {
83
- return createServerFn(
84
- void 0,
85
- Object.assign(resolvedOptions, { middleware })
86
- );
83
+ return createServerFn(void 0, Object.assign(resolvedOptions, { middleware }));
87
84
  },
88
85
  validator: (validator) => {
89
- return createServerFn(
90
- void 0,
91
- Object.assign(resolvedOptions, { validator })
92
- );
86
+ return createServerFn(void 0, Object.assign(resolvedOptions, { validator }));
93
87
  },
94
88
  type: (type) => {
95
- return createServerFn(
96
- void 0,
97
- Object.assign(resolvedOptions, { type })
98
- );
89
+ return createServerFn(void 0, Object.assign(resolvedOptions, { type }));
99
90
  },
100
91
  handler: (...args) => {
101
92
  const [extractedFn, serverFn] = args;
@@ -118,6 +109,9 @@ function createServerFn(options, __opts) {
118
109
  signal: opts == null ? void 0 : opts.signal,
119
110
  context: {}
120
111
  }).then((d) => {
112
+ if (resolvedOptions.response === "full") {
113
+ return d;
114
+ }
121
115
  if (d.error) throw d.error;
122
116
  return d.result;
123
117
  });
@@ -234,7 +228,7 @@ const applyMiddleware = async (middlewareFn, ctx, nextFn) => {
234
228
  ...userCtx.sendContext ?? {}
235
229
  },
236
230
  headers: mergeHeaders(ctx.headers, userCtx.headers),
237
- result: userCtx.result !== void 0 ? userCtx.result : ctx.result,
231
+ result: userCtx.result !== void 0 ? userCtx.result : ctx.response === "raw" ? userCtx : ctx.result,
238
232
  error: userCtx.error ?? ctx.error
239
233
  });
240
234
  }