zap

  • Version 2.1.1
  • Published
  • 31.3 kB
  • 3 dependencies
  • MIT license

Install

npm i zap
yarn add zap
pnpm add zap

Overview

Lightweight HTTP server framework for Node

Index

Functions

function buffer

buffer: (
req: http.IncomingMessage,
{ limit, encoding }?: RequestBodyOptions
) => Promise<Buffer>;

    function fromRequest

    fromRequest: <Fn extends (req: ServerRequest, ...rest: any[]) => any>(
    fn: Fn
    ) => Fn;
    • Creates a function that caches its results for a given request. Both successful responses and errors are cached.

      Parameter fn

      The function that should be cached.

      Returns

      The results of calling the function

    function getHeader

    getHeader: (req: http.IncomingMessage, header: string) => string | undefined;

      function httpError

      httpError: (code: number, message: string, metadata?: unknown) => HttpError;

        function json

        json: (req: http.IncomingMessage, options?: RequestBodyOptions) => Promise<any>;

          function notFound

          notFound: () => HttpError;

            function redirect

            redirect: (location: string, statusCode?: number) => RedirectError;

              function route

              route: <
              ResponseBody extends unknown,
              Method extends HttpMethod = HttpMethod,
              Route extends string = string
              >(
              method: Method,
              path: Route,
              handler: Handler<ResponseBody, ServerRequest<RouteParams<Route>>>
              ) => RouteHandler<Method, Route, ResponseBody>;

                function router

                router: (
                ...handlers: RouteHandler<HttpMethod, any, ResponseBodyType>[]
                ) => Handler;

                  function send

                  send: (res: http.ServerResponse, code: number, body?: ResponseBodyType) => void;

                    function serve

                    serve: (
                    handler: Handler,
                    options?: ServeOptions
                    ) => (req: http.IncomingMessage, res: http.ServerResponse) => Promise<void>;

                      function text

                      text: (
                      req: http.IncomingMessage,
                      options?: RequestBodyOptions
                      ) => Promise<string>;

                        Classes

                        class HttpError

                        class HttpError extends Error {}

                          constructor

                          constructor(statusCode: number, message: string, metadata: {});

                            property metadata

                            metadata: {};

                              property statusCode

                              statusCode: number;

                                class RedirectError

                                class RedirectError extends Error {}

                                  constructor

                                  constructor(statusCode: number, location: string);

                                    property location

                                    location: string;

                                      property statusCode

                                      statusCode: number;

                                        Interfaces

                                        interface RequestBodyOptions

                                        interface RequestBodyOptions {}

                                          property encoding

                                          encoding?: string;

                                            property limit

                                            limit?: string;

                                              interface RouteHandler

                                              interface RouteHandler<
                                              Method extends HttpMethod = HttpMethod,
                                              Route extends string = string,
                                              ResponseBody extends ResponseBodyType = ResponseBodyType
                                              > extends Handler<ResponseBody, ServerRequest<RouteParams<Route>>> {}

                                                property compilePath

                                                compilePath: (params?: RouteParams<Route>) => string;

                                                  property matchPath

                                                  matchPath: (path: string) =>
                                                  | false
                                                  | {
                                                  params: RouteParams<Route>;
                                                  path: string;
                                                  index: number;
                                                  };

                                                    property method

                                                    method: Method;

                                                      property route

                                                      route: Route;

                                                        interface ServeOptions

                                                        interface ServeOptions {}

                                                          property errorHandler

                                                          errorHandler?: ErrorHandler;

                                                            property trustProxy

                                                            trustProxy?: boolean;

                                                              interface ServerRequest

                                                              interface ServerRequest<Params = unknown> extends http.IncomingMessage {}

                                                                property params

                                                                params: Params;

                                                                  property parsedURL

                                                                  parsedURL: URL;

                                                                    property protocol

                                                                    protocol: 'http' | 'https';

                                                                      interface ServerResponse

                                                                      interface ServerResponse extends http.ServerResponse {}

                                                                        Type Aliases

                                                                        type ErrorHandler

                                                                        type ErrorHandler = (
                                                                        req: ServerRequest,
                                                                        res: ServerResponse,
                                                                        err: unknown
                                                                        ) => void | ResponseBodyType | Promise<ResponseBodyType>;

                                                                          type Handler

                                                                          type Handler<
                                                                          ResponseBody extends ResponseBodyType = ResponseBodyType,
                                                                          Request extends ServerRequest = ServerRequest
                                                                          > = (
                                                                          req: Request,
                                                                          res: ServerResponse
                                                                          ) => void | ResponseBody | Promise<ResponseBody>;

                                                                            type HttpMethod

                                                                            type HttpMethod = 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'OPTIONS' | 'PATCH';

                                                                              type ResponseBodyType

                                                                              type ResponseBodyType = string | object | number | Buffer | Stream | Error | null;

                                                                                type RouteParams

                                                                                type RouteParams<T extends string> = T extends `${string}:${infer P}?/${infer Rest}`
                                                                                ? {
                                                                                [K in P]?: string;
                                                                                } & RouteParams<Rest>
                                                                                : T extends `${string}:${infer P}*/${infer Rest}`
                                                                                ? {
                                                                                [K in P]?: string[];
                                                                                } & RouteParams<Rest>
                                                                                : T extends `${string}:${infer P}+/${infer Rest}`
                                                                                ? {
                                                                                [K in P]: string[];
                                                                                } & RouteParams<Rest>
                                                                                : T extends `${string}:${infer P}/${infer Rest}`
                                                                                ? {
                                                                                [K in P]: string;
                                                                                } & RouteParams<Rest>
                                                                                : T extends `${string}:${infer P}?`
                                                                                ? {
                                                                                [K in P]?: string;
                                                                                }
                                                                                : T extends `${string}:${infer P}*`
                                                                                ? {
                                                                                [K in P]?: string[];
                                                                                }
                                                                                : T extends `${string}:${infer P}+`
                                                                                ? {
                                                                                [K in P]: string[];
                                                                                }
                                                                                : T extends `${string}:${infer P}`
                                                                                ? {
                                                                                [K in P]: string;
                                                                                }
                                                                                : {};

                                                                                  Package Files (1)

                                                                                  Dependencies (3)

                                                                                  Dev Dependencies (7)

                                                                                  Peer Dependencies (0)

                                                                                  No peer dependencies.

                                                                                  Badge

                                                                                  To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

                                                                                  You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/zap.

                                                                                  • Markdown
                                                                                    [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/zap)
                                                                                  • HTML
                                                                                    <a href="https://www.jsdocs.io/package/zap"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>