@tanstack/react-start-client 1.111.11 → 1.112.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/createMiddleware.cjs.map +1 -1
- package/dist/cjs/createMiddleware.d.cts +27 -25
- package/dist/cjs/createServerFn.cjs +7 -13
- package/dist/cjs/createServerFn.cjs.map +1 -1
- package/dist/cjs/createServerFn.d.cts +46 -37
- package/dist/cjs/ssr-client.cjs +35 -19
- package/dist/cjs/ssr-client.cjs.map +1 -1
- package/dist/esm/createMiddleware.d.ts +27 -25
- package/dist/esm/createMiddleware.js.map +1 -1
- package/dist/esm/createServerFn.d.ts +46 -37
- package/dist/esm/createServerFn.js +7 -13
- package/dist/esm/createServerFn.js.map +1 -1
- package/dist/esm/ssr-client.js +35 -19
- package/dist/esm/ssr-client.js.map +1 -1
- package/package.json +3 -3
- package/src/createMiddleware.ts +112 -34
- package/src/createServerFn.ts +239 -87
- package/src/ssr-client.tsx +52 -27
- package/src/tests/createServerFn.test-d.tsx +175 -64
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createServerFn.js","sources":["../../src/createServerFn.ts"],"sourcesContent":["import { default as invariant } from 'tiny-invariant'\nimport { default as warning } from 'tiny-warning'\nimport { isNotFound, isRedirect } from '@tanstack/react-router'\nimport { mergeHeaders } from './headers'\nimport { globalMiddleware } from './registerGlobalMiddleware'\nimport { startSerializer } from './serializer'\nimport type {\n AnyValidator,\n Constrain,\n Expand,\n ResolveValidatorInput,\n SerializerParse,\n SerializerStringify,\n SerializerStringifyBy,\n Validator,\n} from '@tanstack/router-core'\nimport type {\n AnyMiddleware,\n AssignAllClientSendContext,\n AssignAllServerContext,\n IntersectAllValidatorInputs,\n IntersectAllValidatorOutputs,\n MiddlewareClientFnResult,\n MiddlewareServerFnResult,\n} from './createMiddleware'\n\nexport interface JsonResponse<TData> extends Response {\n json: () => Promise<TData>\n}\n\nexport type CompiledFetcherFnOptions = {\n method: Method\n data: unknown\n headers?: HeadersInit\n signal?: AbortSignal\n context?: any\n}\n\nexport type Fetcher<TMiddlewares, TValidator, TResponse> =\n undefined extends IntersectAllValidatorInputs<TMiddlewares, TValidator>\n ? OptionalFetcher<TMiddlewares, TValidator, TResponse>\n : RequiredFetcher<TMiddlewares, TValidator, TResponse>\n\nexport interface FetcherBase {\n url: string\n __executeServer: (opts: {\n method: Method\n data: unknown\n headers?: HeadersInit\n context?: any\n signal: AbortSignal\n }) => Promise<unknown>\n}\n\nexport type FetchResult<\n TMiddlewares,\n TResponse,\n TFullResponse extends boolean,\n> = false extends TFullResponse\n ? Promise<FetcherData<TResponse>>\n : Promise<FullFetcherData<TMiddlewares, TResponse>>\n\nexport interface OptionalFetcher<TMiddlewares, TValidator, TResponse>\n extends FetcherBase {\n <TFullResponse extends boolean>(\n options?: OptionalFetcherDataOptions<\n TMiddlewares,\n TValidator,\n TFullResponse\n >,\n ): FetchResult<TMiddlewares, TResponse, TFullResponse>\n}\n\nexport interface RequiredFetcher<TMiddlewares, TValidator, TResponse>\n extends FetcherBase {\n <TFullResponse extends boolean>(\n opts: RequiredFetcherDataOptions<TMiddlewares, TValidator, TFullResponse>,\n ): FetchResult<TMiddlewares, TResponse, TFullResponse>\n}\n\nexport type FetcherBaseOptions<TFullResponse extends boolean = false> = {\n headers?: HeadersInit\n type?: ServerFnType\n signal?: AbortSignal\n fullResponse?: TFullResponse\n}\n\nexport type ServerFnType = 'static' | 'dynamic'\n\nexport interface OptionalFetcherDataOptions<\n TMiddlewares,\n TValidator,\n TFullResponse extends boolean,\n> extends FetcherBaseOptions<TFullResponse> {\n data?: Expand<IntersectAllValidatorInputs<TMiddlewares, TValidator>>\n}\n\nexport interface RequiredFetcherDataOptions<\n TMiddlewares,\n TValidator,\n TFullResponse extends boolean,\n> extends FetcherBaseOptions<TFullResponse> {\n data: Expand<IntersectAllValidatorInputs<TMiddlewares, TValidator>>\n}\n\nexport interface FullFetcherData<TMiddlewares, TResponse> {\n error: unknown\n result: FetcherData<TResponse>\n context: AssignAllClientSendContext<TMiddlewares>\n}\n\nexport type FetcherData<TResponse> =\n TResponse extends JsonResponse<any>\n ? SerializerParse<ReturnType<TResponse['json']>>\n : SerializerParse<TResponse>\n\nexport type RscStream<T> = {\n __cacheState: T\n}\n\nexport type Method = 'GET' | 'POST'\n\nexport type ServerFn<TMethod, TMiddlewares, TValidator, TResponse> = (\n ctx: ServerFnCtx<TMethod, TMiddlewares, TValidator>,\n) => Promise<SerializerStringify<TResponse>> | SerializerStringify<TResponse>\n\nexport interface ServerFnCtx<TMethod, TMiddlewares, TValidator> {\n method: TMethod\n data: Expand<IntersectAllValidatorOutputs<TMiddlewares, TValidator>>\n context: Expand<AssignAllServerContext<TMiddlewares>>\n signal: AbortSignal\n}\n\nexport type CompiledFetcherFn<TResponse> = {\n (\n opts: CompiledFetcherFnOptions & ServerFnBaseOptions<Method>,\n ): Promise<TResponse>\n url: string\n}\n\ntype ServerFnBaseOptions<\n TMethod extends Method = 'GET',\n TResponse = unknown,\n TMiddlewares = unknown,\n TInput = unknown,\n> = {\n method: TMethod\n validateClient?: boolean\n middleware?: Constrain<TMiddlewares, ReadonlyArray<AnyMiddleware>>\n validator?: ConstrainValidator<TInput>\n extractedFn?: CompiledFetcherFn<TResponse>\n serverFn?: ServerFn<TMethod, TMiddlewares, TInput, TResponse>\n functionId: string\n type: ServerFnTypeOrTypeFn<TMethod, TMiddlewares, AnyValidator>\n}\n\nexport type ValidatorSerializerStringify<TValidator> = Validator<\n SerializerStringifyBy<\n ResolveValidatorInput<TValidator>,\n Date | undefined | FormData\n >,\n any\n>\n\nexport type ConstrainValidator<TValidator> = unknown extends TValidator\n ? TValidator\n : Constrain<TValidator, ValidatorSerializerStringify<TValidator>>\n\nexport interface ServerFnMiddleware<TMethod extends Method, TValidator> {\n middleware: <const TNewMiddlewares = undefined>(\n middlewares: Constrain<TNewMiddlewares, ReadonlyArray<AnyMiddleware>>,\n ) => ServerFnAfterMiddleware<TMethod, TNewMiddlewares, TValidator>\n}\n\nexport interface ServerFnAfterMiddleware<\n TMethod extends Method,\n TMiddlewares,\n TValidator,\n> extends ServerFnValidator<TMethod, TMiddlewares>,\n ServerFnTyper<TMethod, TMiddlewares, TValidator>,\n ServerFnHandler<TMethod, TMiddlewares, TValidator> {}\n\nexport type ValidatorFn<TMethod extends Method, TMiddlewares> = <TValidator>(\n validator: ConstrainValidator<TValidator>,\n) => ServerFnAfterValidator<TMethod, TMiddlewares, TValidator>\n\nexport interface ServerFnValidator<TMethod extends Method, TMiddlewares> {\n validator: ValidatorFn<TMethod, TMiddlewares>\n}\n\nexport interface ServerFnAfterValidator<\n TMethod extends Method,\n TMiddlewares,\n TValidator,\n> extends ServerFnMiddleware<TMethod, TValidator>,\n ServerFnTyper<TMethod, TMiddlewares, TValidator>,\n ServerFnHandler<TMethod, TMiddlewares, TValidator> {}\n\n// Typer\nexport interface ServerFnTyper<\n TMethod extends Method,\n TMiddlewares,\n TValidator,\n> {\n type: (\n typer: ServerFnTypeOrTypeFn<TMethod, TMiddlewares, TValidator>,\n ) => ServerFnAfterTyper<TMethod, TMiddlewares, TValidator>\n}\n\nexport type ServerFnTypeOrTypeFn<\n TMethod extends Method,\n TMiddlewares,\n TValidator,\n> =\n | ServerFnType\n | ((ctx: ServerFnCtx<TMethod, TMiddlewares, TValidator>) => ServerFnType)\n\nexport interface ServerFnAfterTyper<\n TMethod extends Method,\n TMiddlewares,\n TValidator,\n> extends ServerFnHandler<TMethod, TMiddlewares, TValidator> {}\n\n// Handler\nexport interface ServerFnHandler<\n TMethod extends Method,\n TMiddlewares,\n TValidator,\n> {\n handler: <TNewResponse>(\n fn?: ServerFn<TMethod, TMiddlewares, TValidator, TNewResponse>,\n ) => Fetcher<TMiddlewares, TValidator, TNewResponse>\n}\n\nexport interface ServerFnBuilder<TMethod extends Method = 'GET'>\n extends ServerFnMiddleware<TMethod, undefined>,\n ServerFnValidator<TMethod, undefined>,\n ServerFnTyper<TMethod, undefined, undefined>,\n ServerFnHandler<TMethod, undefined, undefined> {\n options: ServerFnBaseOptions<TMethod, unknown, undefined, undefined>\n}\n\ntype StaticCachedResult = {\n ctx?: {\n result: any\n context: any\n }\n error?: any\n}\n\nexport type ServerFnStaticCache = {\n getItem: (\n ctx: MiddlewareResult,\n ) => StaticCachedResult | Promise<StaticCachedResult | undefined>\n setItem: (\n ctx: MiddlewareResult,\n response: StaticCachedResult,\n ) => Promise<void>\n fetchItem: (\n ctx: MiddlewareResult,\n ) => StaticCachedResult | Promise<StaticCachedResult | undefined>\n}\n\nlet serverFnStaticCache: ServerFnStaticCache | undefined\n\nexport function setServerFnStaticCache(\n cache?: ServerFnStaticCache | (() => ServerFnStaticCache | undefined),\n) {\n const previousCache = serverFnStaticCache\n serverFnStaticCache = typeof cache === 'function' ? cache() : cache\n\n return () => {\n serverFnStaticCache = previousCache\n }\n}\n\nexport function createServerFnStaticCache(\n serverFnStaticCache: ServerFnStaticCache,\n) {\n return serverFnStaticCache\n}\n\nsetServerFnStaticCache(() => {\n const getStaticCacheUrl = (options: MiddlewareResult, hash: string) => {\n return `/__tsr/staticServerFnCache/${options.functionId}__${hash}.json`\n }\n\n const jsonToFilenameSafeString = (json: any) => {\n // Custom replacer to sort keys\n const sortedKeysReplacer = (key: string, value: any) =>\n value && typeof value === 'object' && !Array.isArray(value)\n ? Object.keys(value)\n .sort()\n .reduce((acc: any, curr: string) => {\n acc[curr] = value[curr]\n return acc\n }, {})\n : value\n\n // Convert JSON to string with sorted keys\n const jsonString = JSON.stringify(json ?? '', sortedKeysReplacer)\n\n // Replace characters invalid in filenames\n return jsonString\n .replace(/[/\\\\?%*:|\"<>]/g, '-') // Replace invalid characters with a dash\n .replace(/\\s+/g, '_') // Optionally replace whitespace with underscores\n }\n\n const staticClientCache =\n typeof document !== 'undefined' ? new Map<string, any>() : null\n\n return createServerFnStaticCache({\n getItem: async (ctx) => {\n if (typeof document === 'undefined') {\n const hash = jsonToFilenameSafeString(ctx.data)\n const url = getStaticCacheUrl(ctx, hash)\n const publicUrl = process.env.TSS_OUTPUT_PUBLIC_DIR!\n\n // Use fs instead of fetch to read from filesystem\n const { promises: fs } = await import('node:fs')\n const path = await import('node:path')\n const filePath = path.join(publicUrl, url)\n\n const [cachedResult, readError] = await fs\n .readFile(filePath, 'utf-8')\n .then((c) => [\n startSerializer.parse(c) as {\n ctx: unknown\n error: any\n },\n null,\n ])\n .catch((e) => [null, e])\n\n if (readError && readError.code !== 'ENOENT') {\n throw readError\n }\n\n return cachedResult as StaticCachedResult\n }\n\n return undefined\n },\n setItem: async (ctx, response) => {\n const { promises: fs } = await import('node:fs')\n const path = await import('node:path')\n\n const hash = jsonToFilenameSafeString(ctx.data)\n const url = getStaticCacheUrl(ctx, hash)\n const publicUrl = process.env.TSS_OUTPUT_PUBLIC_DIR!\n const filePath = path.join(publicUrl, url)\n\n // Ensure the directory exists\n await fs.mkdir(path.dirname(filePath), { recursive: true })\n\n // Store the result with fs\n await fs.writeFile(filePath, startSerializer.stringify(response))\n },\n fetchItem: async (ctx) => {\n const hash = jsonToFilenameSafeString(ctx.data)\n const url = getStaticCacheUrl(ctx, hash)\n\n let result: any = staticClientCache?.get(url)\n\n if (!result) {\n result = await fetch(url, {\n method: 'GET',\n })\n .then((r) => r.text())\n .then((d) => startSerializer.parse(d))\n\n staticClientCache?.set(url, result)\n }\n\n return result\n },\n })\n})\n\nexport function createServerFn<\n TMethod extends Method,\n TResponse = unknown,\n TMiddlewares = undefined,\n TValidator = undefined,\n>(\n options?: {\n method?: TMethod\n type?: ServerFnType\n },\n __opts?: ServerFnBaseOptions<TMethod, TResponse, TMiddlewares, TValidator>,\n): ServerFnBuilder<TMethod> {\n const resolvedOptions = (__opts || options || {}) as ServerFnBaseOptions<\n TMethod,\n TResponse,\n TMiddlewares,\n TValidator\n >\n\n if (typeof resolvedOptions.method === 'undefined') {\n resolvedOptions.method = 'GET' as TMethod\n }\n\n return {\n options: resolvedOptions as any,\n middleware: (middleware) => {\n return createServerFn<TMethod, TResponse, TMiddlewares, TValidator>(\n undefined,\n Object.assign(resolvedOptions, { middleware }),\n ) as any\n },\n validator: (validator) => {\n return createServerFn<TMethod, TResponse, TMiddlewares, TValidator>(\n undefined,\n Object.assign(resolvedOptions, { validator }),\n ) as any\n },\n type: (type) => {\n return createServerFn<TMethod, TResponse, TMiddlewares, TValidator>(\n undefined,\n Object.assign(resolvedOptions, { type }),\n ) as any\n },\n handler: (...args) => {\n // This function signature changes due to AST transformations\n // in the babel plugin. We need to cast it to the correct\n // function signature post-transformation\n const [extractedFn, serverFn] = args as unknown as [\n CompiledFetcherFn<TResponse>,\n ServerFn<TMethod, TMiddlewares, TValidator, TResponse>,\n ]\n\n // Keep the original function around so we can use it\n // in the server environment\n Object.assign(resolvedOptions, {\n ...extractedFn,\n extractedFn,\n serverFn,\n })\n\n const resolvedMiddleware = [\n ...(resolvedOptions.middleware || []),\n serverFnBaseToMiddleware(resolvedOptions),\n ]\n\n // We want to make sure the new function has the same\n // properties as the original function\n return Object.assign(\n async (opts?: CompiledFetcherFnOptions) => {\n // Start by executing the client-side middleware chain\n return executeMiddleware(resolvedMiddleware, 'client', {\n ...extractedFn,\n ...resolvedOptions,\n data: opts?.data as any,\n headers: opts?.headers,\n signal: opts?.signal,\n context: {},\n }).then((d) => {\n if (d.error) throw d.error\n return d.result\n })\n },\n {\n // This copies over the URL, function ID\n ...extractedFn,\n // The extracted function on the server-side calls\n // this function\n __executeServer: async (opts_: any, signal: AbortSignal) => {\n const opts =\n opts_ instanceof FormData ? extractFormDataContext(opts_) : opts_\n\n opts.type =\n typeof resolvedOptions.type === 'function'\n ? resolvedOptions.type(opts)\n : resolvedOptions.type\n\n const ctx = {\n ...extractedFn,\n ...opts,\n signal,\n }\n\n const run = () =>\n executeMiddleware(resolvedMiddleware, 'server', ctx).then(\n (d) => ({\n // Only send the result and sendContext back to the client\n result: d.result,\n error: d.error,\n context: d.sendContext,\n }),\n )\n\n if (ctx.type === 'static') {\n let response: StaticCachedResult | undefined\n\n // If we can get the cached item, try to get it\n if (serverFnStaticCache?.getItem) {\n // If this throws, it's okay to let it bubble up\n response = await serverFnStaticCache.getItem(ctx)\n }\n\n if (!response) {\n // If there's no cached item, execute the server function\n response = await run()\n .then((d) => {\n return {\n ctx: d,\n error: null,\n }\n })\n .catch((e) => {\n return {\n ctx: undefined,\n error: e,\n }\n })\n\n if (serverFnStaticCache?.setItem) {\n await serverFnStaticCache.setItem(ctx, response)\n }\n }\n\n invariant(\n response,\n 'No response from both server and static cache!',\n )\n\n if (response.error) {\n throw response.error\n }\n\n return response.ctx\n }\n\n return run()\n },\n },\n ) as any\n },\n }\n}\n\nfunction extractFormDataContext(formData: FormData) {\n const serializedContext = formData.get('__TSR_CONTEXT')\n formData.delete('__TSR_CONTEXT')\n\n if (typeof serializedContext !== 'string') {\n return {\n context: {},\n data: formData,\n }\n }\n\n try {\n const context = startSerializer.parse(serializedContext)\n return {\n context,\n data: formData,\n }\n } catch {\n return {\n data: formData,\n }\n }\n}\n\nfunction flattenMiddlewares(\n middlewares: Array<AnyMiddleware>,\n): Array<AnyMiddleware> {\n const seen = new Set<AnyMiddleware>()\n const flattened: Array<AnyMiddleware> = []\n\n const recurse = (middleware: Array<AnyMiddleware>) => {\n middleware.forEach((m) => {\n if (m.options.middleware) {\n recurse(m.options.middleware)\n }\n\n if (!seen.has(m)) {\n seen.add(m)\n flattened.push(m)\n }\n })\n }\n\n recurse(middlewares)\n\n return flattened\n}\n\nexport type MiddlewareOptions = {\n method: Method\n data: any\n headers?: HeadersInit\n signal?: AbortSignal\n sendContext?: any\n context?: any\n type: ServerFnTypeOrTypeFn<any, any, any>\n functionId: string\n}\n\nexport type MiddlewareResult = MiddlewareOptions & {\n result?: unknown\n error?: unknown\n type: ServerFnTypeOrTypeFn<any, any, any>\n}\n\nexport type NextFn = (ctx: MiddlewareResult) => Promise<MiddlewareResult>\n\nexport type MiddlewareFn = (\n ctx: MiddlewareOptions & {\n next: NextFn\n },\n) => Promise<MiddlewareResult>\n\nconst applyMiddleware = async (\n middlewareFn: MiddlewareFn,\n ctx: MiddlewareOptions,\n nextFn: NextFn,\n) => {\n return middlewareFn({\n ...ctx,\n next: (async (userCtx: MiddlewareResult | undefined = {} as any) => {\n // Return the next middleware\n return nextFn({\n ...ctx,\n ...userCtx,\n context: {\n ...ctx.context,\n ...userCtx.context,\n },\n sendContext: {\n ...ctx.sendContext,\n ...(userCtx.sendContext ?? {}),\n },\n headers: mergeHeaders(ctx.headers, userCtx.headers),\n result:\n userCtx.result !== undefined ? userCtx.result : (ctx as any).result,\n error: userCtx.error ?? (ctx as any).error,\n })\n }) as any,\n } as any)\n}\n\nfunction execValidator(validator: AnyValidator, input: unknown): unknown {\n if (validator == null) return {}\n\n if ('~standard' in validator) {\n const result = validator['~standard'].validate(input)\n\n if (result instanceof Promise)\n throw new Error('Async validation not supported')\n\n if (result.issues)\n throw new Error(JSON.stringify(result.issues, undefined, 2))\n\n return result.value\n }\n\n if ('parse' in validator) {\n return validator.parse(input)\n }\n\n if (typeof validator === 'function') {\n return validator(input)\n }\n\n throw new Error('Invalid validator type!')\n}\n\nasync function executeMiddleware(\n middlewares: Array<AnyMiddleware>,\n env: 'client' | 'server',\n opts: MiddlewareOptions,\n): Promise<MiddlewareResult> {\n const flattenedMiddlewares = flattenMiddlewares([\n ...globalMiddleware,\n ...middlewares,\n ])\n\n const next: NextFn = async (ctx) => {\n // Get the next middleware\n const nextMiddleware = flattenedMiddlewares.shift()\n\n // If there are no more middlewares, return the context\n if (!nextMiddleware) {\n return ctx\n }\n\n if (\n nextMiddleware.options.validator &&\n (env === 'client' ? nextMiddleware.options.validateClient : true)\n ) {\n // Execute the middleware's input function\n ctx.data = await execValidator(nextMiddleware.options.validator, ctx.data)\n }\n\n const middlewareFn = (\n env === 'client'\n ? nextMiddleware.options.client\n : nextMiddleware.options.server\n ) as MiddlewareFn | undefined\n\n if (middlewareFn) {\n // Execute the middleware\n return applyMiddleware(middlewareFn, ctx, async (newCtx) => {\n return next(newCtx).catch((error) => {\n if (isRedirect(error) || isNotFound(error)) {\n return {\n ...newCtx,\n error,\n }\n }\n\n throw error\n })\n })\n }\n\n return next(ctx)\n }\n\n // Start the middleware chain\n return next({\n ...opts,\n headers: opts.headers || {},\n sendContext: opts.sendContext || {},\n context: opts.context || {},\n })\n}\n\nfunction serverFnBaseToMiddleware(\n options: ServerFnBaseOptions<any, any, any, any>,\n): AnyMiddleware {\n return {\n _types: undefined!,\n options: {\n validator: options.validator,\n validateClient: options.validateClient,\n client: async ({ next, sendContext, ...ctx }) => {\n const payload = {\n ...ctx,\n // switch the sendContext over to context\n context: sendContext,\n type: typeof ctx.type === 'function' ? ctx.type(ctx) : ctx.type,\n } as any\n\n if (\n ctx.type === 'static' &&\n process.env.NODE_ENV === 'production' &&\n typeof document !== 'undefined'\n ) {\n invariant(\n serverFnStaticCache,\n 'serverFnStaticCache.fetchItem is not available!',\n )\n\n const result = await serverFnStaticCache.fetchItem(payload)\n\n if (result) {\n if (result.error) {\n throw result.error\n }\n\n return next(result.ctx)\n }\n\n warning(\n result,\n `No static cache item found for ${payload.functionId}__${JSON.stringify(payload.data)}, falling back to server function...`,\n )\n }\n\n // Execute the extracted function\n // but not before serializing the context\n const res = await options.extractedFn?.(payload)\n\n return next(res) as unknown as MiddlewareClientFnResult<any, any, any>\n },\n server: async ({ next, ...ctx }) => {\n // Execute the server function\n const result = await options.serverFn?.(ctx)\n\n return next({\n ...ctx,\n result,\n } as any) as unknown as MiddlewareServerFnResult<any, any, any, any>\n },\n },\n }\n}\n"],"names":["serverFnStaticCache"],"mappings":";;;;;;AAuQA,IAAI;AAEG,SAAS,uBACd,OACA;AACA,QAAM,gBAAgB;AACtB,wBAAsB,OAAO,UAAU,aAAa,MAAU,IAAA;AAE9D,SAAO,MAAM;AACW,0BAAA;AAAA,EACxB;AACF;AAEO,SAAS,0BACdA,sBACA;AACOA,SAAAA;AACT;AAEA,uBAAuB,MAAM;AACrB,QAAA,oBAAoB,CAAC,SAA2B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AAA,YACE;AAAA,YACA;AAAA,UACF;AAEA,gBAAM,SAAS,MAAM,oBAAoB,UAAU,OAAO;AAE1D,cAAI,QAAQ;AACV,gBAAI,OAAO,OAAO;AAChB,oBAAM,OAAO;AAAA,YAAA;AAGR,mBAAA,KAAK,OAAO,GAAG;AAAA,UAAA;AAGxB;AAAA,YACE;AAAA,YACA,kCAAkC,QAAQ,UAAU,KAAK,KAAK,UAAU,QAAQ,IAAI,CAAC;AAAA,UACvF;AAAA,QAAA;AAKF,cAAM,MAAM,QAAM,aAAQ,gBAAR,iCAAsB;AAExC,eAAO,KAAK,GAAG;AAAA,MACjB;AAAA,MACA,QAAQ,OAAO,EAAE,MAAM,GAAG,UAAU;;AAElC,cAAM,SAAS,QAAM,aAAQ,aAAR,iCAAmB;AAExC,eAAO,KAAK;AAAA,UACV,GAAG;AAAA,UACH;AAAA,QAAA,CACM;AAAA,MAAA;AAAA,IACV;AAAA,EAEJ;AACF;"}
|
|
1
|
+
{"version":3,"file":"createServerFn.js","sources":["../../src/createServerFn.ts"],"sourcesContent":["import { default as invariant } from 'tiny-invariant'\nimport { default as warning } from 'tiny-warning'\nimport { isNotFound, isRedirect } from '@tanstack/react-router'\nimport { mergeHeaders } from './headers'\nimport { globalMiddleware } from './registerGlobalMiddleware'\nimport { startSerializer } from './serializer'\nimport type { Readable } from 'node:stream'\nimport type {\n AnyValidator,\n Constrain,\n Expand,\n ResolveValidatorInput,\n SerializerParse,\n SerializerStringify,\n SerializerStringifyBy,\n Validator,\n} from '@tanstack/router-core'\nimport type {\n AnyMiddleware,\n AssignAllClientSendContext,\n AssignAllServerContext,\n IntersectAllValidatorInputs,\n IntersectAllValidatorOutputs,\n MiddlewareClientFnResult,\n MiddlewareServerFnResult,\n} from './createMiddleware'\n\nexport interface JsonResponse<TData> extends Response {\n json: () => Promise<TData>\n}\n\nexport type CompiledFetcherFnOptions = {\n method: Method\n data: unknown\n response?: ServerFnResponseType\n headers?: HeadersInit\n signal?: AbortSignal\n context?: any\n}\n\nexport type Fetcher<\n TMiddlewares,\n TValidator,\n TResponse,\n TServerFnResponseType extends ServerFnResponseType,\n> =\n undefined extends IntersectAllValidatorInputs<TMiddlewares, TValidator>\n ? OptionalFetcher<\n TMiddlewares,\n TValidator,\n TResponse,\n TServerFnResponseType\n >\n : RequiredFetcher<\n TMiddlewares,\n TValidator,\n TResponse,\n TServerFnResponseType\n >\n\nexport interface FetcherBase {\n url: string\n __executeServer: (opts: {\n method: Method\n response?: ServerFnResponseType\n data: unknown\n headers?: HeadersInit\n context?: any\n signal: AbortSignal\n }) => Promise<unknown>\n}\n\nexport type FetchResult<\n TMiddlewares,\n TResponse,\n TServerFnResponseType extends ServerFnResponseType,\n> = TServerFnResponseType extends 'raw'\n ? Promise<Response>\n : TServerFnResponseType extends 'full'\n ? Promise<FullFetcherData<TMiddlewares, TResponse>>\n : Promise<FetcherData<TResponse>>\n\nexport interface OptionalFetcher<\n TMiddlewares,\n TValidator,\n TResponse,\n TServerFnResponseType extends ServerFnResponseType,\n> extends FetcherBase {\n (\n options?: OptionalFetcherDataOptions<TMiddlewares, TValidator>,\n ): FetchResult<TMiddlewares, TResponse, TServerFnResponseType>\n}\n\nexport interface RequiredFetcher<\n TMiddlewares,\n TValidator,\n TResponse,\n TServerFnResponseType extends ServerFnResponseType,\n> extends FetcherBase {\n (\n opts: RequiredFetcherDataOptions<TMiddlewares, TValidator>,\n ): FetchResult<TMiddlewares, TResponse, TServerFnResponseType>\n}\n\nexport type FetcherBaseOptions = {\n headers?: HeadersInit\n type?: ServerFnType\n signal?: AbortSignal\n}\n\nexport type ServerFnType = 'static' | 'dynamic'\n\nexport interface OptionalFetcherDataOptions<TMiddlewares, TValidator>\n extends FetcherBaseOptions {\n data?: Expand<IntersectAllValidatorInputs<TMiddlewares, TValidator>>\n}\n\nexport interface RequiredFetcherDataOptions<TMiddlewares, TValidator>\n extends FetcherBaseOptions {\n data: Expand<IntersectAllValidatorInputs<TMiddlewares, TValidator>>\n}\n\nexport interface FullFetcherData<TMiddlewares, TResponse> {\n error: unknown\n result: FetcherData<TResponse>\n context: AssignAllClientSendContext<TMiddlewares>\n}\n\nexport type FetcherData<TResponse> =\n TResponse extends JsonResponse<any>\n ? SerializerParse<ReturnType<TResponse['json']>>\n : SerializerParse<TResponse>\n\nexport type RscStream<T> = {\n __cacheState: T\n}\n\nexport type Method = 'GET' | 'POST'\nexport type ServerFnResponseType = 'data' | 'full' | 'raw'\n\n// see https://h3.unjs.io/guide/event-handler#responses-types\nexport type RawResponse = Response | ReadableStream | Readable | null | string\n\nexport type ServerFnReturnType<\n TServerFnResponseType extends ServerFnResponseType,\n TResponse,\n> = TServerFnResponseType extends 'raw'\n ? RawResponse | Promise<RawResponse>\n : Promise<SerializerStringify<TResponse>> | SerializerStringify<TResponse>\nexport type ServerFn<\n TMethod,\n TServerFnResponseType extends ServerFnResponseType,\n TMiddlewares,\n TValidator,\n TResponse,\n> = (\n ctx: ServerFnCtx<TMethod, TServerFnResponseType, TMiddlewares, TValidator>,\n) => ServerFnReturnType<TServerFnResponseType, TResponse>\n\nexport interface ServerFnCtx<\n TMethod,\n TServerFnResponseType extends ServerFnResponseType,\n TMiddlewares,\n TValidator,\n> {\n method: TMethod\n response: TServerFnResponseType\n data: Expand<IntersectAllValidatorOutputs<TMiddlewares, TValidator>>\n context: Expand<AssignAllServerContext<TMiddlewares>>\n signal: AbortSignal\n}\n\nexport type CompiledFetcherFn<\n TResponse,\n TServerFnResponseType extends ServerFnResponseType,\n> = {\n (\n opts: CompiledFetcherFnOptions &\n ServerFnBaseOptions<Method, TServerFnResponseType>,\n ): Promise<TResponse>\n url: string\n}\n\ntype ServerFnBaseOptions<\n TMethod extends Method = 'GET',\n TServerFnResponseType extends ServerFnResponseType = 'data',\n TResponse = unknown,\n TMiddlewares = unknown,\n TInput = unknown,\n> = {\n method: TMethod\n response?: TServerFnResponseType\n validateClient?: boolean\n middleware?: Constrain<TMiddlewares, ReadonlyArray<AnyMiddleware>>\n validator?: ConstrainValidator<TInput>\n extractedFn?: CompiledFetcherFn<TResponse, TServerFnResponseType>\n serverFn?: ServerFn<\n TMethod,\n TServerFnResponseType,\n TMiddlewares,\n TInput,\n TResponse\n >\n functionId: string\n type: ServerFnTypeOrTypeFn<\n TMethod,\n TServerFnResponseType,\n TMiddlewares,\n AnyValidator\n >\n}\n\nexport type ValidatorSerializerStringify<TValidator> = Validator<\n SerializerStringifyBy<\n ResolveValidatorInput<TValidator>,\n Date | undefined | FormData\n >,\n any\n>\n\nexport type ConstrainValidator<TValidator> = unknown extends TValidator\n ? TValidator\n : Constrain<TValidator, ValidatorSerializerStringify<TValidator>>\n\nexport interface ServerFnMiddleware<\n TMethod extends Method,\n TServerFnResponseType extends ServerFnResponseType,\n TValidator,\n> {\n middleware: <const TNewMiddlewares = undefined>(\n middlewares: Constrain<TNewMiddlewares, ReadonlyArray<AnyMiddleware>>,\n ) => ServerFnAfterMiddleware<\n TMethod,\n TServerFnResponseType,\n TNewMiddlewares,\n TValidator\n >\n}\n\nexport interface ServerFnAfterMiddleware<\n TMethod extends Method,\n TServerFnResponseType extends ServerFnResponseType,\n TMiddlewares,\n TValidator,\n> extends ServerFnValidator<TMethod, TServerFnResponseType, TMiddlewares>,\n ServerFnTyper<TMethod, TServerFnResponseType, TMiddlewares, TValidator>,\n ServerFnHandler<TMethod, TServerFnResponseType, TMiddlewares, TValidator> {}\n\nexport type ValidatorFn<\n TMethod extends Method,\n TServerFnResponseType extends ServerFnResponseType,\n TMiddlewares,\n> = <TValidator>(\n validator: ConstrainValidator<TValidator>,\n) => ServerFnAfterValidator<\n TMethod,\n TServerFnResponseType,\n TMiddlewares,\n TValidator\n>\n\nexport interface ServerFnValidator<\n TMethod extends Method,\n TServerFnResponseType extends ServerFnResponseType,\n TMiddlewares,\n> {\n validator: ValidatorFn<TMethod, TServerFnResponseType, TMiddlewares>\n}\n\nexport interface ServerFnAfterValidator<\n TMethod extends Method,\n TServerFnResponseType extends ServerFnResponseType,\n TMiddlewares,\n TValidator,\n> extends ServerFnMiddleware<TMethod, TServerFnResponseType, TValidator>,\n ServerFnTyper<TMethod, TServerFnResponseType, TMiddlewares, TValidator>,\n ServerFnHandler<TMethod, TServerFnResponseType, TMiddlewares, TValidator> {}\n\n// Typer\nexport interface ServerFnTyper<\n TMethod extends Method,\n TServerFnResponseType extends ServerFnResponseType,\n TMiddlewares,\n TValidator,\n> {\n type: (\n typer: ServerFnTypeOrTypeFn<\n TMethod,\n TServerFnResponseType,\n TMiddlewares,\n TValidator\n >,\n ) => ServerFnAfterTyper<\n TMethod,\n TServerFnResponseType,\n TMiddlewares,\n TValidator\n >\n}\n\nexport type ServerFnTypeOrTypeFn<\n TMethod extends Method,\n TServerFnResponseType extends ServerFnResponseType,\n TMiddlewares,\n TValidator,\n> =\n | ServerFnType\n | ((\n ctx: ServerFnCtx<\n TMethod,\n TServerFnResponseType,\n TMiddlewares,\n TValidator\n >,\n ) => ServerFnType)\n\nexport interface ServerFnAfterTyper<\n TMethod extends Method,\n TServerFnResponseType extends ServerFnResponseType,\n TMiddlewares,\n TValidator,\n> extends ServerFnHandler<\n TMethod,\n TServerFnResponseType,\n TMiddlewares,\n TValidator\n > {}\n\n// Handler\nexport interface ServerFnHandler<\n TMethod extends Method,\n TServerFnResponseType extends ServerFnResponseType,\n TMiddlewares,\n TValidator,\n> {\n handler: <TNewResponse>(\n fn?: ServerFn<\n TMethod,\n TServerFnResponseType,\n TMiddlewares,\n TValidator,\n TNewResponse\n >,\n ) => Fetcher<TMiddlewares, TValidator, TNewResponse, TServerFnResponseType>\n}\n\nexport interface ServerFnBuilder<\n TMethod extends Method = 'GET',\n TServerFnResponseType extends ServerFnResponseType = 'data',\n> extends ServerFnMiddleware<TMethod, TServerFnResponseType, undefined>,\n ServerFnValidator<TMethod, TServerFnResponseType, undefined>,\n ServerFnTyper<TMethod, TServerFnResponseType, undefined, undefined>,\n ServerFnHandler<TMethod, TServerFnResponseType, undefined, undefined> {\n options: ServerFnBaseOptions<\n TMethod,\n TServerFnResponseType,\n unknown,\n undefined,\n undefined\n >\n}\n\ntype StaticCachedResult = {\n ctx?: {\n result: any\n context: any\n }\n error?: any\n}\n\nexport type ServerFnStaticCache = {\n getItem: (\n ctx: MiddlewareResult,\n ) => StaticCachedResult | Promise<StaticCachedResult | undefined>\n setItem: (\n ctx: MiddlewareResult,\n response: StaticCachedResult,\n ) => Promise<void>\n fetchItem: (\n ctx: MiddlewareResult,\n ) => StaticCachedResult | Promise<StaticCachedResult | undefined>\n}\n\nlet serverFnStaticCache: ServerFnStaticCache | undefined\n\nexport function setServerFnStaticCache(\n cache?: ServerFnStaticCache | (() => ServerFnStaticCache | undefined),\n) {\n const previousCache = serverFnStaticCache\n serverFnStaticCache = typeof cache === 'function' ? cache() : cache\n\n return () => {\n serverFnStaticCache = previousCache\n }\n}\n\nexport function createServerFnStaticCache(\n serverFnStaticCache: ServerFnStaticCache,\n) {\n return serverFnStaticCache\n}\n\nsetServerFnStaticCache(() => {\n const getStaticCacheUrl = (options: MiddlewareResult, hash: string) => {\n return `/__tsr/staticServerFnCache/${options.functionId}__${hash}.json`\n }\n\n const jsonToFilenameSafeString = (json: any) => {\n // Custom replacer to sort keys\n const sortedKeysReplacer = (key: string, value: any) =>\n value && typeof value === 'object' && !Array.isArray(value)\n ? Object.keys(value)\n .sort()\n .reduce((acc: any, curr: string) => {\n acc[curr] = value[curr]\n return acc\n }, {})\n : value\n\n // Convert JSON to string with sorted keys\n const jsonString = JSON.stringify(json ?? '', sortedKeysReplacer)\n\n // Replace characters invalid in filenames\n return jsonString\n .replace(/[/\\\\?%*:|\"<>]/g, '-') // Replace invalid characters with a dash\n .replace(/\\s+/g, '_') // Optionally replace whitespace with underscores\n }\n\n const staticClientCache =\n typeof document !== 'undefined' ? new Map<string, any>() : null\n\n return createServerFnStaticCache({\n getItem: async (ctx) => {\n if (typeof document === 'undefined') {\n const hash = jsonToFilenameSafeString(ctx.data)\n const url = getStaticCacheUrl(ctx, hash)\n const publicUrl = process.env.TSS_OUTPUT_PUBLIC_DIR!\n\n // Use fs instead of fetch to read from filesystem\n const { promises: fs } = await import('node:fs')\n const path = await import('node:path')\n const filePath = path.join(publicUrl, url)\n\n const [cachedResult, readError] = await fs\n .readFile(filePath, 'utf-8')\n .then((c) => [\n startSerializer.parse(c) as {\n ctx: unknown\n error: any\n },\n null,\n ])\n .catch((e) => [null, e])\n\n if (readError && readError.code !== 'ENOENT') {\n throw readError\n }\n\n return cachedResult as StaticCachedResult\n }\n\n return undefined\n },\n setItem: async (ctx, response) => {\n const { promises: fs } = await import('node:fs')\n const path = await import('node:path')\n\n const hash = jsonToFilenameSafeString(ctx.data)\n const url = getStaticCacheUrl(ctx, hash)\n const publicUrl = process.env.TSS_OUTPUT_PUBLIC_DIR!\n const filePath = path.join(publicUrl, url)\n\n // Ensure the directory exists\n await fs.mkdir(path.dirname(filePath), { recursive: true })\n\n // Store the result with fs\n await fs.writeFile(filePath, startSerializer.stringify(response))\n },\n fetchItem: async (ctx) => {\n const hash = jsonToFilenameSafeString(ctx.data)\n const url = getStaticCacheUrl(ctx, hash)\n\n let result: any = staticClientCache?.get(url)\n\n if (!result) {\n result = await fetch(url, {\n method: 'GET',\n })\n .then((r) => r.text())\n .then((d) => startSerializer.parse(d))\n\n staticClientCache?.set(url, result)\n }\n\n return result\n },\n })\n})\n\nexport function createServerFn<\n TMethod extends Method,\n TServerFnResponseType extends ServerFnResponseType = 'data',\n TResponse = unknown,\n TMiddlewares = undefined,\n TValidator = undefined,\n>(\n options?: {\n method?: TMethod\n response?: TServerFnResponseType\n type?: ServerFnType\n },\n __opts?: ServerFnBaseOptions<\n TMethod,\n TServerFnResponseType,\n TResponse,\n TMiddlewares,\n TValidator\n >,\n): ServerFnBuilder<TMethod, TServerFnResponseType> {\n const resolvedOptions = (__opts || options || {}) as ServerFnBaseOptions<\n TMethod,\n ServerFnResponseType,\n TResponse,\n TMiddlewares,\n TValidator\n >\n\n if (typeof resolvedOptions.method === 'undefined') {\n resolvedOptions.method = 'GET' as TMethod\n }\n\n return {\n options: resolvedOptions as any,\n middleware: (middleware) => {\n return createServerFn<\n TMethod,\n ServerFnResponseType,\n TResponse,\n TMiddlewares,\n TValidator\n >(undefined, Object.assign(resolvedOptions, { middleware })) as any\n },\n validator: (validator) => {\n return createServerFn<\n TMethod,\n ServerFnResponseType,\n TResponse,\n TMiddlewares,\n TValidator\n >(undefined, Object.assign(resolvedOptions, { validator })) as any\n },\n type: (type) => {\n return createServerFn<\n TMethod,\n ServerFnResponseType,\n TResponse,\n TMiddlewares,\n TValidator\n >(undefined, Object.assign(resolvedOptions, { type })) as any\n },\n handler: (...args) => {\n // This function signature changes due to AST transformations\n // in the babel plugin. We need to cast it to the correct\n // function signature post-transformation\n const [extractedFn, serverFn] = args as unknown as [\n CompiledFetcherFn<TResponse, TServerFnResponseType>,\n ServerFn<\n TMethod,\n TServerFnResponseType,\n TMiddlewares,\n TValidator,\n TResponse\n >,\n ]\n\n // Keep the original function around so we can use it\n // in the server environment\n Object.assign(resolvedOptions, {\n ...extractedFn,\n extractedFn,\n serverFn,\n })\n\n const resolvedMiddleware = [\n ...(resolvedOptions.middleware || []),\n serverFnBaseToMiddleware(resolvedOptions),\n ]\n\n // We want to make sure the new function has the same\n // properties as the original function\n return Object.assign(\n async (opts?: CompiledFetcherFnOptions) => {\n // Start by executing the client-side middleware chain\n return executeMiddleware(resolvedMiddleware, 'client', {\n ...extractedFn,\n ...resolvedOptions,\n data: opts?.data as any,\n headers: opts?.headers,\n signal: opts?.signal,\n context: {},\n }).then((d) => {\n if (resolvedOptions.response === 'full') {\n return d\n }\n if (d.error) throw d.error\n return d.result\n })\n },\n {\n // This copies over the URL, function ID\n ...extractedFn,\n // The extracted function on the server-side calls\n // this function\n __executeServer: async (opts_: any, signal: AbortSignal) => {\n const opts =\n opts_ instanceof FormData ? extractFormDataContext(opts_) : opts_\n\n opts.type =\n typeof resolvedOptions.type === 'function'\n ? resolvedOptions.type(opts)\n : resolvedOptions.type\n\n const ctx = {\n ...extractedFn,\n ...opts,\n signal,\n }\n\n const run = () =>\n executeMiddleware(resolvedMiddleware, 'server', ctx).then(\n (d) => ({\n // Only send the result and sendContext back to the client\n result: d.result,\n error: d.error,\n context: d.sendContext,\n }),\n )\n\n if (ctx.type === 'static') {\n let response: StaticCachedResult | undefined\n\n // If we can get the cached item, try to get it\n if (serverFnStaticCache?.getItem) {\n // If this throws, it's okay to let it bubble up\n response = await serverFnStaticCache.getItem(ctx)\n }\n\n if (!response) {\n // If there's no cached item, execute the server function\n response = await run()\n .then((d) => {\n return {\n ctx: d,\n error: null,\n }\n })\n .catch((e) => {\n return {\n ctx: undefined,\n error: e,\n }\n })\n\n if (serverFnStaticCache?.setItem) {\n await serverFnStaticCache.setItem(ctx, response)\n }\n }\n\n invariant(\n response,\n 'No response from both server and static cache!',\n )\n\n if (response.error) {\n throw response.error\n }\n\n return response.ctx\n }\n\n return run()\n },\n },\n ) as any\n },\n }\n}\n\nfunction extractFormDataContext(formData: FormData) {\n const serializedContext = formData.get('__TSR_CONTEXT')\n formData.delete('__TSR_CONTEXT')\n\n if (typeof serializedContext !== 'string') {\n return {\n context: {},\n data: formData,\n }\n }\n\n try {\n const context = startSerializer.parse(serializedContext)\n return {\n context,\n data: formData,\n }\n } catch {\n return {\n data: formData,\n }\n }\n}\n\nfunction flattenMiddlewares(\n middlewares: Array<AnyMiddleware>,\n): Array<AnyMiddleware> {\n const seen = new Set<AnyMiddleware>()\n const flattened: Array<AnyMiddleware> = []\n\n const recurse = (middleware: Array<AnyMiddleware>) => {\n middleware.forEach((m) => {\n if (m.options.middleware) {\n recurse(m.options.middleware)\n }\n\n if (!seen.has(m)) {\n seen.add(m)\n flattened.push(m)\n }\n })\n }\n\n recurse(middlewares)\n\n return flattened\n}\n\nexport type MiddlewareOptions = {\n method: Method\n response?: ServerFnResponseType\n data: any\n headers?: HeadersInit\n signal?: AbortSignal\n sendContext?: any\n context?: any\n type: ServerFnTypeOrTypeFn<any, any, any, any>\n functionId: string\n}\n\nexport type MiddlewareResult = MiddlewareOptions & {\n result?: unknown\n error?: unknown\n type: ServerFnTypeOrTypeFn<any, any, any, any>\n}\n\nexport type NextFn = (ctx: MiddlewareResult) => Promise<MiddlewareResult>\n\nexport type MiddlewareFn = (\n ctx: MiddlewareOptions & {\n next: NextFn\n },\n) => Promise<MiddlewareResult>\n\nconst applyMiddleware = async (\n middlewareFn: MiddlewareFn,\n ctx: MiddlewareOptions,\n nextFn: NextFn,\n) => {\n return middlewareFn({\n ...ctx,\n next: (async (userCtx: MiddlewareResult | undefined = {} as any) => {\n // Return the next middleware\n return nextFn({\n ...ctx,\n ...userCtx,\n context: {\n ...ctx.context,\n ...userCtx.context,\n },\n sendContext: {\n ...ctx.sendContext,\n ...(userCtx.sendContext ?? {}),\n },\n headers: mergeHeaders(ctx.headers, userCtx.headers),\n result:\n userCtx.result !== undefined\n ? userCtx.result\n : ctx.response === 'raw'\n ? userCtx\n : (ctx as any).result,\n error: userCtx.error ?? (ctx as any).error,\n })\n }) as any,\n } as any)\n}\n\nfunction execValidator(validator: AnyValidator, input: unknown): unknown {\n if (validator == null) return {}\n\n if ('~standard' in validator) {\n const result = validator['~standard'].validate(input)\n\n if (result instanceof Promise)\n throw new Error('Async validation not supported')\n\n if (result.issues)\n throw new Error(JSON.stringify(result.issues, undefined, 2))\n\n return result.value\n }\n\n if ('parse' in validator) {\n return validator.parse(input)\n }\n\n if (typeof validator === 'function') {\n return validator(input)\n }\n\n throw new Error('Invalid validator type!')\n}\n\nasync function executeMiddleware(\n middlewares: Array<AnyMiddleware>,\n env: 'client' | 'server',\n opts: MiddlewareOptions,\n): Promise<MiddlewareResult> {\n const flattenedMiddlewares = flattenMiddlewares([\n ...globalMiddleware,\n ...middlewares,\n ])\n\n const next: NextFn = async (ctx) => {\n // Get the next middleware\n const nextMiddleware = flattenedMiddlewares.shift()\n\n // If there are no more middlewares, return the context\n if (!nextMiddleware) {\n return ctx\n }\n\n if (\n nextMiddleware.options.validator &&\n (env === 'client' ? nextMiddleware.options.validateClient : true)\n ) {\n // Execute the middleware's input function\n ctx.data = await execValidator(nextMiddleware.options.validator, ctx.data)\n }\n\n const middlewareFn = (\n env === 'client'\n ? nextMiddleware.options.client\n : nextMiddleware.options.server\n ) as MiddlewareFn | undefined\n\n if (middlewareFn) {\n // Execute the middleware\n return applyMiddleware(middlewareFn, ctx, async (newCtx) => {\n return next(newCtx).catch((error) => {\n if (isRedirect(error) || isNotFound(error)) {\n return {\n ...newCtx,\n error,\n }\n }\n\n throw error\n })\n })\n }\n\n return next(ctx)\n }\n\n // Start the middleware chain\n return next({\n ...opts,\n headers: opts.headers || {},\n sendContext: opts.sendContext || {},\n context: opts.context || {},\n })\n}\n\nfunction serverFnBaseToMiddleware(\n options: ServerFnBaseOptions<any, any, any, any, any>,\n): AnyMiddleware {\n return {\n _types: undefined!,\n options: {\n validator: options.validator,\n validateClient: options.validateClient,\n client: async ({ next, sendContext, ...ctx }) => {\n const payload = {\n ...ctx,\n // switch the sendContext over to context\n context: sendContext,\n type: typeof ctx.type === 'function' ? ctx.type(ctx) : ctx.type,\n } as any\n\n if (\n ctx.type === 'static' &&\n process.env.NODE_ENV === 'production' &&\n typeof document !== 'undefined'\n ) {\n invariant(\n serverFnStaticCache,\n 'serverFnStaticCache.fetchItem is not available!',\n )\n\n const result = await serverFnStaticCache.fetchItem(payload)\n\n if (result) {\n if (result.error) {\n throw result.error\n }\n\n return next(result.ctx)\n }\n\n warning(\n result,\n `No static cache item found for ${payload.functionId}__${JSON.stringify(payload.data)}, falling back to server function...`,\n )\n }\n\n // Execute the extracted function\n // but not before serializing the context\n const res = await options.extractedFn?.(payload)\n\n return next(res) as unknown as MiddlewareClientFnResult<any, any, any>\n },\n server: async ({ next, ...ctx }) => {\n // Execute the server function\n const result = await options.serverFn?.(ctx)\n\n return next({\n ...ctx,\n result,\n } as any) as unknown as MiddlewareServerFnResult<any, any, any, any>\n },\n },\n }\n}\n"],"names":["serverFnStaticCache"],"mappings":";;;;;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AA,MACxB,QAAQ,OAAO,EAAE,MAAM,aAAa,GAAG,UAAU;;AAC/C,cAAM,UAAU;AAAA,UACd,GAAG;AAAA;AAAA,UAEH,SAAS;AAAA,UACT,MAAM,OAAO,IAAI,SAAS,aAAa,IAAI,KAAK,GAAG,IAAI,IAAI;AAAA,QAC7D;AAGE,YAAA,IAAI,SAAS,YACb,QAAQ,IAAI,aAAa,gBACzB,OAAO,aAAa,aACpB;AACA;AAAA,YACE;AAAA,YACA;AAAA,UACF;AAEA,gBAAM,SAAS,MAAM,oBAAoB,UAAU,OAAO;AAE1D,cAAI,QAAQ;AACV,gBAAI,OAAO,OAAO;AAChB,oBAAM,OAAO;AAAA,YAAA;AAGR,mBAAA,KAAK,OAAO,GAAG;AAAA,UAAA;AAGxB;AAAA,YACE;AAAA,YACA,kCAAkC,QAAQ,UAAU,KAAK,KAAK,UAAU,QAAQ,IAAI,CAAC;AAAA,UACvF;AAAA,QAAA;AAKF,cAAM,MAAM,QAAM,aAAQ,gBAAR,iCAAsB;AAExC,eAAO,KAAK,GAAG;AAAA,MACjB;AAAA,MACA,QAAQ,OAAO,EAAE,MAAM,GAAG,UAAU;;AAElC,cAAM,SAAS,QAAM,aAAQ,aAAR,iCAAmB;AAExC,eAAO,KAAK;AAAA,UACV,GAAG;AAAA,UACH;AAAA,QAAA,CACM;AAAA,MAAA;AAAA,IACV;AAAA,EAEJ;AACF;"}
|
package/dist/esm/ssr-client.js
CHANGED
|
@@ -38,24 +38,16 @@ function hydrate(router) {
|
|
|
38
38
|
})
|
|
39
39
|
);
|
|
40
40
|
matches.forEach((match) => {
|
|
41
|
-
var _a2
|
|
42
|
-
const route = router.looseRoutesById[match.routeId];
|
|
41
|
+
var _a2;
|
|
43
42
|
const dehydratedMatch = window.__TSR_SSR__.matches.find(
|
|
44
43
|
(d) => d.id === match.id
|
|
45
44
|
);
|
|
46
45
|
if (dehydratedMatch) {
|
|
47
46
|
Object.assign(match, dehydratedMatch);
|
|
48
|
-
const parentMatch = matches[match.index - 1];
|
|
49
|
-
const parentContext = (parentMatch == null ? void 0 : parentMatch.context) ?? router.options.context ?? {};
|
|
50
47
|
if (dehydratedMatch.__beforeLoadContext) {
|
|
51
48
|
match.__beforeLoadContext = router.ssr.serializer.parse(
|
|
52
49
|
dehydratedMatch.__beforeLoadContext
|
|
53
50
|
);
|
|
54
|
-
match.context = {
|
|
55
|
-
...parentContext,
|
|
56
|
-
...match.__routeContext,
|
|
57
|
-
...match.__beforeLoadContext
|
|
58
|
-
};
|
|
59
51
|
}
|
|
60
52
|
if (dehydratedMatch.loaderData) {
|
|
61
53
|
match.loaderData = router.ssr.serializer.parse(
|
|
@@ -74,27 +66,51 @@ function hydrate(router) {
|
|
|
74
66
|
updatedAt: Date.now()
|
|
75
67
|
});
|
|
76
68
|
}
|
|
69
|
+
return match;
|
|
70
|
+
});
|
|
71
|
+
router.__store.setState((s) => {
|
|
72
|
+
return {
|
|
73
|
+
...s,
|
|
74
|
+
matches
|
|
75
|
+
};
|
|
76
|
+
});
|
|
77
|
+
(_c = (_b = router.options).hydrate) == null ? void 0 : _c.call(_b, dehydratedData);
|
|
78
|
+
router.state.matches.forEach((match) => {
|
|
79
|
+
var _a2, _b2, _c2, _d, _e, _f;
|
|
80
|
+
const route = router.looseRoutesById[match.routeId];
|
|
81
|
+
const parentMatch = router.state.matches[match.index - 1];
|
|
82
|
+
const parentContext = (parentMatch == null ? void 0 : parentMatch.context) ?? router.options.context ?? {};
|
|
83
|
+
const contextFnContext = {
|
|
84
|
+
deps: match.loaderDeps,
|
|
85
|
+
params: match.params,
|
|
86
|
+
context: parentContext,
|
|
87
|
+
location: router.state.location,
|
|
88
|
+
navigate: (opts) => router.navigate({ ...opts, _fromLocation: router.state.location }),
|
|
89
|
+
buildLocation: router.buildLocation,
|
|
90
|
+
cause: match.cause,
|
|
91
|
+
abortController: match.abortController,
|
|
92
|
+
preload: false,
|
|
93
|
+
matches
|
|
94
|
+
};
|
|
95
|
+
match.__routeContext = ((_b2 = (_a2 = route.options).context) == null ? void 0 : _b2.call(_a2, contextFnContext)) ?? {};
|
|
96
|
+
match.context = {
|
|
97
|
+
...parentContext,
|
|
98
|
+
...match.__routeContext,
|
|
99
|
+
...match.__beforeLoadContext
|
|
100
|
+
};
|
|
77
101
|
const assetContext = {
|
|
78
102
|
matches: router.state.matches,
|
|
79
103
|
match,
|
|
80
104
|
params: match.params,
|
|
81
105
|
loaderData: match.loaderData
|
|
82
106
|
};
|
|
83
|
-
const headFnContent = (
|
|
84
|
-
const scripts = (
|
|
107
|
+
const headFnContent = (_d = (_c2 = route.options).head) == null ? void 0 : _d.call(_c2, assetContext);
|
|
108
|
+
const scripts = (_f = (_e = route.options).scripts) == null ? void 0 : _f.call(_e, assetContext);
|
|
85
109
|
match.meta = headFnContent == null ? void 0 : headFnContent.meta;
|
|
86
110
|
match.links = headFnContent == null ? void 0 : headFnContent.links;
|
|
87
111
|
match.headScripts = headFnContent == null ? void 0 : headFnContent.scripts;
|
|
88
112
|
match.scripts = scripts;
|
|
89
|
-
return match;
|
|
90
113
|
});
|
|
91
|
-
router.__store.setState((s) => {
|
|
92
|
-
return {
|
|
93
|
-
...s,
|
|
94
|
-
matches
|
|
95
|
-
};
|
|
96
|
-
});
|
|
97
|
-
(_c = (_b = router.options).hydrate) == null ? void 0 : _c.call(_b, dehydratedData);
|
|
98
114
|
return routeChunkPromise;
|
|
99
115
|
}
|
|
100
116
|
function deepMutableSetByPath(obj, path, value) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ssr-client.js","sources":["../../src/ssr-client.tsx"],"sourcesContent":["import { isPlainObject } from '@tanstack/router-core'\n\nimport invariant from 'tiny-invariant'\n\nimport { startSerializer } from './serializer'\nimport type {\n AnyRouter,\n ControllablePromise,\n MakeRouteMatch,\n} from '@tanstack/react-router'\n\nimport type {
|
|
1
|
+
{"version":3,"file":"ssr-client.js","sources":["../../src/ssr-client.tsx"],"sourcesContent":["import { isPlainObject } from '@tanstack/router-core'\n\nimport invariant from 'tiny-invariant'\n\nimport { startSerializer } from './serializer'\nimport type {\n AnyRouter,\n ControllablePromise,\n MakeRouteMatch,\n} from '@tanstack/react-router'\n\nimport type {\n DeferredPromiseState,\n Manifest,\n RouteContextOptions,\n} from '@tanstack/router-core'\n\ndeclare global {\n interface Window {\n __TSR_SSR__?: StartSsrGlobal\n }\n}\n\nexport interface StartSsrGlobal {\n matches: Array<SsrMatch>\n streamedValues: Record<\n string,\n {\n value: any\n parsed: any\n }\n >\n cleanScripts: () => void\n dehydrated?: any\n initMatch: (match: SsrMatch) => void\n resolvePromise: (opts: {\n matchId: string\n id: number\n promiseState: DeferredPromiseState<any>\n }) => void\n injectChunk: (opts: { matchId: string; id: number; chunk: string }) => void\n closeStream: (opts: { matchId: string; id: number }) => void\n}\n\nexport interface SsrMatch {\n id: string\n __beforeLoadContext: string\n loaderData?: string\n error?: string\n extracted?: Array<ClientExtractedEntry>\n updatedAt: MakeRouteMatch['updatedAt']\n status: MakeRouteMatch['status']\n}\n\nexport type ClientExtractedEntry =\n | ClientExtractedStream\n | ClientExtractedPromise\n\nexport interface ClientExtractedPromise extends ClientExtractedBaseEntry {\n type: 'promise'\n value?: ControllablePromise<any>\n}\n\nexport interface ClientExtractedStream extends ClientExtractedBaseEntry {\n type: 'stream'\n value?: ReadableStream & { controller?: ReadableStreamDefaultController }\n}\n\nexport interface ClientExtractedBaseEntry {\n type: string\n path: Array<string>\n}\n\nexport interface ResolvePromiseState {\n matchId: string\n id: number\n promiseState: DeferredPromiseState<any>\n}\n\nexport interface DehydratedRouter {\n manifest: Manifest | undefined\n dehydratedData: any\n}\n\nexport function hydrate(router: AnyRouter) {\n invariant(\n window.__TSR_SSR__?.dehydrated,\n 'Expected to find a dehydrated data on window.__TSR_SSR__.dehydrated... but we did not. Please file an issue!',\n )\n\n const { manifest, dehydratedData } = startSerializer.parse(\n window.__TSR_SSR__.dehydrated,\n ) as DehydratedRouter\n\n router.ssr = {\n manifest,\n serializer: startSerializer,\n }\n\n router.clientSsr = {\n getStreamedValue: <T,>(key: string): T | undefined => {\n if (router.isServer) {\n return undefined\n }\n\n const streamedValue = window.__TSR_SSR__?.streamedValues[key]\n\n if (!streamedValue) {\n return\n }\n\n if (!streamedValue.parsed) {\n streamedValue.parsed = router.ssr!.serializer.parse(streamedValue.value)\n }\n\n return streamedValue.parsed\n },\n }\n\n // Hydrate the router state\n const matches = router.matchRoutes(router.state.location)\n // kick off loading the route chunks\n const routeChunkPromise = Promise.all(\n matches.map((match) => {\n const route = router.looseRoutesById[match.routeId]!\n return router.loadRouteChunk(route)\n }),\n )\n // Right after hydration and before the first render, we need to rehydrate each match\n // First step is to reyhdrate loaderData and __beforeLoadContext\n matches.forEach((match) => {\n const dehydratedMatch = window.__TSR_SSR__!.matches.find(\n (d) => d.id === match.id,\n )\n\n if (dehydratedMatch) {\n Object.assign(match, dehydratedMatch)\n\n // Handle beforeLoadContext\n if (dehydratedMatch.__beforeLoadContext) {\n match.__beforeLoadContext = router.ssr!.serializer.parse(\n dehydratedMatch.__beforeLoadContext,\n ) as any\n }\n\n // Handle loaderData\n if (dehydratedMatch.loaderData) {\n match.loaderData = router.ssr!.serializer.parse(\n dehydratedMatch.loaderData,\n )\n }\n\n // Handle error\n if (dehydratedMatch.error) {\n match.error = router.ssr!.serializer.parse(dehydratedMatch.error)\n }\n\n // Handle extracted\n ;(match as unknown as SsrMatch).extracted?.forEach((ex) => {\n deepMutableSetByPath(match, ['loaderData', ...ex.path], ex.value)\n })\n } else {\n Object.assign(match, {\n status: 'success',\n updatedAt: Date.now(),\n })\n }\n\n return match\n })\n\n router.__store.setState((s) => {\n return {\n ...s,\n matches,\n }\n })\n\n // Allow the user to handle custom hydration data\n router.options.hydrate?.(dehydratedData)\n\n // now that all necessary data is hydrated:\n // 1) fully reconstruct the route context\n // 2) execute `head()` and `scripts()` for each match\n router.state.matches.forEach((match) => {\n const route = router.looseRoutesById[match.routeId]!\n\n const parentMatch = router.state.matches[match.index - 1]\n const parentContext = parentMatch?.context ?? router.options.context ?? {}\n\n // `context()` was already executed by `matchRoutes`, however route context was not yet fully reconstructed\n // so run it again and merge route context\n const contextFnContext: RouteContextOptions<any, any, any, any> = {\n deps: match.loaderDeps,\n params: match.params,\n context: parentContext,\n location: router.state.location,\n navigate: (opts: any) =>\n router.navigate({ ...opts, _fromLocation: router.state.location }),\n buildLocation: router.buildLocation,\n cause: match.cause,\n abortController: match.abortController,\n preload: false,\n matches,\n }\n match.__routeContext = route.options.context?.(contextFnContext) ?? {}\n\n match.context = {\n ...parentContext,\n ...match.__routeContext,\n ...match.__beforeLoadContext,\n }\n\n const assetContext = {\n matches: router.state.matches,\n match,\n params: match.params,\n loaderData: match.loaderData,\n }\n const headFnContent = route.options.head?.(assetContext)\n\n const scripts = route.options.scripts?.(assetContext)\n\n match.meta = headFnContent?.meta\n match.links = headFnContent?.links\n match.headScripts = headFnContent?.scripts\n match.scripts = scripts\n })\n\n return routeChunkPromise\n}\n\nfunction deepMutableSetByPath<T>(obj: T, path: Array<string>, value: any) {\n // mutable set by path retaining array and object references\n if (path.length === 1) {\n ;(obj as any)[path[0]!] = value\n }\n\n const [key, ...rest] = path\n\n if (Array.isArray(obj)) {\n deepMutableSetByPath(obj[Number(key)], rest, value)\n } else if (isPlainObject(obj)) {\n deepMutableSetByPath((obj as any)[key!], rest, value)\n }\n}\n"],"names":["_a","_b","_c"],"mappings":";;;AAoFO,SAAS,QAAQ,QAAmB;;AACzC;AAAA,KACE,YAAO,gBAAP,mBAAoB;AAAA,IACpB;AAAA,EACF;AAEA,QAAM,EAAE,UAAU,eAAe,IAAI,gBAAgB;AAAA,IACnD,OAAO,YAAY;AAAA,EACrB;AAEA,SAAO,MAAM;AAAA,IACX;AAAA,IACA,YAAY;AAAA,EACd;AAEA,SAAO,YAAY;AAAA,IACjB,kBAAkB,CAAK,QAA+B;;AACpD,UAAI,OAAO,UAAU;AACZ,eAAA;AAAA,MAAA;AAGT,YAAM,iBAAgBA,MAAA,OAAO,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wB,KAAQ,MAAqB,OAAY;AAEpE,MAAA,KAAK,WAAW,GAAG;AACnB,QAAY,KAAK,CAAC,CAAE,IAAI;AAAA,EAAA;AAG5B,QAAM,CAAC,KAAK,GAAG,IAAI,IAAI;AAEnB,MAAA,MAAM,QAAQ,GAAG,GAAG;AACtB,yBAAqB,IAAI,OAAO,GAAG,CAAC,GAAG,MAAM,KAAK;AAAA,EAAA,WACzC,cAAc,GAAG,GAAG;AAC7B,yBAAsB,IAAY,GAAI,GAAG,MAAM,KAAK;AAAA,EAAA;AAExD;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/react-start-client",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.112.0",
|
|
4
4
|
"description": "Modern and scalable routing for React applications",
|
|
5
5
|
"author": "Tanner Linsley",
|
|
6
6
|
"license": "MIT",
|
|
@@ -52,8 +52,8 @@
|
|
|
52
52
|
"tiny-invariant": "^1.3.3",
|
|
53
53
|
"tiny-warning": "^1.0.3",
|
|
54
54
|
"vinxi": "^0.5.3",
|
|
55
|
-
"@tanstack/react-router": "^1.
|
|
56
|
-
"@tanstack/router-core": "^1.
|
|
55
|
+
"@tanstack/react-router": "^1.112.0",
|
|
56
|
+
"@tanstack/router-core": "^1.112.0"
|
|
57
57
|
},
|
|
58
58
|
"devDependencies": {
|
|
59
59
|
"@testing-library/react": "^16.2.0",
|
package/src/createMiddleware.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type {
|
|
2
2
|
ConstrainValidator,
|
|
3
3
|
Method,
|
|
4
|
+
ServerFnResponseType,
|
|
4
5
|
ServerFnTypeOrTypeFn,
|
|
5
6
|
} from './createServerFn'
|
|
6
7
|
import type {
|
|
@@ -131,6 +132,7 @@ export interface MiddlewareOptions<
|
|
|
131
132
|
in out TValidator,
|
|
132
133
|
in out TServerContext,
|
|
133
134
|
in out TClientContext,
|
|
135
|
+
in out TServerFnResponseType extends ServerFnResponseType,
|
|
134
136
|
> {
|
|
135
137
|
validateClient?: boolean
|
|
136
138
|
middleware?: TMiddlewares
|
|
@@ -139,14 +141,16 @@ export interface MiddlewareOptions<
|
|
|
139
141
|
TMiddlewares,
|
|
140
142
|
TValidator,
|
|
141
143
|
TServerContext,
|
|
142
|
-
TClientContext
|
|
144
|
+
TClientContext,
|
|
145
|
+
TServerFnResponseType
|
|
143
146
|
>
|
|
144
147
|
server?: MiddlewareServerFn<
|
|
145
148
|
TMiddlewares,
|
|
146
149
|
TValidator,
|
|
147
150
|
TServerContext,
|
|
148
151
|
unknown,
|
|
149
|
-
unknown
|
|
152
|
+
unknown,
|
|
153
|
+
TServerFnResponseType
|
|
150
154
|
>
|
|
151
155
|
}
|
|
152
156
|
|
|
@@ -169,10 +173,12 @@ export interface MiddlewareServerFnOptions<
|
|
|
169
173
|
in out TMiddlewares,
|
|
170
174
|
in out TValidator,
|
|
171
175
|
in out TServerSendContext,
|
|
176
|
+
in out TServerFnResponseType,
|
|
172
177
|
> {
|
|
173
178
|
data: Expand<IntersectAllValidatorOutputs<TMiddlewares, TValidator>>
|
|
174
179
|
context: Expand<AssignAllServerContext<TMiddlewares, TServerSendContext>>
|
|
175
180
|
next: MiddlewareServerNextFn<TMiddlewares, TServerSendContext>
|
|
181
|
+
response: TServerFnResponseType
|
|
176
182
|
method: Method
|
|
177
183
|
filename: string
|
|
178
184
|
functionId: string
|
|
@@ -185,11 +191,13 @@ export type MiddlewareServerFn<
|
|
|
185
191
|
TServerSendContext,
|
|
186
192
|
TNewServerContext,
|
|
187
193
|
TSendContext,
|
|
194
|
+
TServerFnResponseType extends ServerFnResponseType,
|
|
188
195
|
> = (
|
|
189
196
|
options: MiddlewareServerFnOptions<
|
|
190
197
|
TMiddlewares,
|
|
191
198
|
TValidator,
|
|
192
|
-
TServerSendContext
|
|
199
|
+
TServerSendContext,
|
|
200
|
+
TServerFnResponseType
|
|
193
201
|
>,
|
|
194
202
|
) => MiddlewareServerFnResult<
|
|
195
203
|
TMiddlewares,
|
|
@@ -233,16 +241,23 @@ export type MiddlewareClientNextFn<TMiddlewares> = <
|
|
|
233
241
|
export interface MiddlewareClientFnOptions<
|
|
234
242
|
in out TMiddlewares,
|
|
235
243
|
in out TValidator,
|
|
244
|
+
in out TServerFnResponseType extends ServerFnResponseType,
|
|
236
245
|
> {
|
|
237
246
|
data: Expand<IntersectAllValidatorInputs<TMiddlewares, TValidator>>
|
|
238
247
|
context: Expand<AssignAllClientContextBeforeNext<TMiddlewares>>
|
|
239
248
|
sendContext: Expand<AssignAllServerSendContext<TMiddlewares>>
|
|
240
249
|
method: Method
|
|
250
|
+
response: TServerFnResponseType
|
|
241
251
|
signal: AbortSignal
|
|
242
252
|
next: MiddlewareClientNextFn<TMiddlewares>
|
|
243
253
|
filename: string
|
|
244
254
|
functionId: string
|
|
245
|
-
type: ServerFnTypeOrTypeFn<
|
|
255
|
+
type: ServerFnTypeOrTypeFn<
|
|
256
|
+
Method,
|
|
257
|
+
TServerFnResponseType,
|
|
258
|
+
TMiddlewares,
|
|
259
|
+
TValidator
|
|
260
|
+
>
|
|
246
261
|
}
|
|
247
262
|
|
|
248
263
|
export type MiddlewareClientFn<
|
|
@@ -250,8 +265,13 @@ export type MiddlewareClientFn<
|
|
|
250
265
|
TValidator,
|
|
251
266
|
TSendContext,
|
|
252
267
|
TClientContext,
|
|
268
|
+
TServerFnResponseType extends ServerFnResponseType,
|
|
253
269
|
> = (
|
|
254
|
-
options: MiddlewareClientFnOptions<
|
|
270
|
+
options: MiddlewareClientFnOptions<
|
|
271
|
+
TMiddlewares,
|
|
272
|
+
TValidator,
|
|
273
|
+
TServerFnResponseType
|
|
274
|
+
>,
|
|
255
275
|
) => MiddlewareClientFnResult<TMiddlewares, TSendContext, TClientContext>
|
|
256
276
|
|
|
257
277
|
export type MiddlewareClientFnResult<
|
|
@@ -290,7 +310,15 @@ export type ClientResultWithContext<
|
|
|
290
310
|
headers: HeadersInit
|
|
291
311
|
}
|
|
292
312
|
|
|
293
|
-
export type AnyMiddleware = MiddlewareWithTypes<
|
|
313
|
+
export type AnyMiddleware = MiddlewareWithTypes<
|
|
314
|
+
any,
|
|
315
|
+
any,
|
|
316
|
+
any,
|
|
317
|
+
any,
|
|
318
|
+
any,
|
|
319
|
+
any,
|
|
320
|
+
any
|
|
321
|
+
>
|
|
294
322
|
|
|
295
323
|
export interface MiddlewareTypes<
|
|
296
324
|
in out TMiddlewares,
|
|
@@ -341,6 +369,7 @@ export interface MiddlewareWithTypes<
|
|
|
341
369
|
TServerSendContext,
|
|
342
370
|
TClientContext,
|
|
343
371
|
TClientSendContext,
|
|
372
|
+
TServerFnResponseType extends ServerFnResponseType,
|
|
344
373
|
> {
|
|
345
374
|
_types: MiddlewareTypes<
|
|
346
375
|
TMiddlewares,
|
|
@@ -354,26 +383,44 @@ export interface MiddlewareWithTypes<
|
|
|
354
383
|
TMiddlewares,
|
|
355
384
|
TValidator,
|
|
356
385
|
TServerContext,
|
|
357
|
-
TClientContext
|
|
386
|
+
TClientContext,
|
|
387
|
+
TServerFnResponseType
|
|
358
388
|
>
|
|
359
389
|
}
|
|
360
390
|
|
|
361
|
-
export interface MiddlewareAfterValidator<
|
|
362
|
-
|
|
391
|
+
export interface MiddlewareAfterValidator<
|
|
392
|
+
TMiddlewares,
|
|
393
|
+
TValidator,
|
|
394
|
+
TServerFnResponseType extends ServerFnResponseType,
|
|
395
|
+
> extends MiddlewareWithTypes<
|
|
363
396
|
TMiddlewares,
|
|
364
397
|
TValidator,
|
|
365
398
|
undefined,
|
|
366
399
|
undefined,
|
|
367
400
|
undefined,
|
|
368
|
-
undefined
|
|
401
|
+
undefined,
|
|
402
|
+
ServerFnResponseType
|
|
403
|
+
>,
|
|
404
|
+
MiddlewareServer<
|
|
405
|
+
TMiddlewares,
|
|
406
|
+
TValidator,
|
|
407
|
+
undefined,
|
|
408
|
+
undefined,
|
|
409
|
+
TServerFnResponseType
|
|
369
410
|
>,
|
|
370
|
-
|
|
371
|
-
MiddlewareClient<TMiddlewares, TValidator> {}
|
|
411
|
+
MiddlewareClient<TMiddlewares, TValidator, ServerFnResponseType> {}
|
|
372
412
|
|
|
373
|
-
export interface MiddlewareValidator<
|
|
413
|
+
export interface MiddlewareValidator<
|
|
414
|
+
TMiddlewares,
|
|
415
|
+
TServerFnResponseType extends ServerFnResponseType,
|
|
416
|
+
> {
|
|
374
417
|
validator: <TNewValidator>(
|
|
375
418
|
input: ConstrainValidator<TNewValidator>,
|
|
376
|
-
) => MiddlewareAfterValidator<
|
|
419
|
+
) => MiddlewareAfterValidator<
|
|
420
|
+
TMiddlewares,
|
|
421
|
+
TNewValidator,
|
|
422
|
+
TServerFnResponseType
|
|
423
|
+
>
|
|
377
424
|
}
|
|
378
425
|
|
|
379
426
|
export interface MiddlewareAfterServer<
|
|
@@ -383,13 +430,15 @@ export interface MiddlewareAfterServer<
|
|
|
383
430
|
TServerSendContext,
|
|
384
431
|
TClientContext,
|
|
385
432
|
TClientSendContext,
|
|
433
|
+
TServerFnResponseType extends ServerFnResponseType,
|
|
386
434
|
> extends MiddlewareWithTypes<
|
|
387
435
|
TMiddlewares,
|
|
388
436
|
TValidator,
|
|
389
437
|
TServerContext,
|
|
390
438
|
TServerSendContext,
|
|
391
439
|
TClientContext,
|
|
392
|
-
TClientSendContext
|
|
440
|
+
TClientSendContext,
|
|
441
|
+
TServerFnResponseType
|
|
393
442
|
> {}
|
|
394
443
|
|
|
395
444
|
export interface MiddlewareServer<
|
|
@@ -397,6 +446,7 @@ export interface MiddlewareServer<
|
|
|
397
446
|
TValidator,
|
|
398
447
|
TServerSendContext,
|
|
399
448
|
TClientContext,
|
|
449
|
+
TServerFnResponseType extends ServerFnResponseType,
|
|
400
450
|
> {
|
|
401
451
|
server: <TNewServerContext = undefined, TSendContext = undefined>(
|
|
402
452
|
server: MiddlewareServerFn<
|
|
@@ -404,7 +454,8 @@ export interface MiddlewareServer<
|
|
|
404
454
|
TValidator,
|
|
405
455
|
TServerSendContext,
|
|
406
456
|
TNewServerContext,
|
|
407
|
-
TSendContext
|
|
457
|
+
TSendContext,
|
|
458
|
+
TServerFnResponseType
|
|
408
459
|
>,
|
|
409
460
|
) => MiddlewareAfterServer<
|
|
410
461
|
TMiddlewares,
|
|
@@ -412,7 +463,8 @@ export interface MiddlewareServer<
|
|
|
412
463
|
TNewServerContext,
|
|
413
464
|
TServerSendContext,
|
|
414
465
|
TClientContext,
|
|
415
|
-
TSendContext
|
|
466
|
+
TSendContext,
|
|
467
|
+
ServerFnResponseType
|
|
416
468
|
>
|
|
417
469
|
}
|
|
418
470
|
|
|
@@ -421,62 +473,87 @@ export interface MiddlewareAfterClient<
|
|
|
421
473
|
TValidator,
|
|
422
474
|
TServerSendContext,
|
|
423
475
|
TClientContext,
|
|
476
|
+
TServerFnResponseType extends ServerFnResponseType,
|
|
424
477
|
> extends MiddlewareWithTypes<
|
|
425
478
|
TMiddlewares,
|
|
426
479
|
TValidator,
|
|
427
480
|
undefined,
|
|
428
481
|
TServerSendContext,
|
|
429
482
|
TClientContext,
|
|
430
|
-
undefined
|
|
483
|
+
undefined,
|
|
484
|
+
TServerFnResponseType
|
|
431
485
|
>,
|
|
432
486
|
MiddlewareServer<
|
|
433
487
|
TMiddlewares,
|
|
434
488
|
TValidator,
|
|
435
489
|
TServerSendContext,
|
|
436
|
-
TClientContext
|
|
490
|
+
TClientContext,
|
|
491
|
+
TServerFnResponseType
|
|
437
492
|
> {}
|
|
438
493
|
|
|
439
|
-
export interface MiddlewareClient<
|
|
494
|
+
export interface MiddlewareClient<
|
|
495
|
+
TMiddlewares,
|
|
496
|
+
TValidator,
|
|
497
|
+
TServerFnResponseType extends ServerFnResponseType,
|
|
498
|
+
> {
|
|
440
499
|
client: <TSendServerContext = undefined, TNewClientContext = undefined>(
|
|
441
500
|
client: MiddlewareClientFn<
|
|
442
501
|
TMiddlewares,
|
|
443
502
|
TValidator,
|
|
444
503
|
TSendServerContext,
|
|
445
|
-
TNewClientContext
|
|
504
|
+
TNewClientContext,
|
|
505
|
+
TServerFnResponseType
|
|
446
506
|
>,
|
|
447
507
|
) => MiddlewareAfterClient<
|
|
448
508
|
TMiddlewares,
|
|
449
509
|
TValidator,
|
|
450
510
|
TSendServerContext,
|
|
451
|
-
TNewClientContext
|
|
511
|
+
TNewClientContext,
|
|
512
|
+
ServerFnResponseType
|
|
452
513
|
>
|
|
453
514
|
}
|
|
454
515
|
|
|
455
|
-
export interface MiddlewareAfterMiddleware<
|
|
456
|
-
|
|
516
|
+
export interface MiddlewareAfterMiddleware<
|
|
517
|
+
TMiddlewares,
|
|
518
|
+
TServerFnResponseType extends ServerFnResponseType,
|
|
519
|
+
> extends MiddlewareWithTypes<
|
|
457
520
|
TMiddlewares,
|
|
458
521
|
undefined,
|
|
459
522
|
undefined,
|
|
460
523
|
undefined,
|
|
461
524
|
undefined,
|
|
462
|
-
undefined
|
|
525
|
+
undefined,
|
|
526
|
+
TServerFnResponseType
|
|
527
|
+
>,
|
|
528
|
+
MiddlewareServer<
|
|
529
|
+
TMiddlewares,
|
|
530
|
+
undefined,
|
|
531
|
+
undefined,
|
|
532
|
+
undefined,
|
|
533
|
+
TServerFnResponseType
|
|
463
534
|
>,
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
MiddlewareValidator<TMiddlewares> {}
|
|
535
|
+
MiddlewareClient<TMiddlewares, undefined, TServerFnResponseType>,
|
|
536
|
+
MiddlewareValidator<TMiddlewares, TServerFnResponseType> {}
|
|
467
537
|
|
|
468
|
-
export interface Middleware extends
|
|
538
|
+
export interface Middleware<TServerFnResponseType extends ServerFnResponseType>
|
|
539
|
+
extends MiddlewareAfterMiddleware<unknown, TServerFnResponseType> {
|
|
469
540
|
middleware: <const TNewMiddlewares = undefined>(
|
|
470
541
|
middlewares: Constrain<TNewMiddlewares, ReadonlyArray<AnyMiddleware>>,
|
|
471
|
-
) => MiddlewareAfterMiddleware<TNewMiddlewares>
|
|
542
|
+
) => MiddlewareAfterMiddleware<TNewMiddlewares, TServerFnResponseType>
|
|
472
543
|
}
|
|
473
544
|
|
|
474
545
|
export function createMiddleware(
|
|
475
546
|
options?: {
|
|
476
547
|
validateClient?: boolean
|
|
477
548
|
},
|
|
478
|
-
__opts?: MiddlewareOptions<
|
|
479
|
-
|
|
549
|
+
__opts?: MiddlewareOptions<
|
|
550
|
+
unknown,
|
|
551
|
+
undefined,
|
|
552
|
+
undefined,
|
|
553
|
+
undefined,
|
|
554
|
+
ServerFnResponseType
|
|
555
|
+
>,
|
|
556
|
+
): Middleware<ServerFnResponseType> {
|
|
480
557
|
// const resolvedOptions = (__opts || options) as MiddlewareOptions<
|
|
481
558
|
const resolvedOptions =
|
|
482
559
|
__opts ||
|
|
@@ -484,7 +561,8 @@ export function createMiddleware(
|
|
|
484
561
|
unknown,
|
|
485
562
|
undefined,
|
|
486
563
|
undefined,
|
|
487
|
-
undefined
|
|
564
|
+
undefined,
|
|
565
|
+
ServerFnResponseType
|
|
488
566
|
>)
|
|
489
567
|
|
|
490
568
|
return {
|
|
@@ -513,5 +591,5 @@ export function createMiddleware(
|
|
|
513
591
|
Object.assign(resolvedOptions, { server }),
|
|
514
592
|
) as any
|
|
515
593
|
},
|
|
516
|
-
} as unknown as Middleware
|
|
594
|
+
} as unknown as Middleware<ServerFnResponseType>
|
|
517
595
|
}
|