@types/koa__router

  • Version 12.0.4
  • 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?: LayerOptions
    );

      property methods

      methods: string[];

        property name

        name: string;

          property opts

          opts: LayerOptions;

            property paramNames

            paramNames: ParamName[];

              property path

              path: string | RegExp;

                property regexp

                regexp: RegExp;

                  property stack

                  stack: Koa.Middleware<
                  StateT,
                  ContextT & RouterParamContext<StateT, ContextT>,
                  BodyT
                  >[];

                    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, middleware: ParamMiddleware) => Layer;
                    • Run validations on route named parameters.

                      Example 1

                      router
                      .param('user', function (id, ctx, next) {
                      ctx.user = users[id];
                      if (!ctx.user) return ctx.status = 404;
                      next();
                      })
                      .get('/users/:user', function (ctx, next) {
                      ctx.body = ctx.user;
                      });

                    method params

                    params: <ParamT extends string = string>(
                    path: string | RegExp,
                    captures: ParamT[],
                    params?: Record<string, any>
                    ) => { [key in ParamT]?: string };
                    • 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.

                      Example 1

                      const route = new Layer('/users/:id', ['GET'], fn);
                      route.url({ id: 123 }); // => "/users/123"

                    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 = Koa.DefaultState, ContextT = Koa.DefaultContext> {}

                                        constructor

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

                                        property methods

                                        methods: string[];

                                          property opts

                                          opts: Router.RouterOptions;

                                            property params

                                            params: {};

                                              property stack

                                              stack: Router.Layer[];

                                                method all

                                                all: {
                                                <T = {}, U = {}, B = unknown>(
                                                name: string,
                                                path: string | RegExp,
                                                ...middleware: Array<Router.Middleware<StateT & T, ContextT & U, B>>
                                                ): Router<StateT, ContextT>;
                                                <T = {}, U = {}, B = unknown>(
                                                path: string | RegExp | (string | RegExp)[],
                                                ...middleware: Koa.Middleware<
                                                StateT,
                                                ContextT & Router.RouterParamContext<StateT, ContextT>,
                                                BodyT
                                                >[]
                                                ): Router<StateT, ContextT>;
                                                };
                                                • Register route with all methods.

                                                method allowedMethods

                                                allowedMethods: (
                                                options?: Router.RouterAllowedMethodsOptions
                                                ) => Koa.Middleware<
                                                StateT,
                                                ContextT & Router.RouterParamContext<StateT, ContextT>,
                                                BodyT
                                                >;
                                                • 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.

                                                  Example 1

                                                  var Koa = require('koa');
                                                  var Router = require('koa-router');
                                                  var app = new Koa();
                                                  var router = new Router();
                                                  app.use(router.routes());
                                                  app.use(router.allowedMethods());

                                                  **Example with [Boom](https://github.com/hapijs/boom)**

                                                  var Koa = require('koa');
                                                  var Router = require('koa-router');
                                                  var Boom = require('boom');
                                                  var app = new Koa();
                                                  var router = new Router();
                                                  app.use(router.routes());
                                                  app.use(router.allowedMethods({
                                                  throw: true,
                                                  notImplemented: () => new Boom.notImplemented(),
                                                  methodNotAllowed: () => new Boom.methodNotAllowed()
                                                  }));

                                                method del

                                                del: {
                                                <T = {}, U = {}, B = unknown>(
                                                name: string,
                                                path: string | RegExp,
                                                ...middleware: Array<Router.Middleware<StateT & T, ContextT & U, B>>
                                                ): Router<StateT, ContextT>;
                                                <T = {}, U = {}, B = unknown>(
                                                path: string | RegExp | (string | RegExp)[],
                                                ...middleware: Koa.Middleware<
                                                StateT,
                                                ContextT & Router.RouterParamContext<StateT, ContextT>,
                                                BodyT
                                                >[]
                                                ): Router<StateT, ContextT>;
                                                };
                                                • Alias for router.delete() because delete is a reserved word

                                                method delete

                                                delete: {
                                                <T = {}, U = {}, B = unknown>(
                                                name: string,
                                                path: string | RegExp,
                                                ...middleware: Array<Router.Middleware<StateT & T, ContextT & U, B>>
                                                ): Router<StateT, ContextT>;
                                                <T = {}, U = {}, B = unknown>(
                                                path: string | RegExp | (string | RegExp)[],
                                                ...middleware: Koa.Middleware<
                                                StateT,
                                                ContextT & Router.RouterParamContext<StateT, ContextT>,
                                                BodyT
                                                >[]
                                                ): Router<StateT, ContextT>;
                                                };
                                                • HTTP delete method

                                                method get

                                                get: {
                                                <T = {}, U = {}, B = unknown>(
                                                name: string,
                                                path: string | RegExp,
                                                ...middleware: Array<Router.Middleware<StateT & T, ContextT & U, B>>
                                                ): Router<StateT, ContextT>;
                                                <T = {}, U = {}, B = unknown>(
                                                path: string | RegExp | (string | RegExp)[],
                                                ...middleware: Koa.Middleware<
                                                StateT,
                                                ContextT & Router.RouterParamContext<StateT, ContextT>,
                                                BodyT
                                                >[]
                                                ): Router<StateT, ContextT>;
                                                };
                                                • HTTP get method

                                                method head

                                                head: {
                                                <T = {}, U = {}, B = unknown>(
                                                name: string,
                                                path: string | RegExp,
                                                ...middleware: Array<Router.Middleware<StateT & T, ContextT & U, B>>
                                                ): Router<StateT, ContextT>;
                                                <T = {}, U = {}, B = unknown>(
                                                path: string | RegExp | (string | RegExp)[],
                                                ...middleware: Koa.Middleware<
                                                StateT,
                                                ContextT & Router.RouterParamContext<StateT, ContextT>,
                                                BodyT
                                                >[]
                                                ): Router<StateT, ContextT>;
                                                };
                                                • HTTP head method

                                                link: {
                                                <T = {}, U = {}, B = unknown>(
                                                name: string,
                                                path: string | RegExp,
                                                ...middleware: Array<Router.Middleware<StateT & T, ContextT & U, B>>
                                                ): Router<StateT, ContextT>;
                                                <T = {}, U = {}, B = unknown>(
                                                path: string | RegExp | (string | RegExp)[],
                                                ...middleware: Koa.Middleware<
                                                StateT,
                                                ContextT & Router.RouterParamContext<StateT, ContextT>,
                                                BodyT
                                                >[]
                                                ): Router<StateT, ContextT>;
                                                };
                                                • HTTP link method

                                                method match

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

                                                method middleware

                                                middleware: () => Koa.Middleware<
                                                StateT,
                                                ContextT & Router.RouterParamContext<StateT, ContextT>,
                                                BodyT
                                                >;
                                                • Returns router middleware which dispatches a route matching the request.

                                                method options

                                                options: {
                                                <T = {}, U = {}, B = unknown>(
                                                name: string,
                                                path: string | RegExp,
                                                ...middleware: Array<Router.Middleware<StateT & T, ContextT & U, B>>
                                                ): Router<StateT, ContextT>;
                                                <T = {}, U = {}, B = unknown>(
                                                path: string | RegExp | (string | RegExp)[],
                                                ...middleware: Koa.Middleware<
                                                StateT,
                                                ContextT & Router.RouterParamContext<StateT, ContextT>,
                                                BodyT
                                                >[]
                                                ): Router<StateT, ContextT>;
                                                };
                                                • HTTP options method

                                                method param

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

                                                  Example 1

                                                  router
                                                  .param('user', (id, ctx, next) => {
                                                  ctx.user = users[id];
                                                  if (!ctx.user) return ctx.status = 404;
                                                  return next();
                                                  })
                                                  .get('/users/:user', ctx => {
                                                  ctx.body = ctx.user;
                                                  })
                                                  .get('/users/:user/friends', ctx => {
                                                  return ctx.user.getFriends().then(function(friends) {
                                                  ctx.body = friends;
                                                  });
                                                  })
                                                  // /users/3 => {"id": 3, "name": "Alex"}
                                                  // /users/3/friends => [{"id": 4, "name": "TJ"}]

                                                method patch

                                                patch: {
                                                <T = {}, U = {}, B = unknown>(
                                                name: string,
                                                path: string | RegExp,
                                                ...middleware: Array<Router.Middleware<StateT & T, ContextT & U, B>>
                                                ): Router<StateT, ContextT>;
                                                <T = {}, U = {}, B = unknown>(
                                                path: string | RegExp | (string | RegExp)[],
                                                ...middleware: Koa.Middleware<
                                                StateT,
                                                ContextT & Router.RouterParamContext<StateT, ContextT>,
                                                BodyT
                                                >[]
                                                ): Router<StateT, ContextT>;
                                                };
                                                • HTTP patch method

                                                method post

                                                post: {
                                                <T = {}, U = {}, B = unknown>(
                                                name: string,
                                                path: string | RegExp,
                                                ...middleware: Array<Router.Middleware<StateT & T, ContextT & U, B>>
                                                ): Router<StateT, ContextT>;
                                                <T = {}, U = {}, B = unknown>(
                                                path: string | RegExp | (string | RegExp)[],
                                                ...middleware: Koa.Middleware<
                                                StateT,
                                                ContextT & Router.RouterParamContext<StateT, ContextT>,
                                                BodyT
                                                >[]
                                                ): Router<StateT, ContextT>;
                                                };
                                                • HTTP post method

                                                method prefix

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

                                                  Example 1

                                                  router.prefix('/things/:thing_id')

                                                method put

                                                put: {
                                                <T = {}, U = {}, B = unknown>(
                                                name: string,
                                                path: string | RegExp,
                                                ...middleware: Array<Router.Middleware<StateT & T, ContextT & U, B>>
                                                ): Router<StateT, ContextT>;
                                                <T = {}, U = {}, B = unknown>(
                                                path: string | RegExp | (string | RegExp)[],
                                                ...middleware: Koa.Middleware<
                                                StateT,
                                                ContextT & Router.RouterParamContext<StateT, ContextT>,
                                                BodyT
                                                >[]
                                                ): Router<StateT, ContextT>;
                                                };
                                                • HTTP put method

                                                method redirect

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

                                                  Both source and destination can be route names.

                                                  router.redirect('/login', 'sign-in');

                                                  This is equivalent to:

                                                  router.all('/login', ctx => {
                                                  ctx.redirect('/sign-in');
                                                  ctx.status = 301;
                                                  });

                                                method register

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

                                                method route

                                                route: (name: string) => Router.Layer | boolean;
                                                • Lookup route with given name.

                                                method routes

                                                routes: () => Koa.Middleware<
                                                StateT,
                                                ContextT & Router.RouterParamContext<StateT, ContextT>,
                                                BodyT
                                                >;
                                                • Returns router middleware which dispatches a route matching the request.

                                                unlink: {
                                                <T = {}, U = {}, B = unknown>(
                                                name: string,
                                                path: string | RegExp,
                                                ...middleware: Array<Router.Middleware<StateT & T, ContextT & U, B>>
                                                ): Router<StateT, ContextT>;
                                                <T = {}, U = {}, B = unknown>(
                                                path: string | RegExp | (string | RegExp)[],
                                                ...middleware: Koa.Middleware<
                                                StateT,
                                                ContextT & Router.RouterParamContext<StateT, ContextT>,
                                                BodyT
                                                >[]
                                                ): Router<StateT, ContextT>;
                                                };
                                                • 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 for route. Takes a route name and map of named params.

                                                  Example 1

                                                  router.get('user', '/users/:id', (ctx, next) => {
                                                  // ...
                                                  });
                                                  router.url('user', 3);
                                                  // => "/users/3"
                                                  router.url('user', { id: 3 });
                                                  // => "/users/3"
                                                  router.use((ctx, next) => {
                                                  // redirect to named route
                                                  ctx.redirect(ctx.router.url('sign-in'));
                                                  })
                                                  router.url('user', { id: 3 }, { query: { limit: 1 } });
                                                  // => "/users/3?limit=1"
                                                  router.url('user', { id: 3 }, { query: "limit=1" });
                                                  // => "/users/3?limit=1"

                                                method use

                                                use: {
                                                (...middleware: Array<Router.Middleware<StateT, ContextT>>): Router<
                                                StateT,
                                                ContextT
                                                >;
                                                (
                                                path: string | RegExp | string[],
                                                ...middleware: Koa.Middleware<
                                                StateT,
                                                ContextT & Router.RouterParamContext<StateT, ContextT>,
                                                BodyT
                                                >[]
                                                ): Router<StateT, ContextT>;
                                                };
                                                • 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 LayerOptions

                                                interface LayerOptions {}

                                                  property end

                                                  end?: boolean | undefined;
                                                  • (default: false)

                                                  property ignoreCaptures

                                                  ignoreCaptures?: boolean | undefined;

                                                    property name

                                                    name: string | null;
                                                    • Route name

                                                    property prefix

                                                    prefix?: string | undefined;
                                                    • (default: '')

                                                    property sensitive

                                                    sensitive?: boolean | undefined;
                                                    • Case sensitive (default: false)

                                                    property strict

                                                    strict?: boolean | undefined;
                                                    • Require the trailing slash (default: false)

                                                    interface ParamMiddleware

                                                    interface ParamMiddleware<
                                                    StateT = Koa.DefaultState,
                                                    ContextT = Koa.DefaultContext,
                                                    BodyT = unknown
                                                    > {}

                                                      call signature

                                                      (
                                                      param: string,
                                                      ctx: RouterContext<StateT, ContextT, BodyT>,
                                                      next: Koa.Next
                                                      ): any;

                                                        interface RouterAllowedMethodsOptions

                                                        interface RouterAllowedMethodsOptions {}

                                                          property methodNotAllowed

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

                                                          property notImplemented

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

                                                          property throw

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

                                                          interface RouterOptions

                                                          interface RouterOptions {}

                                                            property exclusive

                                                            exclusive?: boolean | undefined;
                                                            • Only run last matched route's controller when there are multiple matches

                                                            property host

                                                            host?: string | RegExp | undefined;
                                                            • Host for router match

                                                            property methods

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

                                                            property prefix

                                                            prefix?: string | undefined;
                                                            • Prefix for all routes.

                                                            property routerPath

                                                            routerPath?: string | undefined;

                                                              property sensitive

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

                                                              property strict

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

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

                                                              interface RouterParamContext

                                                              interface RouterParamContext<
                                                              StateT = Koa.DefaultState,
                                                              ContextT = Koa.DefaultContext
                                                              > {}

                                                                property params

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

                                                                property router

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

                                                                interface RoutesMatch

                                                                interface RoutesMatch {}

                                                                  property path

                                                                  path: Layer[];

                                                                    property pathAndMethod

                                                                    pathAndMethod: Layer[];

                                                                      property route

                                                                      route: boolean;

                                                                        interface UrlOptionsQuery

                                                                        interface UrlOptionsQuery {}

                                                                          property query

                                                                          query: object | string;

                                                                            Type Aliases

                                                                            type Middleware

                                                                            type Middleware<
                                                                            StateT = Koa.DefaultState,
                                                                            ContextT = Koa.DefaultContext,
                                                                            BodyT = unknown
                                                                            > = Koa.Middleware<StateT, ContextT & RouterParamContext<StateT, ContextT>, BodyT>;

                                                                              type RouterContext

                                                                              type RouterContext<
                                                                              StateT = Koa.DefaultState,
                                                                              ContextT = Koa.DefaultContext,
                                                                              BodyT = unknown
                                                                              > = Koa.ParameterizedContext<
                                                                              StateT,
                                                                              ContextT & RouterParamContext<StateT, ContextT>,
                                                                              BodyT
                                                                              >;

                                                                                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>