@sepo27/servicio 0.3.2 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -13,7 +13,7 @@ export class ServicioApp {
|
|
|
13
13
|
this._express = _express;
|
|
14
14
|
}
|
|
15
15
|
/*** Public ***/
|
|
16
|
-
|
|
16
|
+
useRoute(route) {
|
|
17
17
|
const expressMethod = route.endpoint.method.toLowerCase();
|
|
18
18
|
// @ts-ignore: TODO ?
|
|
19
19
|
this._express[expressMethod](route.endpoint.path, this.wrapRouteHandler(route.handler));
|
|
@@ -26,7 +26,7 @@ describe('ServicioApp', () => {
|
|
|
26
26
|
afterEach(() => {
|
|
27
27
|
mock.reset();
|
|
28
28
|
});
|
|
29
|
-
it('
|
|
29
|
+
it('useRoute() adds route to express app for all methods', () => {
|
|
30
30
|
const paths = ['/foo', '/bar/:bar', '/abc'], endpoints = Object.values(HttpMethod).map(method => {
|
|
31
31
|
const pathIdx = Math.floor(Math.random() * paths.length), endpointPath = paths[pathIdx];
|
|
32
32
|
return { method, path: endpointPath };
|
|
@@ -35,24 +35,24 @@ describe('ServicioApp', () => {
|
|
|
35
35
|
endpoints.forEach(endpoint => {
|
|
36
36
|
const route = new Route(endpoint, () => Promise.resolve(new SuccessResponse()));
|
|
37
37
|
const expressMethodMock = expressMethodMockMap[endpoint.method];
|
|
38
|
-
app.
|
|
38
|
+
app.useRoute(route);
|
|
39
39
|
expect(expressMethodMock).toHaveBeenCalledWith(route.endpoint.path, wrapRouteHandlerMock);
|
|
40
40
|
});
|
|
41
41
|
});
|
|
42
|
-
it('
|
|
42
|
+
it('useRoute() wraps route handler to send http status', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
43
43
|
const response = new SuccessResponse(HttpStatus.ACCEPTED), route = new Route('GET /foo', () => Promise.resolve(response));
|
|
44
44
|
const resMock = yield runRouteHandlerWrapper(route);
|
|
45
45
|
expect(resMock.status).toHaveBeenCalledWith(response.status);
|
|
46
46
|
expect(resMock.send).toHaveBeenCalled();
|
|
47
47
|
}));
|
|
48
|
-
it('
|
|
48
|
+
it('useRoute() wraps route handler to send http body', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
49
49
|
const responseBody = { foo: 'bar' }, response = new SuccessResponse({ status: HttpStatus.OK, body: responseBody }), route = new Route('GET /foo', () => Promise.resolve(response));
|
|
50
50
|
const resMock = yield runRouteHandlerWrapper(route);
|
|
51
51
|
expect(resMock.status).toHaveBeenCalledWith(response.status);
|
|
52
52
|
expect(resMock.status).toHaveBeenCalledBefore(resMock.send);
|
|
53
53
|
expect(resMock.send).toHaveBeenCalledWith(responseBody);
|
|
54
54
|
}));
|
|
55
|
-
it('
|
|
55
|
+
it('useRoute() wraps route handler to send http headers', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
56
56
|
const responseHeaders = { 'x-header': 'abc' }, response = new SuccessResponse({ status: HttpStatus.OK, headers: responseHeaders }), route = new Route('GET /foo', () => Promise.resolve(response));
|
|
57
57
|
const resMock = yield runRouteHandlerWrapper(route);
|
|
58
58
|
expect(resMock.status).toHaveBeenCalledWith(response.status);
|
|
@@ -60,7 +60,7 @@ describe('ServicioApp', () => {
|
|
|
60
60
|
expect(resMock.set).toHaveBeenCalledBefore(resMock.send);
|
|
61
61
|
expect(resMock.send).toHaveBeenCalled();
|
|
62
62
|
}));
|
|
63
|
-
it('
|
|
63
|
+
it('useRoute() wraps route handler to send http status, body and headers', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
64
64
|
const responseHeaders = { 'x-header': 'abc' }, responseBody = { ok: true }, response = new SuccessResponse({ status: HttpStatus.OK, headers: responseHeaders, body: responseBody }), route = new Route('GET /foo', () => Promise.resolve(response));
|
|
65
65
|
const resMock = yield runRouteHandlerWrapper(route);
|
|
66
66
|
expect(resMock.status).toHaveBeenCalledWith(response.status);
|
|
@@ -82,7 +82,7 @@ describe('ServicioApp', () => {
|
|
|
82
82
|
function runRouteHandlerWrapper(route) {
|
|
83
83
|
return __awaiter(this, void 0, void 0, function* () {
|
|
84
84
|
const expressMethodMock = jest.spyOn(expressApp, route.endpoint.method.toLowerCase());
|
|
85
|
-
app.
|
|
85
|
+
app.useRoute(route);
|
|
86
86
|
const routeHandlerWrapper = expressMethodMock.mock.calls[0][1];
|
|
87
87
|
const resMock = mock.express.res;
|
|
88
88
|
// @ts-ignore
|
|
@@ -34,7 +34,7 @@ describe('servicio', () => {
|
|
|
34
34
|
},
|
|
35
35
|
});
|
|
36
36
|
const route = new Route('GET /abc', () => response);
|
|
37
|
-
app.
|
|
37
|
+
app.useRoute(route);
|
|
38
38
|
yield agent
|
|
39
39
|
.get('/abc')
|
|
40
40
|
.expect(HttpStatus.OK)
|
|
@@ -46,14 +46,14 @@ describe('servicio', () => {
|
|
|
46
46
|
const error = new BadRequestError(), route = new Route('GET /xyz', () => {
|
|
47
47
|
throw error;
|
|
48
48
|
});
|
|
49
|
-
app.
|
|
49
|
+
app.useRoute(route);
|
|
50
50
|
yield agent.get('/xyz').expect(error.status);
|
|
51
51
|
}));
|
|
52
52
|
it('handles http error with status by error handler', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
53
53
|
const error = new InternalServerError(), route = new Route('GET /xyz', () => {
|
|
54
54
|
throw error;
|
|
55
55
|
});
|
|
56
|
-
app.
|
|
56
|
+
app.useRoute(route).useErrorHandler();
|
|
57
57
|
yield agent.get('/xyz').expect(error.status);
|
|
58
58
|
}));
|
|
59
59
|
it('handles http error with status and body by error handler', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
@@ -62,7 +62,7 @@ describe('servicio', () => {
|
|
|
62
62
|
}), route = new Route('GET /baz', () => {
|
|
63
63
|
throw error;
|
|
64
64
|
});
|
|
65
|
-
app.
|
|
65
|
+
app.useRoute(route).useErrorHandler();
|
|
66
66
|
yield agent
|
|
67
67
|
.get('/baz')
|
|
68
68
|
.expect(error.status)
|