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.
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;
}
}
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());