Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
"@types/jest": "^25.1.4",
"@types/node": "^13.9.0",
"@types/supertest": "^2.0.7",
"class-transformer": "^0.3.2",
"class-validator": "^0.13.1",
"jest": "^25.1.0",
"prettier": "^1.15.3",
"rxjs": "^6.0.0",
Expand Down
4 changes: 4 additions & 0 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ describe("json-rpc-e2e", () => {
expect(res).toStrictEqual({ test: "hi" });
});

it(`should throw if sends an invalid DTO`, async () => {
let res = service.invokeClientService({ test: "" });
await expect(res).rejects.toThrow("hi");
});
it(`should fail to make a request with an unauthorized JsonRpcClient`, async () => {
let result = unauthorizedService.invokeClientService({ test: "hi" });
await expect(result).rejects.toThrowError("Forbidden resource");
Expand Down
21 changes: 10 additions & 11 deletions src/test-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import {
UseInterceptors,
UseGuards,
Scope,
Inject
ValidationPipe
} from "@nestjs/common";
import { Ctx } from "@nestjs/microservices";

import { IsNotEmpty } from "class-validator";

import { CodedRpcException, JsonRpcContext, RpcController, RpcMethod, RpcService } from ".";

const initialModuleState = {
Expand Down Expand Up @@ -83,6 +85,11 @@ class TestGuard implements CanActivate {

type IRpcTestService = RpcController<ITestClientService>;

export class TestDto {
@IsNotEmpty()
test!: string;
}

@RpcService({
namespace: "test"
})
Expand All @@ -92,14 +99,6 @@ export class TestService implements IRpcTestService {
DecorationsState.serviceConstructorCount = DecorationsState.serviceConstructorCount + 1;
}

@UsePipes(TestPipe)
@UseInterceptors(TestInterceptor)
@UseGuards(TestGuard)
@RpcMethod()
public async invoke(params: { test: string }) {
return Promise.resolve(params);
}

@UsePipes(TestPipe)
@UseInterceptors(TestInterceptor)
@UseGuards(TestGuard)
Expand All @@ -110,10 +109,11 @@ export class TestService implements IRpcTestService {
}

@UsePipes(TestPipe)
@UsePipes(ValidationPipe)
@UseInterceptors(TestInterceptor)
@UseGuards(TestGuard)
@RpcMethod()
public async invokeClientService(params: { test: string }) {
public async invokeClientService(params: TestDto) {
return Promise.resolve(params);
}

Expand All @@ -131,7 +131,6 @@ export class TestService implements IRpcTestService {
}

export interface ITestClientService {
invoke(params: { test: string }): Promise<{ test: string }>;
invokeClientService(params: { test: string }): Promise<{ test: string }>;
testError(params: { errorTest: string }): Promise<void>;
injectContext(params: {}): Promise<{ key: string | undefined }>;
Expand Down