Skip to content

Commit

Permalink
feat(module): let base module work for http express
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcdo29 committed Mar 27, 2020
1 parent 39d69be commit 1bb52a7
Show file tree
Hide file tree
Showing 74 changed files with 1,049 additions and 146 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"start:test": "node dist-test/main.js",
"start:test:ws": "node dist-test/main.js ws",
"format": "prettier --write \"packages/**/*.ts\"",
"lint": "eslint --ext .ts packages/",
"lint": "eslint --ext .ts .",
"test": "jest",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
Expand Down
3 changes: 3 additions & 0 deletions packages/logger/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,8 @@
},
"bugs": {
"url": "https://github.com/jmcdo29/nestjs-ogma/issues"
},
"devDependencies": {
"rxjs": "^6.5.4"
}
}
10 changes: 7 additions & 3 deletions packages/nestjs-module/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@
"@ogma/logger": "^0.1.0"
},
"peerDependencies": {
"@nestjs/common": "^6.0.0",
"@nestjs/core": "^6.0.0",
"reflect-metadata": "^0.1.0"
"@nestjs/common": "^7.0.0",
"@nestjs/core": "^7.0.0",
"reflect-metadata": "^0.1.0",
"rxjs": "^6.0.0"
},
"publishConfig": {
"access": "public"
Expand All @@ -47,5 +48,8 @@
},
"bugs": {
"url": "https://github.com/jmcdo29/nestjs-ogma/issues"
},
"devDependencies": {
"rxjs": "^6.5.4"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Inject } from '@nestjs/common';
import { createProviderToken } from '../ogma.provider';

export const OgmaLogger = (topic: string | Function) =>
Inject(createProviderToken(typeof topic === 'function' ? topic.name : topic));
2 changes: 2 additions & 0 deletions packages/nestjs-module/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ export * from './interceptor/ogma.interceptor';
export * from './ogma.module';
export * from './ogma.service';
export * from './interfaces/ogma-options.interface';
export * from './interceptor/abstract-interceptor.service';
export { Type } from './interfaces/ogma-options.interface';
25 changes: 17 additions & 8 deletions packages/nestjs-module/src/interceptor/ogma.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
import { OgmaService } from '../ogma.service';
import { DelegatorService } from './delegator.service';
import { LogObject } from './interfaces/log.interface';
import { TcpContext } from '@nestjs/microservices';

