@types/koa__router

  • Version 8.0.11
  • Published
  • 16.4 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;

                property regexp

                regexp: RegExp;

                  property stack

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

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

                                                method allowedMethods

                                                allowedMethods: (
                                                options?: Router.RouterAllowedMethodsOptions
                                                ) => Koa.Middleware<
                                                StateT,
                                                ContextT & Router.RouterParamContext<StateT, ContextT>
                                                >;
                                                • 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: {
                                                <T = {}, U = {}>(
                                                name: string,
                                                path: string | RegExp,
                                                ...middleware: Array<Router.Middleware<StateT & T, ContextT & U>>
                                                ): Router<StateT, ContextT>;
                                                <T = {}, U = {}>(
                                                path: string | RegExp | (string | RegExp)[],
                                                ...middleware: Koa.Middleware<
                                                StateT,
                                                ContextT & Router.RouterParamContext<StateT, ContextT>
                                                >[]
                                                ): Router<StateT, ContextT>;
                                                };
                                                • Alias for router.delete() because delete is a reserved word

                                                method delete

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

                                                method get

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

                                                method head

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

                                                link: {
                                                <T = {}, U = {}>(
                                                name: string,
                                                path: string | RegExp,
                                                ...middleware: Array<Router.Middleware<StateT & T, ContextT & U>>
                                                ): Router<StateT, ContextT>;
                                                <T = {}, U = {}>(
                                                path: string | RegExp | (string | RegExp)[],
                                                ...middleware: Koa.Middleware<
                                                StateT,
                                                ContextT & Router.RouterParamContext<StateT, ContextT>
                                                >[]
                                                ): 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>
                                                >;
                                                • Returns router middleware which dispatches a route matching the request.

                                                method options

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

                                                method param

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

                                                method patch

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

                                                method post

                                                post: {
                                                <T = {}, U = {}>(
                                                name: string,
                                                path: string | RegExp,
                                                ...middleware: Array<Router.Middleware<StateT & T, ContextT & U>>
                                                ): Router<StateT, ContextT>;
                                                <T = {}, U = {}>(
                                                path: string | RegExp | (string | RegExp)[],
                                                ...middleware: Koa.Middleware<
                                                StateT,
                                                ContextT & Router.RouterParamContext<StateT, ContextT>
                                                >[]
                                                ): 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.

                                                method put

                                                put: {
                                                <T = {}, U = {}>(
                                                name: string,
                                                path: string | RegExp,
                                                ...middleware: Array<Router.Middleware<StateT & T, ContextT & U>>
                                                ): Router<StateT, ContextT>;
                                                <T = {}, U = {}>(
                                                path: string | RegExp | (string | RegExp)[],
                                                ...middleware: Koa.Middleware<
                                                StateT,
                                                ContextT & Router.RouterParamContext<StateT, ContextT>
                                                >[]
                                                ): 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.

                                                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>
                                                >;
                                                • Returns router middleware which dispatches a route matching the request.

                                                unlink: {
                                                <T = {}, U = {}>(
                                                name: string,
                                                path: string | RegExp,
                                                ...middleware: Array<Router.Middleware<StateT & T, ContextT & U>>
                                                ): Router<StateT, ContextT>;
                                                <T = {}, U = {}>(
                                                path: string | RegExp | (string | RegExp)[],
                                                ...middleware: Koa.Middleware<
                                                StateT,
                                                ContextT & Router.RouterParamContext<StateT, ContextT>
                                                >[]
                                                ): 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 from url pattern and given params.

                                                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>
                                                >[]
                                                ): 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;

                                                    property ignoreCaptures

                                                    ignoreCaptures?: boolean | undefined;

                                                      property name

                                                      name: string | null;

                                                        property prefix

                                                        prefix?: string | undefined;

                                                          property sensitive

                                                          sensitive?: boolean | undefined;

                                                            property strict

                                                            strict?: boolean | undefined;

                                                              interface ParamMiddleware

                                                              interface ParamMiddleware {}

                                                                call signature

                                                                (param: string, ctx: RouterContext, 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 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
                                                                                      > = Koa.Middleware<StateT, ContextT & RouterParamContext<StateT, ContextT>>;

                                                                                        type RouterContext

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

                                                                                          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>