@types/koa-router

  • Version 7.4.2
  • Published
  • 20.9 kB
  • 1 dependency
  • MIT license

Install

npm i @types/koa-router
yarn add @types/koa-router
pnpm add @types/koa-router

Overview

TypeScript definitions for koa-router

Index

Classes

class Layer

class Layer {}

    constructor

    constructor(
    path: string | RegExp,
    methods: string[],
    middleware: any,
    opts?: ILayerOptions
    );

      constructor

      constructor(
      path: string | RegExp,
      methods: string[],
      middleware: any[],
      opts?: ILayerOptions
      );

        property methods

        methods: string[];

          property name

          name: string;

            property opts

            opts: ILayerOptions;

              property paramNames

              paramNames: ParamName[];

                property path

                path: string;

                  property regexp

                  regexp: RegExp;

                    property stack

                    stack: any[];

                      method captures

                      captures: (path: string) => string[];
                      • Returns array of regexp url path captures.

                      method match

                      match: (path: string) => boolean;
                      • Returns whether request path matches route.

                      method param

                      param: (param: string, fn: any) => Layer;
                      • Run validations on route named parameters.

                      method params

                      params: (
                      path: string | RegExp,
                      captures: string[],
                      existingParams?: Object
                      ) => Object;
                      • Returns map of URL parameters for given path and paramNames.

                      method setPrefix

                      setPrefix: (prefix: string) => Layer;
                      • Prefix route path.

                      method url

                      url: (params: Object) => string;
                      • Generate URL for route using given params.

                      class ParamName

                      class ParamName {}

                        property asterisk

                        asterisk: boolean;

                          property delimiter

                          delimiter: string;

                            property name

                            name: string;

                              property optional

                              optional: boolean;

                                property partial

                                partial: boolean;

                                  property pattern

                                  pattern: string;

                                    property prefix

                                    prefix: string;

                                      property repeat

                                      repeat: string;

                                        class Router

                                        class Router<StateT = any, CustomT = {}> {}

                                          constructor

                                          constructor(opt?: Router.IRouterOptions);
                                          • Create a new router.

                                          property params

                                          params: Object;

                                            property stack

                                            stack: Router.Layer[];

                                              method all

                                              all: {
                                              (
                                              name: string,
                                              path: string | RegExp,
                                              ...middleware: Array<Router.IMiddleware<StateT, CustomT>>
                                              ): Router<StateT, CustomT>;
                                              (path: string | RegExp | (string | RegExp)[], ...middleware: any[]): Router<
                                              StateT,
                                              CustomT
                                              >;
                                              <T, U>(
                                              name: string,
                                              path: string | RegExp,
                                              middleware: any,
                                              routeHandler: any
                                              ): Router<StateT & T, CustomT & U>;
                                              <T, U>(
                                              path: string | RegExp | (string | RegExp)[],
                                              middleware: any,
                                              routeHandler: any
                                              ): Router<StateT & T, CustomT & U>;
                                              };
                                              • Register route with all methods.

                                              method allowedMethods

                                              allowedMethods: (options?: Router.IRouterAllowedMethodsOptions) => any;
                                              • Returns separate middleware for responding to OPTIONS requests with an Allow header containing the allowed methods, as well as responding with 405 Method Not Allowed and 501 Not Implemented as appropriate.

                                              method del

                                              del: {
                                              (
                                              name: string,
                                              path: string | RegExp,
                                              ...middleware: Array<Router.IMiddleware<StateT, CustomT>>
                                              ): Router<StateT, CustomT>;
                                              (path: string | RegExp | (string | RegExp)[], ...middleware: any[]): Router<
                                              StateT,
                                              CustomT
                                              >;
                                              <T, U>(
                                              name: string,
                                              path: string | RegExp,
                                              middleware: any,
                                              routeHandler: any
                                              ): Router<StateT & T, CustomT & U>;
                                              <T, U>(
                                              path: string | RegExp | (string | RegExp)[],
                                              middleware: any,
                                              routeHandler: any
                                              ): Router<StateT & T, CustomT & U>;
                                              };
                                              • Alias for router.delete() because delete is a reserved word

                                              method delete

                                              delete: {
                                              (
                                              name: string,
                                              path: string | RegExp,
                                              ...middleware: Array<Router.IMiddleware<StateT, CustomT>>
                                              ): Router<StateT, CustomT>;
                                              (path: string | RegExp | (string | RegExp)[], ...middleware: any[]): Router<
                                              StateT,
                                              CustomT
                                              >;
                                              <T, U>(
                                              name: string,
                                              path: string | RegExp,
                                              middleware: any,
                                              routeHandler: any
                                              ): Router<StateT & T, CustomT & U>;
                                              <T, U>(
                                              path: string | RegExp | (string | RegExp)[],
                                              middleware: any,
                                              routeHandler: any
                                              ): Router<StateT & T, CustomT & U>;
                                              };
                                              • HTTP delete method

                                              method get

                                              get: {
                                              (
                                              name: string,
                                              path: string | RegExp,
                                              ...middleware: Array<Router.IMiddleware<StateT, CustomT>>
                                              ): Router<StateT, CustomT>;
                                              (path: string | RegExp | (string | RegExp)[], ...middleware: any[]): Router<
                                              StateT,
                                              CustomT
                                              >;
                                              <T, U>(
                                              name: string,
                                              path: string | RegExp,
                                              middleware: any,
                                              routeHandler: any
                                              ): Router<StateT & T, CustomT & U>;
                                              <T, U>(
                                              path: string | RegExp | (string | RegExp)[],
                                              middleware: any,
                                              routeHandler: any
                                              ): Router<StateT & T, CustomT & U>;
                                              };
                                              • HTTP get method

                                              method head

                                              head: {
                                              (
                                              name: string,
                                              path: string | RegExp,
                                              ...middleware: Array<Router.IMiddleware<StateT, CustomT>>
                                              ): Router<StateT, CustomT>;
                                              (path: string | RegExp | (string | RegExp)[], ...middleware: any[]): Router<
                                              StateT,
                                              CustomT
                                              >;
                                              <T, U>(
                                              name: string,
                                              path: string | RegExp,
                                              middleware: any,
                                              routeHandler: any
                                              ): Router<StateT & T, CustomT & U>;
                                              <T, U>(
                                              path: string | RegExp | (string | RegExp)[],
                                              middleware: any,
                                              routeHandler: any
                                              ): Router<StateT & T, CustomT & U>;
                                              };
                                              • HTTP head method

                                              link: {
                                              (
                                              name: string,
                                              path: string | RegExp,
                                              ...middleware: Array<Router.IMiddleware<StateT, CustomT>>
                                              ): Router<StateT, CustomT>;
                                              (path: string | RegExp | (string | RegExp)[], ...middleware: any[]): Router<
                                              StateT,
                                              CustomT
                                              >;
                                              <T, U>(
                                              name: string,
                                              path: string | RegExp,
                                              middleware: any,
                                              routeHandler: any
                                              ): Router<StateT & T, CustomT & U>;
                                              <T, U>(
                                              path: string | RegExp | (string | RegExp)[],
                                              middleware: any,
                                              routeHandler: any
                                              ): Router<StateT & T, CustomT & U>;
                                              };
                                              • HTTP link method

                                              method match

                                              match: (path: string, method: string) => Router.IRoutesMatch;
                                              • Match given path and return corresponding routes.

                                              method middleware

                                              middleware: () => any;
                                              • Returns router middleware which dispatches a route matching the request.

                                              method options

                                              options: {
                                              (
                                              name: string,
                                              path: string | RegExp,
                                              ...middleware: Array<Router.IMiddleware<StateT, CustomT>>
                                              ): Router<StateT, CustomT>;
                                              (path: string | RegExp | (string | RegExp)[], ...middleware: any[]): Router<
                                              StateT,
                                              CustomT
                                              >;
                                              <T, U>(
                                              name: string,
                                              path: string | RegExp,
                                              middleware: any,
                                              routeHandler: any
                                              ): Router<StateT & T, CustomT & U>;
                                              <T, U>(
                                              path: string | RegExp | (string | RegExp)[],
                                              middleware: any,
                                              routeHandler: any
                                              ): Router<StateT & T, CustomT & U>;
                                              };
                                              • HTTP options method

                                              method param

                                              param: (
                                              param: string,
                                              middleware: Router.IParamMiddleware<StateT, CustomT>
                                              ) => Router<StateT, CustomT>;
                                              • Run middleware for named route parameters. Useful for auto-loading or validation.

                                              method patch

                                              patch: {
                                              (
                                              name: string,
                                              path: string | RegExp,
                                              ...middleware: Array<Router.IMiddleware<StateT, CustomT>>
                                              ): Router<StateT, CustomT>;
                                              (path: string | RegExp | (string | RegExp)[], ...middleware: any[]): Router<
                                              StateT,
                                              CustomT
                                              >;
                                              <T, U>(
                                              name: string,
                                              path: string | RegExp,
                                              middleware: any,
                                              routeHandler: any
                                              ): Router<StateT & T, CustomT & U>;
                                              <T, U>(
                                              path: string | RegExp | (string | RegExp)[],
                                              middleware: any,
                                              routeHandler: any
                                              ): Router<StateT & T, CustomT & U>;
                                              };
                                              • HTTP patch method

                                              method post

                                              post: {
                                              (
                                              name: string,
                                              path: string | RegExp,
                                              ...middleware: Array<Router.IMiddleware<StateT, CustomT>>
                                              ): Router<StateT, CustomT>;
                                              (path: string | RegExp | (string | RegExp)[], ...middleware: any[]): Router<
                                              StateT,
                                              CustomT
                                              >;
                                              <T, U>(
                                              name: string,
                                              path: string | RegExp,
                                              middleware: any,
                                              routeHandler: any
                                              ): Router<StateT & T, CustomT & U>;
                                              <T, U>(
                                              path: string | RegExp | (string | RegExp)[],
                                              middleware: any,
                                              routeHandler: any
                                              ): Router<StateT & T, CustomT & U>;
                                              };
                                              • HTTP post method

                                              method prefix

                                              prefix: (prefix: string) => Router<StateT, CustomT>;
                                              • Set the path prefix for a Router instance that was already initialized.

                                              method put

                                              put: {
                                              (
                                              name: string,
                                              path: string | RegExp,
                                              ...middleware: Array<Router.IMiddleware<StateT, CustomT>>
                                              ): Router<StateT, CustomT>;
                                              (path: string | RegExp | (string | RegExp)[], ...middleware: any[]): Router<
                                              StateT,
                                              CustomT
                                              >;
                                              <T, U>(
                                              name: string,
                                              path: string | RegExp,
                                              middleware: any,
                                              routeHandler: any
                                              ): Router<StateT & T, CustomT & U>;
                                              <T, U>(
                                              path: string | RegExp | (string | RegExp)[],
                                              middleware: any,
                                              routeHandler: any
                                              ): Router<StateT & T, CustomT & U>;
                                              };
                                              • HTTP put method

                                              method redirect

                                              redirect: (
                                              source: string,
                                              destination: string,
                                              code?: number
                                              ) => Router<StateT, CustomT>;
                                              • Redirect source to destination URL with optional 30x status code.

                                                Both source and destination can be route names.

                                              method register

                                              register: (
                                              path: string | RegExp,
                                              methods: string[],
                                              middleware:
                                              | Router.IMiddleware<StateT, CustomT>
                                              | Array<Router.IMiddleware<StateT, CustomT>>,
                                              opts?: Router.ILayerOptions
                                              ) => Router.Layer;
                                              • Create and register a route.

                                              method route

                                              route: { (name: string): Router.Layer; (name: string): boolean };
                                              • Lookup route with given name.

                                              method routes

                                              routes: () => any;
                                              • Returns router middleware which dispatches a route matching the request.

                                              unlink: {
                                              (
                                              name: string,
                                              path: string | RegExp,
                                              ...middleware: Array<Router.IMiddleware<StateT, CustomT>>
                                              ): Router<StateT, CustomT>;
                                              (path: string | RegExp | (string | RegExp)[], ...middleware: any[]): Router<
                                              StateT,
                                              CustomT
                                              >;
                                              <T, U>(
                                              name: string,
                                              path: string | RegExp,
                                              middleware: any,
                                              routeHandler: any
                                              ): Router<StateT & T, CustomT & U>;
                                              <T, U>(
                                              path: string | RegExp | (string | RegExp)[],
                                              middleware: any,
                                              routeHandler: any
                                              ): Router<StateT & T, CustomT & U>;
                                              };
                                              • HTTP unlink method

                                              method url

                                              static url: (path: string | RegExp, params: Object) => string;
                                              • Generate URL for route. Takes either map of named params or series of arguments (for regular expression routes)

                                                router = new Router(); router.get('user', "/users/:id", ...

                                                router.url('user', { id: 3 }); // => "/users/3"

                                                Query can be generated from third argument:

                                                router.url('user', { id: 3 }, { query: { limit: 1 } }); // => "/users/3?limit=1"

                                                router.url('user', { id: 3 }, { query: "limit=1" }); // => "/users/3?limit=1"

                                              • Generate URL from url pattern and given params.

                                              method use

                                              use: {
                                              (...middleware: Array<Router.IMiddleware<StateT, CustomT>>): Router<
                                              StateT,
                                              CustomT
                                              >;
                                              (path: string | RegExp | string[], ...middleware: any[]): Router<
                                              StateT,
                                              CustomT
                                              >;
                                              };
                                              • Use given middleware.

                                                Middleware run in the order they are defined by .use(). They are invoked sequentially, requests start at the first middleware and work their way "down" the middleware stack.

                                              Interfaces

                                              interface ILayerOptions

                                              interface ILayerOptions {}

                                                property end

                                                end?: boolean;

                                                  property ignoreCaptures

                                                  ignoreCaptures?: boolean;

                                                    property name

                                                    name: string;

                                                      property prefix

                                                      prefix?: string;

                                                        property sensitive

                                                        sensitive?: boolean;

                                                          property strict

                                                          strict?: boolean;

                                                            interface IParamMiddleware

                                                            interface IParamMiddleware<STateT = any, CustomT = {}> {}

                                                              call signature

                                                              (
                                                              param: string,
                                                              ctx: RouterContext<STateT, CustomT>,
                                                              next: () => Promise<any>
                                                              ): any;

                                                                interface IRouterAllowedMethodsOptions

                                                                interface IRouterAllowedMethodsOptions {}

                                                                  property methodNotAllowed

                                                                  methodNotAllowed?: () => any;
                                                                  • throw the returned value in place of the default MethodNotAllowed error

                                                                  property notImplemented

                                                                  notImplemented?: () => any;
                                                                  • throw the returned value in place of the default NotImplemented error

                                                                  property throw

                                                                  throw?: boolean;
                                                                  • throw error instead of setting status and header

                                                                  interface IRouterContext

                                                                  interface IRouterContext extends RouterContext {}

                                                                    interface IRouterOptions

                                                                    interface IRouterOptions {}

                                                                      property methods

                                                                      methods?: string[];
                                                                      • Methods which should be supported by the router.

                                                                      property prefix

                                                                      prefix?: string;
                                                                      • Prefix for all routes.

                                                                      property routerPath

                                                                      routerPath?: string;

                                                                        property sensitive

                                                                        sensitive?: boolean;
                                                                        • Whether or not routing should be case-sensitive.

                                                                        property strict

                                                                        strict?: boolean;
                                                                        • Whether or not routes should matched strictly.

                                                                          If strict matching is enabled, the trailing slash is taken into account when matching routes.

                                                                        interface IRouterParamContext

                                                                        interface IRouterParamContext<StateT = any, CustomT = {}> {}

                                                                          property params

                                                                          params: Record<string, string>;
                                                                          • url params

                                                                          property router

                                                                          router: Router<StateT, CustomT>;
                                                                          • the router instance

                                                                          interface IRoutesMatch

                                                                          interface IRoutesMatch {}

                                                                            property path

                                                                            path: Layer[];

                                                                              property pathAndMethod

                                                                              pathAndMethod: Layer[];

                                                                                property route

                                                                                route: boolean;

                                                                                  interface IUrlOptionsQuery

                                                                                  interface IUrlOptionsQuery {}

                                                                                    property query

                                                                                    query: object | string;

                                                                                      Type Aliases

                                                                                      type IMiddleware

                                                                                      type IMiddleware<StateT = any, CustomT = {}> = Koa.Middleware<
                                                                                      StateT,
                                                                                      CustomT & IRouterParamContext<StateT, CustomT>
                                                                                      >;

                                                                                        type RouterContext

                                                                                        type RouterContext<StateT = any, CustomT = {}> = Koa.ParameterizedContext<
                                                                                        StateT,
                                                                                        CustomT & IRouterParamContext<StateT, CustomT>
                                                                                        >;

                                                                                          Package Files (1)

                                                                                          Dependencies (1)

                                                                                          Dev Dependencies (0)

                                                                                          No dev dependencies.

                                                                                          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/@types/koa-router.

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