@tanstack/start-client-core 1.131.6 → 1.132.0-alpha.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.
Files changed (44) hide show
  1. package/dist/esm/createIsomorphicFn.js.map +1 -1
  2. package/dist/esm/createMiddleware.d.ts +1 -2
  3. package/dist/esm/createMiddleware.js.map +1 -1
  4. package/dist/esm/createServerFn.d.ts +3 -28
  5. package/dist/esm/createServerFn.js +9 -148
  6. package/dist/esm/createServerFn.js.map +1 -1
  7. package/dist/esm/envOnly.js.map +1 -1
  8. package/dist/esm/index.d.ts +2 -2
  9. package/dist/esm/index.js +1 -2
  10. package/dist/esm/registerGlobalMiddleware.js.map +1 -1
  11. package/dist/esm/serializer.js +0 -10
  12. package/dist/esm/serializer.js.map +1 -1
  13. package/package.json +3 -7
  14. package/src/createMiddleware.ts +0 -7
  15. package/src/createServerFn.ts +8 -319
  16. package/src/index.tsx +0 -3
  17. package/src/serializer.ts +0 -13
  18. package/src/tests/createServerFn.test-d.ts +0 -6
  19. package/dist/cjs/createIsomorphicFn.cjs +0 -12
  20. package/dist/cjs/createIsomorphicFn.cjs.map +0 -1
  21. package/dist/cjs/createIsomorphicFn.d.cts +0 -12
  22. package/dist/cjs/createMiddleware.cjs +0 -37
  23. package/dist/cjs/createMiddleware.cjs.map +0 -1
  24. package/dist/cjs/createMiddleware.d.cts +0 -177
  25. package/dist/cjs/createServerFn.cjs +0 -385
  26. package/dist/cjs/createServerFn.cjs.map +0 -1
  27. package/dist/cjs/createServerFn.d.cts +0 -159
  28. package/dist/cjs/envOnly.cjs +0 -7
  29. package/dist/cjs/envOnly.cjs.map +0 -1
  30. package/dist/cjs/envOnly.d.cts +0 -4
  31. package/dist/cjs/index.cjs +0 -40
  32. package/dist/cjs/index.cjs.map +0 -1
  33. package/dist/cjs/index.d.cts +0 -11
  34. package/dist/cjs/registerGlobalMiddleware.cjs +0 -9
  35. package/dist/cjs/registerGlobalMiddleware.cjs.map +0 -1
  36. package/dist/cjs/registerGlobalMiddleware.d.cts +0 -5
  37. package/dist/cjs/serializer.cjs +0 -162
  38. package/dist/cjs/serializer.cjs.map +0 -1
  39. package/dist/cjs/serializer.d.cts +0 -23
  40. package/dist/cjs/tests/createIsomorphicFn.test-d.d.cts +0 -1
  41. package/dist/cjs/tests/createServerFn.test-d.d.cts +0 -1
  42. package/dist/cjs/tests/createServerMiddleware.test-d.d.cts +0 -1
  43. package/dist/cjs/tests/envOnly.test-d.d.cts +0 -1
  44. package/dist/cjs/tests/serializer.test.d.cts +0 -1
