Skip to content

Commit

Permalink
feat(fastify): implemets FastifyInterceptorService for OgmaInterceptor
Browse files Browse the repository at this point in the history
Pretty much just like the ExpressInterceptorService, but for Fastify.
  • Loading branch information
jmcdo29 committed Mar 27, 2020
1 parent bbe6335 commit 9f49298
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
19 changes: 15 additions & 4 deletions packages/platform-fastify/README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
# `@ogma/platform-fastify`

> TODO: description
The `FastifyInterceptorService` parser for the `OgmaInterceptor`. This plugin class parses Fastify request and response object to be able to successfully log the data about the request. For more information, check out [the @ogma/nestjs-module](packages/nestjs-module/README.md) documentation.

## Installation

Nothing special, standard `npm i @ogma/platform-fastify` or `yarn add @ogma/platform-fastify`

## Usage

```
const platformFastify = require('@ogma/platform-fastify');
This plugin is to be used in the `OgmaInterceptorOptions` portion of the `OgmaModule` during `forRoot` or `forRootAsync` registration. It can be used like so:

// TODO: DEMONSTRATE API
```ts
@Module(
OgmaModule.forRoot({
interceptor: {
http: FastifyInterceptorService
}
})
)
export class AppModule {}
```
6 changes: 5 additions & 1 deletion packages/platform-fastify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,21 @@
"url": "git+https://github.com/jmcdo29/nestjs-ogma.git"
},
"scripts": {
"build": "tsc -b tsconfig.build.json"
"build": "tsc -b tsconfig.build.json",
"test": "jest",
"test:cov": "jest --coverage"
},
"bugs": {
"url": "https://github.com/jmcdo29/nestjs-ogma/issues"
},
"devDependencies": {
"@nestjs/platform-fastify": "7.0.6",
"@ogma/nestjs-module": "^0.1.0",
"fastify": "^2.13.0",
"rxjs": "^6.5.4"
},
"peerDependencies": {
"@nestjs/platform-fastify": "7.0.6",
"@ogma/nestjs-module": "^0.1.0",
"fastify": "^2.13.0"
}
Expand Down
8 changes: 4 additions & 4 deletions packages/platform-fastify/src/fastify-interceptor.service.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Injectable, ExecutionContext, HttpException } from '@nestjs/common';
import { HTTP_CODE_METADATA } from '@nestjs/common/constants';
import { AbstractInterceptorService } from '@ogma/nestjs-module';
import { FastifyRequest, FastifyReply } from 'fastify';
import { HTTP_CODE_METADATA } from '@nestjs/common/constants';
import { ServerResponse } from 'http';

@Injectable()
export class FastifyInterceptorService extends AbstractInterceptorService {
getCallerIp(context: ExecutionContext): string[] | string {
const req = this.getRequest(context);
return req.ips.length ? req.ips : req.ip;
return req.ips && req.ips.length ? req.ips : req.ip;
}

getCallPoint(context: ExecutionContext): string {
Expand All @@ -33,7 +33,7 @@ export class FastifyInterceptorService extends AbstractInterceptorService {
): string {
let status;
const res = this.getResponse(context);
status = res.status;
status = res.res.statusCode;
const reflectStatus = this.reflector.get<number>(
HTTP_CODE_METADATA,
context.getHandler(),
Expand All @@ -50,7 +50,7 @@ export class FastifyInterceptorService extends AbstractInterceptorService {
}

private getResponse(context: ExecutionContext): FastifyReply<ServerResponse> {
return context.switchToHttp().getResponse();
return context.switchToHttp().getResponse<FastifyReply<ServerResponse>>();
}

private determineStatusCodeFromError(error: HttpException & Error): number {
Expand Down

0 comments on commit 9f49298

Please sign in to comment.