Skip to content

Commit

Permalink
refactor(interceptor): adds contributing and build scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcdo29 committed Mar 18, 2020
1 parent 271998a commit 35bc265
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 21 deletions.
26 changes: 26 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Contributions

Any and all contributions are welcome! This is a decently sized project with a good scoped of functionality.

## How to Contribute

1. Create a fork of the repository
2. Clone the code to your local machine
3. Create a new branch with the feature you are working on (e.g. WebSocket-Interceptor) or with the issue number (e.g. issue/42)
4. Implement your changes, ensure tests are still passing, or add tests if it is a new feature
5. Push back to your version on GitHub
6. Raise a Pull Request to the main repository

## Local Testing

Besides unit and e2e tests, there is also the ability to just run a local NestJS application with the `OgmaModule` imported into it. This code is already setup and ready to be worked with as necessary, all you need to do is `npm run build:all` to build both the `OgmaModule` code and the NestJS application and then `npm run start:test` to start the test application. From there, you can use Postman, cURL, the Node REPL, or any other method of testing you find pleasing to use.

### Breaking apart the builds

If you need to only recompile the library code, you can use `npm run build` to build the main lib.

Similarly if you only need to rebuild the NestJS server, use `npm run build:test`.

## Issues

Please raise an issue, or discuss with me [via email](mailto:[email protected]) or [Discord](https://discordapp.com) (PerfectOrphan31#6003) before opening a Pull Request so we can see if they align with the goals of the project.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
"commit": "git-cz",
"prebuild": "rimraf dist",
"build": "nest build",
"build:all": "npm run build && npm run build:test",
"prebuild:test": "rimraf dist-test",
"postbuild:test": "rm -rf dist-test/src && mv dist-test/test/* dist-test && rmdir dist-test/test",
"build:test": "tsc -p tsconfig.test.json",
"start:test": "node dist-test/main.js",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
Expand Down
2 changes: 1 addition & 1 deletion src/interceptor/abstract-interceptor.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export abstract class AbstractInterceptorService implements InterceptorService {
callPoint: this.getCallPoint(context),
status: this.getStatus(context, options.color && !options.json, error),
responseTime: this.getResponseTime(startTime),
contentLength: Buffer.from(error.message || '').byteLength,
contentLength: Buffer.from(JSON.stringify(error.message)).byteLength,
protocol: this.getProtocol(context),
};
}
Expand Down
38 changes: 22 additions & 16 deletions src/interceptor/delegator.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,25 @@ export class DelegatorService {
let logObject: LogObject = {} as any;
switch (context.getType()) {
case 'http':
logObject = this.httpParser.getSuccessContext(
data,
context,
startTime,
options,
);
// hack to handle graphql context properly
if (context.getArgs().length === 3) {
logObject = this.httpParser.getSuccessContext(
data,
context,
startTime,
options,
);
}
break;
/* case 'ws':
case 'ws':
logObject = this.wsParser.getSuccessContext(
data,
context,
startTime,
options,
);
break;
case 'rpc':
/* case 'rpc':
logObject = this.rpcParser.getSuccessContext(
data,
context,
Expand All @@ -59,22 +62,25 @@ export class DelegatorService {
let logObject: LogObject = {} as any;
switch (context.getType()) {
case 'http':
logObject = this.httpParser.getErrorContext(
error,
context,
startTime,
options,
);
// hack to handle graphql context properly
if (context.getArgs().length === 3) {
logObject = this.httpParser.getErrorContext(
error,
context,
startTime,
options,
);
}
break;
/* case 'ws':
case 'ws':
logObject = this.wsParser.getErrorContext(
error,
context,
startTime,
options,
);
break;
case 'rpc':
/* case 'rpc':
logObject = this.rpcParser.getErrorContext(
error,
context,
Expand Down
28 changes: 27 additions & 1 deletion src/interceptor/websocket-interceptor.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,30 @@
import { ExecutionContext, HttpException, Injectable } from '@nestjs/common';
import { AbstractInterceptorService } from './abstract-interceptor.service';

@Injectable()
export class WebsocketInterceptorService {}
export class WebsocketInterceptorService extends AbstractInterceptorService {
getCallPoint(context: ExecutionContext): string {
console.log(JSON.stringify(context.switchToWs(), null, 2));
return 'something';
}

getCallerIp(context: ExecutionContext): string[] | string {
return 'something';
}

getMethod(context: ExecutionContext): string {
return 'something';
}

getProtocol(context: ExecutionContext): string {
return 'something';
}

getStatus(
context: ExecutionContext,
inColor: boolean,
error?: HttpException | Error,
): string {
return 'something';
}
}
20 changes: 19 additions & 1 deletion test/app.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Controller, Get } from '@nestjs/common';
import { BadRequestException, Controller, Get, HttpCode } from '@nestjs/common';
import { OgmaSkip } from '../dist/decorators/skip.decorator';
import { AppService } from './app.service';

@Controller()
Expand All @@ -9,4 +10,21 @@ export class AppController {
getMessage(): object {
return this.appService.getSimpleMessage();
}

@Get('status')
@HttpCode(201)
getStatus(): object {
return this.appService.getSimpleMessage();
}

@Get('error')
getError(): never {
throw new BadRequestException('Bad');
}

@OgmaSkip()
@Get('skip')
skipMessage(): object {
return this.appService.getSimpleMessage();
}
}
6 changes: 4 additions & 2 deletions tsconfig.test.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "./dist-test",
"declaration": false
"declaration": false,
"sourceMap": false
},
"exclude": ["node_modules", "src", "dist", "**/*spec.ts"]
"include": ["test"],
"exclude": ["node_modules", "src", "src/interfaces", "dist", "**/*spec.ts"]
}

0 comments on commit 35bc265

Please sign in to comment.