@tanstack/start-client-core 1.132.0-alpha.2 → 1.132.0-alpha.20

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.
Files changed (53) hide show
  1. package/dist/esm/constants.d.ts +5 -0
  2. package/dist/esm/constants.js +13 -0
  3. package/dist/esm/constants.js.map +1 -0
  4. package/dist/esm/createClientRpc.d.ts +6 -0
  5. package/dist/esm/createClientRpc.js +26 -0
  6. package/dist/esm/createClientRpc.js.map +1 -0
  7. package/dist/esm/createMiddleware.d.ts +42 -42
  8. package/dist/esm/createMiddleware.js.map +1 -1
  9. package/dist/esm/createServerFn.d.ts +57 -61
  10. package/dist/esm/createServerFn.js +40 -46
  11. package/dist/esm/createServerFn.js.map +1 -1
  12. package/dist/esm/envOnly.d.ts +2 -2
  13. package/dist/esm/envOnly.js +4 -4
  14. package/dist/esm/envOnly.js.map +1 -1
  15. package/dist/esm/getRouterInstance.d.ts +1 -0
  16. package/dist/esm/getRouterInstance.js +7 -0
  17. package/dist/esm/getRouterInstance.js.map +1 -0
  18. package/dist/esm/index.d.ts +6 -5
  19. package/dist/esm/index.js +13 -8
  20. package/dist/esm/index.js.map +1 -1
  21. package/dist/esm/serializer/ServerFunctionSerializationAdapter.d.ts +5 -0
  22. package/dist/esm/serializer/ServerFunctionSerializationAdapter.js +17 -0
  23. package/dist/esm/serializer/ServerFunctionSerializationAdapter.js.map +1 -0
  24. package/dist/esm/serializer/getClientSerovalPlugins.d.ts +3 -0
  25. package/dist/esm/serializer/getClientSerovalPlugins.js +13 -0
  26. package/dist/esm/serializer/getClientSerovalPlugins.js.map +1 -0
  27. package/dist/esm/serializer/getDefaultSerovalPlugins.d.ts +1 -0
  28. package/dist/esm/serializer/getDefaultSerovalPlugins.js +16 -0
  29. package/dist/esm/serializer/getDefaultSerovalPlugins.js.map +1 -0
  30. package/dist/esm/serverFnFetcher.d.ts +1 -0
  31. package/dist/esm/serverFnFetcher.js +217 -0
  32. package/dist/esm/serverFnFetcher.js.map +1 -0
  33. package/package.json +5 -4
  34. package/src/constants.ts +8 -0
  35. package/src/createClientRpc.ts +26 -0
  36. package/src/createMiddleware.ts +113 -95
  37. package/src/createServerFn.ts +230 -208
  38. package/src/envOnly.ts +2 -2
  39. package/src/getRouterInstance.ts +7 -0
  40. package/src/index.tsx +11 -15
  41. package/src/serializer/ServerFunctionSerializationAdapter.ts +16 -0
  42. package/src/serializer/getClientSerovalPlugins.ts +10 -0
  43. package/src/serializer/getDefaultSerovalPlugins.ts +19 -0
  44. package/src/serverFnFetcher.ts +299 -0
  45. package/src/tests/createServerFn.test-d.ts +134 -108
  46. package/src/tests/createServerMiddleware.test-d.ts +16 -9
  47. package/src/tests/envOnly.test-d.ts +9 -9
  48. package/dist/esm/serializer.d.ts +0 -23
  49. package/dist/esm/serializer.js +0 -162
  50. package/dist/esm/serializer.js.map +0 -1
  51. package/dist/esm/tests/serializer.test.d.ts +0 -1
  52. package/src/serializer.ts +0 -219
  53. package/src/tests/serializer.test.tsx +0 -151
@@ -0,0 +1,5 @@
1
+ export declare const TSS_FORMDATA_CONTEXT = "__TSS_CONTEXT";
2
+ export declare const TSS_SERVER_FUNCTION: unique symbol;
3
+ export declare const TSS_SERVER_FUNCTION_FACTORY: unique symbol;
4
+ export declare const X_TSS_SERIALIZED = "x-tss-serialized";
5
+ export {};
@@ -0,0 +1,13 @@
1
+ const TSS_FORMDATA_CONTEXT = "__TSS_CONTEXT";
2
+ const TSS_SERVER_FUNCTION = Symbol.for("TSS_SERVER_FUNCTION");
3
+ const TSS_SERVER_FUNCTION_FACTORY = Symbol.for(
4
+ "TSS_SERVER_FUNCTION_FACTORY"
5
+ );
6
+ const X_TSS_SERIALIZED = "x-tss-serialized";
7
+ export {
8
+ TSS_FORMDATA_CONTEXT,
9
+ TSS_SERVER_FUNCTION,
10
+ TSS_SERVER_FUNCTION_FACTORY,
11
+ X_TSS_SERIALIZED
12
+ };
13
+ //# sourceMappingURL=constants.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.js","sources":["../../src/constants.ts"],"sourcesContent":["export const TSS_FORMDATA_CONTEXT = '__TSS_CONTEXT'\nexport const TSS_SERVER_FUNCTION = Symbol.for('TSS_SERVER_FUNCTION')\nexport const TSS_SERVER_FUNCTION_FACTORY = Symbol.for(\n 'TSS_SERVER_FUNCTION_FACTORY',\n)\n\nexport const X_TSS_SERIALIZED = 'x-tss-serialized'\nexport {}\n"],"names":[],"mappings":"AAAO,MAAM,uBAAuB;AAC7B,MAAM,sBAAsB,OAAO,IAAI,qBAAqB;AAC5D,MAAM,8BAA8B,OAAO;AAAA,EAChD;AACF;AAEO,MAAM,mBAAmB;"}
@@ -0,0 +1,6 @@
1
+ import { TSS_SERVER_FUNCTION } from './constants.js';
2
+ export declare const createClientRpc: (functionId: string) => ((...args: Array<any>) => Promise<any>) & {
3
+ url: string;
4
+ functionId: string;
5
+ [TSS_SERVER_FUNCTION]: boolean;
6
+ };
@@ -0,0 +1,26 @@
1
+ import { TSS_SERVER_FUNCTION } from "./constants.js";
2
+ import { serverFnFetcher } from "./serverFnFetcher.js";
3
+ let baseUrl;
4
+ function sanitizeBase(base) {
5
+ return base.replace(/^\/|\/$/g, "");
6
+ }
7
+ const createClientRpc = (functionId) => {
8
+ if (!baseUrl) {
9
+ const sanitizedAppBase = sanitizeBase(process.env.TSS_APP_BASE || "/");
10
+ const sanitizedServerBase = sanitizeBase(process.env.TSS_SERVER_FN_BASE);
11
+ baseUrl = `${sanitizedAppBase ? `/${sanitizedAppBase}` : ""}/${sanitizedServerBase}/`;
12
+ }
13
+ const url = baseUrl + functionId;
14
+ const clientFn = (...args) => {
15
+ return serverFnFetcher(url, args, fetch);
16
+ };
17
+ return Object.assign(clientFn, {
18
+ url,
19
+ functionId,
20
+ [TSS_SERVER_FUNCTION]: true
21
+ });
22
+ };
23
+ export {
24
+ createClientRpc
25
+ };
26
+ //# sourceMappingURL=createClientRpc.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createClientRpc.js","sources":["../../src/createClientRpc.ts"],"sourcesContent":["import { TSS_SERVER_FUNCTION } from './constants'\nimport { serverFnFetcher } from './serverFnFetcher'\n\nlet baseUrl: string\nfunction sanitizeBase(base: string) {\n return base.replace(/^\\/|\\/$/g, '')\n}\n\nexport const createClientRpc = (functionId: string) => {\n if (!baseUrl) {\n const sanitizedAppBase = sanitizeBase(process.env.TSS_APP_BASE || '/')\n const sanitizedServerBase = sanitizeBase(process.env.TSS_SERVER_FN_BASE!)\n baseUrl = `${sanitizedAppBase ? `/${sanitizedAppBase}` : ''}/${sanitizedServerBase}/`\n }\n const url = baseUrl + functionId\n\n const clientFn = (...args: Array<any>) => {\n return serverFnFetcher(url, args, fetch)\n }\n\n return Object.assign(clientFn, {\n url,\n functionId,\n [TSS_SERVER_FUNCTION]: true,\n })\n}\n"],"names":[],"mappings":";;AAGA,IAAI;AACJ,SAAS,aAAa,MAAc;AAClC,SAAO,KAAK,QAAQ,YAAY,EAAE;AACpC;AAEO,MAAM,kBAAkB,CAAC,eAAuB;AACrD,MAAI,CAAC,SAAS;AACZ,UAAM,mBAAmB,aAAa,QAAQ,IAAI,gBAAgB,GAAG;AACrE,UAAM,sBAAsB,aAAa,QAAQ,IAAI,kBAAmB;AACxE,cAAU,GAAG,mBAAmB,IAAI,gBAAgB,KAAK,EAAE,IAAI,mBAAmB;AAAA,EACpF;AACA,QAAM,MAAM,UAAU;AAEtB,QAAM,WAAW,IAAI,SAAqB;AACxC,WAAO,gBAAgB,KAAK,MAAM,KAAK;AAAA,EACzC;AAEA,SAAO,OAAO,OAAO,UAAU;AAAA,IAC7B;AAAA,IACA;AAAA,IACA,CAAC,mBAAmB,GAAG;AAAA,EAAA,CACxB;AACH;"}
@@ -1,20 +1,18 @@
1
- import { ConstrainValidator, Method, ServerFnResponseType } from './createServerFn.js';
2
- import { AnyRouter, Assign, Constrain, Expand, IntersectAssign, RegisteredRouter, ResolveValidatorInput, ResolveValidatorOutput } from '@tanstack/router-core';
3
- import { SerializerStringify } from './serializer.js';
4
- export declare function createMiddleware<TType extends MiddlewareType>(options: {
1
+ import { AnyServerFn, ConstrainValidator, Method } from './createServerFn.js';
2
+ import { AnyRouter, Assign, Constrain, Expand, IntersectAssign, Register, RegisteredRouter, ResolveValidatorInput, ResolveValidatorOutput, ValidateSerializableInput } from '@tanstack/router-core';
3
+ export declare function createMiddleware<TRegister extends Register, TType extends MiddlewareType>(options: {
5
4
  type: TType;
6
- validateClient?: boolean;
7
- }, __opts?: FunctionMiddlewareOptions<unknown, undefined, undefined, undefined, ServerFnResponseType>): CreateMiddlewareResult<TType>;
5
+ }, __opts?: FunctionMiddlewareOptions<TRegister, unknown, undefined, undefined, undefined>): CreateMiddlewareResult<TRegister, TType>;
8
6
  export type MiddlewareType = 'request' | 'function';
