@types/koa

  • Version 2.15.0
  • Published
  • 25.4 kB
  • 8 dependencies
  • MIT license

Install

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

Overview

TypeScript definitions for koa

Index

Variables

variable HttpError

const HttpError: any;
  • A re-export of HttpError from the http-error package.

    This is the error type that is thrown by ctx.assert() and ctx.throw().

Classes

class Application

class Application<
StateT = Application.DefaultState,
ContextT = Application.DefaultContext
> extends EventEmitter {}

    constructor

    constructor(options?: {
    env?: string | undefined;
    keys?: string[] | undefined;
    proxy?: boolean | undefined;
    subdomainOffset?: number | undefined;
    proxyIpHeader?: string | undefined;
    maxIpsCount?: number | undefined;
    asyncLocalStorage?: boolean | undefined;
    });
    • Parameter options

      Application options

      Parameter

      {string} [options.env='development'] Environment

      Parameter

      {string[]} [options.keys] Signed cookie keys

      Parameter

      {boolean} [options.proxy] Trust proxy headers

      Parameter

      {number} [options.subdomainOffset] Subdomain offset

      Parameter

      {string} [options.proxyIpHeader] Proxy IP header, defaults to X-Forwarded-For

      Parameter

      {number} [options.maxIpsCount] Max IPs read from proxy IP header, default to 0 (means infinity)

      Parameter

      {boolean} [options.asyncLocalStorage] Enable AsyncLocalStorage

    property context

    context: Application.BaseContext;

      property ctxStorage

      ctxStorage: any;

        property currentContext

        readonly currentContext: {};
        • return current context from async local storage

        property env

        env: string;

          property keys

          keys: any;

            property maxIpsCount

            maxIpsCount: number;

              property middleware

              middleware: compose.Middleware<
              Application.ParameterizedContext<StateT, ContextT, ResponseBodyT>
              >[];

                property proxy

                proxy: boolean;

                  property proxyIpHeader

                  proxyIpHeader: string;

                    property request

                    request: Application.BaseRequest;

                      property response

                      response: Application.BaseResponse;

                        property silent

                        silent: boolean;

                          property subdomainOffset

                          subdomainOffset: number;

                            method callback

                            callback: () => (
                            req: IncomingMessage | Http2ServerRequest,
                            res: ServerResponse | Http2ServerResponse
                            ) => Promise<void>;
                            • Return a request handler callback for node's native http/http2 server.

                            method createContext

                            createContext: <StateT = Application.DefaultState>(
                            req: IncomingMessage,
                            res: ServerResponse
                            ) => Application.ParameterizedContext<StateT>;
                            • Initialize a new context.

                              private

                            method inspect

                            inspect: () => any;
                            • Return JSON representation. We only bother showing settings.

                            method listen

                            listen: {
                            (
                            port?: number,
                            hostname?: string,
                            backlog?: number,
                            listeningListener?: () => void
                            ): Server;
                            (port: number, hostname?: string, listeningListener?: () => void): Server;
                            (port: number, backlog?: number, listeningListener?: () => void): Server;
                            (port: number, listeningListener?: () => void): Server;
                            (path: string, backlog?: number, listeningListener?: () => void): Server;
                            (path: string, listeningListener?: () => void): Server;
                            (options: ListenOptions, listeningListener?: () => void): Server;
                            (handle: any, backlog?: number, listeningListener?: () => void): Server;
                            (handle: any, listeningListener?: () => void): Server;
                            };
                            • Shorthand for:

                              http.createServer(app.callback()).listen(...)

                            method onerror

                            onerror: (err: Error) => void;
                            • Default error handler.

                              private

                            method toJSON

                            toJSON: () => any;
                            • Return JSON representation. We only bother showing settings.

                            method use

                            use: <NewStateT = {}, NewContextT = {}>(
                            middleware: compose.Middleware<
                            Application.ParameterizedContext<StateT, ContextT, ResponseBodyT>
                            >
                            ) => Application<StateT & NewStateT, ContextT & NewContextT>;
                            • Use the given middleware fn.

                              Old-style middleware will be converted.

                            Interfaces

                            interface BaseContext

                            interface BaseContext extends ContextDelegatedRequest, ContextDelegatedResponse {}

                              property assert

                              assert: typeof httpAssert;
                              • Similar to .throw(), adds assertion.

                                this.assert(this.user, 401, 'Please login!');

                                See: https://github.com/jshttp/http-assert

                              method inspect

                              inspect: () => any;
                              • util.inspect() implementation, which just returns the JSON output.

                              method onerror

                              onerror: (err: Error) => void;
                              • Default error handling.

                              method throw

                              throw: {
                              (message: string, code?: number, properties?: {}): never;
                              (status: number): never;
                              (...properties: (string | number | {})[]): never;
                              };
                              • Throw an error with msg and optional status defaulting to 500. Note that these are user-level errors, and the message may be exposed to the client.

                                this.throw(403) this.throw('name required', 400) this.throw(400, 'name required') this.throw('something exploded') this.throw(new Error('invalid'), 400); this.throw(400, new Error('invalid'));

                                See: https://github.com/jshttp/http-errors

                              method toJSON

                              toJSON: () => any;
                              • Return JSON representation.

                                Here we explicitly invoke .toJSON() on each object, as iteration will otherwise fail due to the getters and cause utilities such as clone() to fail.

                              interface BaseRequest

                              interface BaseRequest extends ContextDelegatedRequest {}

                                property charset

                                charset: string;
                                • Get the charset when present or undefined.

                                property length

                                length: number;
                                • Return parsed Content-Length when present.

                                property type

                                type: string;
                                • Return the request mime type void of parameters such as "charset".

                                method inspect

                                inspect: () => any;
                                • Inspect implementation.

                                method toJSON

                                toJSON: () => any;
                                • Return JSON representation.

                                interface BaseResponse

                                interface BaseResponse extends ContextDelegatedResponse {}

                                  property header

                                  header: OutgoingHttpHeaders;
                                  • Return response header.

                                  property headers

                                  headers: OutgoingHttpHeaders;
                                  • Return response header, alias as response.header

                                  property socket

                                  socket: Socket;
                                  • Return the request socket.

                                    {Connection} public

                                  method get

                                  get: (field: string) => string;
                                  • Return response header. If the header is not set, will return an empty string.

                                    The Referrer header field is special-cased, both Referrer and Referer are interchangeable.

                                    Examples:

                                    this.get('Content-Type'); // => "text/plain"

                                    this.get('content-type'); // => "text/plain"

                                    this.get('Something'); // => ''

                                  method inspect

                                  inspect: () => any;
                                  • Inspect implementation.

                                  method is

                                  is: {
                                  (...types: string[]): string | false | null;
                                  (types: string[]): string | false;
                                  };
                                  • Check whether the response is one of the listed types. Pretty much the same as this.request.is().

                                    Parameter

                                    {String|Array} types... {String|false} public

                                  method toJSON

                                  toJSON: () => any;
                                  • Return JSON representation.

                                  interface Context

                                  interface Context extends ParameterizedContext {}

                                    interface DefaultContext

                                    interface DefaultContext extends DefaultContextExtends {}
                                    • This interface can be augmented by users to add types to Koa's default context

                                    index signature

                                    [key: PropertyKey]: any;
                                    • Custom properties.

                                    interface DefaultState

                                    interface DefaultState extends DefaultStateExtends {}
                                    • This interface can be augmented by users to add types to Koa's default state

                                    interface ExtendableContext

                                    interface ExtendableContext extends BaseContext {}

                                      property accept

                                      accept: accepts.Accepts;

                                        property app

                                        app: Application;

                                          property cookies

                                          cookies: Cookies;

                                            property originalUrl

                                            originalUrl: string;

                                              property req

                                              req: IncomingMessage;

                                                property request

                                                request: Request;

                                                  property res

                                                  res: ServerResponse;

                                                    property respond

                                                    respond?: boolean | undefined;
                                                    • To bypass Koa's built-in response handling, you may explicitly set ctx.respond = false;

                                                    property response

                                                    response: Response;

                                                      interface Request

                                                      interface Request extends BaseRequest {}

                                                        property accept

                                                        accept: accepts.Accepts;

                                                          property app

                                                          app: Application;

                                                            property ctx

                                                            ctx: Context;

                                                              property ip

                                                              ip: string;

                                                                property originalUrl

                                                                originalUrl: string;

                                                                  property req

                                                                  req: IncomingMessage;

                                                                    property res

                                                                    res: ServerResponse;

                                                                      property response

                                                                      response: Response;

                                                                        interface Response

                                                                        interface Response extends BaseResponse {}

                                                                          property app

                                                                          app: Application;

                                                                            property ctx

                                                                            ctx: Context;

                                                                              property req

                                                                              req: IncomingMessage;

                                                                                property request

                                                                                request: Request;

                                                                                  property res

                                                                                  res: ServerResponse;

                                                                                    Type Aliases

                                                                                    type DefaultContextExtends

                                                                                    type DefaultContextExtends = {};

                                                                                      type DefaultStateExtends

                                                                                      type DefaultStateExtends = any;

                                                                                        type Middleware

                                                                                        type Middleware<
                                                                                        StateT = DefaultState,
                                                                                        ContextT = DefaultContext,
                                                                                        ResponseBodyT = any
                                                                                        > = compose.Middleware<ParameterizedContext<StateT, ContextT, ResponseBodyT>>;

                                                                                          type Next

                                                                                          type Next = () => Promise<any>;

                                                                                            type ParameterizedContext

                                                                                            type ParameterizedContext<
                                                                                            StateT = DefaultState,
                                                                                            ContextT = DefaultContext,
                                                                                            ResponseBodyT = unknown
                                                                                            > = ExtendableContext & { state: StateT } & ContextT & {
                                                                                            body: ResponseBodyT;
                                                                                            response: { body: ResponseBodyT };
                                                                                            };

                                                                                              Package Files (1)

                                                                                              Dependencies (8)

                                                                                              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.

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