Skip to content

Commit

Permalink
Merge pull request #163 from getjerry/feats-websockets-ability
Browse files Browse the repository at this point in the history
Feats websockets ability
  • Loading branch information
liquidautumn authored Nov 22, 2021
2 parents e0378b5 + 28d4b08 commit 9168110
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
8 changes: 8 additions & 0 deletions src/proxies/context.proxy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ describe('ContextProxy', () => {
const contextProxy = ContextProxy.create(context);
expect(contextProxy.getRequest()).toEqual(req);
});

it('should get request from ws context', () => {
const req = new Object();
const context = new ExecutionContextHost([{}, undefined, { req }]);
context.getType = jest.fn().mockImplementation(() => 'ws');
const contextProxy = ContextProxy.create(context);
expect(contextProxy.getRequest()).toEqual(req);
});

it('should throw error for rpc context', () => {
const req = new Object();
Expand Down
31 changes: 16 additions & 15 deletions src/proxies/context.proxy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GqlContextType, GqlExecutionContext } from '@nestjs/graphql';
import { ExecutionContext, NotAcceptableException } from '@nestjs/common';
import { ContextType, ExecutionContext, NotAcceptableException } from '@nestjs/common';
import { AuthorizableRequest } from 'interfaces/request.interface';

export class ContextProxy {
Expand All @@ -10,20 +10,21 @@ export class ContextProxy {
}

public getRequest(): AuthorizableRequest {
if (this.context.getType() === 'http') {
return this.context.getArgByIndex(0);
switch (this.context.getType<ContextType | GqlContextType>()) {
case 'http':
case 'ws':
return this.context.switchToHttp().getRequest();
case 'graphql': {
const ctx = GqlExecutionContext.create(this.context);
const request = ctx.getContext().req;
request.params = {
...ctx.getArgs(),
...request.params,
};
return request;
}
default:
throw new NotAcceptableException();
}

if (this.context.getType<GqlContextType>() === 'graphql') {
const ctx = GqlExecutionContext.create(this.context);
const request = ctx.getContext().req;
request.params = {
...ctx.getArgs(),
...request.params,
};
return request;
}

throw new NotAcceptableException();
}
}

0 comments on commit 9168110

Please sign in to comment.