import { IncomingMessage, ServerResponse } from 'http'; import * as ufo from 'ufo'; declare type Encoding = false | 'ascii' | 'utf8' | 'utf-8' | 'utf16le' | 'ucs2' | 'ucs-2' | 'base64' | 'latin1' | 'binary' | 'hex'; declare type Handle = (req: IncomingMessage, res: ServerResponse) => any; declare type PHandle = (req: IncomingMessage, res: ServerResponse) => Promise; declare type Middleware = (req: IncomingMessage, res: ServerResponse, next: (err?: Error) => any) => any; declare type LazyHandle = () => Handle | Promise; declare function promisifyHandle(handle: Handle | Middleware): PHandle; declare function callHandle(handle: Middleware, req: IncomingMessage, res: ServerResponse): Promise; declare function lazyHandle(handle: LazyHandle, promisify?: boolean): PHandle; declare function useBase(base: string, handle: PHandle): PHandle; interface Layer { route: string; match?: Matcher; handle: Handle; } declare type Stack = Layer[]; interface InputLayer { route?: string; match?: Matcher; handle: Handle | LazyHandle; lazy?: boolean; promisify?: boolean; } declare type InputStack = InputLayer[]; declare type Matcher = (url: string, req?: IncomingMessage) => boolean; interface AppUse { (route: string | string[], handle: Middleware | Middleware[], options?: Partial): App; (route: string | string[], handle: Handle | Handle[], options?: Partial): App; (handle: Middleware | Middleware[], options?: Partial): App; (handle: Handle | Handle[], options?: Partial): App; (options: InputLayer): App; } interface App { (req: IncomingMessage, res: ServerResponse): Promise; stack: Stack; _handle: PHandle; use: AppUse; useAsync: AppUse; } interface AppOptions { debug?: boolean; onError?: (error: Error, req: IncomingMessage, res: ServerResponse) => any; } declare function createApp(options?: AppOptions): App; declare function use(app: App, arg1: string | Handle | InputLayer | InputLayer[], arg2?: Handle | Partial | Handle[], arg3?: Partial): App; declare function createHandle(stack: Stack): PHandle; /** * H3 Runtime Error * @class * @extends Error * @property {Number} statusCode An Integer indicating the HTTP response status code. * @property {String} statusMessage A String representing the HTTP status message * @property {Any} data An extra data that will includes in the response.
* This can be used to pass additional information about the error. * @property {Boolean} internal Setting this property to true will mark error as an internal error */ declare class H3Error extends Error { statusCode: number; statusMessage: string; data?: any; } /** * Creates new `Error` that can be used to handle both internal and runtime errors. * * @param input {Partial} * @return {H3Error} An instance of the H3Error */ declare function createError(input: Partial): H3Error; /** * Recieve an error and return the corresponding response.
* H3 internally uses this fucntion to handle unhandled errors.
* Note that calling this function will close the connection and no other data will be sent to client afterwards. * * @param res {ServerResponse} The ServerResponse object is passed as the second parameter in the handler function * @param error {H3Error|Error} Raised error * @param debug {Boolean} Whether application is in debug mode.
* In the debug mode the stack trace of errors will be return in response. */ declare function sendError(res: ServerResponse, error: Error | H3Error, debug?: boolean): void; /** * Reads body of the request and returns encoded raw string (default) or `Buffer` if encoding if falsy. * @param req {IncomingMessage} An IncomingMessage object is created by * http.Server * @param encoding {Encoding} encoding="utf-8" - The character encoding to use. * * @return {String|Buffer} Encoded raw string or raw Buffer of the body */ declare function useRawBody(req: IncomingMessage, encoding?: Encoding): Encoding extends false ? Buffer : Promise; /** * Reads request body and try to safely parse using {@link https://github.com/unjs/destr destr} * @param req {IncomingMessage} An IncomingMessage object is created by * http.Server * @param encoding {Encoding} encoding="utf-8" - The character encoding to use. * * @return {*} The Object, Array, string, number, boolean, or null value corresponding to the request JSON body */ declare function useBody(req: IncomingMessage): Promise; declare const MIMES: { html: string; json: string; }; /** * Additional serialization options */ interface CookieSerializeOptions { /** * Specifies the value for the {@link https://tools.ietf.org/html/rfc6265#section-5.2.3|Domain Set-Cookie attribute}. By default, no * domain is set, and most clients will consider the cookie to apply to only * the current domain. */ domain?: string; /** * Specifies a function that will be used to encode a cookie's value. Since * value of a cookie has a limited character set (and must be a simple * string), this function can be used to encode a value into a string suited * for a cookie's value. * * The default function is the global `encodeURIComponent`, which will * encode a JavaScript string into UTF-8 byte sequences and then URL-encode * any that fall outside of the cookie range. */ encode?(value: string): string; /** * Specifies the `Date` object to be the value for the {@link https://tools.ietf.org/html/rfc6265#section-5.2.1|`Expires` `Set-Cookie` attribute}. By default, * no expiration is set, and most clients will consider this a "non-persistent cookie" and will delete * it on a condition like exiting a web browser application. * * *Note* the {@link https://tools.ietf.org/html/rfc6265#section-5.3|cookie storage model specification} * states that if both `expires` and `maxAge` are set, then `maxAge` takes precedence, but it is * possible not all clients by obey this, so if both are set, they should * point to the same date and time. */ expires?: Date; /** * Specifies the boolean value for the {@link https://tools.ietf.org/html/rfc6265#section-5.2.6|`HttpOnly` `Set-Cookie` attribute}. * When truthy, the `HttpOnly` attribute is set, otherwise it is not. By * default, the `HttpOnly` attribute is not set. * * *Note* be careful when setting this to true, as compliant clients will * not allow client-side JavaScript to see the cookie in `document.cookie`. */ httpOnly?: boolean; /** * Specifies the number (in seconds) to be the value for the `Max-Age` * `Set-Cookie` attribute. The given number will be converted to an integer * by rounding down. By default, no maximum age is set. * * *Note* the {@link https://tools.ietf.org/html/rfc6265#section-5.3|cookie storage model specification} * states that if both `expires` and `maxAge` are set, then `maxAge` takes precedence, but it is * possible not all clients by obey this, so if both are set, they should * point to the same date and time. */ maxAge?: number; /** * Specifies the value for the {@link https://tools.ietf.org/html/rfc6265#section-5.2.4|`Path` `Set-Cookie` attribute}. * By default, the path is considered the "default path". */ path?: string; /** * Specifies the boolean or string to be the value for the {@link https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-03#section-4.1.2.7|`SameSite` `Set-Cookie` attribute}. * * - `true` will set the `SameSite` attribute to `Strict` for strict same * site enforcement. * - `false` will not set the `SameSite` attribute. * - `'lax'` will set the `SameSite` attribute to Lax for lax same site * enforcement. * - `'strict'` will set the `SameSite` attribute to Strict for strict same * site enforcement. * - `'none'` will set the SameSite attribute to None for an explicit * cross-site cookie. * * More information about the different enforcement levels can be found in {@link https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-03#section-4.1.2.7|the specification}. * * *note* This is an attribute that has not yet been fully standardized, and may change in the future. This also means many clients may ignore this attribute until they understand it. */ sameSite?: true | false | 'lax' | 'strict' | 'none'; /** * Specifies the boolean value for the {@link https://tools.ietf.org/html/rfc6265#section-5.2.5|`Secure` `Set-Cookie` attribute}. When truthy, the * `Secure` attribute is set, otherwise it is not. By default, the `Secure` attribute is not set. * * *Note* be careful when setting this to `true`, as compliant clients will * not send the cookie back to the server in the future if the browser does * not have an HTTPS connection. */ secure?: boolean; } declare function useCookies(req: IncomingMessage): Record; declare function useCookie(req: IncomingMessage, name: string): string | undefined; declare function setCookie(res: ServerResponse, name: string, value: string, serializeOptions: CookieSerializeOptions): void; declare function useQuery(req: IncomingMessage): ufo.QueryObject; declare function send(res: ServerResponse, data: any, type?: string): Promise; declare function defaultContentType(res: ServerResponse, type?: string): void; declare function sendRedirect(res: ServerResponse, location: string, code?: number): void; declare function appendHeader(res: ServerResponse, name: string, value: string): void; export { App, AppOptions, AppUse, H3Error, Handle, InputLayer, InputStack, Layer, LazyHandle, MIMES, Matcher, Middleware, PHandle, Stack, appendHeader, callHandle, createApp, createError, createHandle, defaultContentType, lazyHandle, promisifyHandle, send, sendError, sendRedirect, setCookie, use, useBase, useBody, useCookie, useCookies, useQuery, useRawBody };