import type {TypedArray, ReadableStream} from "@encore.dev/internal-runtime/compat/types"; /** * HTTP methods */ export enum Method { GET = "GET", HEAD = "HEAD", POST = "POST", PUT = "PUT", DELETE = "DELETE", CONNECT = "CONNECT", OPTIONS = "OPTIONS", TRACE = "TRACE", PATCH = "PATCH", /** * Represents any HTTP method */ Any = "*", } export enum StatusCode { OK = 200, NOT_FOUND = 404, INTERNAL = 500, } /** * Represents a raw HTTP request */ export interface Request { /** * The HTTP method of the request */ method: Method; /** * The URL of the request */ url: URL; /** * Header returns the values specified for the given header * or an empty array if the header is not present. * * Note: only "Set-Cookie" can have multiple values for Bun, all others * will only have a single value if present. */ header(name: string): string[]; /** * The request body of the request or null if not present */ body: ReadableStream | null; /** * The request body, parsed as JSON. * Returns null if there is no body. */ json(): Promise; } /** * Represents a raw HTTP response. * * The response *must* be closed after it is done being written to. */ export interface Response { /** * Appends a header to the response * * @throws {ResponseStartedError} If the response stream has started */ appendHeader(name: string, value: string): void; /** * Set the HTTP status code of the response * * Can only be set before the first write, if not called * before the first write, the status will be 200 * * @throws {ResponseStartedError} If the response stream has started */ status(code: StatusCode): void; /** * Writes a header to the response * * @throws {ResponseClosedError} If the response has has been closed */ write(data: TypedArray | DataView | ArrayBufferLike | string): Promise; /** * Flushes the response * * @throws {ResponseClosedError} If the response has has been closed */ flush(): Promise; /** * Closes the response */ close(): Promise; } /** * An API route as defined by the Encore application */ export interface ApiRoute { service: string; // The service the route is registered in name: string; // The name of the API endpoint methods: [Method, ...Method[]]; // The HTTP method(s) of the route path: string; // The path to the route handler: (req: Request, res: Response, params: string[]) => Promise; // The handler for the route }