/// import { IncomingMessage, OutgoingHttpHeaders, IncomingHttpHeaders, ServerResponse } from 'http'; import { Parser, Result } from './parse'; export declare type Response = Readonly<[number, OutgoingHttpHeaders, NodeJS.ReadableStream]>; export declare type Request = Readonly<{ request: IncomingMessage; method: string; url: string; headers: Readonly; }>; /** * A Route is a function that receives a Request and returns a success with the route context * information _or_ a failure with the original request. */ export declare type Route = (req: Request) => Promise> | Result; /** * Given a matched Route context and a Request, returns a Response to be served. */ export declare type Responder = (context: T, request: Request) => Promise | Response; /** * Givne a Request, returns a Success of the Response or a Failure of teh originoal Request. * Effectively the combination of a Route and a Responder */ export declare type Handler = (request: Request) => Promise> | Result; /** * Given a context T, body Promise and a Request resolves a Response. This is the interface * for reading request bodies and probably can be renamed to make that clearer. */ export declare type RequestHandler = (context: T, body: Promise, request: Request) => Response | Promise; /** * A RequestHandler that identifies a body a is signed or unsigned. */ export declare type SignedJSONHandler = RequestHandler; /** * Interface for integrating with Node's HTTP lib. Receives an http.IncomingMessage and http.ServerResponse * and returns the Promise to be written to the response. */ export declare type Endpoint = (request: IncomingMessage, response: ServerResponse) => Response | Promise; /** * Creates a Node http server request handler. * * @param handler The HTTP Request handler * @param defaultHandler Handler to use when no handler is identified */ export declare function serve(handler: Handler, defaultHandler?: (req: Request) => Response | Promise): Endpoint; /** * Creates a Response to be served with the given status and headers. * * @param status * @param headers * @param json * @return Response */ export declare function jsonResponse(status: number, headers: OutgoingHttpHeaders, json: any): Response; /** * Creates a Responder than uses the `encoder` to generate the response. * @param encoder Function to transform the context T and request into JSON Response args */ export declare function sendJson(encoder: (context: T, request: Request) => [number, OutgoingHttpHeaders, any]): Responder; /** * Create a route that matches a given request method. * @param method Method to match */ export declare function method(method: T): Route; /** * Creates a route that matches the exact path e.g. `/users/find` * @param path Literal string to match */ export declare function exactPath(path: T): Route; export declare function always(value: T): () => T; /** * Wraps an Endpoint to add logging * @param label Label to use in logged output * @param handler Endpoint with logging */ export declare function log(label: string, handler: Endpoint): Endpoint; /** * Combines multiple routes, executing the first to return a success result. * * When no handler matches a failure is returned. * * @param handler * @param handlers */ export declare function routes(handler: Handler, ...handlers: Handler[]): Handler; /** * A handler that calls the responder when the route matches the incoming request. * * @param route Determines of incoming message matches the route * @param responder Called when the route matches */ export declare function route(route: Route, responder: Responder): Handler; /** * Combines multipe routing contexts into a single context object based on the keys. * * @example * * routeContext({ method: method('POST'), path: exactPath('/some/path)}) * * Matches a request for `POST /some/path` and the context will be of type: * * type { method: 'POST', path: '/some/path'} * * @param route * @param responder */ export declare function routeContext>(route: { [K in keyof T]: Route; }, responder: Responder): Handler; /** * Given two routes, produces a route that requires both to match and combines both * matches into a tuple. * * @param a Route context matcher * @param b Route context matcher */ export declare function both(a: Route, b: Route): Route<[A, B]>; /** * Reads the body into a Buffer and calls the RequestHandler with the buffer. */ export declare function readBody(bodyHandler: RequestHandler): Responder; /** * Read the incoming request body and resolves as parsed JSON object. * * @param bodyHandler R */ export declare function readJson(bodyHandler: RequestHandler): Responder; export declare function parseJson(parser: Parser, handler: RequestHandler>): Responder; /** * Compare a signature with a shared hmac sha256 base64 encoded secret. * * - If no signature is provided, handler is called with Promise<['unsigned', body]> * - If signature is provided and is valid, handler is called with Promise<['signed', any]>. * - If signature is invalid the Promise is reject.d * * @param signature Shared secret used for HMAC digest computation. * @param verifySignature Function that returns the signature for the request * @param bodyHandler Endpoint handler that receives the content [(signed|unsigned), any] */ export declare function readSignedJson(signature: (context: T, request: Request) => void | null | string, verifySignature: (signature: string, body: Buffer, request: Request) => boolean, bodyHandler: SignedJSONHandler): Responder; export declare function checkSignature(buffer: Buffer, secret: string, signature: string): boolean; export declare function context>(matchers: { [K in keyof R]: Route; }): Route; /** * Returns a single request header value. */ export declare function requestHeader(headerNames: string[], defaultValue?: string): (request: Request) => void | string; /** * Request content type header. */ export declare const contentType: (request: Request) => void | string; export declare function prefix(pathPrefix: S): Route; export declare function withPrefix(handler: Handler): Responder; export declare function routeNamespace(pathPrefix: S, handler: Handler): Handler; //# sourceMappingURL=server.d.ts.map