@travetto/rest-koa

4.1.2 • Public • Published

Koa REST Source

Koa provider for the travetto rest module.

Install: @travetto/rest-koa

npm install @travetto/rest-koa

# or

yarn add @travetto/rest-koa

The module is an koa provider for the RESTful API module. This module provides an implementation of RestApplication for automatic injection in the default Rest server.

Customizing Rest App

Code: Customizing the Koa App

import { Middleware } from 'koa';

import { Injectable } from '@travetto/di';
import { KoaRestServer } from '@travetto/rest-koa';

declare let rateLimit: (config: { windowMs: number, max: number }) => Middleware;

@Injectable({ primary: true })
class CustomRestServer extends KoaRestServer {
  override async init() {
    const app = await super.init();
    const limiter = rateLimit({
      windowMs: 15 * 60 * 1000, // 15 minutes
      max: 100 // limit each IP to 100 requests per windowMs
    });

    //  apply to all requests
    app.use(limiter);

    return app;
  }
}

Default Middleware

When working with an koa applications, the module provides what is assumed to be a sufficient set of basic filters. Specifically:

Code: Configured Middleware

const app = new koa();
    app.use(kCompress());

Package Sidebar

Install

npm i @travetto/rest-koa

Homepage

travetto.io

Weekly Downloads

93

Version

4.1.2

License

MIT

Unpacked Size

8.86 kB

Total Files

6

Last publish

Collaborators

  • arcsine