@@ -1,12 +0,0 @@
1
- export type IsomorphicFn<TArgs extends Array<any> = [], TServer = undefined, TClient = undefined> = (...args: TArgs) => TServer | TClient;
2
- export interface ServerOnlyFn<TArgs extends Array<any>, TServer> extends IsomorphicFn<TArgs, TServer> {
3
- client: <TClient>(clientImpl: (...args: TArgs) => TClient) => IsomorphicFn<TArgs, TServer, TClient>;
4
- }
5
- export interface ClientOnlyFn<TArgs extends Array<any>, TClient> extends IsomorphicFn<TArgs, undefined, TClient> {
6
- server: <TServer>(serverImpl: (...args: TArgs) => TServer) => IsomorphicFn<TArgs, TServer, TClient>;
7
- }
8
- export interface IsomorphicFnBase extends IsomorphicFn {
9
- server: <TArgs extends Array<any>, TServer>(serverImpl: (...args: TArgs) => TServer) => ServerOnlyFn<TArgs, TServer>;
10
- client: <TArgs extends Array<any>, TClient>(clientImpl: (...args: TArgs) => TClient) => ClientOnlyFn<TArgs, TClient>;
11
- }
12
- export declare function createIsomorphicFn(): IsomorphicFnBase;
@@ -1,37 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- function createMiddleware(options, __opts) {
4
- const resolvedOptions = {
5
- type: "function",
6
- ...__opts || options
7
- };
8
- return {
9
- options: resolvedOptions,
10
- middleware: (middleware) => {
11
- return createMiddleware(
12
- {},
13
- Object.assign(resolvedOptions, { middleware })
14
- );
15
- },
16
- validator: (validator) => {
17
- return createMiddleware(
18
- {},
19
- Object.assign(resolvedOptions, { validator })
20
- );
21
- },
22
- client: (client) => {
23
- return createMiddleware(
24
- {},
25
- Object.assign(resolvedOptions, { client })
26
- );
27
- },
28
- server: (server) => {
29
- return createMiddleware(
30
- {},
31
- Object.assign(resolvedOptions, { server })
32
- );
33
- }
34
- };
35
- }
36
- exports.createMiddleware = createMiddleware;
37
- //# sourceMappingURL=createMiddleware.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"createMiddleware.cjs","sources":["../../src/createMiddleware.ts"],"sourcesContent":["import type {\n ConstrainValidator,\n Method,\n ServerFnResponseType,\n ServerFnTypeOrTypeFn,\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 type: ServerFnTypeOrTypeFn<\n Method,\n TServerFnResponseType,\n TMiddlewares,\n TValidator\n >\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":";;AAkBgB,SAAA,iBACd,SAIA,QAO+B;AAE/B,QAAM,kBAAkB;AAAA,IACtB,MAAM;AAAA,IACN,GAAI,UACD;AAAA,EAOL;AAEO,SAAA;AAAA,IACL,SAAS;AAAA,IACT,YAAY,CAAC,eAAoB;AACxB,aAAA;AAAA,QACL,CAAC;AAAA,QACD,OAAO,OAAO,iBAAiB,EAAE,WAAY,CAAA;AAAA,MAC/C;AAAA,IACF;AAAA,IACA,WAAW,CAAC,cAAmB;AACtB,aAAA;AAAA,QACL,CAAC;AAAA,QACD,OAAO,OAAO,iBAAiB,EAAE,UAAW,CAAA;AAAA,MAC9C;AAAA,IACF;AAAA,IACA,QAAQ,CAAC,WAAgB;AAChB,aAAA;AAAA,QACL,CAAC;AAAA,QACD,OAAO,OAAO,iBAAiB,EAAE,OAAQ,CAAA;AAAA,MAC3C;AAAA,IACF;AAAA,IACA,QAAQ,CAAC,WAAgB;AAChB,aAAA;AAAA,QACL,CAAC;AAAA,QACD,OAAO,OAAO,iBAAiB,EAAE,OAAQ,CAAA;AAAA,MAC3C;AAAA,IAAA;AAAA,EAEJ;AACF;;"}
@@ -1,177 +0,0 @@
1
- import { ConstrainValidator, Method, ServerFnResponseType, ServerFnTypeOrTypeFn } from './createServerFn.cjs';
2
- import { AnyRouter, Assign, Constrain, Expand, IntersectAssign, RegisteredRouter, ResolveValidatorInput, ResolveValidatorOutput } from '@tanstack/router-core';
3
- import { SerializerStringify } from './serializer.cjs';
4
- export declare function createMiddleware<TType extends MiddlewareType>(options: {
5
- type: TType;
6
- validateClient?: boolean;
7
- }, __opts?: FunctionMiddlewareOptions<unknown, undefined, undefined, undefined, ServerFnResponseType>): CreateMiddlewareResult<TType>;
8
- 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>;
12
- }
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> {
14
- }
15
- export interface FunctionMiddlewareWithTypes<TMiddlewares, TValidator, TServerContext, TServerSendContext, TClientContext, TClientSendContext, TServerFnResponseType extends ServerFnResponseType> {
16
- _types: FunctionMiddlewareTypes<TMiddlewares, TValidator, TServerContext, TServerSendContext, TClientContext, TClientSendContext>;
17
- options: FunctionMiddlewareOptions<TMiddlewares, TValidator, TServerContext, TClientContext, TServerFnResponseType>;
18
- }
19
- export interface FunctionMiddlewareTypes<in out TMiddlewares, in out TValidator, in out TServerContext, in out TServerSendContext, in out TClientContext, in out TClientSendContext> {
20
- type: 'function';
21
- middlewares: TMiddlewares;
22
- input: ResolveValidatorInput<TValidator>;
23
- allInput: IntersectAllValidatorInputs<TMiddlewares, TValidator>;
24
- output: ResolveValidatorOutput<TValidator>;
25
- allOutput: IntersectAllValidatorOutputs<TMiddlewares, TValidator>;
26
- clientContext: TClientContext;
27
- allClientContextBeforeNext: AssignAllClientContextBeforeNext<TMiddlewares, TClientContext>;
28
- allClientContextAfterNext: AssignAllClientContextAfterNext<TMiddlewares, TClientContext, TClientSendContext>;
29
- serverContext: TServerContext;
30
- serverSendContext: TServerSendContext;
31
- allServerSendContext: AssignAllServerSendContext<TMiddlewares, TServerSendContext>;
32
- allServerContext: AssignAllServerContext<TMiddlewares, TServerSendContext, TServerContext>;
33
- clientSendContext: TClientSendContext;
34
- allClientSendContext: AssignAllClientSendContext<TMiddlewares, TClientSendContext>;
35
- validator: TValidator;
36
- }
37
- /**
38
- * Recursively resolve the input type produced by a sequence of middleware
39
- */
40
- 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;
42
- export type AnyFunctionMiddleware = FunctionMiddlewareWithTypes<any, any, any, any, any, any, any>;
43
- /**
44
- * Recursively merge the output type produced by a sequence of middleware
45
- */
46
- export type IntersectAllValidatorOutputs<TMiddlewares, TValidator> = unknown extends TValidator ? TValidator : TValidator extends undefined ? IntersectAllMiddleware<TMiddlewares, 'allOutput'> : IntersectAssign<IntersectAllMiddleware<TMiddlewares, 'allOutput'>, ResolveValidatorOutput<TValidator>>;
47
- /**
48
- * Recursively resolve the client context type produced by a sequence of middleware
49
- */
50
- 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;
52
- export type AssignAllClientContextAfterNext<TMiddlewares, TClientContext = undefined, TSendContext = undefined> = unknown extends TClientContext ? Assign<TClientContext, TSendContext> : Assign<AssignAllMiddleware<TMiddlewares, 'allClientContextAfterNext'>, Assign<TClientContext, TSendContext>>;
53
- export type AssignAllServerSendContext<TMiddlewares, TSendContext = undefined> = unknown extends TSendContext ? TSendContext : Assign<AssignAllMiddleware<TMiddlewares, 'allServerSendContext'>, TSendContext>;
54
- /**
55
- * Recursively resolve the server context type produced by a sequence of middleware
56
- */
57
- export type AssignAllServerContext<TMiddlewares, TSendContext = undefined, TServerContext = undefined> = unknown extends TSendContext ? Assign<TSendContext, TServerContext> : Assign<AssignAllMiddleware<TMiddlewares, 'allServerContext'>, Assign<TSendContext, TServerContext>>;
58
- 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;
61
- middleware?: TMiddlewares;
62
- validator?: ConstrainValidator<TValidator>;
63
- client?: FunctionMiddlewareClientFn<TMiddlewares, TValidator, TServerContext, TClientContext, TServerFnResponseType>;
64
- server?: FunctionMiddlewareServerFn<TMiddlewares, TValidator, TServerContext, unknown, unknown, TServerFnResponseType>;
65
- }
66
- export type FunctionMiddlewareClientNextFn<TMiddlewares> = <TSendContext = undefined, TNewClientContext = undefined>(ctx?: {
67
- context?: TNewClientContext;
68
- sendContext?: SerializerStringify<TSendContext>;
69
- headers?: HeadersInit;
70
- }) => 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>;
73
- }
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> {
76
- request: Request;
77
- context: Expand<AssignAllServerContext<TMiddlewares, TServerSendContext>>;
78
- next: FunctionMiddlewareServerNextFn<TMiddlewares, TServerSendContext>;
79
- response: Response;
80
- method: Method;
81
- signal: AbortSignal;
82
- }
83
- export type FunctionMiddlewareServerNextFn<TMiddlewares, TServerSendContext> = <TNewServerContext = undefined, TSendContext = undefined>(ctx?: {
84
- context?: TNewServerContext;
85
- sendContext?: SerializerStringify<TSendContext>;
86
- }) => Promise<FunctionServerResultWithContext<TMiddlewares, TServerSendContext, TNewServerContext, TSendContext>>;
87
- export type FunctionServerResultWithContext<in out TMiddlewares, in out TServerSendContext, in out TServerContext, in out TSendContext> = {
88
- 'use functions must return the result of next()': true;
89
- _types: {
90
- context: TServerContext;
91
- sendContext: TSendContext;
92
- };
93
- context: Expand<AssignAllServerContext<TMiddlewares, TServerSendContext, TServerContext>>;
94
- sendContext: Expand<AssignAllClientSendContext<TMiddlewares, TSendContext>>;
95
- };
96
- export interface FunctionMiddlewareServerFnOptions<in out TMiddlewares, in out TValidator, in out TServerSendContext, in out TServerFnResponseType> {
97
- data: Expand<IntersectAllValidatorOutputs<TMiddlewares, TValidator>>;
98
- context: Expand<AssignAllServerContext<TMiddlewares, TServerSendContext>>;
99
- next: FunctionMiddlewareServerNextFn<TMiddlewares, TServerSendContext>;
100
- response: TServerFnResponseType;
101
- method: Method;
102
- filename: string;
103
- functionId: string;
104
- signal: AbortSignal;
105
- }
106
- 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> {
108
- }
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>;
111
- }
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> {
114
- data: Expand<IntersectAllValidatorInputs<TMiddlewares, TValidator>>;
115
- context: Expand<AssignAllClientContextBeforeNext<TMiddlewares>>;
116
- sendContext: Expand<AssignAllServerSendContext<TMiddlewares>>;
117
- method: Method;
118
- response: TServerFnResponseType;
119
- signal: AbortSignal;
120
- next: FunctionMiddlewareClientNextFn<TMiddlewares>;
121
- filename: string;
122
- functionId: string;
123
- type: ServerFnTypeOrTypeFn<Method, TServerFnResponseType, TMiddlewares, TValidator>;
124
- router: TRouter;
125
- }
126
- export type FunctionMiddlewareClientFnResult<TMiddlewares, TSendContext, TClientContext> = Promise<FunctionClientResultWithContext<TMiddlewares, TSendContext, TClientContext>> | FunctionClientResultWithContext<TMiddlewares, TSendContext, TClientContext>;
127
- export type FunctionClientResultWithContext<in out TMiddlewares, in out TSendContext, in out TClientContext> = {
128
- 'use functions must return the result of next()': true;
129
- context: Expand<AssignAllClientContextAfterNext<TMiddlewares, TClientContext>>;
130
- sendContext: Expand<AssignAllServerSendContext<TMiddlewares, TSendContext>>;
131
- headers: HeadersInit;
132
- };
133
- 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> {
134
- }
135
- export interface FunctionMiddlewareValidator<TMiddlewares, TServerFnResponseType extends ServerFnResponseType> {
136
- validator: <TNewValidator>(input: ConstrainValidator<TNewValidator>) => FunctionMiddlewareAfterValidator<TMiddlewares, TNewValidator, TServerFnResponseType>;
137
- }
138
- 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> {
139
- }
140
- export interface RequestMiddleware extends RequestMiddlewareAfterMiddleware<undefined> {
141
- middleware: <const TMiddlewares = undefined>(middlewares: Constrain<TMiddlewares, ReadonlyArray<AnyRequestMiddleware>>) => RequestMiddlewareAfterMiddleware<TMiddlewares>;
142
- }
143
- export type AnyRequestMiddleware = RequestMiddlewareWithTypes<any, any>;
144
- export interface RequestMiddlewareWithTypes<TMiddlewares, TServerContext> {
145
- _types: RequestMiddlewareTypes<TMiddlewares, TServerContext>;
146
- }
147
- export interface RequestMiddlewareTypes<TMiddlewares, TServerContext> {
148
- type: 'request';
149
- middlewares: TMiddlewares;
150
- serverContext: TServerContext;
151
- allServerContext: AssignAllServerContext<TMiddlewares, undefined, TServerContext>;
152
- }
153
- export interface RequestMiddlewareAfterMiddleware<TMiddlewares> extends RequestMiddlewareWithTypes<TMiddlewares, undefined>, RequestMiddlewareServer<TMiddlewares> {
154
- }
155
- export interface RequestMiddlewareServer<TMiddlewares> {
156
- server: <TServerContext = undefined>(fn: RequestServerFn<TMiddlewares, TServerContext>) => RequestMiddlewareAfterServer<TMiddlewares, TServerContext>;
157
- }
158
- export type RequestServerFn<TMiddlewares, TServerContext> = (options: RequestServerOptions<TMiddlewares>) => RequestMiddlewareServerFnResult<TMiddlewares, TServerContext>;
159
- export interface RequestServerOptions<TMiddlewares> {
160
- request: Request;
161
- pathname: string;
162
- context: AssignAllServerContext<TMiddlewares>;
163
- next: RequestServerNextFn<TMiddlewares>;
164
- }
165
- export type RequestServerNextFn<TMiddlewares> = <TServerContext = undefined>(options?: RequestServerNextFnOptions<TServerContext>) => RequestMiddlewareServerFnResult<TMiddlewares, TServerContext>;
166
- export interface RequestServerNextFnOptions<TServerContext> {
167
- context?: TServerContext;
168
- }
169
- export type RequestMiddlewareServerFnResult<TMiddlewares, TServerContext> = Promise<RequestServerResult<TMiddlewares, TServerContext>> | RequestServerResult<TMiddlewares, TServerContext>;
170
- export interface RequestServerResult<TMiddlewares, TServerContext> {
171
- request: Request;
172
- pathname: string;
173
- context: Expand<AssignAllServerContext<TMiddlewares, undefined, TServerContext>>;
174
- response: Response;
175
- }
176
- export interface RequestMiddlewareAfterServer<TMiddlewares, TServerContext> extends RequestMiddlewareWithTypes<TMiddlewares, TServerContext> {
177
- }
@@ -1,385 +0,0 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __copyProps = (to, from, except, desc) => {
9
- if (from && typeof from === "object" || typeof from === "function") {
10
- for (let key of __getOwnPropNames(from))
11
- if (!__hasOwnProp.call(to, key) && key !== except)
12
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
13
- }
14
- return to;
15
- };
16
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
17
- // If the importer is in node compatibility mode or this is not an ESM
18
- // file that has been converted to a CommonJS file using a Babel-
19
- // compatible transform (i.e. "__esModule" has not been set), then set
20
- // "default" to the CommonJS "module.exports" for node compatibility.
21
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
22
- mod
23
- ));
24
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
25
- const invariant = require("tiny-invariant");
26
- const warning = require("tiny-warning");
27
- const routerCore = require("@tanstack/router-core");
28
- const client = require("@tanstack/router-core/ssr/client");
29
- const startStorageContext = require("@tanstack/start-storage-context");
30
- const registerGlobalMiddleware = require("./registerGlobalMiddleware.cjs");
31
- const serializer = require("./serializer.cjs");
32
- const createIsomorphicFn = require("./createIsomorphicFn.cjs");
33
- const getRouterInstance = createIsomorphicFn.createIsomorphicFn().client(() => window.__TSR_ROUTER__).server(() => {
34
- var _a;
35
- return (_a = startStorageContext.getStartContext({ throwIfNotFound: false })) == null ? void 0 : _a.router;
36
- });
37
- function createServerFn(options, __opts) {
38
- const resolvedOptions = __opts || options || {};
39
- if (typeof resolvedOptions.method === "undefined") {
40
- resolvedOptions.method = "GET";
41
- }
42
- return {
43
- options: resolvedOptions,
44
- middleware: (middleware) => {
45
- return createServerFn(void 0, Object.assign(resolvedOptions, { middleware }));
46
- },
47
- validator: (validator) => {
48
- return createServerFn(void 0, Object.assign(resolvedOptions, { validator }));
49
- },
50
- type: (type) => {
51
- return createServerFn(void 0, Object.assign(resolvedOptions, { type }));
52
- },
53
- handler: (...args) => {
54
- const [extractedFn, serverFn] = args;
55
- Object.assign(resolvedOptions, {
56
- ...extractedFn,
57
- extractedFn,
58
- serverFn
59
- });
60
- const resolvedMiddleware = [
61
- ...resolvedOptions.middleware || [],
62
- serverFnBaseToMiddleware(resolvedOptions)
63
- ];
64
- return Object.assign(
65
- async (opts) => {
66
- return executeMiddleware(resolvedMiddleware, "client", {
67
- ...extractedFn,
68
- ...resolvedOptions,
69
- data: opts == null ? void 0 : opts.data,
70
- headers: opts == null ? void 0 : opts.headers,
71
- signal: opts == null ? void 0 : opts.signal,
72
- context: {},
73
- router: getRouterInstance()
74
- }).then((d) => {
75
- if (resolvedOptions.response === "full") {
76
- return d;
77
- }
78
- if (d.error) throw d.error;
79
- return d.result;
80
- });
81
- },
82
- {
83
- // This copies over the URL, function ID
84
- ...extractedFn,
85
- // The extracted function on the server-side calls
86
- // this function
87
- __executeServer: async (opts_, signal) => {
88
- var _a, _b;
89
- const opts = opts_ instanceof FormData ? extractFormDataContext(opts_) : opts_;
90
- opts.type = typeof resolvedOptions.type === "function" ? resolvedOptions.type(opts) : resolvedOptions.type;
91
- const ctx = {
92
- ...extractedFn,
93
- ...opts,
94
- signal
95
- };
96
- const run = () => executeMiddleware(resolvedMiddleware, "server", ctx).then(
97
- (d) => ({
98
- // Only send the result and sendContext back to the client
99
- result: d.result,
100
- error: d.error,
101
- context: d.sendContext
102
- })
103
- );
104
- if (ctx.type === "static") {
105
- let response;
106
- if ((_a = exports.serverFnStaticCache) == null ? void 0 : _a.getItem) {
107
- response = await exports.serverFnStaticCache.getItem(ctx);
108
- }
109
- if (!response) {
110
- response = await run().then((d) => {
111
- return {
112
- ctx: d,
113
- error: null
114
- };
115
- }).catch((e) => {
116
- return {
117
- ctx: void 0,
118
- error: e
119
- };
120
- });
121
- if ((_b = exports.serverFnStaticCache) == null ? void 0 : _b.setItem) {
122
- await exports.serverFnStaticCache.setItem(ctx, response);
123
- }
124
- }
125
- invariant(
126
- response,
127
- "No response from both server and static cache!"
128
- );
129
- if (response.error) {
130
- throw response.error;
131
- }
132
- return response.ctx;
133
- }
134
- return run();
135
- }
136
- }
137
- );
138
- }
139
- };
140
- }
141
- async function executeMiddleware(middlewares, env, opts) {
142
- const flattenedMiddlewares = flattenMiddlewares([
143
- ...registerGlobalMiddleware.globalMiddleware,
144
- ...middlewares
145
- ]);
146
- const next = async (ctx) => {
147
- const nextMiddleware = flattenedMiddlewares.shift();
148
- if (!nextMiddleware) {
149
- return ctx;
150
- }
151
- if (nextMiddleware.options.validator && (env === "client" ? nextMiddleware.options.validateClient : true)) {
152
- ctx.data = await execValidator(nextMiddleware.options.validator, ctx.data);
153
- }
154
- const middlewareFn = env === "client" ? nextMiddleware.options.client : nextMiddleware.options.server;
155
- if (middlewareFn) {
156
- return applyMiddleware(middlewareFn, ctx, async (newCtx) => {
157
- return next(newCtx).catch((error) => {
158
- if (routerCore.isRedirect(error) || routerCore.isNotFound(error)) {
159
- return {
160
- ...newCtx,
161
- error
162
- };
163
- }
164
- throw error;
165
- });
166
- });
167
- }
168
- return next(ctx);
169
- };
170
- return next({
171
- ...opts,
172
- headers: opts.headers || {},
173
- sendContext: opts.sendContext || {},
174
- context: opts.context || {}
175
- });
176
- }
177
- exports.serverFnStaticCache = void 0;
178
- function setServerFnStaticCache(cache) {
179
- const previousCache = exports.serverFnStaticCache;
180
- exports.serverFnStaticCache = typeof cache === "function" ? cache() : cache;
181
- return () => {
182
- exports.serverFnStaticCache = previousCache;
183
- };
184
- }
185
- function createServerFnStaticCache(serverFnStaticCache2) {
186
- return serverFnStaticCache2;
187
- }
188
- async function sha1Hash(message) {
189
- const msgBuffer = new TextEncoder().encode(message);
190
- const hashBuffer = await crypto.subtle.digest("SHA-1", msgBuffer);
191
- const hashArray = Array.from(new Uint8Array(hashBuffer));
192
- const hashHex = hashArray.map((b) => b.toString(16).padStart(2, "0")).join("");
193
- return hashHex;
194
- }
195
- setServerFnStaticCache(() => {
196
- const getStaticCacheUrl = async (options, hash) => {
197
- const filename = await sha1Hash(`${options.functionId}__${hash}`);
198
- return `/__tsr/staticServerFnCache/${filename}.json`;
199
- };
200
- const jsonToFilenameSafeString = (json) => {
201
- const sortedKeysReplacer = (key, value) => value && typeof value === "object" && !Array.isArray(value) ? Object.keys(value).sort().reduce((acc, curr) => {
202
- acc[curr] = value[curr];
203
- return acc;
204
- }, {}) : value;
205
- const jsonString = JSON.stringify(json ?? "", sortedKeysReplacer);
206
- return jsonString.replace(/[/\\?%*:|"<>]/g, "-").replace(/\s+/g, "_");
207
- };
208
- const staticClientCache = typeof document !== "undefined" ? /* @__PURE__ */ new Map() : null;
209
- return createServerFnStaticCache({
210
- getItem: async (ctx) => {
211
- if (typeof document === "undefined") {
212
- const hash = jsonToFilenameSafeString(ctx.data);
213
- const url = await getStaticCacheUrl(ctx, hash);
214
- const publicUrl = process.env.TSS_OUTPUT_PUBLIC_DIR;
215
- const { promises: fs } = await import("node:fs");
216
- const path = await import("node:path");
217
- const filePath = path.join(publicUrl, url);
218
- const [cachedResult, readError] = await fs.readFile(filePath, "utf-8").then((c) => [
219
- serializer.startSerializer.parse(c),
220
- null
221
- ]).catch((e) => [null, e]);
222
- if (readError && readError.code !== "ENOENT") {
223
- throw readError;
224
- }
225
- return cachedResult;
226
- }
227
- return void 0;
228
- },
229
- setItem: async (ctx, response) => {
230
- const { promises: fs } = await import("node:fs");
231
- const path = await import("node:path");
232
- const hash = jsonToFilenameSafeString(ctx.data);
233
- const url = await getStaticCacheUrl(ctx, hash);
234
- const publicUrl = process.env.TSS_OUTPUT_PUBLIC_DIR;
235
- const filePath = path.join(publicUrl, url);
236
- await fs.mkdir(path.dirname(filePath), { recursive: true });
237
- await fs.writeFile(filePath, serializer.startSerializer.stringify(response));
238
- },
239
- fetchItem: async (ctx) => {
240
- const hash = jsonToFilenameSafeString(ctx.data);
241
- const url = await getStaticCacheUrl(ctx, hash);
242
- let result = staticClientCache == null ? void 0 : staticClientCache.get(url);
243
- if (!result) {
244
- result = await fetch(url, {
245
- method: "GET"
246
- }).then((r) => r.text()).then((d) => serializer.startSerializer.parse(d));
247
- staticClientCache == null ? void 0 : staticClientCache.set(url, result);
248
- }
249
- return result;
250
- }
251
- });
252
- });
253
- function extractFormDataContext(formData) {
254
- const serializedContext = formData.get("__TSR_CONTEXT");
255
- formData.delete("__TSR_CONTEXT");
256
- if (typeof serializedContext !== "string") {
257
- return {
258
- context: {},
259
- data: formData
260
- };
261
- }
262
- try {
263
- const context = serializer.startSerializer.parse(serializedContext);
264
- return {
265
- context,
266
- data: formData
267
- };
268
- } catch {
269
- return {
270
- data: formData
271
- };
272
- }
273
- }
274
- function flattenMiddlewares(middlewares) {
275
- const seen = /* @__PURE__ */ new Set();
276
- const flattened = [];
277
- const recurse = (middleware) => {
278
- middleware.forEach((m) => {
279
- if (m.options.middleware) {
280
- recurse(m.options.middleware);
281
- }
282
- if (!seen.has(m)) {
283
- seen.add(m);
284
- flattened.push(m);
285
- }
286
- });
287
- };
288
- recurse(middlewares);
289
- return flattened;
290
- }
291
- const applyMiddleware = async (middlewareFn, ctx, nextFn) => {
292
- return middlewareFn({
293
- ...ctx,
294
- next: async (userCtx = {}) => {
295
- return nextFn({
296
- ...ctx,
297
- ...userCtx,
298
- context: {
299
- ...ctx.context,
300
- ...userCtx.context
301
- },
302
- sendContext: {
303
- ...ctx.sendContext,
304
- ...userCtx.sendContext ?? {}
305
- },
306
- headers: client.mergeHeaders(ctx.headers, userCtx.headers),
307
- result: userCtx.result !== void 0 ? userCtx.result : ctx.response === "raw" ? userCtx : ctx.result,
308
- error: userCtx.error ?? ctx.error
309
- });
310
- }
311
- });
312
- };
313
- function execValidator(validator, input) {
314
- if (validator == null) return {};
315
- if ("~standard" in validator) {
316
- const result = validator["~standard"].validate(input);
317
- if (result instanceof Promise)
318
- throw new Error("Async validation not supported");
319
- if (result.issues)
320
- throw new Error(JSON.stringify(result.issues, void 0, 2));
321
- return result.value;
322
- }
323
- if ("parse" in validator) {
324
- return validator.parse(input);
325
- }
326
- if (typeof validator === "function") {
327
- return validator(input);
328
- }
329
- throw new Error("Invalid validator type!");
330
- }
331
- function serverFnBaseToMiddleware(options) {
332
- return {
333
- _types: void 0,
334
- options: {
335
- validator: options.validator,
336
- validateClient: options.validateClient,
337
- client: async ({ next, sendContext, ...ctx }) => {
338
- var _a;
339
- const payload = {
340
- ...ctx,
341
- // switch the sendContext over to context
342
- context: sendContext,
343
- type: typeof ctx.type === "function" ? ctx.type(ctx) : ctx.type
344
- };
345
- if (ctx.type === "static" && process.env.NODE_ENV === "production" && typeof document !== "undefined") {
346
- invariant(
347
- exports.serverFnStaticCache,
348
- "serverFnStaticCache.fetchItem is not available!"
349
- );
350
- const result = await exports.serverFnStaticCache.fetchItem(payload);
351
- if (result) {
352
- if (result.error) {
353
- throw result.error;
354
- }
355
- return next(result.ctx);
356
- }
357
- warning(
358
- result,
359
- `No static cache item found for ${payload.functionId}__${JSON.stringify(payload.data)}, falling back to server function...`
360
- );
361
- }
362
- const res = await ((_a = options.extractedFn) == null ? void 0 : _a.call(options, payload));
363
- return next(res);
364
- },
365
- server: async ({ next, ...ctx }) => {
366
- var _a;
367
- const result = await ((_a = options.serverFn) == null ? void 0 : _a.call(options, ctx));
368
- return next({
369
- ...ctx,
370
- result
371
- });
372
- }
373
- }
374
- };
375
- }
376
- exports.applyMiddleware = applyMiddleware;
377
- exports.createServerFn = createServerFn;
378
- exports.createServerFnStaticCache = createServerFnStaticCache;
379
- exports.execValidator = execValidator;
380
- exports.executeMiddleware = executeMiddleware;
381
- exports.extractFormDataContext = extractFormDataContext;
382
- exports.flattenMiddlewares = flattenMiddlewares;
383
- exports.serverFnBaseToMiddleware = serverFnBaseToMiddleware;
384
- exports.setServerFnStaticCache = setServerFnStaticCache;
385
- //# sourceMappingURL=createServerFn.cjs.map