import type { z } from "zod"; import type { FunctionRuntimeProps } from "../function-props.js"; import type { HttpMethod } from "../http-method.js"; import { CommandSpec } from "../internal/service-spec.js"; import type { ServiceContext } from "../service.js"; import type { Middleware } from "./middleware.js"; import type { ParsePath } from "./path.js"; export interface CommandContext { service: ServiceContext; } export type AnyCommand = Command; export interface Command extends Omit, "input" | "outputs" | "output"> { kind: "Command"; input?: z.ZodType; output?: CommandOutputOptions; /** * Other possible outputs of the command, for example, errors. */ otherOutputs?: CommandOutputOptions[]; handler: CommandHandler; middlewares?: Middleware[]; } export type RestOptions = { path?: Path; method?: Method; /** * Maps parameters from different sources to a single input object. * * ```ts * // POST /properties/:userId?expectedVersion=1 * // { age: 35 } * command("/properties/:userId", { * params: { * expectedVersion: "query", * contentType: { in: "headers", name: "content-type" }, * }, * input: z.object({ * userId: z.string(), * expectedVersion: z.number().optional(), * contentType: z.string(), * age: z.number() * }), * }, ({userId}) => { console.log(userId); }); * ``` * * userId - assumed to come from the path * expectedVersion - explicitly mapped from the query string * contentType - explicitly mapped from the headers and renamed * age - assumed to come from the body * * Default location: * GET/DELETE/HEAD/OPTIONS - query * POST/PUT/PATCH - body */ params?: RestParams; }; export type RestParams = { [k in keyof Partial]: k extends ParsePath> ? "path" : Method extends "GET" | "DELETE" | "HEAD" | "OPTIONS" ? RestParam<"query" | "header" | "path"> : RestParam; }; export type RestParamLocation = "query" | "body" | "header" | "path"; export type RestParam = In | { in: Exclude; /** * The name of the parameter in the source location. * * For example, if the query string parameter is `UserID`, but the input schema uses `userId`. * * ```ts * // /properties?UserID=... * command("/properties", { * params: { * userId: { * in: "query", * name: "UserID" * }, * }, * input: z.object({ * userId: z.string().optional() * }), * }, ({userId}) => { console.log(userId); }); * ``` */ name?: string; }; export interface Headers { [headerName: string]: string; } export type CommandHandler = (input: T, context: Context) => Promise | U; export type CommandInput = C extends Command ? Input : never; export interface CommandOutputOptions { /** * @default - {@link z.any} */ schema?: z.ZodType; description: string; restStatusCode: number; } export type CommandOutput = z.ZodType | CommandOutputOptions; export interface CommandOptions extends FunctionRuntimeProps, Pick, "path" | "method" | "summary" | "description" | "params" | "validate"> { input?: z.ZodType; /** * The output schema of the command. * * When a description of the output can is provided, it will be used the {@link ApiSpecification}. * * When a rest status is provided and the command supports a rest path, that status will be used to return a successful result. * Note: RPC commands will always return 200. * * @default 200 {@link z.any} OK */ output?: CommandOutput; } export declare function command(name: Name, handler: CommandHandler): Command; export declare function command(name: Name, options: CommandOptions, handler: CommandHandler): Command; export declare function parseCommandArgs(args: any[]): { sourceLocation: import("../internal/service-spec.js").SourceLocation; name: Name; options: CommandOptions; handler: CommandHandler; }; //# sourceMappingURL=command.d.ts.map