Install: @travetto/rest-fastify
npm install @travetto/rest-fastify
# or
yarn add @travetto/rest-fastify
The module is an fastify provider for the RESTful API module. This module provides an implementation of RestApplication for automatic injection in the default Rest server.
Code: Customizing the Fastify App
import { FastifyPluginAsync } from 'fastify';
import { Injectable } from '@travetto/di';
import { FastifyRestServer } from '@travetto/rest-fastify';
declare let rateLimit: (config: { windowMs: number, max: number }) => FastifyPluginAsync;
@Injectable({ primary: true })
class CustomRestServer extends FastifyRestServer {
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.register(limiter);
return app;
}
}
When working with an fastify applications, the module provides what is assumed to be a sufficient set of basic filters. Specifically:
Code: Configured Middleware
const app = fastify(fastConf);
app.register(compress);
app.removeAllContentTypeParsers();
app.addContentTypeParser(/.*/, (req, body, done) => done(null, body));