import z from "zod/v4"; import { AnyZodOrCoValueSchema, CoMapSchema, CoMapSchemaInit, Loaded, ResolveQuery, ResolveQueryStrict, Simplify } from "../internal.js"; import { Account } from "./account.js"; type MessageShape = Record; type RequestSchemaDefinition> = true> = S | { schema: S; resolve?: R; }; /** * Configuration options for defining HTTP request/response schemas in Jazz. * * This interface defines the structure for creating typed HTTP routes with * request and response validation using CoMap schemas. * * @template RequestShape - The shape of the request message schema (must extend MessageShape) * @template RequestResolve - The resolve query type for the request CoMap schema * @template ResponseShape - The shape of the response message schema (must extend MessageShape) * @template ResponseResolve - The resolve query type for the response CoMap schema */ interface RequestOptions>, ResponseShape extends MessageShape, ResponseResolve extends ResolveQuery>> { /** * The URL endpoint for the HTTP route. * This is used by the client to send requests to the server. */ url: string; /** * The id of the worker Account or Group. */ workerId: string; /** * Schema definition for the request payload. * Can be either a direct schema object or an object with schema and optional resolve properties. * The schema defines the structure and validation rules for incoming requests. */ request: RequestSchemaDefinition, RequestResolve>>; /** * Schema definition for the response payload. * Can be either a direct schema object or an object with schema and optional resolve properties. * The schema defines the structure and validation rules for outgoing responses. */ response: RequestSchemaDefinition, ResponseResolve>>; } type AsNullablePayload = T extends Record ? undefined : never; type MessageValuePayload = Simplify> | AsNullablePayload; export declare class HttpRoute> = any, ResponseShape extends MessageShape = z.core.$ZodLooseShape, ResponseResolve extends ResolveQuery> = any> { private requestDefinition; private responseDefinition; private url; private workerId; constructor(params: RequestOptions); send(values: MessageValuePayload, options?: { owner?: Account; }): Promise, ResponseResolve>>; handle: (request: Request, as: Account, callback: (value: Loaded, RequestResolve>, madeBy: Account) => Promise> | MessageValuePayload) => Promise; executeHandleRequest: (request: Request, as: Account, callback: (value: Loaded, RequestResolve>, madeBy: Account) => Promise> | MessageValuePayload) => Promise; get requestSchema(): CoMapSchema; get responseSchema(): CoMapSchema; } /** * Define a request route. * * @param params - The parameters for the request route. * @returns The request route. * * @see {@link https://jazz.tools/docs/react/server-side/http-requests} */ export declare function experimental_defineRequest>, ResponseShape extends MessageShape, ResponseResolve extends ResolveQuery>>(params: RequestOptions): HttpRoute; export declare class JazzRequestError { readonly message: string; readonly code: number; readonly details?: unknown | undefined; readonly isJazzRequestError = true; constructor(message: string, code: number, details?: unknown | undefined); toJSON(): { message: string; code: number; details: unknown; }; } export declare function isJazzRequestError(error: unknown): error is JazzRequestError; /** * Authenticates a Request by verifying a signed authentication token. * * - If a token is not provided, the returned account is `undefined` and no error is returned. * - If a valid token is provided, the signer account is returned. * - If an invalid token is provided, an error is returned detailing the validation error, and the returned account is `undefined`. * * @see {@link generateAuthToken} for generating a token. * * Note: This function does not perform any authorization checks, it only verifies if - **when provided** - a token is valid. It is up to the caller to perform any additional authorization checks, if needed. * * @param request - The request to authenticate. * @param options - The options for the authentication. * @param options.expiration - The expiration time of the token in milliseconds, defaults to 1 minute. * @param options.loadAs - The account to load the token from, defaults to the current active account. * @param options.getToken - If specified, this function will be used to get the token from the request. By default the token is expected to be in the `Authorization` header in the form of `Jazz `. * @returns The account if it is valid, otherwise an error. * * @example * ```ts * const { account, error } = await authenticateRequest(request); * if (error) { * return new Response(JSON.stringify(error), { status: 401 }); * } * ``` */ export declare function authenticateRequest(request: Request, options?: { expiration?: number; loadAs?: Account; getToken?: (request: Request) => string | undefined | null; }): Promise<{ account?: Account; error?: never; } | { account?: never; error: { message: string; details?: unknown; }; }>; /** * Generates an authentication token for a given account. This token can be used to authenticate a request. See {@link authenticateRequest} for more details. * * @param as - The account to generate the token for, defaults to the current active account. * @returns The authentication token. * * @example Make a fetch request with the token * ```ts * const token = generateAuthToken(); * const response = await fetch(url, { * headers: { * Authorization: `Jazz ${token}`, * }, * }); * ``` */ export declare function generateAuthToken(as?: Account): string; export declare function parseAuthToken(authToken: string, options?: { loadAs?: Account; expiration?: number; }): Promise<{ account: Account; error?: never; } | { account?: never; error: { message: string; details?: unknown; }; }>; export {}; //# sourceMappingURL=request.d.ts.map