Skip to content

Commit

Permalink
improvement(deps): remove dependency on fastify and @types/express
Browse files Browse the repository at this point in the history
By removing the hard dependnecy on the types FastifyRequest, Request, FastifyReply<ServerResponse>,
and Response, the fastify and @types/express packages are no longer hard dependencies which will cut
down on the package size. To get around this, minimum types were created (or used) to make sure that
there could still be a distinction between Fastify and Express requests.

fix #4
  • Loading branch information
jmcdo29 committed Feb 19, 2020
1 parent a60bcc6 commit f5a2c41
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 80 deletions.
204 changes: 142 additions & 62 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@
},
"dependencies": {
"@golevelup/nestjs-modules": "^0.3.0",
"@types/express": "^4.17.2",
"fastify": "^2.11.0",
"ogma": "^2.0.0"
},
"devDependencies": {
Expand All @@ -56,10 +54,12 @@
"@nestjs/core": "^6.10.13",
"@nestjs/schematics": "^6.7.0",
"@nestjs/testing": "^6.10.13",
"@types/express": "^4.17.2",
"@types/jest": "^24.0.25",
"@types/node": "^13.1.1",
"conventional-changelog-cli": "^2.0.31",
"cz-conventional-changelog": "^3.0.2",
"fastify": "^2.12.0",
"husky": "^3.1.0",
"jest": "^24.9.0",
"lint-staged": "^9.5.0",
Expand Down
6 changes: 6 additions & 0 deletions src/interfaces/express-like.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { IncomingMessage } from 'http';

export interface ExpressLikeRequest extends IncomingMessage {
ips: string[];
ip: string;
}
18 changes: 18 additions & 0 deletions src/interfaces/fastify-like.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { IncomingMessage, ServerResponse } from 'http';

export interface FastifyLikeRequest {
query: Record<string, any>;
params: Record<string, any>;
headers: Record<string, any>;
body: any;
req: IncomingMessage;
id: any;
ip: string;
ips: string[];
hostname: string;
raw: IncomingMessage;
}

export interface FastifyLikeResponse {
res: ServerResponse;
}
7 changes: 1 addition & 6 deletions src/interfaces/ogma-options.interface.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import { ExecutionContext } from '@nestjs/common';
import { Request, Response } from 'express';
import { FastifyReply, FastifyRequest } from 'fastify';
import { ServerResponse } from 'http';
import { OgmaOptions } from 'ogma';

type OgmaRequest = FastifyRequest | Request;
type OgmaResponse = FastifyReply<ServerResponse> | Response;
import { OgmaRequest, OgmaResponse } from './ogma-types.interface';

export interface OgmaModuleOptions {
service?: OgmaServiceOptions;
Expand Down
9 changes: 9 additions & 0 deletions src/interfaces/ogma-types.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { ServerResponse } from 'http';
import { ExpressLikeRequest } from './express-like.interface';
import {
FastifyLikeRequest,
FastifyLikeResponse,
} from './fastify-like.interface';

export type OgmaRequest = FastifyLikeRequest | ExpressLikeRequest;
export type OgmaResponse = FastifyLikeResponse | ServerResponse;
17 changes: 7 additions & 10 deletions src/ogma.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@ import {
Injectable,
NestInterceptor,
} from '@nestjs/common';
import { Request, Response } from 'express';
import { FastifyReply, FastifyRequest } from 'fastify';
import { ServerResponse } from 'http';
import { color, OgmaOptions } from 'ogma';
import { Observable, throwError } from 'rxjs';
import { catchError, tap } from 'rxjs/operators';
import {
FastifyLikeRequest,
FastifyLikeResponse,
} from './interfaces/fastify-like.interface';
import { OgmaInterceptorOptions } from './interfaces/ogma-options.interface';
import { OgmaRequest, OgmaResponse } from './interfaces/ogma-types.interface';
import { OgmaService } from './ogma.service';

type OgmaRequest = FastifyRequest | Request;
type OgmaResponse = FastifyReply<ServerResponse> | Response;

@Injectable()
export class OgmaInterceptor implements NestInterceptor {
private json: boolean;
Expand Down Expand Up @@ -146,13 +145,11 @@ export class OgmaInterceptor implements NestInterceptor {
)} ${requestTime} - ${contentLength}`;
}

private isFastifyRequest(req: OgmaRequest): req is FastifyRequest {
private isFastifyRequest(req: OgmaRequest): req is FastifyLikeRequest {
return Object.keys(req).indexOf('raw') !== -1;
}

private isFastifyResponse(
res: OgmaResponse,
): res is FastifyReply<ServerResponse> {
private isFastifyResponse(res: OgmaResponse): res is FastifyLikeResponse {
return Object.keys(res).indexOf('res') !== -1;
}

Expand Down

0 comments on commit f5a2c41

Please sign in to comment.