9
- export type CreateMiddlewareResult<TType extends MiddlewareType> = 'function' extends TType ? FunctionMiddleware<ServerFnResponseType> : RequestMiddleware;
10
- export interface FunctionMiddleware<TServerFnResponseType extends ServerFnResponseType> extends FunctionMiddlewareAfterMiddleware<unknown, TServerFnResponseType> {
11
- middleware: <const TNewMiddlewares = undefined>(middlewares: Constrain<TNewMiddlewares, ReadonlyArray<AnyFunctionMiddleware>>) => FunctionMiddlewareAfterMiddleware<TNewMiddlewares, TServerFnResponseType>;
7
+ export type CreateMiddlewareResult<TRegister extends Register, TType extends MiddlewareType> = 'function' extends TType ? FunctionMiddleware<TRegister> : RequestMiddleware;
8
+ export interface FunctionMiddleware<TRegister extends Register> extends FunctionMiddlewareAfterMiddleware<TRegister, unknown> {
9
+ middleware: <const TNewMiddlewares = undefined>(middlewares: Constrain<TNewMiddlewares, ReadonlyArray<AnyFunctionMiddleware>>) => FunctionMiddlewareAfterMiddleware<TRegister, TNewMiddlewares>;
12
10
  }
13
- export interface FunctionMiddlewareAfterMiddleware<TMiddlewares, TServerFnResponseType extends ServerFnResponseType> extends FunctionMiddlewareWithTypes<TMiddlewares, undefined, undefined, undefined, undefined, undefined, TServerFnResponseType>, FunctionMiddlewareServer<TMiddlewares, undefined, undefined, undefined, TServerFnResponseType>, FunctionMiddlewareClient<TMiddlewares, undefined, TServerFnResponseType>, FunctionMiddlewareValidator<TMiddlewares, TServerFnResponseType> {
11
+ export interface FunctionMiddlewareAfterMiddleware<TRegister extends Register, TMiddlewares> extends FunctionMiddlewareWithTypes<TRegister, TMiddlewares, undefined, undefined, undefined, undefined, undefined>, FunctionMiddlewareServer<TRegister, TMiddlewares, undefined, undefined, undefined>, FunctionMiddlewareClient<TRegister, TMiddlewares, undefined>, FunctionMiddlewareValidator<TRegister, TMiddlewares> {
14
12
  }
15
- export interface FunctionMiddlewareWithTypes<TMiddlewares, TValidator, TServerContext, TServerSendContext, TClientContext, TClientSendContext, TServerFnResponseType extends ServerFnResponseType> {
13
+ export interface FunctionMiddlewareWithTypes<TRegister extends Register, TMiddlewares, TValidator, TServerContext, TServerSendContext, TClientContext, TClientSendContext> {
16
14
  _types: FunctionMiddlewareTypes<TMiddlewares, TValidator, TServerContext, TServerSendContext, TClientContext, TClientSendContext>;
17
- options: FunctionMiddlewareOptions<TMiddlewares, TValidator, TServerContext, TClientContext, TServerFnResponseType>;
15
+ options: FunctionMiddlewareOptions<TRegister, TMiddlewares, TValidator, TServerContext, TClientContext>;
18
16
  }
19
17
  export interface FunctionMiddlewareTypes<in out TMiddlewares, in out TValidator, in out TServerContext, in out TServerSendContext, in out TClientContext, in out TClientSendContext> {
20
18
  type: 'function';
@@ -38,7 +36,7 @@ export interface FunctionMiddlewareTypes<in out TMiddlewares, in out TValidator,
38
36
  * Recursively resolve the input type produced by a sequence of middleware
39
37
  */
40
38
  export type IntersectAllValidatorInputs<TMiddlewares, TValidator> = unknown extends TValidator ? TValidator : TValidator extends undefined ? IntersectAllMiddleware<TMiddlewares, 'allInput'> : IntersectAssign<IntersectAllMiddleware<TMiddlewares, 'allInput'>, ResolveValidatorInput<TValidator>>;
41
- export type IntersectAllMiddleware<TMiddlewares, TType extends keyof AnyFunctionMiddleware['_types'] | keyof AnyRequestMiddleware['_types'], TAcc = undefined> = TMiddlewares extends readonly [infer TMiddleware, ...infer TRest] ? TMiddleware extends AnyFunctionMiddleware | AnyRequestMiddleware ? IntersectAllMiddleware<TRest, TType, IntersectAssign<TAcc, TMiddleware['_types'][TType & keyof TMiddleware['_types']]>> : TAcc : TAcc;
39
+ export type IntersectAllMiddleware<TMiddlewares, TType extends keyof AnyFunctionMiddleware['_types'] | keyof AnyRequestMiddleware['_types'] | keyof AnyServerFn['_types'], TAcc = undefined> = TMiddlewares extends readonly [infer TMiddleware, ...infer TRest] ? TMiddleware extends AnyFunctionMiddleware | AnyRequestMiddleware | AnyServerFn ? IntersectAllMiddleware<TRest, TType, IntersectAssign<TAcc, TMiddleware['_types'][TType & keyof TMiddleware['_types']]>> : TAcc : TAcc;
42
40
  export type AnyFunctionMiddleware = FunctionMiddlewareWithTypes<any, any, any, any, any, any, any>;
43
41
  /**
44
42
  * Recursively merge the output type produced by a sequence of middleware
@@ -48,7 +46,7 @@ export type IntersectAllValidatorOutputs<TMiddlewares, TValidator> = unknown ext
48
46
  * Recursively resolve the client context type produced by a sequence of middleware
49
47
  */
50
48
  export type AssignAllClientContextBeforeNext<TMiddlewares, TClientContext = undefined> = unknown extends TClientContext ? TClientContext : Assign<AssignAllMiddleware<TMiddlewares, 'allClientContextBeforeNext'>, TClientContext>;
51
- export type AssignAllMiddleware<TMiddlewares, TType extends keyof AnyFunctionMiddleware['_types'] | keyof AnyRequestMiddleware['_types'], TAcc = undefined> = TMiddlewares extends readonly [infer TMiddleware, ...infer TRest] ? TMiddleware extends AnyFunctionMiddleware | AnyRequestMiddleware ? AssignAllMiddleware<TRest, TType, Assign<TAcc, TMiddleware['_types'][TType & keyof TMiddleware['_types']]>> : TAcc : TAcc;
49
+ export type AssignAllMiddleware<TMiddlewares, TType extends keyof AnyFunctionMiddleware['_types'] | keyof AnyRequestMiddleware['_types'] | keyof AnyServerFn['_types'], TAcc = undefined> = TMiddlewares extends readonly [infer TMiddleware, ...infer TRest] ? TMiddleware extends AnyFunctionMiddleware | AnyRequestMiddleware | AnyServerFn ? AssignAllMiddleware<TRest, TType, Assign<TAcc, TMiddleware['_types'][TType & keyof TMiddleware['_types']]>> : TAcc : TAcc;
52
50
  export type AssignAllClientContextAfterNext<TMiddlewares, TClientContext = undefined, TSendContext = undefined> = unknown extends TClientContext ? Assign<TClientContext, TSendContext> : Assign<AssignAllMiddleware<TMiddlewares, 'allClientContextAfterNext'>, Assign<TClientContext, TSendContext>>;
53
51
  export type AssignAllServerSendContext<TMiddlewares, TSendContext = undefined> = unknown extends TSendContext ? TSendContext : Assign<AssignAllMiddleware<TMiddlewares, 'allServerSendContext'>, TSendContext>;
54
52
  /**
@@ -56,33 +54,32 @@ export type AssignAllServerSendContext<TMiddlewares, TSendContext = undefined> =
56
54
  */
57
55
  export type AssignAllServerContext<TMiddlewares, TSendContext = undefined, TServerContext = undefined> = unknown extends TSendContext ? Assign<TSendContext, TServerContext> : Assign<AssignAllMiddleware<TMiddlewares, 'allServerContext'>, Assign<TSendContext, TServerContext>>;
58
56
  export type AssignAllClientSendContext<TMiddlewares, TSendContext = undefined> = unknown extends TSendContext ? TSendContext : Assign<AssignAllMiddleware<TMiddlewares, 'allClientSendContext'>, TSendContext>;
59
- export interface FunctionMiddlewareOptions<in out TMiddlewares, in out TValidator, in out TServerContext, in out TClientContext, in out TServerFnResponseType extends ServerFnResponseType> {
60
- validateClient?: boolean;
57
+ export interface FunctionMiddlewareOptions<in out TRegister extends Register, in out TMiddlewares, in out TValidator, in out TServerContext, in out TClientContext> {
61
58
  middleware?: TMiddlewares;
62
- validator?: ConstrainValidator<TValidator>;
63
- client?: FunctionMiddlewareClientFn<TMiddlewares, TValidator, TServerContext, TClientContext, TServerFnResponseType>;
64
- server?: FunctionMiddlewareServerFn<TMiddlewares, TValidator, TServerContext, unknown, unknown, TServerFnResponseType>;
59
+ validator?: ConstrainValidator<TRegister, 'GET', TValidator>;
60
+ client?: FunctionMiddlewareClientFn<TRegister, TMiddlewares, TValidator, TServerContext, TClientContext>;
61
+ server?: FunctionMiddlewareServerFn<TRegister, TMiddlewares, TValidator, TServerContext, unknown, unknown>;
65
62
  }
66
- export type FunctionMiddlewareClientNextFn<TMiddlewares> = <TSendContext = undefined, TNewClientContext = undefined>(ctx?: {
63
+ export type FunctionMiddlewareClientNextFn<TRegister extends Register, TMiddlewares> = <TSendContext = undefined, TNewClientContext = undefined>(ctx?: {
67
64
  context?: TNewClientContext;
68
- sendContext?: SerializerStringify<TSendContext>;
65
+ sendContext?: ValidateSerializableInput<TRegister, TSendContext>;
69
66
  headers?: HeadersInit;
70
67
  }) => Promise<FunctionClientResultWithContext<TMiddlewares, TSendContext, TNewClientContext>>;
71
- export interface FunctionMiddlewareServer<TMiddlewares, TValidator, TServerSendContext, TClientContext, TServerFnResponseType extends ServerFnResponseType> {
72
- server: <TNewServerContext = undefined, TSendContext = undefined>(server: FunctionMiddlewareServerFn<TMiddlewares, TValidator, TServerSendContext, TNewServerContext, TSendContext, TServerFnResponseType>) => FunctionMiddlewareAfterServer<TMiddlewares, TValidator, TNewServerContext, TServerSendContext, TClientContext, TSendContext, ServerFnResponseType>;
68
+ export interface FunctionMiddlewareServer<TRegister extends Register, TMiddlewares, TValidator, TServerSendContext, TClientContext> {
69
+ server: <TNewServerContext = undefined, TSendContext = undefined>(server: FunctionMiddlewareServerFn<TRegister, TMiddlewares, TValidator, TServerSendContext, TNewServerContext, TSendContext>) => FunctionMiddlewareAfterServer<TRegister, TMiddlewares, TValidator, TNewServerContext, TServerSendContext, TClientContext, TSendContext>;
73
70
  }
74
- export type FunctionMiddlewareServerFn<TMiddlewares, TValidator, TServerSendContext, TNewServerContext, TSendContext, TServerFnResponseType extends ServerFnResponseType> = (options: FunctionMiddlewareServerFnOptions<TMiddlewares, TValidator, TServerSendContext, TServerFnResponseType>) => FunctionMiddlewareServerFnResult<TMiddlewares, TServerSendContext, TNewServerContext, TSendContext>;
75
- export interface RequestMiddlewareServerFnOptions<in out TMiddlewares, in out TServerSendContext> {
71
+ export type FunctionMiddlewareServerFn<TRegister extends Register, TMiddlewares, TValidator, TServerSendContext, TNewServerContext, TSendContext> = (options: FunctionMiddlewareServerFnOptions<TRegister, TMiddlewares, TValidator, TServerSendContext>) => FunctionMiddlewareServerFnResult<TMiddlewares, TServerSendContext, TNewServerContext, TSendContext>;
72
+ export interface RequestMiddlewareServerFnOptions<in out TRegister extends Register, in out TMiddlewares, in out TServerSendContext> {
76
73
  request: Request;
77
74
  context: Expand<AssignAllServerContext<TMiddlewares, TServerSendContext>>;
78
- next: FunctionMiddlewareServerNextFn<TMiddlewares, TServerSendContext>;
75
+ next: FunctionMiddlewareServerNextFn<TRegister, TMiddlewares, TServerSendContext>;
79
76
  response: Response;
80
77
  method: Method;
81
78
  signal: AbortSignal;
82
79
  }
83
- export type FunctionMiddlewareServerNextFn<TMiddlewares, TServerSendContext> = <TNewServerContext = undefined, TSendContext = undefined>(ctx?: {
80
+ export type FunctionMiddlewareServerNextFn<TRegister extends Register, TMiddlewares, TServerSendContext> = <TNewServerContext = undefined, TSendContext = undefined>(ctx?: {
84
81
  context?: TNewServerContext;
85
- sendContext?: SerializerStringify<TSendContext>;
82
+ sendContext?: ValidateSerializableInput<TRegister, TSendContext>;
86
83
  }) => Promise<FunctionServerResultWithContext<TMiddlewares, TServerSendContext, TNewServerContext, TSendContext>>;
87
84
  export type FunctionServerResultWithContext<in out TMiddlewares, in out TServerSendContext, in out TServerContext, in out TSendContext> = {
88
85
  'use functions must return the result of next()': true;
@@ -93,31 +90,29 @@ export type FunctionServerResultWithContext<in out TMiddlewares, in out TServerS
93
90
  context: Expand<AssignAllServerContext<TMiddlewares, TServerSendContext, TServerContext>>;
94
91
  sendContext: Expand<AssignAllClientSendContext<TMiddlewares, TSendContext>>;
95
92
  };
96
- export interface FunctionMiddlewareServerFnOptions<in out TMiddlewares, in out TValidator, in out TServerSendContext, in out TServerFnResponseType> {
93
+ export interface FunctionMiddlewareServerFnOptions<in out TRegister extends Register, in out TMiddlewares, in out TValidator, in out TServerSendContext> {
97
94
  data: Expand<IntersectAllValidatorOutputs<TMiddlewares, TValidator>>;
98
95
  context: Expand<AssignAllServerContext<TMiddlewares, TServerSendContext>>;
99
- next: FunctionMiddlewareServerNextFn<TMiddlewares, TServerSendContext>;
100
- response: TServerFnResponseType;
96
+ next: FunctionMiddlewareServerNextFn<TRegister, TMiddlewares, TServerSendContext>;
101
97
  method: Method;
102
98
  filename: string;
103
99
  functionId: string;
104
100
  signal: AbortSignal;
105
101
  }
106
102
  export type FunctionMiddlewareServerFnResult<TMiddlewares, TServerSendContext, TServerContext, TSendContext> = Promise<FunctionServerResultWithContext<TMiddlewares, TServerSendContext, TServerContext, TSendContext>> | FunctionServerResultWithContext<TMiddlewares, TServerSendContext, TServerContext, TSendContext>;
107
- export interface FunctionMiddlewareAfterServer<TMiddlewares, TValidator, TServerContext, TServerSendContext, TClientContext, TClientSendContext, TServerFnResponseType extends ServerFnResponseType> extends FunctionMiddlewareWithTypes<TMiddlewares, TValidator, TServerContext, TServerSendContext, TClientContext, TClientSendContext, TServerFnResponseType> {
103
+ export interface FunctionMiddlewareAfterServer<TRegister extends Register, TMiddlewares, TValidator, TServerContext, TServerSendContext, TClientContext, TClientSendContext> extends FunctionMiddlewareWithTypes<TRegister, TMiddlewares, TValidator, TServerContext, TServerSendContext, TClientContext, TClientSendContext> {
108
104
  }
109
- export interface FunctionMiddlewareClient<TMiddlewares, TValidator, TServerFnResponseType extends ServerFnResponseType, TRouter extends AnyRouter = RegisteredRouter> {
110
- client: <TSendServerContext = undefined, TNewClientContext = undefined>(client: FunctionMiddlewareClientFn<TMiddlewares, TValidator, TSendServerContext, TNewClientContext, TServerFnResponseType, TRouter>) => FunctionMiddlewareAfterClient<TMiddlewares, TValidator, TSendServerContext, TNewClientContext, ServerFnResponseType>;
105
+ export interface FunctionMiddlewareClient<TRegister extends Register, TMiddlewares, TValidator, TRouter extends AnyRouter = RegisteredRouter> {
106
+ client: <TSendServerContext = undefined, TNewClientContext = undefined>(client: FunctionMiddlewareClientFn<TRegister, TMiddlewares, TValidator, TSendServerContext, TNewClientContext, TRouter>) => FunctionMiddlewareAfterClient<TRegister, TMiddlewares, TValidator, TSendServerContext, TNewClientContext>;
111
107
  }
112
- export type FunctionMiddlewareClientFn<TMiddlewares, TValidator, TSendContext, TClientContext, TServerFnResponseType extends ServerFnResponseType, TRouter extends AnyRouter = RegisteredRouter> = (options: FunctionMiddlewareClientFnOptions<TMiddlewares, TValidator, TServerFnResponseType, TRouter>) => FunctionMiddlewareClientFnResult<TMiddlewares, TSendContext, TClientContext>;
113
- export interface FunctionMiddlewareClientFnOptions<in out TMiddlewares, in out TValidator, in out TServerFnResponseType extends ServerFnResponseType, in out TRouter extends AnyRouter> {
108
+ export type FunctionMiddlewareClientFn<TRegister extends Register, TMiddlewares, TValidator, TSendContext, TClientContext, TRouter extends AnyRouter = RegisteredRouter> = (options: FunctionMiddlewareClientFnOptions<TRegister, TMiddlewares, TValidator, TRouter>) => FunctionMiddlewareClientFnResult<TMiddlewares, TSendContext, TClientContext>;
109
+ export interface FunctionMiddlewareClientFnOptions<in out TRegister extends Register, in out TMiddlewares, in out TValidator, in out TRouter extends AnyRouter> {
114
110
  data: Expand<IntersectAllValidatorInputs<TMiddlewares, TValidator>>;
115
111
  context: Expand<AssignAllClientContextBeforeNext<TMiddlewares>>;
116
112
  sendContext: Expand<AssignAllServerSendContext<TMiddlewares>>;
117
113
  method: Method;
118
- response: TServerFnResponseType;
119
114
  signal: AbortSignal;
120
- next: FunctionMiddlewareClientNextFn<TMiddlewares>;
115
+ next: FunctionMiddlewareClientNextFn<TRegister, TMiddlewares>;
121
116
  filename: string;
122
117
  functionId: string;
123
118
  router: TRouter;
@@ -129,12 +124,12 @@ export type FunctionClientResultWithContext<in out TMiddlewares, in out TSendCon
129
124
  sendContext: Expand<AssignAllServerSendContext<TMiddlewares, TSendContext>>;
130
125
  headers: HeadersInit;
131
126
  };
132
- export interface FunctionMiddlewareAfterClient<TMiddlewares, TValidator, TServerSendContext, TClientContext, TServerFnResponseType extends ServerFnResponseType> extends FunctionMiddlewareWithTypes<TMiddlewares, TValidator, undefined, TServerSendContext, TClientContext, undefined, TServerFnResponseType>, FunctionMiddlewareServer<TMiddlewares, TValidator, TServerSendContext, TClientContext, TServerFnResponseType> {
127
+ export interface FunctionMiddlewareAfterClient<TRegister extends Register, TMiddlewares, TValidator, TServerSendContext, TClientContext> extends FunctionMiddlewareWithTypes<TRegister, TMiddlewares, TValidator, undefined, TServerSendContext, TClientContext, undefined>, FunctionMiddlewareServer<TRegister, TMiddlewares, TValidator, TServerSendContext, TClientContext> {
133
128
  }
134
- export interface FunctionMiddlewareValidator<TMiddlewares, TServerFnResponseType extends ServerFnResponseType> {
135
- validator: <TNewValidator>(input: ConstrainValidator<TNewValidator>) => FunctionMiddlewareAfterValidator<TMiddlewares, TNewValidator, TServerFnResponseType>;
129
+ export interface FunctionMiddlewareValidator<TRegister extends Register, TMiddlewares> {
130
+ validator: <TNewValidator>(input: ConstrainValidator<TRegister, 'GET', TNewValidator>) => FunctionMiddlewareAfterValidator<TRegister, TMiddlewares, TNewValidator>;
136
131
  }
137
- export interface FunctionMiddlewareAfterValidator<TMiddlewares, TValidator, TServerFnResponseType extends ServerFnResponseType> extends FunctionMiddlewareWithTypes<TMiddlewares, TValidator, undefined, undefined, undefined, undefined, ServerFnResponseType>, FunctionMiddlewareServer<TMiddlewares, TValidator, undefined, undefined, TServerFnResponseType>, FunctionMiddlewareClient<TMiddlewares, TValidator, ServerFnResponseType> {
132
+ export interface FunctionMiddlewareAfterValidator<TRegister extends Register, TMiddlewares, TValidator> extends FunctionMiddlewareWithTypes<TRegister, TMiddlewares, TValidator, undefined, undefined, undefined, undefined>, FunctionMiddlewareServer<TRegister, TMiddlewares, TValidator, undefined, undefined>, FunctionMiddlewareClient<TRegister, TMiddlewares, TValidator> {
138
133
  }
139
134
  export interface RequestMiddleware extends RequestMiddlewareAfterMiddleware<undefined> {
140
135
  middleware: <const TMiddlewares = undefined>(middlewares: Constrain<TMiddlewares, ReadonlyArray<AnyRequestMiddleware>>) => RequestMiddlewareAfterMiddleware<TMiddlewares>;
@@ -142,6 +137,11 @@ export interface RequestMiddleware extends RequestMiddlewareAfterMiddleware<unde
142
137
  export type AnyRequestMiddleware = RequestMiddlewareWithTypes<any, any>;
143
138
  export interface RequestMiddlewareWithTypes<TMiddlewares, TServerContext> {
144
139
  _types: RequestMiddlewareTypes<TMiddlewares, TServerContext>;
140
+ options: RequestMiddlewareOptions<TMiddlewares, TServerContext>;
141
+ }
142
+ export interface RequestMiddlewareOptions<in out TMiddlewares, in out TServerContext> {
143
+ middleware?: TMiddlewares;
144
+ server?: RequestServerFn<TMiddlewares, TServerContext>;
145
145
  }
146
146
  export interface RequestMiddlewareTypes<TMiddlewares, TServerContext> {
147
147
  type: 'request';
@@ -1 +1 @@
1
- {"version":3,"file":"createMiddleware.js","sources":["../../src/createMiddleware.ts"],"sourcesContent":["import type {\n ConstrainValidator,\n Method,\n ServerFnResponseType,\n} from './createServerFn'\nimport type {\n AnyRouter,\n Assign,\n Constrain,\n Expand,\n IntersectAssign,\n RegisteredRouter,\n ResolveValidatorInput,\n ResolveValidatorOutput,\n} from '@tanstack/router-core'\nimport type { SerializerStringify } from './serializer'\n\nexport function createMiddleware<TType extends MiddlewareType>(\n options: {\n type: TType\n validateClient?: boolean\n },\n __opts?: FunctionMiddlewareOptions<\n unknown,\n undefined,\n undefined,\n undefined,\n ServerFnResponseType\n >,\n): CreateMiddlewareResult<TType> {\n // const resolvedOptions = (__opts || options) as MiddlewareOptions<\n const resolvedOptions = {\n type: 'function',\n ...(__opts ||\n (options as FunctionMiddlewareOptions<\n unknown,\n undefined,\n undefined,\n undefined,\n ServerFnResponseType\n >)),\n }\n\n return {\n options: resolvedOptions,\n middleware: (middleware: any) => {\n return createMiddleware(\n {} as any,\n Object.assign(resolvedOptions, { middleware }),\n ) as any\n },\n validator: (validator: any) => {\n return createMiddleware(\n {} as any,\n Object.assign(resolvedOptions, { validator }),\n ) as any\n },\n client: (client: any) => {\n return createMiddleware(\n {} as any,\n Object.assign(resolvedOptions, { client }),\n ) as any\n },\n server: (server: any) => {\n return createMiddleware(\n {} as any,\n Object.assign(resolvedOptions, { server }),\n ) as any\n },\n } as unknown as CreateMiddlewareResult<TType>\n}\n\nexport type MiddlewareType = 'request' | 'function'\n\nexport type CreateMiddlewareResult<TType extends MiddlewareType> =\n 'function' extends TType\n ? FunctionMiddleware<ServerFnResponseType>\n : RequestMiddleware\n\nexport interface FunctionMiddleware<\n TServerFnResponseType extends ServerFnResponseType,\n> extends FunctionMiddlewareAfterMiddleware<unknown, TServerFnResponseType> {\n middleware: <const TNewMiddlewares = undefined>(\n middlewares: Constrain<\n TNewMiddlewares,\n ReadonlyArray<AnyFunctionMiddleware>\n >,\n ) => FunctionMiddlewareAfterMiddleware<TNewMiddlewares, TServerFnResponseType>\n}\n\nexport interface FunctionMiddlewareAfterMiddleware<\n TMiddlewares,\n TServerFnResponseType extends ServerFnResponseType,\n> extends FunctionMiddlewareWithTypes<\n TMiddlewares,\n undefined,\n undefined,\n undefined,\n undefined,\n undefined,\n TServerFnResponseType\n >,\n FunctionMiddlewareServer<\n TMiddlewares,\n undefined,\n undefined,\n undefined,\n TServerFnResponseType\n >,\n FunctionMiddlewareClient<TMiddlewares, undefined, TServerFnResponseType>,\n FunctionMiddlewareValidator<TMiddlewares, TServerFnResponseType> {}\n\nexport interface FunctionMiddlewareWithTypes<\n TMiddlewares,\n TValidator,\n TServerContext,\n TServerSendContext,\n TClientContext,\n TClientSendContext,\n TServerFnResponseType extends ServerFnResponseType,\n> {\n _types: FunctionMiddlewareTypes<\n TMiddlewares,\n TValidator,\n TServerContext,\n TServerSendContext,\n TClientContext,\n TClientSendContext\n >\n options: FunctionMiddlewareOptions<\n TMiddlewares,\n TValidator,\n TServerContext,\n TClientContext,\n TServerFnResponseType\n >\n}\n\nexport interface FunctionMiddlewareTypes<\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 type: 'function'\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\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 : TValidator extends undefined\n ? IntersectAllMiddleware<TMiddlewares, 'allInput'>\n : IntersectAssign<\n IntersectAllMiddleware<TMiddlewares, 'allInput'>,\n ResolveValidatorInput<TValidator>\n >\n\nexport type IntersectAllMiddleware<\n TMiddlewares,\n TType extends\n | keyof AnyFunctionMiddleware['_types']\n | keyof AnyRequestMiddleware['_types'],\n TAcc = undefined,\n> = TMiddlewares extends readonly [infer TMiddleware, ...infer TRest]\n ? TMiddleware extends AnyFunctionMiddleware | AnyRequestMiddleware\n ? IntersectAllMiddleware<\n TRest,\n TType,\n IntersectAssign<\n TAcc,\n TMiddleware['_types'][TType & keyof TMiddleware['_types']]\n >\n >\n : TAcc\n : TAcc\n\nexport type AnyFunctionMiddleware = FunctionMiddlewareWithTypes<\n any,\n any,\n any,\n any,\n any,\n any,\n any\n>\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 : TValidator extends undefined\n ? IntersectAllMiddleware<TMiddlewares, 'allOutput'>\n : IntersectAssign<\n IntersectAllMiddleware<TMiddlewares, 'allOutput'>,\n ResolveValidatorOutput<TValidator>\n >\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 AssignAllMiddleware<\n TMiddlewares,\n TType extends\n | keyof AnyFunctionMiddleware['_types']\n | keyof AnyRequestMiddleware['_types'],\n TAcc = undefined,\n> = TMiddlewares extends readonly [infer TMiddleware, ...infer TRest]\n ? TMiddleware extends AnyFunctionMiddleware | AnyRequestMiddleware\n ? AssignAllMiddleware<\n TRest,\n TType,\n Assign<TAcc, TMiddleware['_types'][TType & keyof TMiddleware['_types']]>\n >\n : TAcc\n : TAcc\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\nexport type AssignAllServerSendContext<\n TMiddlewares,\n TSendContext = undefined,\n> = unknown extends TSendContext\n ? TSendContext\n : Assign<\n AssignAllMiddleware<TMiddlewares, 'allServerSendContext'>,\n 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 AssignAllClientSendContext<\n TMiddlewares,\n TSendContext = undefined,\n> = unknown extends TSendContext\n ? TSendContext\n : Assign<\n AssignAllMiddleware<TMiddlewares, 'allClientSendContext'>,\n TSendContext\n >\n\nexport interface FunctionMiddlewareOptions<\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?: FunctionMiddlewareClientFn<\n TMiddlewares,\n TValidator,\n TServerContext,\n TClientContext,\n TServerFnResponseType\n >\n server?: FunctionMiddlewareServerFn<\n TMiddlewares,\n TValidator,\n TServerContext,\n unknown,\n unknown,\n TServerFnResponseType\n >\n}\n\nexport type FunctionMiddlewareClientNextFn<TMiddlewares> = <\n TSendContext = undefined,\n TNewClientContext = undefined,\n>(ctx?: {\n context?: TNewClientContext\n sendContext?: SerializerStringify<TSendContext>\n headers?: HeadersInit\n}) => Promise<\n FunctionClientResultWithContext<TMiddlewares, TSendContext, TNewClientContext>\n>\n\nexport interface FunctionMiddlewareServer<\n TMiddlewares,\n TValidator,\n TServerSendContext,\n TClientContext,\n TServerFnResponseType extends ServerFnResponseType,\n> {\n server: <TNewServerContext = undefined, TSendContext = undefined>(\n server: FunctionMiddlewareServerFn<\n TMiddlewares,\n TValidator,\n TServerSendContext,\n TNewServerContext,\n TSendContext,\n TServerFnResponseType\n >,\n ) => FunctionMiddlewareAfterServer<\n TMiddlewares,\n TValidator,\n TNewServerContext,\n TServerSendContext,\n TClientContext,\n TSendContext,\n ServerFnResponseType\n >\n}\nexport type FunctionMiddlewareServerFn<\n TMiddlewares,\n TValidator,\n TServerSendContext,\n TNewServerContext,\n TSendContext,\n TServerFnResponseType extends ServerFnResponseType,\n> = (\n options: FunctionMiddlewareServerFnOptions<\n TMiddlewares,\n TValidator,\n TServerSendContext,\n TServerFnResponseType\n >,\n) => FunctionMiddlewareServerFnResult<\n TMiddlewares,\n TServerSendContext,\n TNewServerContext,\n TSendContext\n>\n\nexport interface RequestMiddlewareServerFnOptions<\n in out TMiddlewares,\n in out TServerSendContext,\n> {\n request: Request\n context: Expand<AssignAllServerContext<TMiddlewares, TServerSendContext>>\n next: FunctionMiddlewareServerNextFn<TMiddlewares, TServerSendContext>\n response: Response\n method: Method\n signal: AbortSignal\n}\n\nexport type FunctionMiddlewareServerNextFn<TMiddlewares, TServerSendContext> = <\n TNewServerContext = undefined,\n TSendContext = undefined,\n>(ctx?: {\n context?: TNewServerContext\n sendContext?: SerializerStringify<TSendContext>\n}) => Promise<\n FunctionServerResultWithContext<\n TMiddlewares,\n TServerSendContext,\n TNewServerContext,\n TSendContext\n >\n>\n\nexport type FunctionServerResultWithContext<\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 interface FunctionMiddlewareServerFnOptions<\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: FunctionMiddlewareServerNextFn<TMiddlewares, TServerSendContext>\n response: TServerFnResponseType\n method: Method\n filename: string\n functionId: string\n signal: AbortSignal\n}\n\nexport type FunctionMiddlewareServerFnResult<\n TMiddlewares,\n TServerSendContext,\n TServerContext,\n TSendContext,\n> =\n | Promise<\n FunctionServerResultWithContext<\n TMiddlewares,\n TServerSendContext,\n TServerContext,\n TSendContext\n >\n >\n | FunctionServerResultWithContext<\n TMiddlewares,\n TServerSendContext,\n TServerContext,\n TSendContext\n >\n\nexport interface FunctionMiddlewareAfterServer<\n TMiddlewares,\n TValidator,\n TServerContext,\n TServerSendContext,\n TClientContext,\n TClientSendContext,\n TServerFnResponseType extends ServerFnResponseType,\n> extends FunctionMiddlewareWithTypes<\n TMiddlewares,\n TValidator,\n TServerContext,\n TServerSendContext,\n TClientContext,\n TClientSendContext,\n TServerFnResponseType\n > {}\n\nexport interface FunctionMiddlewareClient<\n TMiddlewares,\n TValidator,\n TServerFnResponseType extends ServerFnResponseType,\n TRouter extends AnyRouter = RegisteredRouter,\n> {\n client: <TSendServerContext = undefined, TNewClientContext = undefined>(\n client: FunctionMiddlewareClientFn<\n TMiddlewares,\n TValidator,\n TSendServerContext,\n TNewClientContext,\n TServerFnResponseType,\n TRouter\n >,\n ) => FunctionMiddlewareAfterClient<\n TMiddlewares,\n TValidator,\n TSendServerContext,\n TNewClientContext,\n ServerFnResponseType\n >\n}\n\nexport type FunctionMiddlewareClientFn<\n TMiddlewares,\n TValidator,\n TSendContext,\n TClientContext,\n TServerFnResponseType extends ServerFnResponseType,\n TRouter extends AnyRouter = RegisteredRouter,\n> = (\n options: FunctionMiddlewareClientFnOptions<\n TMiddlewares,\n TValidator,\n TServerFnResponseType,\n TRouter\n >,\n) => FunctionMiddlewareClientFnResult<\n TMiddlewares,\n TSendContext,\n TClientContext\n>\n\nexport interface FunctionMiddlewareClientFnOptions<\n in out TMiddlewares,\n in out TValidator,\n in out TServerFnResponseType extends ServerFnResponseType,\n in out TRouter extends AnyRouter,\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: FunctionMiddlewareClientNextFn<TMiddlewares>\n filename: string\n functionId: string\n router: TRouter\n}\n\nexport type FunctionMiddlewareClientFnResult<\n TMiddlewares,\n TSendContext,\n TClientContext,\n> =\n | Promise<\n FunctionClientResultWithContext<\n TMiddlewares,\n TSendContext,\n TClientContext\n >\n >\n | FunctionClientResultWithContext<TMiddlewares, TSendContext, TClientContext>\n\nexport type FunctionClientResultWithContext<\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 interface FunctionMiddlewareAfterClient<\n TMiddlewares,\n TValidator,\n TServerSendContext,\n TClientContext,\n TServerFnResponseType extends ServerFnResponseType,\n> extends FunctionMiddlewareWithTypes<\n TMiddlewares,\n TValidator,\n undefined,\n TServerSendContext,\n TClientContext,\n undefined,\n TServerFnResponseType\n >,\n FunctionMiddlewareServer<\n TMiddlewares,\n TValidator,\n TServerSendContext,\n TClientContext,\n TServerFnResponseType\n > {}\n\nexport interface FunctionMiddlewareValidator<\n TMiddlewares,\n TServerFnResponseType extends ServerFnResponseType,\n> {\n validator: <TNewValidator>(\n input: ConstrainValidator<TNewValidator>,\n ) => FunctionMiddlewareAfterValidator<\n TMiddlewares,\n TNewValidator,\n TServerFnResponseType\n >\n}\n\nexport interface FunctionMiddlewareAfterValidator<\n TMiddlewares,\n TValidator,\n TServerFnResponseType extends ServerFnResponseType,\n> extends FunctionMiddlewareWithTypes<\n TMiddlewares,\n TValidator,\n undefined,\n undefined,\n undefined,\n undefined,\n ServerFnResponseType\n >,\n FunctionMiddlewareServer<\n TMiddlewares,\n TValidator,\n undefined,\n undefined,\n TServerFnResponseType\n >,\n FunctionMiddlewareClient<TMiddlewares, TValidator, ServerFnResponseType> {}\n\nexport interface RequestMiddleware\n extends RequestMiddlewareAfterMiddleware<undefined> {\n middleware: <const TMiddlewares = undefined>(\n middlewares: Constrain<TMiddlewares, ReadonlyArray<AnyRequestMiddleware>>,\n ) => RequestMiddlewareAfterMiddleware<TMiddlewares>\n}\n\nexport type AnyRequestMiddleware = RequestMiddlewareWithTypes<any, any>\n\nexport interface RequestMiddlewareWithTypes<TMiddlewares, TServerContext> {\n _types: RequestMiddlewareTypes<TMiddlewares, TServerContext>\n}\n\nexport interface RequestMiddlewareTypes<TMiddlewares, TServerContext> {\n type: 'request'\n middlewares: TMiddlewares\n serverContext: TServerContext\n allServerContext: AssignAllServerContext<\n TMiddlewares,\n undefined,\n TServerContext\n >\n}\n\nexport interface RequestMiddlewareAfterMiddleware<TMiddlewares>\n extends RequestMiddlewareWithTypes<TMiddlewares, undefined>,\n RequestMiddlewareServer<TMiddlewares> {}\n\nexport interface RequestMiddlewareServer<TMiddlewares> {\n server: <TServerContext = undefined>(\n fn: RequestServerFn<TMiddlewares, TServerContext>,\n ) => RequestMiddlewareAfterServer<TMiddlewares, TServerContext>\n}\n\nexport type RequestServerFn<TMiddlewares, TServerContext> = (\n options: RequestServerOptions<TMiddlewares>,\n) => RequestMiddlewareServerFnResult<TMiddlewares, TServerContext>\n\nexport interface RequestServerOptions<TMiddlewares> {\n request: Request\n pathname: string\n context: AssignAllServerContext<TMiddlewares>\n next: RequestServerNextFn<TMiddlewares>\n}\n\nexport type RequestServerNextFn<TMiddlewares> = <TServerContext = undefined>(\n options?: RequestServerNextFnOptions<TServerContext>,\n) => RequestMiddlewareServerFnResult<TMiddlewares, TServerContext>\n\nexport interface RequestServerNextFnOptions<TServerContext> {\n context?: TServerContext\n}\n\nexport type RequestMiddlewareServerFnResult<TMiddlewares, TServerContext> =\n | Promise<RequestServerResult<TMiddlewares, TServerContext>>\n | RequestServerResult<TMiddlewares, TServerContext>\n\nexport interface RequestServerResult<TMiddlewares, TServerContext> {\n request: Request\n pathname: string\n context: Expand<\n AssignAllServerContext<TMiddlewares, undefined, TServerContext>\n >\n response: Response\n}\n\nexport interface RequestMiddlewareAfterServer<TMiddlewares, TServerContext>\n extends RequestMiddlewareWithTypes<TMiddlewares, TServerContext> {}\n"],"names":[],"mappings":"AAiBO,SAAS,iBACd,SAIA,QAO+B;AAE/B,QAAM,kBAAkB;AAAA,IACtB,MAAM;AAAA,IACN,GAAI,UACD;AAAA,EAAA;AASL,SAAO;AAAA,IACL,SAAS;AAAA,IACT,YAAY,CAAC,eAAoB;AAC/B,aAAO;AAAA,QACL,CAAA;AAAA,QACA,OAAO,OAAO,iBAAiB,EAAE,YAAY;AAAA,MAAA;AAAA,IAEjD;AAAA,IACA,WAAW,CAAC,cAAmB;AAC7B,aAAO;AAAA,QACL,CAAA;AAAA,QACA,OAAO,OAAO,iBAAiB,EAAE,WAAW;AAAA,MAAA;AAAA,IAEhD;AAAA,IACA,QAAQ,CAAC,WAAgB;AACvB,aAAO;AAAA,QACL,CAAA;AAAA,QACA,OAAO,OAAO,iBAAiB,EAAE,QAAQ;AAAA,MAAA;AAAA,IAE7C;AAAA,IACA,QAAQ,CAAC,WAAgB;AACvB,aAAO;AAAA,QACL,CAAA;AAAA,QACA,OAAO,OAAO,iBAAiB,EAAE,QAAQ;AAAA,MAAA;AAAA,IAE7C;AAAA,EAAA;AAEJ;"}
1
+ {"version":3,"file":"createMiddleware.js","sources":["../../src/createMiddleware.ts"],"sourcesContent":["import type { AnyServerFn, ConstrainValidator, Method } from './createServerFn'\nimport type {\n AnyRouter,\n Assign,\n Constrain,\n Expand,\n IntersectAssign,\n Register,\n RegisteredRouter,\n ResolveValidatorInput,\n ResolveValidatorOutput,\n ValidateSerializableInput,\n} from '@tanstack/router-core'\n\nexport function createMiddleware<\n TRegister extends Register,\n TType extends MiddlewareType,\n>(\n options: {\n type: TType\n },\n __opts?: FunctionMiddlewareOptions<\n TRegister,\n unknown,\n undefined,\n undefined,\n undefined\n >,\n): CreateMiddlewareResult<TRegister, TType> {\n // const resolvedOptions = (__opts || options) as MiddlewareOptions<\n const resolvedOptions = {\n type: 'function',\n ...(__opts ||\n (options as FunctionMiddlewareOptions<\n TRegister,\n unknown,\n undefined,\n undefined,\n undefined\n >)),\n }\n\n return {\n options: resolvedOptions,\n middleware: (middleware: any) => {\n return createMiddleware(\n {} as any,\n Object.assign(resolvedOptions, { middleware }),\n ) as any\n },\n validator: (validator: any) => {\n return createMiddleware(\n {} as any,\n Object.assign(resolvedOptions, { validator }),\n ) as any\n },\n client: (client: any) => {\n return createMiddleware(\n {} as any,\n Object.assign(resolvedOptions, { client }),\n ) as any\n },\n server: (server: any) => {\n return createMiddleware(\n {} as any,\n Object.assign(resolvedOptions, { server }),\n ) as any\n },\n } as unknown as CreateMiddlewareResult<TRegister, TType>\n}\n\nexport type MiddlewareType = 'request' | 'function'\n\nexport type CreateMiddlewareResult<\n TRegister extends Register,\n TType extends MiddlewareType,\n> = 'function' extends TType ? FunctionMiddleware<TRegister> : RequestMiddleware\n\nexport interface FunctionMiddleware<TRegister extends Register>\n extends FunctionMiddlewareAfterMiddleware<TRegister, unknown> {\n middleware: <const TNewMiddlewares = undefined>(\n middlewares: Constrain<\n TNewMiddlewares,\n ReadonlyArray<AnyFunctionMiddleware>\n >,\n ) => FunctionMiddlewareAfterMiddleware<TRegister, TNewMiddlewares>\n}\n\nexport interface FunctionMiddlewareAfterMiddleware<\n TRegister extends Register,\n TMiddlewares,\n> extends FunctionMiddlewareWithTypes<\n TRegister,\n TMiddlewares,\n undefined,\n undefined,\n undefined,\n undefined,\n undefined\n >,\n FunctionMiddlewareServer<\n TRegister,\n TMiddlewares,\n undefined,\n undefined,\n undefined\n >,\n FunctionMiddlewareClient<TRegister, TMiddlewares, undefined>,\n FunctionMiddlewareValidator<TRegister, TMiddlewares> {}\n\nexport interface FunctionMiddlewareWithTypes<\n TRegister extends Register,\n TMiddlewares,\n TValidator,\n TServerContext,\n TServerSendContext,\n TClientContext,\n TClientSendContext,\n> {\n _types: FunctionMiddlewareTypes<\n TMiddlewares,\n TValidator,\n TServerContext,\n TServerSendContext,\n TClientContext,\n TClientSendContext\n >\n options: FunctionMiddlewareOptions<\n TRegister,\n TMiddlewares,\n TValidator,\n TServerContext,\n TClientContext\n >\n}\n\nexport interface FunctionMiddlewareTypes<\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 type: 'function'\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\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 : TValidator extends undefined\n ? IntersectAllMiddleware<TMiddlewares, 'allInput'>\n : IntersectAssign<\n IntersectAllMiddleware<TMiddlewares, 'allInput'>,\n ResolveValidatorInput<TValidator>\n >\n\nexport type IntersectAllMiddleware<\n TMiddlewares,\n TType extends\n | keyof AnyFunctionMiddleware['_types']\n | keyof AnyRequestMiddleware['_types']\n | keyof AnyServerFn['_types'],\n TAcc = undefined,\n> = TMiddlewares extends readonly [infer TMiddleware, ...infer TRest]\n ? TMiddleware extends\n | AnyFunctionMiddleware\n | AnyRequestMiddleware\n | AnyServerFn\n ? IntersectAllMiddleware<\n TRest,\n TType,\n IntersectAssign<\n TAcc,\n TMiddleware['_types'][TType & keyof TMiddleware['_types']]\n >\n >\n : TAcc\n : TAcc\n\nexport type AnyFunctionMiddleware = FunctionMiddlewareWithTypes<\n any,\n any,\n any,\n any,\n any,\n any,\n any\n>\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 : TValidator extends undefined\n ? IntersectAllMiddleware<TMiddlewares, 'allOutput'>\n : IntersectAssign<\n IntersectAllMiddleware<TMiddlewares, 'allOutput'>,\n ResolveValidatorOutput<TValidator>\n >\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 AssignAllMiddleware<\n TMiddlewares,\n TType extends\n | keyof AnyFunctionMiddleware['_types']\n | keyof AnyRequestMiddleware['_types']\n | keyof AnyServerFn['_types'],\n TAcc = undefined,\n> = TMiddlewares extends readonly [infer TMiddleware, ...infer TRest]\n ? TMiddleware extends\n | AnyFunctionMiddleware\n | AnyRequestMiddleware\n | AnyServerFn\n ? AssignAllMiddleware<\n TRest,\n TType,\n Assign<TAcc, TMiddleware['_types'][TType & keyof TMiddleware['_types']]>\n >\n : TAcc\n : TAcc\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\nexport type AssignAllServerSendContext<\n TMiddlewares,\n TSendContext = undefined,\n> = unknown extends TSendContext\n ? TSendContext\n : Assign<\n AssignAllMiddleware<TMiddlewares, 'allServerSendContext'>,\n 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 AssignAllClientSendContext<\n TMiddlewares,\n TSendContext = undefined,\n> = unknown extends TSendContext\n ? TSendContext\n : Assign<\n AssignAllMiddleware<TMiddlewares, 'allClientSendContext'>,\n TSendContext\n >\n\nexport interface FunctionMiddlewareOptions<\n in out TRegister extends Register,\n in out TMiddlewares,\n in out TValidator,\n in out TServerContext,\n in out TClientContext,\n> {\n middleware?: TMiddlewares\n validator?: ConstrainValidator<TRegister, 'GET', TValidator>\n client?: FunctionMiddlewareClientFn<\n TRegister,\n TMiddlewares,\n TValidator,\n TServerContext,\n TClientContext\n >\n server?: FunctionMiddlewareServerFn<\n TRegister,\n TMiddlewares,\n TValidator,\n TServerContext,\n unknown,\n unknown\n >\n}\n\nexport type FunctionMiddlewareClientNextFn<\n TRegister extends Register,\n TMiddlewares,\n> = <TSendContext = undefined, TNewClientContext = undefined>(ctx?: {\n context?: TNewClientContext\n sendContext?: ValidateSerializableInput<TRegister, TSendContext>\n headers?: HeadersInit\n}) => Promise<\n FunctionClientResultWithContext<TMiddlewares, TSendContext, TNewClientContext>\n>\n\nexport interface FunctionMiddlewareServer<\n TRegister extends Register,\n TMiddlewares,\n TValidator,\n TServerSendContext,\n TClientContext,\n> {\n server: <TNewServerContext = undefined, TSendContext = undefined>(\n server: FunctionMiddlewareServerFn<\n TRegister,\n TMiddlewares,\n TValidator,\n TServerSendContext,\n TNewServerContext,\n TSendContext\n >,\n ) => FunctionMiddlewareAfterServer<\n TRegister,\n TMiddlewares,\n TValidator,\n TNewServerContext,\n TServerSendContext,\n TClientContext,\n TSendContext\n >\n}\n\nexport type FunctionMiddlewareServerFn<\n TRegister extends Register,\n TMiddlewares,\n TValidator,\n TServerSendContext,\n TNewServerContext,\n TSendContext,\n> = (\n options: FunctionMiddlewareServerFnOptions<\n TRegister,\n TMiddlewares,\n TValidator,\n TServerSendContext\n >,\n) => FunctionMiddlewareServerFnResult<\n TMiddlewares,\n TServerSendContext,\n TNewServerContext,\n TSendContext\n>\n\nexport interface RequestMiddlewareServerFnOptions<\n in out TRegister extends Register,\n in out TMiddlewares,\n in out TServerSendContext,\n> {\n request: Request\n context: Expand<AssignAllServerContext<TMiddlewares, TServerSendContext>>\n next: FunctionMiddlewareServerNextFn<\n TRegister,\n TMiddlewares,\n TServerSendContext\n >\n response: Response\n method: Method\n signal: AbortSignal\n}\n\nexport type FunctionMiddlewareServerNextFn<\n TRegister extends Register,\n TMiddlewares,\n TServerSendContext,\n> = <TNewServerContext = undefined, TSendContext = undefined>(ctx?: {\n context?: TNewServerContext\n sendContext?: ValidateSerializableInput<TRegister, TSendContext>\n}) => Promise<\n FunctionServerResultWithContext<\n TMiddlewares,\n TServerSendContext,\n TNewServerContext,\n TSendContext\n >\n>\n\nexport type FunctionServerResultWithContext<\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 interface FunctionMiddlewareServerFnOptions<\n in out TRegister extends Register,\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: FunctionMiddlewareServerNextFn<\n TRegister,\n TMiddlewares,\n TServerSendContext\n >\n method: Method\n filename: string\n functionId: string\n signal: AbortSignal\n}\n\nexport type FunctionMiddlewareServerFnResult<\n TMiddlewares,\n TServerSendContext,\n TServerContext,\n TSendContext,\n> =\n | Promise<\n FunctionServerResultWithContext<\n TMiddlewares,\n TServerSendContext,\n TServerContext,\n TSendContext\n >\n >\n | FunctionServerResultWithContext<\n TMiddlewares,\n TServerSendContext,\n TServerContext,\n TSendContext\n >\n\nexport interface FunctionMiddlewareAfterServer<\n TRegister extends Register,\n TMiddlewares,\n TValidator,\n TServerContext,\n TServerSendContext,\n TClientContext,\n TClientSendContext,\n> extends FunctionMiddlewareWithTypes<\n TRegister,\n TMiddlewares,\n TValidator,\n TServerContext,\n TServerSendContext,\n TClientContext,\n TClientSendContext\n > {}\n\nexport interface FunctionMiddlewareClient<\n TRegister extends Register,\n TMiddlewares,\n TValidator,\n TRouter extends AnyRouter = RegisteredRouter,\n> {\n client: <TSendServerContext = undefined, TNewClientContext = undefined>(\n client: FunctionMiddlewareClientFn<\n TRegister,\n TMiddlewares,\n TValidator,\n TSendServerContext,\n TNewClientContext,\n TRouter\n >,\n ) => FunctionMiddlewareAfterClient<\n TRegister,\n TMiddlewares,\n TValidator,\n TSendServerContext,\n TNewClientContext\n >\n}\n\nexport type FunctionMiddlewareClientFn<\n TRegister extends Register,\n TMiddlewares,\n TValidator,\n TSendContext,\n TClientContext,\n TRouter extends AnyRouter = RegisteredRouter,\n> = (\n options: FunctionMiddlewareClientFnOptions<\n TRegister,\n TMiddlewares,\n TValidator,\n TRouter\n >,\n) => FunctionMiddlewareClientFnResult<\n TMiddlewares,\n TSendContext,\n TClientContext\n>\n\nexport interface FunctionMiddlewareClientFnOptions<\n in out TRegister extends Register,\n in out TMiddlewares,\n in out TValidator,\n in out TRouter extends AnyRouter,\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: FunctionMiddlewareClientNextFn<TRegister, TMiddlewares>\n filename: string\n functionId: string\n router: TRouter\n}\n\nexport type FunctionMiddlewareClientFnResult<\n TMiddlewares,\n TSendContext,\n TClientContext,\n> =\n | Promise<\n FunctionClientResultWithContext<\n TMiddlewares,\n TSendContext,\n TClientContext\n >\n >\n | FunctionClientResultWithContext<TMiddlewares, TSendContext, TClientContext>\n\nexport type FunctionClientResultWithContext<\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 interface FunctionMiddlewareAfterClient<\n TRegister extends Register,\n TMiddlewares,\n TValidator,\n TServerSendContext,\n TClientContext,\n> extends FunctionMiddlewareWithTypes<\n TRegister,\n TMiddlewares,\n TValidator,\n undefined,\n TServerSendContext,\n TClientContext,\n undefined\n >,\n FunctionMiddlewareServer<\n TRegister,\n TMiddlewares,\n TValidator,\n TServerSendContext,\n TClientContext\n > {}\n\nexport interface FunctionMiddlewareValidator<\n TRegister extends Register,\n TMiddlewares,\n> {\n validator: <TNewValidator>(\n input: ConstrainValidator<TRegister, 'GET', TNewValidator>,\n ) => FunctionMiddlewareAfterValidator<TRegister, TMiddlewares, TNewValidator>\n}\n\nexport interface FunctionMiddlewareAfterValidator<\n TRegister extends Register,\n TMiddlewares,\n TValidator,\n> extends FunctionMiddlewareWithTypes<\n TRegister,\n TMiddlewares,\n TValidator,\n undefined,\n undefined,\n undefined,\n undefined\n >,\n FunctionMiddlewareServer<\n TRegister,\n TMiddlewares,\n TValidator,\n undefined,\n undefined\n >,\n FunctionMiddlewareClient<TRegister, TMiddlewares, TValidator> {}\n\nexport interface RequestMiddleware\n extends RequestMiddlewareAfterMiddleware<undefined> {\n middleware: <const TMiddlewares = undefined>(\n middlewares: Constrain<TMiddlewares, ReadonlyArray<AnyRequestMiddleware>>,\n ) => RequestMiddlewareAfterMiddleware<TMiddlewares>\n}\n\nexport type AnyRequestMiddleware = RequestMiddlewareWithTypes<any, any>\n\nexport interface RequestMiddlewareWithTypes<TMiddlewares, TServerContext> {\n _types: RequestMiddlewareTypes<TMiddlewares, TServerContext>\n options: RequestMiddlewareOptions<TMiddlewares, TServerContext>\n}\n\nexport interface RequestMiddlewareOptions<\n in out TMiddlewares,\n in out TServerContext,\n> {\n middleware?: TMiddlewares\n server?: RequestServerFn<TMiddlewares, TServerContext>\n}\nexport interface RequestMiddlewareTypes<TMiddlewares, TServerContext> {\n type: 'request'\n middlewares: TMiddlewares\n serverContext: TServerContext\n allServerContext: AssignAllServerContext<\n TMiddlewares,\n undefined,\n TServerContext\n >\n}\n\nexport interface RequestMiddlewareAfterMiddleware<TMiddlewares>\n extends RequestMiddlewareWithTypes<TMiddlewares, undefined>,\n RequestMiddlewareServer<TMiddlewares> {}\n\nexport interface RequestMiddlewareServer<TMiddlewares> {\n server: <TServerContext = undefined>(\n fn: RequestServerFn<TMiddlewares, TServerContext>,\n ) => RequestMiddlewareAfterServer<TMiddlewares, TServerContext>\n}\n\nexport type RequestServerFn<TMiddlewares, TServerContext> = (\n options: RequestServerOptions<TMiddlewares>,\n) => RequestMiddlewareServerFnResult<TMiddlewares, TServerContext>\n\nexport interface RequestServerOptions<TMiddlewares> {\n request: Request\n pathname: string\n context: AssignAllServerContext<TMiddlewares>\n next: RequestServerNextFn<TMiddlewares>\n}\n\nexport type RequestServerNextFn<TMiddlewares> = <TServerContext = undefined>(\n options?: RequestServerNextFnOptions<TServerContext>,\n) => RequestMiddlewareServerFnResult<TMiddlewares, TServerContext>\n\nexport interface RequestServerNextFnOptions<TServerContext> {\n context?: TServerContext\n}\n\nexport type RequestMiddlewareServerFnResult<TMiddlewares, TServerContext> =\n | Promise<RequestServerResult<TMiddlewares, TServerContext>>\n | RequestServerResult<TMiddlewares, TServerContext>\n\nexport interface RequestServerResult<TMiddlewares, TServerContext> {\n request: Request\n pathname: string\n context: Expand<\n AssignAllServerContext<TMiddlewares, undefined, TServerContext>\n >\n response: Response\n}\n\nexport interface RequestMiddlewareAfterServer<TMiddlewares, TServerContext>\n extends RequestMiddlewareWithTypes<TMiddlewares, TServerContext> {}\n"],"names":[],"mappings":"AAcO,SAAS,iBAId,SAGA,QAO0C;AAE1C,QAAM,kBAAkB;AAAA,IACtB,MAAM;AAAA,IACN,GAAI,UACD;AAAA,EAAA;AASL,SAAO;AAAA,IACL,SAAS;AAAA,IACT,YAAY,CAAC,eAAoB;AAC/B,aAAO;AAAA,QACL,CAAA;AAAA,QACA,OAAO,OAAO,iBAAiB,EAAE,YAAY;AAAA,MAAA;AAAA,IAEjD;AAAA,IACA,WAAW,CAAC,cAAmB;AAC7B,aAAO;AAAA,QACL,CAAA;AAAA,QACA,OAAO,OAAO,iBAAiB,EAAE,WAAW;AAAA,MAAA;AAAA,IAEhD;AAAA,IACA,QAAQ,CAAC,WAAgB;AACvB,aAAO;AAAA,QACL,CAAA;AAAA,QACA,OAAO,OAAO,iBAAiB,EAAE,QAAQ;AAAA,MAAA;AAAA,IAE7C;AAAA,IACA,QAAQ,CAAC,WAAgB;AACvB,aAAO;AAAA,QACL,CAAA;AAAA,QACA,OAAO,OAAO,iBAAiB,EAAE,QAAQ;AAAA,MAAA;AAAA,IAE7C;AAAA,EAAA;AAEJ;"}
@@ -1,39 +1,34 @@
1
- import { SerializerParse, SerializerStringify, SerializerStringifyBy } from './serializer.js';
2
- import { AnyRouter, AnyValidator, Constrain, Expand, ResolveValidatorInput, Validator } from '@tanstack/router-core';
1
+ import { TSS_SERVER_FUNCTION_FACTORY } from './constants.js';
2
+ import { AnyRouter, AnyValidator, Constrain, Expand, Register, RegisteredSerializableInput, ResolveValidatorInput, ValidateSerializable, ValidateSerializableInput, ValidateSerializableInputResult, Validator } from '@tanstack/router-core';
3
3
  import { JsonResponse } from '@tanstack/router-core/ssr/client';
4
- import { Readable } from 'node:stream';
5
- import { AnyFunctionMiddleware, AssignAllClientSendContext, AssignAllServerContext, IntersectAllValidatorInputs, IntersectAllValidatorOutputs } from './createMiddleware.js';
6
- export declare function createServerFn<TMethod extends Method, TServerFnResponseType extends ServerFnResponseType = 'data', TResponse = unknown, TMiddlewares = undefined, TValidator = undefined>(options?: {
4
+ import { AnyFunctionMiddleware, AnyRequestMiddleware, AssignAllServerContext, IntersectAllValidatorInputs, IntersectAllValidatorOutputs } from './createMiddleware.js';
5
+ export declare function createServerFn<TRegister extends Register, TMethod extends Method, TResponse = unknown, TMiddlewares = undefined, TValidator = undefined>(options?: {
7
6
  method?: TMethod;
8
- response?: TServerFnResponseType;
9
- }, __opts?: ServerFnBaseOptions<TMethod, TServerFnResponseType, TResponse, TMiddlewares, TValidator>): ServerFnBuilder<TMethod, TServerFnResponseType>;
10
- export declare function executeMiddleware(middlewares: Array<AnyFunctionMiddleware>, env: 'client' | 'server', opts: ServerFnMiddlewareOptions): Promise<ServerFnMiddlewareResult>;
7
+ }, __opts?: ServerFnBaseOptions<TRegister, TMethod, TResponse, TMiddlewares, TValidator>): ServerFnBuilder<TRegister, TMethod>;
8
+ export declare function executeMiddleware(middlewares: Array<AnyFunctionMiddleware | AnyRequestMiddleware>, env: 'client' | 'server', opts: ServerFnMiddlewareOptions): Promise<ServerFnMiddlewareResult>;
11
9
  export type CompiledFetcherFnOptions = {
12
10
  method: Method;
13
11
  data: unknown;
14
- response?: ServerFnResponseType;
15
12
  headers?: HeadersInit;
16
13
  signal?: AbortSignal;
17
14
  context?: any;
18
15
  };
19
- export type Fetcher<TMiddlewares, TValidator, TResponse, TServerFnResponseType extends ServerFnResponseType> = undefined extends IntersectAllValidatorInputs<TMiddlewares, TValidator> ? OptionalFetcher<TMiddlewares, TValidator, TResponse, TServerFnResponseType> : RequiredFetcher<TMiddlewares, TValidator, TResponse, TServerFnResponseType>;
16
+ export type Fetcher<TRegister extends Register, TMiddlewares, TValidator, TResponse> = undefined extends IntersectAllValidatorInputs<TMiddlewares, TValidator> ? OptionalFetcher<TRegister, TMiddlewares, TValidator, TResponse> : RequiredFetcher<TRegister, TMiddlewares, TValidator, TResponse>;
20
17
  export interface FetcherBase {
21
18
  url: string;
22
19
  __executeServer: (opts: {
23
20
  method: Method;
24
- response?: ServerFnResponseType;
25
21
  data: unknown;
26
22
  headers?: HeadersInit;
27
23
  context?: any;
28
24
  signal: AbortSignal;
29
25
  }) => Promise<unknown>;
30
26
  }
31
- export type FetchResult<TMiddlewares, TResponse, TServerFnResponseType extends ServerFnResponseType> = TServerFnResponseType extends 'raw' ? Promise<Response> : TServerFnResponseType extends 'full' ? Promise<FullFetcherData<TMiddlewares, TResponse>> : Promise<FetcherData<TResponse>>;
32
- export interface OptionalFetcher<TMiddlewares, TValidator, TResponse, TServerFnResponseType extends ServerFnResponseType> extends FetcherBase {
33
- (options?: OptionalFetcherDataOptions<TMiddlewares, TValidator>): FetchResult<TMiddlewares, TResponse, TServerFnResponseType>;
27
+ export interface OptionalFetcher<TRegister extends Register, TMiddlewares, TValidator, TResponse> extends FetcherBase {
28
+ (options?: OptionalFetcherDataOptions<TMiddlewares, TValidator>): Promise<FetcherData<TRegister, TResponse>>;
34
29
  }
35
- export interface RequiredFetcher<TMiddlewares, TValidator, TResponse, TServerFnResponseType extends ServerFnResponseType> extends FetcherBase {
36
- (opts: RequiredFetcherDataOptions<TMiddlewares, TValidator>): FetchResult<TMiddlewares, TResponse, TServerFnResponseType>;
30
+ export interface RequiredFetcher<TRegister extends Register, TMiddlewares, TValidator, TResponse> extends FetcherBase {
31
+ (opts: RequiredFetcherDataOptions<TMiddlewares, TValidator>): Promise<FetcherData<TRegister, TResponse>>;
37
32
  }
38
33
  export type FetcherBaseOptions = {
39
34
  headers?: HeadersInit;
@@ -45,74 +40,75 @@ export interface OptionalFetcherDataOptions<TMiddlewares, TValidator> extends Fe
45
40
  export interface RequiredFetcherDataOptions<TMiddlewares, TValidator> extends FetcherBaseOptions {
46
41
  data: Expand<IntersectAllValidatorInputs<TMiddlewares, TValidator>>;
47
42
  }
48
- export interface FullFetcherData<TMiddlewares, TResponse> {
49
- error: unknown;
50
- result: FetcherData<TResponse>;
51
- context: AssignAllClientSendContext<TMiddlewares>;
52
- }
53
- export type FetcherData<TResponse> = TResponse extends JsonResponse<any> ? SerializerParse<ReturnType<TResponse['json']>> : SerializerParse<TResponse>;
43
+ export type FetcherData<TRegister extends Register, TResponse> = TResponse extends Response ? Response : TResponse extends JsonResponse<any> ? ValidateSerializableInputResult<TRegister, ReturnType<TResponse['json']>> : ValidateSerializableInputResult<TRegister, TResponse>;
54
44
  export type RscStream<T> = {
55
45
  __cacheState: T;
56
46
  };
57
47
  export type Method = 'GET' | 'POST';
58
- export type ServerFnResponseType = 'data' | 'full' | 'raw';
59
- export type RawResponse = Response | ReadableStream | Readable | null | string;
60
- export type ServerFnReturnType<TServerFnResponseType extends ServerFnResponseType, TResponse> = TServerFnResponseType extends 'raw' ? RawResponse | Promise<RawResponse> : Promise<SerializerStringify<TResponse>> | SerializerStringify<TResponse>;
61
- export type ServerFn<TMethod, TServerFnResponseType extends ServerFnResponseType, TMiddlewares, TValidator, TResponse> = (ctx: ServerFnCtx<TMethod, TServerFnResponseType, TMiddlewares, TValidator>) => ServerFnReturnType<TServerFnResponseType, TResponse>;
62
- export interface ServerFnCtx<TMethod, TServerFnResponseType extends ServerFnResponseType, TMiddlewares, TValidator> {
48
+ export type ServerFnReturnType<TRegister extends Register, TResponse> = Response | Promise<ValidateSerializableInput<TRegister, TResponse>> | ValidateSerializableInput<TRegister, TResponse>;
49
+ export type ServerFn<TRegister extends Register, TMethod, TMiddlewares, TValidator, TResponse> = (ctx: ServerFnCtx<TMethod, TMiddlewares, TValidator>) => ServerFnReturnType<TRegister, TResponse>;
50
+ export interface ServerFnCtx<TMethod, TMiddlewares, TValidator> {
63
51
  method: TMethod;
64
- response: TServerFnResponseType;
65
52
  data: Expand<IntersectAllValidatorOutputs<TMiddlewares, TValidator>>;
66
53
  context: Expand<AssignAllServerContext<TMiddlewares>>;
67
54
  signal: AbortSignal;
68
55
  }
69
- export type CompiledFetcherFn<TResponse, TServerFnResponseType extends ServerFnResponseType> = {
70
- (opts: CompiledFetcherFnOptions & ServerFnBaseOptions<Method, TServerFnResponseType>): Promise<TResponse>;
56
+ export type CompiledFetcherFn<TRegister extends Register, TResponse> = {
57
+ (opts: CompiledFetcherFnOptions & ServerFnBaseOptions<TRegister, Method>): Promise<TResponse>;
71
58
  url: string;
72
59
  };
73
- export type ServerFnBaseOptions<TMethod extends Method = 'GET', TServerFnResponseType extends ServerFnResponseType = 'data', TResponse = unknown, TMiddlewares = unknown, TInput = unknown> = {
60
+ export type ServerFnBaseOptions<TRegister extends Register, TMethod extends Method = 'GET', TResponse = unknown, TMiddlewares = unknown, TInput = unknown> = {
74
61
  method: TMethod;
75
- response?: TServerFnResponseType;
76
- validateClient?: boolean;
77
- middleware?: Constrain<TMiddlewares, ReadonlyArray<AnyFunctionMiddleware>>;
78
- validator?: ConstrainValidator<TInput>;
79
- extractedFn?: CompiledFetcherFn<TResponse, TServerFnResponseType>;
80
- serverFn?: ServerFn<TMethod, TServerFnResponseType, TMiddlewares, TInput, TResponse>;
62
+ middleware?: Constrain<TMiddlewares, ReadonlyArray<AnyFunctionMiddleware | AnyRequestMiddleware>>;
63
+ validator?: ConstrainValidator<TRegister, TMethod, TInput>;
64
+ extractedFn?: CompiledFetcherFn<TRegister, TResponse>;
65
+ serverFn?: ServerFn<TRegister, TMethod, TMiddlewares, TInput, TResponse>;
81
66
  functionId: string;
82
67
  };
83
- export type ValidatorInputStringify<TValidator> = SerializerStringifyBy<ResolveValidatorInput<TValidator>, Date | undefined | FormData>;
84
- export type ValidatorSerializerStringify<TValidator> = ValidatorInputStringify<TValidator> extends infer TInput ? Validator<TInput, any> : never;
85
- export type ConstrainValidator<TValidator> = (unknown extends TValidator ? TValidator : ResolveValidatorInput<TValidator> extends ValidatorInputStringify<TValidator> ? TValidator : never) | ValidatorSerializerStringify<TValidator>;
86
- export interface ServerFnMiddleware<TMethod extends Method, TServerFnResponseType extends ServerFnResponseType, TValidator> {
87
- middleware: <const TNewMiddlewares = undefined>(middlewares: Constrain<TNewMiddlewares, ReadonlyArray<AnyFunctionMiddleware>>) => ServerFnAfterMiddleware<TMethod, TServerFnResponseType, TNewMiddlewares, TValidator>;
68
+ export type ValidateValidatorInput<TRegister extends Register, TMethod extends Method, TValidator> = TMethod extends 'POST' ? ResolveValidatorInput<TValidator> extends FormData ? ResolveValidatorInput<TValidator> : ValidateSerializable<ResolveValidatorInput<TValidator>, RegisteredSerializableInput<TRegister>> : ValidateSerializable<ResolveValidatorInput<TValidator>, RegisteredSerializableInput<TRegister>>;
69
+ export type ValidateValidator<TRegister extends Register, TMethod extends Method, TValidator> = ValidateValidatorInput<TRegister, TMethod, TValidator> extends infer TInput ? Validator<TInput, any> : never;
70
+ export type ConstrainValidator<TRegister extends Register, TMethod extends Method, TValidator> = (unknown extends TValidator ? TValidator : ResolveValidatorInput<TValidator> extends ValidateValidator<TRegister, TMethod, TValidator> ? TValidator : never) | ValidateValidator<TRegister, TMethod, TValidator>;
71
+ export type AppendMiddlewares<TMiddlewares, TNewMiddlewares> = TMiddlewares extends ReadonlyArray<any> ? TNewMiddlewares extends ReadonlyArray<any> ? readonly [...TMiddlewares, ...TNewMiddlewares] : TMiddlewares : TNewMiddlewares;
72
+ export interface ServerFnMiddleware<TRegister extends Register, TMethod extends Method, TMiddlewares, TValidator> {
73
+ middleware: <const TNewMiddlewares>(middlewares: Constrain<TNewMiddlewares, ReadonlyArray<AnyFunctionMiddleware | AnyRequestMiddleware | AnyServerFn>>) => ServerFnAfterMiddleware<TRegister, TMethod, AppendMiddlewares<TMiddlewares, TNewMiddlewares>, TValidator>;
88
74
  }
89
- export interface ServerFnAfterMiddleware<TMethod extends Method, TServerFnResponseType extends ServerFnResponseType, TMiddlewares, TValidator> extends ServerFnValidator<TMethod, TServerFnResponseType, TMiddlewares>, ServerFnHandler<TMethod, TServerFnResponseType, TMiddlewares, TValidator> {
75
+ export interface ServerFnAfterMiddleware<TRegister extends Register, TMethod extends Method, TMiddlewares, TValidator> extends ServerFnWithTypes<TRegister, TMethod, TMiddlewares, TValidator, undefined>, ServerFnMiddleware<TRegister, TMethod, TMiddlewares, undefined>, ServerFnValidator<TRegister, TMethod, TMiddlewares>, ServerFnHandler<TRegister, TMethod, TMiddlewares, TValidator> {
76
+ <TNewMethod extends Method = TMethod>(options?: {
77
+ method?: TNewMethod;
78
+ }): ServerFnAfterMiddleware<TRegister, TNewMethod, TMiddlewares, TValidator>;
90
79
  }
91
- export type ValidatorFn<TMethod extends Method, TServerFnResponseType extends ServerFnResponseType, TMiddlewares> = <TValidator>(validator: ConstrainValidator<TValidator>) => ServerFnAfterValidator<TMethod, TServerFnResponseType, TMiddlewares, TValidator>;
92
- export interface ServerFnValidator<TMethod extends Method, TServerFnResponseType extends ServerFnResponseType, TMiddlewares> {
93
- validator: ValidatorFn<TMethod, TServerFnResponseType, TMiddlewares>;
80
+ export type ValidatorFn<TRegister extends Register, TMethod extends Method, TMiddlewares> = <TValidator>(validator: ConstrainValidator<TRegister, TMethod, TValidator>) => ServerFnAfterValidator<TRegister, TMethod, TMiddlewares, TValidator>;
81
+ export interface ServerFnValidator<TRegister extends Register, TMethod extends Method, TMiddlewares> {
82
+ validator: ValidatorFn<TRegister, TMethod, TMiddlewares>;
94
83
  }
95
- export interface ServerFnAfterValidator<TMethod extends Method, TServerFnResponseType extends ServerFnResponseType, TMiddlewares, TValidator> extends ServerFnMiddleware<TMethod, TServerFnResponseType, TValidator>, ServerFnHandler<TMethod, TServerFnResponseType, TMiddlewares, TValidator> {
84
+ export interface ServerFnAfterValidator<TRegister extends Register, TMethod extends Method, TMiddlewares, TValidator> extends ServerFnWithTypes<TRegister, TMethod, TMiddlewares, TValidator, undefined>, ServerFnMiddleware<TRegister, TMethod, TMiddlewares, TValidator>, ServerFnHandler<TRegister, TMethod, TMiddlewares, TValidator> {
96
85
  }
97
- export interface ServerFnAfterTyper<TMethod extends Method, TServerFnResponseType extends ServerFnResponseType, TMiddlewares, TValidator> extends ServerFnHandler<TMethod, TServerFnResponseType, TMiddlewares, TValidator> {
86
+ export interface ServerFnAfterTyper<TRegister extends Register, TMethod extends Method, TMiddlewares, TValidator> extends ServerFnWithTypes<TRegister, TMethod, TMiddlewares, TValidator, undefined>, ServerFnHandler<TRegister, TMethod, TMiddlewares, TValidator> {
98
87
  }
99
- export interface ServerFnHandler<TMethod extends Method, TServerFnResponseType extends ServerFnResponseType, TMiddlewares, TValidator> {
100
- handler: <TNewResponse>(fn?: ServerFn<TMethod, TServerFnResponseType, TMiddlewares, TValidator, TNewResponse>) => Fetcher<TMiddlewares, TValidator, TNewResponse, TServerFnResponseType>;
88
+ export interface ServerFnHandler<TRegister extends Register, TMethod extends Method, TMiddlewares, TValidator> {
89
+ handler: <TNewResponse>(fn?: ServerFn<TRegister, TMethod, TMiddlewares, TValidator, TNewResponse>) => Fetcher<TRegister, TMiddlewares, TValidator, TNewResponse>;
101
90
  }
102
- export interface ServerFnBuilder<TMethod extends Method = 'GET', TServerFnResponseType extends ServerFnResponseType = 'data'> extends ServerFnMiddleware<TMethod, TServerFnResponseType, undefined>, ServerFnValidator<TMethod, TServerFnResponseType, undefined>, ServerFnHandler<TMethod, TServerFnResponseType, undefined, undefined> {
103
- options: ServerFnBaseOptions<TMethod, TServerFnResponseType, unknown, undefined, undefined>;
91
+ export interface ServerFnBuilder<TRegister extends Register, TMethod extends Method = 'GET'> extends ServerFnWithTypes<TRegister, TMethod, undefined, undefined, undefined>, ServerFnMiddleware<TRegister, TMethod, undefined, undefined>, ServerFnValidator<TRegister, TMethod, undefined>, ServerFnHandler<TRegister, TMethod, undefined, undefined> {
92
+ options: ServerFnBaseOptions<TRegister, TMethod, unknown, undefined, undefined>;
104
93
  }
105
- export declare function extractFormDataContext(formData: FormData): {
106
- context: unknown;
107
- data: FormData;
108
- } | {
109
- data: FormData;
110
- context?: undefined;
111
- };
112
- export declare function flattenMiddlewares(middlewares: Array<AnyFunctionMiddleware>): Array<AnyFunctionMiddleware>;
94
+ export interface ServerFnWithTypes<in out TRegister extends Register, in out TMethod extends Method, in out TMiddlewares, in out TValidator, in out TResponse> {
95
+ _types: ServerFnTypes<TMethod, TMiddlewares, TValidator, TResponse>;
96
+ options: ServerFnBaseOptions<TRegister, TMethod, unknown, undefined, undefined>;
97
+ [TSS_SERVER_FUNCTION_FACTORY]: true;
98
+ }
99
+ export type AnyServerFn = ServerFnWithTypes<any, any, any, any, any>;
100
+ export interface ServerFnTypes<in out TMethod extends Method, in out TMiddlewares, in out TValidator, in out TResponse> {
101
+ method: TMethod;
102
+ middlewares: TMiddlewares;
103
+ validator: TValidator;
104
+ response: TResponse;
105
+ allServerContext: AssignAllServerContext<TMiddlewares>;
106
+ allInput: IntersectAllValidatorInputs<TMiddlewares, TValidator>;
107
+ allOutput: IntersectAllValidatorOutputs<TMiddlewares, TValidator>;
108
+ }
109
+ export declare function flattenMiddlewares(middlewares: Array<AnyFunctionMiddleware | AnyRequestMiddleware>): Array<AnyFunctionMiddleware | AnyRequestMiddleware>;
113
110
  export type ServerFnMiddlewareOptions = {
114
111
  method: Method;
115
- response?: ServerFnResponseType;
116
112
  data: any;
117
113
  headers?: HeadersInit;
118
114
  signal?: AbortSignal;