Skip to content
This repository has been archived by the owner on Jul 8, 2021. It is now read-only.

Commit

Permalink
chore(release): released v0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
mentos1386 committed Jul 14, 2018
0 parents commit 639f9ae
Show file tree
Hide file tree
Showing 16 changed files with 10,996 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# dependencies
/node_modules

# IDE
/.idea
/.awcache
/.vscode

# misc
npm-debug.log
.DS_Store

# tests
/coverage
/.nyc_output

# dist
/dist
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 Tine Jozelj <mentos1386@tjo.space>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
74 changes: 74 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<p align="center">
<a href="http://nestjs.com/" target="blank"><img src="http://kamilmysliwiec.com/public/nest-logo.png#1" alt="Nest Logo" /> </a>
</p>

<p align="center">Morgan Module for Nest framework</p>

<p align="center">
<a href="https://www.npmjs.com/package/nest-morgan"><img src="https://img.shields.io/npm/v/nest-morgan.svg" alt="NPM Version" /></a>
<a href="https://www.npmjs.com/package/nest-morgan"><img src="https://img.shields.io/npm/l/nest-morgan.svg" alt="Package License" /></a>
<a href="https://www.npmjs.com/package/nest-morgan"><img src="https://img.shields.io/npm/dm/nest-morgan.svg" alt="NPM Downloads" /></a>
</p>

## Description
This's a [Morgan](https://github.com/expressjs/morgan) module for [Nest](https://github.com/nestjs/nest).

## Installation

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

## Quick Start


### Include Module

> app.module.ts
```ts
@Module({
imports: [
...
MorganModule.forRoot(),
]
})
export class ApplicationModule {}
```

#### Global
If you want to set up interceptor as global, you have to follow Nest
instructions [here](https://docs.nestjs.com/interceptors). Something like
this:

> app.module.ts
```ts
import { Module } from '@nestjs/common';
import { APP_INTERCEPTOR } from '@nestjs/core';
import { MorganModule, MorganInterceptor } from 'nest-morgan';

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

### Using Interceptor
> app.controller.ts
```ts
@UseInterceptors(MorganInterceptor('combined'))
@Get('/some/route')
public async someRoute() {
...
}
```
5 changes: 5 additions & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export * from './morgan.constants';
export * from './morgan.interceptor.abstract';
export * from './morgan.interceptor.mixin';
export * from './morgan.module';
export * from './morgan.providers';
1 change: 1 addition & 0 deletions lib/morgan.constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const MORGAN_PROVIDER = 'MORGAN_PROVIDER';
40 changes: 40 additions & 0 deletions lib/morgan.interceptor.abstract.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {
ExecutionContext, Inject, Injectable,
NestInterceptor,
} from '@nestjs/common';
import { Observable } from 'rxjs/Observable';
import * as morgan from 'morgan';
import { MORGAN_PROVIDER } from './morgan.constants';

@Injectable()
export abstract class AbstractMorganInterceptor implements NestInterceptor {

protected abstract readonly options: morgan.Options;
protected abstract readonly format: string | morgan.FormatFn;

constructor(
@Inject(MORGAN_PROVIDER) private morganInstance: morgan.Morgan,
) {
}

intercept(
context: ExecutionContext,
call$: Observable<any>,
): Observable<any> {
const httpRequest = context.switchToHttp().getRequest();
const httpResponse = context.switchToHttp().getResponse();

// If is here just to satisfy TypeScript
if (typeof this.format === 'string') {
this.morganInstance(this.format, this.options)(httpRequest, httpResponse, this.handleError)
} else {
this.morganInstance(this.format, this.options)(httpRequest, httpResponse, this.handleError)
}

return call$;
}

private handleError(err: any) {
throw err
}
}
14 changes: 14 additions & 0 deletions lib/morgan.interceptor.mixin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { mixin } from '@nestjs/common';
import * as morgan from 'morgan';
import { AbstractMorganInterceptor } from './morgan.interceptor.abstract';

// tslint:disable-next-line:function-name
export function MorganInterceptor(
format: string | morgan.FormatFn,
options: morgan.Options = {},
) {
return mixin(class extends AbstractMorganInterceptor {
protected readonly options = options;
protected readonly format = format;
});
}
19 changes: 19 additions & 0 deletions lib/morgan.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { DynamicModule, Global, Module } from '@nestjs/common';
import { morganProviders } from './morgan.providers';

@Global()
@Module({
providers: [
...morganProviders,
],
exports: [
...morganProviders,
],
})
export class MorganModule {
static forRoot(): DynamicModule {
return {
module: MorganModule,
};
}
}
11 changes: 11 additions & 0 deletions lib/morgan.providers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { MORGAN_PROVIDER } from "./morgan.constants";
import * as morgan from "morgan";

export const morganProviders = [
{
provide: MORGAN_PROVIDER,
useFactory: (): morgan.Morgan => {
return morgan
},
},
];
Loading

0 comments on commit 639f9ae

Please sign in to comment.