@travetto/rest-koa
v5.0.17
Published
Koa provider for the travetto rest module.
Downloads
606
Readme
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';
import { TimeUtil } from '@travetto/runtime';
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: TimeUtil.asMillis(15, 'm'), // 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());