@Injectable()
export class OgmaInterceptor implements NestInterceptor {
Expand All @@ -42,10 +41,7 @@ export class OgmaInterceptor implements NestInterceptor {
return next.handle().pipe(
tap(
(data) => {
this.service.log(context.switchToRpc().getContext());
this.service.log(
context.switchToRpc().getContext() instanceof TcpContext,
);
console.log(this.options);
if (!this.shouldSkip(context)) {
logObject = this.delegate.getContextSuccessString(
data,
Expand All @@ -72,10 +68,23 @@ export class OgmaInterceptor implements NestInterceptor {
}

public shouldSkip(context: ExecutionContext): boolean {
return (
const decoratorSkip =
this.reflector.get(OGMA_INTERCEPTOR_SKIP, context.getClass()) ||
this.reflector.get(OGMA_INTERCEPTOR_SKIP, context.getHandler())
);
this.reflector.get(OGMA_INTERCEPTOR_SKIP, context.getHandler());
if (decoratorSkip) {
return true;
}
switch (context.getType()) {
case 'http':
if (context.getArgs().length === 3) {
return !this.options.http;
}
return !this.options.gql;
case 'ws':
return !this.options.ws;
case 'rpc':
return !this.options.rpc;
}
}

public log(logObject: string | LogObject, context: ExecutionContext): void {
Expand Down
76 changes: 42 additions & 34 deletions packages/nestjs-module/src/ogma-core.module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { createConfigurableDynamicRootModule } from '@golevelup/nestjs-modules';
import { CallHandler, ExecutionContext, Module } from '@nestjs/common';
import {
CallHandler,
ExecutionContext,
Module,
NestInterceptor,
} from '@nestjs/common';
import { APP_INTERCEPTOR, Reflector } from '@nestjs/core';
import { DelegatorService } from './interceptor/delegator.service';
import { OgmaInterceptor } from './interceptor/ogma.interceptor';
Expand All @@ -15,18 +20,19 @@ import {
OGMA_SERVICE_OPTIONS,
} from './ogma.constants';
import { OgmaService } from './ogma.service';
import {
createOgmaProvider,
wsInterceptorProvider,
httpInterceptorProvider,
gqlInterceptorProviders,
rpcInterceptorProvider,
} from './ogma.provider';
import { createOgmaProvider } from './ogma.provider';
import { WebsocketInterceptorService } from './interceptor/websocket-interceptor.service';
import { HttpInterceptorService } from './interceptor/http-interceptor.service';
import { GqlInterceptorService } from './interceptor/gql-interceptor.service';
import { RpcInterceptorService } from './interceptor/rpc-interceptor.service';

let ogmaInterceptorDefaults: OgmaInterceptorOptions = {
http: HttpInterceptorService,
ws: false,
rpc: false,
gql: false,
};

@Module({})
export class OgmaCoreModule extends createConfigurableDynamicRootModule<
OgmaCoreModule,
Expand All @@ -35,18 +41,15 @@ export class OgmaCoreModule extends createConfigurableDynamicRootModule<
providers: [
{
provide: OGMA_INTERCEPTOR_OPTIONS,
inject: [OGMA_OPTIONS],
useFactory: (
options: OgmaModuleOptions,
): OgmaInterceptorOptions | boolean => {
const intOpts = options.interceptor;
if (intOpts !== undefined) {
if (intOpts === false) {
return intOpts;
}
return OgmaCoreModule.mergeInterceptorDefaults(intOpts);
} else {
return true;
): OgmaInterceptorOptions | false => {
const intOpts = options?.interceptor ?? undefined;
if (intOpts === false) {
return intOpts;
}
return OgmaCoreModule.mergeInterceptorDefaults(intOpts);
},
},
{
Expand All @@ -62,8 +65,9 @@ export class OgmaCoreModule extends createConfigurableDynamicRootModule<
delegate: DelegatorService,
reflector: Reflector,
) => {
let interceptor;
let interceptor: NestInterceptor;
if (options) {
console.log(delegate);
interceptor = new OgmaInterceptor(
options,
service,
Expand Down Expand Up @@ -91,40 +95,44 @@ export class OgmaCoreModule extends createConfigurableDynamicRootModule<
inject: [OGMA_SERVICE_OPTIONS],
},
{
provide: WebsocketInterceptorService,
useClass: wsInterceptorProvider(),
provide: HttpInterceptorService,
useClass: ogmaInterceptorDefaults.http
? ogmaInterceptorDefaults.http
: HttpInterceptorService,
},
{
provide: HttpInterceptorService,
useClass: httpInterceptorProvider(),
provide: WebsocketInterceptorService,
useClass: ogmaInterceptorDefaults.ws
? ogmaInterceptorDefaults.ws
: WebsocketInterceptorService,
},
{
provide: GqlInterceptorService,
useClass: gqlInterceptorProviders(),
useClass: ogmaInterceptorDefaults.gql
? ogmaInterceptorDefaults.gql
: GqlInterceptorService,
},
{
provide: RpcInterceptorService,
useClass: rpcInterceptorProvider(),
useClass: ogmaInterceptorDefaults.rpc
? ogmaInterceptorDefaults.rpc
: RpcInterceptorService,
},
OgmaService,
DelegatorService,
],
}) {
static interceptorOptions: OgmaInterceptorOptions = {
http: HttpInterceptorService,
ws: false as const,
rpc: false as const,
gql: false as const,
};
static Deferred = OgmaCoreModule.externallyConfigured(OgmaCoreModule, 0);

static mergeInterceptorDefaults(
options: OgmaInterceptorOptions | true,
): OgmaInterceptorOptions {
if (typeof options !== 'boolean') {
OgmaCoreModule.interceptorOptions = {
...OgmaCoreModule.interceptorOptions,
if (typeof options !== 'boolean' && options !== undefined) {
ogmaInterceptorDefaults = {
...ogmaInterceptorDefaults,
...options,
};
}
return OgmaCoreModule.interceptorOptions;
return ogmaInterceptorDefaults;
}
}
1 change: 1 addition & 0 deletions packages/nestjs-module/src/ogma.constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export const MICROSERVICE_METADATA = 'microservices:pattern';
export const OGMA_INTERCEPTOR_OPTIONS = 'OGMA_INTERCEPTOR_OPTIONS';
export const OGMA_SERVICE_OPTIONS = 'OGMA_SERVICE_OPTIONS';
export const OGMA_INTERCEPTOR_SKIP = 'OGMA_INTERCEPTOR_SKIP';
export const OGMA_SERVICE_TOKEN = 'OGMA_SERVICE';
49 changes: 17 additions & 32 deletions packages/nestjs-module/src/ogma.module.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,17 @@
import { AsyncModuleConfig } from '@golevelup/nestjs-modules';
import { DynamicModule, Module, Scope } from '@nestjs/common';
import { Ogma } from '@ogma/logger';
import { DelegatorService } from './interceptor/delegator.service';
import { HttpInterceptorService } from './interceptor/http-interceptor.service';
import { RpcInterceptorService } from './interceptor/rpc-interceptor.service';
import { WebsocketInterceptorService } from './interceptor/websocket-interceptor.service';
import {
OgmaModuleOptions,
OgmaServiceOptions,
} from './interfaces/ogma-options.interface';
import {
OGMA_CONTEXT,
OGMA_INSTANCE,
OGMA_INTERCEPTOR_OPTIONS,
OGMA_SERVICE_OPTIONS,
} from './ogma.constants';
import { createOgmaProvider } from './ogma.provider';
import { DynamicModule, Module, Provider } from '@nestjs/common';
import { OgmaModuleOptions } from './interfaces/ogma-options.interface';
import { createLoggerProviders } from './ogma.provider';
import { OgmaService } from './ogma.service';
import { OgmaCoreModule } from './ogma-core.module';

@Module({
imports: [OgmaCoreModule.Deferred],
exports: [
OgmaCoreModule,
OGMA_INTERCEPTOR_OPTIONS,
OGMA_SERVICE_OPTIONS,
OgmaService,
DelegatorService,
HttpInterceptorService,
WebsocketInterceptorService,
RpcInterceptorService,
],
exports: [OgmaCoreModule, OgmaService],
})
export class OgmaModule {
static ogmaInstance?: Ogma;

static forRoot(options: OgmaModuleOptions): DynamicModule {
return OgmaCoreModule.forRoot(OgmaCoreModule, options);
static forRoot(options?: OgmaModuleOptions): DynamicModule {
return OgmaCoreModule.forRoot(OgmaCoreModule, options ?? {});
}

static forRootAsync(
Expand All @@ -52,7 +27,7 @@ export class OgmaModule {
* @param context string context for the OgmaService to use in logging
* @param options optional additional options for creating a new Ogma instance
*/
static forFeature(context = '', options?: OgmaServiceOptions): DynamicModule {
/* static forFeature(context = '', options?: OgmaServiceOptions): DynamicModule {
return {
module: OgmaModule,
imports: [OgmaCoreModule.Deferred],
Expand Down Expand Up @@ -82,5 +57,15 @@ export class OgmaModule {
],
exports: [OgmaService],
};
} */
static forFeature(context: string | Function): DynamicModule {
const providers: Provider[] = createLoggerProviders(context);

return {
imports: [OgmaCoreModule.Deferred],
module: OgmaModule,
providers,
exports: providers,
};
}
}
48 changes: 18 additions & 30 deletions packages/nestjs-module/src/ogma.provider.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,32 @@
import { Ogma, OgmaOptions } from '@ogma/logger';
import { AbstractInterceptorService } from './interceptor/abstract-interceptor.service';
import { GqlInterceptorService } from './interceptor/gql-interceptor.service';
import { HttpInterceptorService } from './interceptor/http-interceptor.service';
import { RpcInterceptorService } from './interceptor/rpc-interceptor.service';
import { WebsocketInterceptorService } from './interceptor/websocket-interceptor.service';
import { Type } from './interfaces';
import { OgmaCoreModule } from './ogma-core.module';
import { OgmaModule } from './ogma.module';
import { Provider } from '@nestjs/common';
import { OgmaService } from './ogma.service';
import { OGMA_INSTANCE, OGMA_SERVICE_TOKEN } from './ogma.constants';

/**
* @internal
*/
export function createOgmaProvider(options?: Partial<OgmaOptions>): Ogma {
OgmaModule.ogmaInstance = new Ogma({
return new Ogma({
...options,
application: options?.application || 'Nest',
});
return OgmaModule.ogmaInstance;
}

export function wsInterceptorProvider(): Type<AbstractInterceptorService> {
return OgmaCoreModule.interceptorOptions.ws
? OgmaCoreModule.interceptorOptions.ws
: WebsocketInterceptorService;
export function createProviderToken(topic: string): string {
return OGMA_SERVICE_TOKEN + topic;
}

export function httpInterceptorProvider(): Type<AbstractInterceptorService> {
return OgmaCoreModule.interceptorOptions.http
? OgmaCoreModule.interceptorOptions.http
: HttpInterceptorService;
}

export function gqlInterceptorProviders(): Type<AbstractInterceptorService> {
return OgmaCoreModule.interceptorOptions.gql
? OgmaCoreModule.interceptorOptions.gql
: GqlInterceptorService;
}

export function rpcInterceptorProvider(): Type<AbstractInterceptorService> {
return OgmaCoreModule.interceptorOptions.rpc
? OgmaCoreModule.interceptorOptions.rpc
: RpcInterceptorService;
export function createLoggerProviders(topic: string | Function): Provider[] {
topic = typeof topic === 'function' ? topic.name : topic;
const token = createProviderToken(topic);
return [
{
inject: [OGMA_INSTANCE],
provide: token,
useFactory: (ogmaInstance: Ogma): OgmaService => {
return new OgmaService(ogmaInstance, topic as string);
},
},
];
}
10 changes: 2 additions & 8 deletions packages/nestjs-module/src/ogma.service.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
import {
Inject,
Injectable,
LoggerService,
Optional,
Scope,
} from '@nestjs/common';
import { Inject, Injectable, LoggerService, Optional } from '@nestjs/common';
import { Ogma } from '@ogma/logger';
import { OGMA_CONTEXT, OGMA_INSTANCE } from './ogma.constants';

@Injectable({ scope: Scope.TRANSIENT })
@Injectable()
export class OgmaService implements LoggerService {
private readonly context?: string;
private readonly ogma: Ogma;
Expand Down
Loading

0 comments on commit 1bb52a7

Please sign in to comment.