Skip to content

Commit cd62156

Browse files
authored
Merge pull request #17 from hfour/fix/formatting-check
Set up prettier check
2 parents 3885abc + 2bf1090 commit cd62156

File tree

3 files changed

+72
-60
lines changed

3 files changed

+72
-60
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
"scripts": {
1010
"build": "tsc -p tsconfig.json",
1111
"prepublish": "tsc -p tsconfig.json",
12+
"prettier:check": "prettier --no-color --check \"src/**/*.ts\" \"test/**/*.ts\"",
1213
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
13-
"test": "jest",
14+
"test": "yarn prettier:check && jest",
1415
"test:watch": "jest --watch",
1516
"test:cov": "jest --coverage"
1617
},

src/index.spec.ts

Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as request from 'supertest';
1+
import * as request from "supertest";
22

33
import { Test } from "@nestjs/testing";
44
import { INestApplication, INestMicroservice } from "@nestjs/common";
@@ -15,7 +15,7 @@ describe("json-rpc-e2e", () => {
1515
let service: ITestClientService;
1616
let unauthorizedService: ITestClientService;
1717

18-
describe('standalone', () => {
18+
describe("standalone", () => {
1919
beforeAll(async () => {
2020
let moduleRef = await Test.createTestingModule({
2121
controllers: [TestService]
@@ -61,7 +61,10 @@ describe("json-rpc-e2e", () => {
6161
});
6262

6363
it(`should fail to invoke unexposed methods`, async () => {
64-
const expectedCodedException = new CodedRpcException("Method not found: test.notExposed", 404);
64+
const expectedCodedException = new CodedRpcException(
65+
"Method not found: test.notExposed",
66+
404
67+
);
6568
const resp = service.notExposed({ test: "data" });
6669
await expect(resp).rejects.toThrowError(expectedCodedException);
6770
});
@@ -83,17 +86,17 @@ describe("json-rpc-e2e", () => {
8386
});
8487
});
8588

86-
describe('hybrid', () => {
89+
describe("hybrid", () => {
8790
beforeAll(async () => {
8891
let moduleRef = await Test.createTestingModule({
8992
controllers: [TestService]
9093
}).compile();
91-
94+
9295
app = moduleRef.createNestApplication();
9396

9497
server = new JsonRpcServer({
95-
path: '/rpc',
96-
adapter: app.getHttpAdapter(),
98+
path: "/rpc",
99+
adapter: app.getHttpAdapter()
97100
});
98101

99102
app.connectMicroservice({ strategy: server });
@@ -102,46 +105,46 @@ describe("json-rpc-e2e", () => {
102105
await app.init();
103106
});
104107

105-
it('should invoke RPC methods on incoming HTTP requests', async () => {
108+
it("should invoke RPC methods on incoming HTTP requests", async () => {
106109
await request(app.getHttpServer())
107-
.post('/rpc')
108-
.set('Authorization', 'Bearer xyz')
110+
.post("/rpc")
111+
.set("Authorization", "Bearer xyz")
109112
.send({
110-
jsonrpc: '2.0',
111-
method: 'test.invokeClientService',
112-
id: '1',
113+
jsonrpc: "2.0",
114+
method: "test.invokeClientService",
115+
id: "1",
113116
params: {
114-
test: 'hi',
115-
},
117+
test: "hi"
118+
}
116119
})
117120
.expect({
118-
jsonrpc: '2.0',
119-
id: '1',
120-
result: { test: 'hi' }
121+
jsonrpc: "2.0",
122+
id: "1",
123+
result: { test: "hi" }
121124
});
122125
});
123126

124-
it('should wrap json-rpc errors in http 200', async () => {
127+
it("should wrap json-rpc errors in http 200", async () => {
125128
await request(app.getHttpServer())
126-
.post('/rpc')
127-
.set('Authorization', 'Bearer xyz')
128-
.send({
129-
jsonrpc: '2.0',
130-
method: 'test.notExposed',
131-
id: '1',
132-
params: {
133-
test: 'hi',
134-
},
135-
})
136-
.expect({
137-
jsonrpc: '2.0',
138-
id: '1',
139-
error: {
140-
code: 404,
141-
data: {},
142-
message: 'Method not found: test.notExposed',
143-
}
144-
});
129+
.post("/rpc")
130+
.set("Authorization", "Bearer xyz")
131+
.send({
132+
jsonrpc: "2.0",
133+
method: "test.notExposed",
134+
id: "1",
135+
params: {
136+
test: "hi"
137+
}
138+
})
139+
.expect({
140+
jsonrpc: "2.0",
141+
id: "1",
142+
error: {
143+
code: 404,
144+
data: {},
145+
message: "Method not found: test.notExposed"
146+
}
147+
});
145148
});
146149
});
147150
});

src/server.ts

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as express from "express";
22
import * as http from "http";
33

44
import { Server, CustomTransportStrategy } from "@nestjs/microservices";
5-
import { ExpressAdapter } from '@nestjs/platform-express';
5+
import { ExpressAdapter } from "@nestjs/platform-express";
66

77
import { JsonRpcResponse } from "./transport-types";
88
import { CodedRpcException } from "./coded-error";
@@ -30,11 +30,11 @@ interface HybridJsonRpcServerOptions {
3030
}
3131

3232
interface StandaloneJsonRpcServerOptions {
33-
/**
34-
* Listening port for the HTTP server
35-
*/
33+
/**
34+
* Listening port for the HTTP server
35+
*/
3636
port: number;
37-
37+
3838
/**
3939
* Listening host (optional, defaults to any)
4040
*/
@@ -90,26 +90,34 @@ export class JsonRpcServer extends Server implements CustomTransportStrategy {
9090
app.initHttpServer({});
9191
}
9292

93-
app.getInstance().post(this.options.path, express.json(), async (req: express.Request, res: express.Response) => {
94-
let handler = this.getHandlerByPattern(req.body.method);
93+
app
94+
.getInstance()
95+
.post(
96+
this.options.path,
97+
express.json(),
98+
async (req: express.Request, res: express.Response) => {
99+
let handler = this.getHandlerByPattern(req.body.method);
95100

96-
if (handler == null) {
97-
let error = new CodedRpcException("Method not found: " + req.body.method, 404);
98-
return res.status(200).json(serializeResponse(req.body.id, { error }));
99-
}
101+
if (handler == null) {
102+
let error = new CodedRpcException("Method not found: " + req.body.method, 404);
103+
return res.status(200).json(serializeResponse(req.body.id, { error }));
104+
}
100105

101-
let context = new JsonRpcContext(req, app.getHttpServer());
106+
let context = new JsonRpcContext(req, app.getHttpServer());
102107

103-
let observableResult = this.transformToObservable(await handler(req.body.params, context));
104-
let promiseResult = observableResult.toPromise();
108+
let observableResult = this.transformToObservable(
109+
await handler(req.body.params, context)
110+
);
111+
let promiseResult = observableResult.toPromise();
105112

106-
let response = await promiseResult.then(
107-
value => ({ value }),
108-
error => ({ error })
109-
);
113+
let response = await promiseResult.then(
114+
value => ({ value }),
115+
error => ({ error })
116+
);
110117

111-
res.status(200).json(serializeResponse(req.body.id, response));
112-
});
118+
res.status(200).json(serializeResponse(req.body.id, response));
119+
}
120+
);
113121

114122
await invokeAsync(cb => {
115123
if (this.isStandalone(this.options)) {
@@ -122,7 +130,7 @@ export class JsonRpcServer extends Server implements CustomTransportStrategy {
122130
cb();
123131
}
124132
});
125-
133+
126134
callback();
127135
}
128136

0 commit comments

Comments
 (0)