Coder Social home page Coder Social logo

nest-morgan's Introduction

Nest Logo

Morgan Module for Nest framework

NPM Version Package License NPM Downloads Github Actions - Test

NOT MAINTAINED!

Hey all. This project is not maintained and archived. You should check out iamolegga/nestjs-pino which is maintained and with a bunch of cool features.

Description

This's a Morgan module for Nest.

Installation

$ npm i --save nest-morgan morgan @types/morgan

Versions

  • 2.x Is for Nest v7.x
    • Remove the need to use MorganModule.forRoot() #17.
  • 1.x Is for Nest v6.x
  • 0.x Is for Nest v5.x

Quick Start

Include Module

app.module.ts

@Module({
  imports: [MorganModule],
})
export class ApplicationModule {}

Global

If you want to set up interceptor as global, you have to follow Nest instructions here. Something like this:

app.module.ts

import { Module } from "@nestjs/common";
import { APP_INTERCEPTOR } from "@nestjs/core";
import { MorganModule, MorganInterceptor } from "nest-morgan";

@Module({
  imports: [MorganModule],
  providers: [
    {
      provide: APP_INTERCEPTOR,
      useClass: MorganInterceptor("combined"),
    },
  ],
})
export class ApplicationModule {}

Using Interceptor

app.controller.ts

  @UseInterceptors(MorganInterceptor('combined'))
  @Get('/some/route')
  public async someRoute() {
    ...
  }

nest-morgan's People

Contributors

cdupetit avatar mentos1386 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

nest-morgan's Issues

Custom logger

Hello,

Would it be possible to add a way to customize how the message is logged?
I'd like to use a custom Logger instead of console.error for the messages.

Thanks for the library!

Using interceptor for logging is not a good idea. Middlewares FTW

Request lifecycle in Nest 6.x puts interceptors after guards. That means if some AuthGuard denies request, it won't be logged. Nest middlewares are the very first thing to execute and, conveniently, happen to be express-compatible (even when fastify adapter is used). That makes putting vanilla morgan in app.use() before app.listen() in main.ts the only way to correctly log all requests.

Issues with Nest 6.0

Seems to be an issues after upgrading to nest 6

TS2416: Property 'intercept' in type 'AbstractMorganInterceptor' is not assignable to the same property in base type 'NestInterceptor<any, any>'.
  Type '(context: ExecutionContext, call$: Observable<any>) => Observable<any>' is not assignable to type '(context: ExecutionContext, next: CallHandler<any>) => Observable<any> | Promise<Observable<any>>'.
    Types of parameters 'call$' and 'next' are incompatible.
      Type 'CallHandler<any>' is missing the following properties from type 'Observable<any>': _isScalar, source, operator, lift, and 6 more.

Build error

hello @mentos1386,
When I run a build of my nest app, I get some errors.
The build run correctly with @types/morgan version 1.9.0 but failed with the latest version 1.9.1.
Thanks in advance.
Best regards

Versions

    "@nestjs/common": "^7.3.2",
    "@nestjs/core": "^7.3.2",
    "@nestjs/platform-express": "^7.3.2",
    "morgan": "^1.10.0",
    "nest-morgan": "^1.0.1",
    "@types/morgan": "^1.9.1",
    "typescript": "^3.9.7",

Command
tsc -p tsconfig.build.json

tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es6",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true
  },
  "exclude": ["node_modules"]
}

tsconfig.build.json

{
  "extends": "./tsconfig.json",
  "exclude": ["node_modules", "test", "dist", "**/*spec.ts"]
}

Compilation Error

node_modules/nest-morgan/morgan.interceptor.abstract.d.ts:6:42 - error TS2314: Generic type 'Options<Request, Response>' requires 2 type argument(s).

6     protected abstract readonly options: morgan.Options;
                                           ~~~~~~~~~~~~~~

node_modules/nest-morgan/morgan.interceptor.abstract.d.ts:8:33 - error TS2314: Generic type 'Morgan<Request, Response>' requires 2 type argument(s).

8     constructor(morganInstance: morgan.Morgan);
                                  ~~~~~~~~~~~~~

node_modules/nest-morgan/morgan.interceptor.mixin.d.ts:2:87 - error TS2314: Generic type 'Options<Request, Response>' requires 2 type argument(s).

2 export declare function MorganInterceptor(format: string | morgan.FormatFn, options?: morgan.Options): import("@nestjs/common").Type<any>;
                                                                                        ~~~~~~~~~~~~~~

node_modules/nest-morgan/morgan.providers.d.ts:4:23 - error TS2314: Generic type 'Morgan<Request, Response>' requires 2 type argument(s).

4     useFactory: () => morgan.Morgan;
                        ~~~~~~~~~~~~~

Deprecate forRoot method

The forRoot method only returns the module. No dynamic module is created. So I think it should be deprecated in favour of using MorganModule directly.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.