import { FarmCacheInvalidationTarget } from './cache.mjs'; type MultipartField = string | number | boolean | bigint | Blob | Date | null | undefined; type MultipartValues = Record; /** * A real FormData value that retains the submitted value shape for generated * API-client inference. */ type TypedFormData = FormData & { readonly __farmMultipartInput: TValues; }; type MultipartSchema = TSchema & { readonly __farmMultipartSchema: true; }; type FarmStreamResponse = Response & { readonly __farmStreamItem: TItem; }; interface FarmAPIStream extends AsyncIterable { readonly response: Response; cancel(reason?: unknown): Promise; } type SchemaLike = { parse(data: unknown): unknown; }; /** * Mark a body schema as multipart. The handler still receives the schema's * parsed object; generated clients require `toFormData(...)` for the request. */ declare function multipart(schema: TSchema): MultipartSchema; declare function isMultipartSchema(value: unknown): value is MultipartSchema; /** * Encode a typed object as multipart FormData without converting File or Blob * values to JSON or base64. */ declare function toFormData(values: TValues): TypedFormData; /** * Stream JSON values as newline-delimited JSON. Each source value becomes one * independently decodable item instead of buffering the whole response. */ declare function jsonStream(source: AsyncIterable | Iterable, init?: ResponseInit): FarmStreamResponse; declare function isJSONStreamResponse(response: { headers?: Pick | null; }): boolean; /** * Decode a Farm JSON stream lazily. The response body is read only as the * consumer advances the async iterator, preserving fetch backpressure. */ declare function readJSONStream(response: Response): FarmAPIStream; declare function isFarmAPIStream(value: unknown): value is FarmAPIStream; /** Server-only schema contract shared by declarative routes and the API runtime. */ interface RouteSchema { readonly _input?: unknown; readonly _output?: unknown; parse?: (value: unknown) => unknown; parseAsync?: (value: unknown) => Promise; readonly "~standard"?: { readonly types?: { input: unknown; output: unknown; }; validate(value: unknown): unknown; }; } type RouteSchemaInput = T extends { _input: infer I; } ? I : T extends { "~standard": { types?: { input: infer I; }; }; } ? I : unknown; type RouteSchemaOutput = T extends { _output: infer O; } ? O : T extends { "~standard": { types?: { output: infer O; }; }; } ? O : T extends { parse: (...args: any[]) => infer O; } ? Awaited : unknown; type AnySchema = RouteSchema; type MaybePromise = T | Promise; type Simplify = { [TKey in keyof T]: T[TKey]; } & {}; type UnionToIntersection$1 = (T extends unknown ? (value: T) => void : never) extends (value: infer TIntersection) => void ? TIntersection : never; type InferOutput = RouteSchemaOutput; type InferInput = T extends { _input: infer I; } ? I : T extends { "~standard": unknown; } ? RouteSchemaInput : T extends { parse: (data: infer I) => unknown; } ? I : unknown; type InferBodyInput = T extends MultipartSchema ? TypedFormData> : InferInput; type InferHeadersOutput = [T] extends [never] ? Record : T extends AnySchema ? InferOutput : Record; type EndpointErrorSchema = AnySchema & { parse: (data: unknown) => unknown; }; type EndpointErrorDefinitionBase = { status: TStatus; /** Public message safe to expose to API callers. */ message?: string; }; type EndpointErrorDefinition = EndpointErrorDefinitionBase & ({ /** Schema for the public error payload exposed to API callers. */ data: TSchema; schema?: never; } | { data?: never; /** @deprecated Use `data` for consistency with server functions. */ schema: TSchema; }); type EndpointErrorDefinitions = Record>; type InferEndpointErrorSchema = TDefinition extends { data: infer TSchema extends AnySchema; } ? TSchema : TDefinition extends { schema: infer TSchema extends AnySchema; } ? TSchema : never; type EndpointErrorContracts = { [TCode in keyof TErrors]: { data: InferOutput>; status: TErrors[TCode]["status"]; }; }; type EndpointErrorHandler = (code: TCode, data: InferInput>) => never; /** @deprecated Use `EndpointErrorHandler`. */ type EndpointFail = EndpointErrorHandler; declare class EndpointFailure extends Error { readonly code: TCode; readonly data: TData; readonly status: number; constructor(code: TCode, data: TData, options: { status: number; message: string; }); } declare function isEndpointFailure(value: unknown): value is EndpointFailure; type EndpointParamValue = string | string[]; type EndpointParams = Record; type EndpointMiddlewareContext> = { body: TBody; query: TQuery; headers: THeaders; request: Request; /** Context accumulated from middleware that ran earlier in the chain. */ context: Readonly; params: EndpointParams; }; type EndpointMiddlewareResult = TProvidedContext | true | false | Response; /** A plain async function. No wrapper or `next()` callback is required. */ type EndpointMiddleware> = (ctx: EndpointMiddlewareContext) => MaybePromise>; type AnyEndpointMiddleware = (ctx: EndpointMiddlewareContext) => unknown; type EndpointInvalidationTarget = FarmCacheInvalidationTarget; type EndpointInvalidationContext> = EndpointMiddlewareContext & { /** The raw value returned by the endpoint handler. */ result: unknown; }; type EndpointInvalidations> = readonly EndpointInvalidationTarget[] | ((context: EndpointInvalidationContext) => readonly EndpointInvalidationTarget[] | Promise); type ValidateEndpointMiddlewares = { readonly [TIndex in keyof TMiddlewares]: TMiddlewares[TIndex] extends AnyEndpointMiddleware ? [Awaited>] extends [EndpointMiddlewareResult] ? TMiddlewares[TIndex] : never : never; }; type ContextFromMiddlewareResult = Exclude extends infer TContext ? [TContext] extends [never] ? {} : TContext extends object ? TContext : {} : {}; type ContextFromMiddleware = TMiddleware extends (...args: any[]) => infer TResult ? ContextFromMiddlewareResult> : {}; type InferEndpointMiddlewareContext any)[], TContext extends object = {}> = TMiddlewares extends readonly [infer TMiddleware, ...infer TRest] ? TMiddleware extends (...args: any[]) => any ? TRest extends readonly ((...args: any[]) => any)[] ? InferEndpointMiddlewareContext>> : Simplify : Simplify : number extends TMiddlewares["length"] ? Simplify>> : Simplify; type EndpointOptions = { method?: "GET" | "HEAD" | "QUERY" | "POST" | "PUT" | "DELETE" | "PATCH" | "OPTIONS"; body?: TBody; query?: TQuery; headers?: THeaders; middleware?: TMiddlewares; /** * Cache keys, tags, and route paths made stale after a successful handler result. * The resolver receives validated input and middleware context. */ invalidates?: EndpointInvalidations, InferOutput, InferOutput, InferHeadersOutput>; errors?: TErrors; /** @deprecated Use plain functions in `middleware` for Farm endpoint middleware. */ use?: any[]; }; type EndpointHandler = (ctx: { body: InferOutput; query: InferOutput; headers: InferHeadersOutput; request: Request; context: Readonly; params: EndpointParams; error: EndpointErrorHandler; /** @deprecated Use `error`. */ fail: EndpointFail; }) => Promise | TResponse; type TypedEndpoint, TErrors = never, TBodyInput = TBody, TQueryInput = TQuery> = { __types: { body: TBody; inputBody: TBodyInput; query: TQuery; inputQuery: TQueryInput; headers: THeaders; response: TResponse; errors: TErrors; }; __path?: string; __method?: string; } & ((options?: { body?: TBodyInput; query?: TQueryInput; }) => Promise); type CreatedEndpoint = TypedEndpoint, InferOutput, Awaited, InferHeadersOutput, EndpointErrorContracts, InferBodyInput, InferInput>; type AnyEndpointOptions = EndpointOptions; type EndpointBodyFromOptions = TOptions extends { body: infer TBody extends AnySchema; } ? TBody : never; type EndpointQueryFromOptions = TOptions extends { query: infer TQuery extends AnySchema; } ? TQuery : never; type EndpointHeadersFromOptions = TOptions extends { headers: infer THeaders extends AnySchema; } ? THeaders : never; type EndpointMiddlewaresFromOptions = TOptions extends { middleware: infer TMiddlewares extends readonly AnyEndpointMiddleware[]; } ? TMiddlewares : readonly []; type EndpointErrorsFromOptions = TOptions extends { errors: infer TErrors extends EndpointErrorDefinitions; } ? TErrors : {}; type MethodlessEndpointOptions = Omit; type EndpointHandlerFromOptions = EndpointHandler, EndpointQueryFromOptions, EndpointHeadersFromOptions, TResponse, InferEndpointMiddlewareContext>, EndpointErrorsFromOptions>; type ValidatedEndpointHandlerFromOptions = EndpointHandlerFromOptions & (EndpointMiddlewaresFromOptions extends ValidateEndpointMiddlewares> ? unknown : never); type CreatedEndpointFromOptions = CreatedEndpoint, EndpointQueryFromOptions, EndpointHeadersFromOptions, TResponse, EndpointErrorsFromOptions>; /** * Create a Farm.js API endpoint * * Supports two patterns: * 1. File-based routing (path auto-inferred from file location): * `export const POST = createEndpoint({ method: 'POST', body: schema }, handler)` * `createEndpoint({ method: 'GET', query: z.object({...}) }, handler)` * * 2. Explicit path (for routes.ts at project root): * `createEndpoint('/api/hello', { method: 'GET', query: z.object({...}) }, handler)` */ declare function createEndpoint(options: TOptions, handler: ValidatedEndpointHandlerFromOptions): CreatedEndpointFromOptions; declare function createEndpoint(path: string, options: TOptions, handler: ValidatedEndpointHandlerFromOptions): CreatedEndpointFromOptions; declare function createEndpoint(options: EndpointOptions, handler: EndpointHandler): CreatedEndpoint; declare function createEndpoint(path: string, options: EndpointOptions, handler: EndpointHandler): CreatedEndpoint; /** * Convenience method for GET requests */ declare function GET(handler: EndpointHandler): CreatedEndpoint; declare function GET(options: TOptions, handler: ValidatedEndpointHandlerFromOptions): CreatedEndpointFromOptions; declare function GET(options: Omit, "method">, handler: EndpointHandler): CreatedEndpoint; /** * Convenience method for HEAD requests */ declare function HEAD(handler: EndpointHandler): CreatedEndpoint; declare function HEAD(options: TOptions, handler: ValidatedEndpointHandlerFromOptions): CreatedEndpointFromOptions; declare function HEAD(options: Omit, "method">, handler: EndpointHandler): CreatedEndpoint; /** * Convenience method for safe, idempotent QUERY requests with a request body */ declare function QUERY(handler: EndpointHandler): CreatedEndpoint; declare function QUERY(options: TOptions, handler: ValidatedEndpointHandlerFromOptions): CreatedEndpointFromOptions; declare function QUERY(options: Omit, "method">, handler: EndpointHandler): CreatedEndpoint; /** * Convenience method for POST requests */ declare function POST(handler: EndpointHandler): CreatedEndpoint; declare function POST(options: TOptions, handler: ValidatedEndpointHandlerFromOptions): CreatedEndpointFromOptions; declare function POST(options: Omit, "method">, handler: EndpointHandler): CreatedEndpoint; /** * Convenience method for PUT requests */ declare function PUT(handler: EndpointHandler): CreatedEndpoint; declare function PUT(options: TOptions, handler: ValidatedEndpointHandlerFromOptions): CreatedEndpointFromOptions; declare function PUT(options: Omit, "method">, handler: EndpointHandler): CreatedEndpoint; /** * Convenience method for DELETE requests */ declare function DELETE(handler: EndpointHandler): CreatedEndpoint; declare function DELETE(options: TOptions, handler: ValidatedEndpointHandlerFromOptions): CreatedEndpointFromOptions; declare function DELETE(options: Omit, "method">, handler: EndpointHandler): CreatedEndpoint; /** * Convenience method for PATCH requests */ declare function PATCH(handler: EndpointHandler): CreatedEndpoint; declare function PATCH(options: TOptions, handler: ValidatedEndpointHandlerFromOptions): CreatedEndpointFromOptions; declare function PATCH(options: Omit, "method">, handler: EndpointHandler): CreatedEndpoint; /** * Convenience method for OPTIONS requests */ declare function OPTIONS(handler: EndpointHandler): CreatedEndpoint; declare function OPTIONS(options: TOptions, handler: ValidatedEndpointHandlerFromOptions): CreatedEndpointFromOptions; declare function OPTIONS(options: Omit, "method">, handler: EndpointHandler): CreatedEndpoint; type RouteMethod = "GET" | "HEAD" | "QUERY" | "POST" | "PUT" | "PATCH" | "DELETE" | "OPTIONS"; type RoutePathParams = Path extends `${infer Head}/${infer Tail}` ? RoutePathParams & RoutePathParams : Path extends `[[...${infer Name}]]` ? { [K in Name]?: string[]; } : Path extends `[...${infer Name}]` ? { [K in Name]: string[]; } : Path extends `[${infer Name}]` ? { [K in Name]: string; } : {}; interface RouteInputSchemas { body?: RouteSchema; query?: RouteSchema; params?: RouteSchema; headers?: RouteSchema; } type Input = K extends keyof I ? RouteSchemaInput : never; type Output = K extends keyof I ? RouteSchemaOutput : Fallback; /** JSON responses have wire types, not server-side instances such as Date. */ type RouteJSON = T extends Response ? unknown : T extends { toJSON(): infer J; } ? RouteJSON : T extends bigint | symbol | ((...args: any[]) => any) ? never : T extends readonly (infer V)[] ? RouteJSON[] : T extends object ? { [K in keyof T]: RouteJSON; } : T; type RouteDefinition

