@travetto/rest-fastify
Version:
Fastify provider for the travetto rest module.
57 lines (42 loc) • 2.02 kB
Markdown
<!-- This file was generated by @travetto/doc and should not be modified directly -->
<!-- Please modify https://github.com/travetto/travetto/tree/main/module/rest-fastify/DOC.tsx and execute "npx trv doc" to rebuild -->
**Install: @travetto/rest-fastify**
```bash
npm install @travetto/rest-fastify
yarn add @travetto/rest-fastify
```
The module is an [fastify](https://www.fastify.io/) provider for the [RESTful API](https://github.com/travetto/travetto/tree/main/module/rest#readme "Declarative api for RESTful APIs with support for the dependency injection module.") module. This module provides an implementation of [RestApplication](https://github.com/travetto/travetto/tree/main/module/rest/src/application/rest.ts#L19) for automatic injection in the default Rest server.
**Code: Customizing the Fastify App**
```typescript
import { FastifyPluginAsync } from 'fastify';
import { Injectable } from '@travetto/di';
import { FastifyRestServer } from '@travetto/rest-fastify';
import { TimeUtil } from '@travetto/runtime';
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: TimeUtil.asMillis(15, 'm'), // 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](https://www.fastify.io/) applications, the module provides what is assumed to be a sufficient set of basic filters. Specifically:
**Code: Configured Middleware**
```typescript
const app = fastify(fastConf);
app.register(compress);
app.removeAllContentTypeParsers();
app.addContentTypeParser(/^.*/, (req, body, done) => done(null, body));
```