= { readonly path: P; readonly method: M; readonly endpoint: E; }; type RouteEndpoint

= TypedEndpoint, Input, RouteJSON, Input, never, Input> & { __types: { params: RoutePathParams

; inputHeaders: Input; }; }; type TrimStart

= P extends `/${infer Rest}` ? TrimStart : P; type Join

= C extends "" ? P : `${P}/${TrimStart}`; type ParamsCheck

= I extends { params: infer S; } ? Exclude, keyof RouteSchemaOutput> extends never ? Exclude, keyof RoutePathParams

> extends never ? unknown : { __error_unknown_route_params: never; } : { __error_missing_route_params: never; } : unknown; type RouteMiddlewareResults = readonly (EndpointMiddlewareResult | Promise)[]; type RouteMiddlewares = { readonly [K in keyof Results]: (context: Parameters[0]) => Results[K]; }; type RouteOptions

= { input?: I & ParamsCheck; /** Validate plain JSON handler results. Raw Response/stream results are never buffered. */ output?: O; middleware?: RouteMiddlewares; handler(request: Request, context: { input: { body: Output; query: Output>; headers: Output>; params: Output>; }; params: Output>; context: Readonly>>; }): R | Promise; }; type RouteBuilder

= (path: C, options: RouteOptions, I, O, R, MiddlewareResults>) => RouteDefinition, M, RouteEndpoint, I, Extract, Response> extends never ? O extends RouteSchema ? RouteSchemaOutput : Awaited : unknown>>; type RouteFactory

= { [M in Lowercase]: RouteBuilder & RouteMethod>; } & { /** Return a new builder; never mutate the parent scope. */ scope(path: C): RouteFactory>; }; type PluginRoutes = readonly RouteDefinition[]; type PluginRoutesFactory = (context: { route: RouteFactory; }) => R; declare function createRouteFactory(): RouteFactory; type UnionToIntersection = (U extends unknown ? (v: U) => void : never) extends (v: infer I) => void ? I : never; type RouteTree

= P extends `${infer Head}/${infer Tail}` ? { [K in Head]: RouteTree; } : P extends "" ? { [K in Lowercase]: E; } : { [K in P]: { [Method in Lowercase]: E; }; }; type HasMethodSegment

= P extends `${infer H}/${infer T}` ? H extends Lowercase ? true : HasMethodSegment : P extends Lowercase ? true : false; type DefinitionTree = D extends RouteDefinition ? string extends P ? {} : (P extends `/api/integrations${string}` ? true : HasMethodSegment

) extends true ? { [K in P extends `/api/${infer C}` ? `/${C}` : "/"]: { [Method in Lowercase]: E; }; } : RouteTree

: {}; type PluginAPIRouter = C extends { plugins: readonly (infer P)[]; } ? UnionToIntersection

; } ? DefinitionTree : {}> : {}; declare function resolvePluginRoutes(plugins?: readonly { name: string; routes?: PluginRoutesFactory; }[]): PluginRoutes; export { type PluginAPIRouter as $, type AnyEndpointMiddleware as A, isMultipartSchema as B, toFormData as C, DELETE as D, EndpointFailure as E, type FarmStreamResponse as F, GET as G, HEAD as H, type InferEndpointMiddlewareContext as I, jsonStream as J, isJSONStreamResponse as K, readJSONStream as L, type MultipartField as M, isFarmAPIStream as N, OPTIONS as O, type PluginRoutesFactory as P, QUERY as Q, type RouteMethod as R, type RoutePathParams as S, type TypedEndpoint as T, type RouteInputSchemas as U, type RouteJSON as V, type RouteDefinition as W, type RouteOptions as X, type RouteFactory as Y, type PluginRoutes as Z, createRouteFactory as _, type EndpointErrorSchema as a, resolvePluginRoutes as a0, type EndpointErrorDefinition as b, type EndpointErrorDefinitions as c, type EndpointErrorContracts as d, type EndpointErrorHandler as e, type EndpointFail as f, type EndpointParamValue as g, type EndpointParams as h, isEndpointFailure as i, type EndpointMiddlewareContext as j, type EndpointMiddlewareResult as k, type EndpointMiddleware as l, type EndpointInvalidationTarget as m, type EndpointInvalidationContext as n, type EndpointInvalidations as o, type EndpointOptions as p, type EndpointHandler as q, createEndpoint as r, POST as s, PUT as t, PATCH as u, type MultipartValues as v, type TypedFormData as w, type MultipartSchema as x, type FarmAPIStream as y, multipart as z };