@sepo27/servicio 0.3.2 → 0.5.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 +14 -0
- package/README.md +71 -10
- package/dist/src/@spec/index.d.ts +1 -1
- package/dist/src/@spec/index.js +1 -1
- package/dist/src/@spec/tbench/TBench.d.ts +6 -0
- package/dist/src/@spec/tbench/TBench.js +7 -0
- package/dist/src/@spec/tbench/TMock.d.ts +10 -0
- package/dist/src/@spec/tbench/TMock.js +53 -0
- package/dist/src/@spec/tbench/index.d.ts +2 -0
- package/dist/src/@spec/tbench/index.js +2 -0
- package/dist/src/@spec/tbench/types.d.ts +15 -0
- package/dist/src/@spec/tbench/types.js +1 -0
- package/dist/src/index.d.ts +12 -2
- package/dist/src/index.js +12 -2
- package/dist/src/lib/express/types.d.ts +2 -1
- package/dist/src/lib/express/types.js +29 -28
- package/dist/src/main/ServicioApp.d.ts +1 -1
- package/dist/src/main/ServicioApp.js +1 -1
- package/dist/src/main/ServicioApp.spec.js +7 -7
- package/dist/src/main/app/App.d.ts +3 -0
- package/dist/src/main/app/App.js +1 -0
- package/dist/src/main/app/AppX.d.ts +9 -0
- package/dist/src/main/app/AppX.js +14 -0
- package/dist/src/main/app/AppX.spec.d.ts +1 -0
- package/dist/src/main/app/AppX.spec.js +17 -0
- package/dist/src/main/app/errorHandler.d.ts +10 -0
- package/dist/src/main/app/errorHandler.js +21 -0
- package/dist/src/main/app/errorHandler.spec.d.ts +1 -0
- package/dist/src/main/app/errorHandler.spec.js +56 -0
- package/dist/src/main/app/servicio.spec.d.ts +1 -0
- package/dist/src/main/app/servicio.spec.js +119 -0
- package/dist/src/main/factories/makeApp.d.ts +2 -0
- package/dist/src/main/factories/makeApp.js +7 -0
- package/dist/src/main/factories/makeApp.spec.d.ts +1 -0
- package/dist/src/main/factories/makeApp.spec.js +19 -0
- package/dist/src/main/factories/makeRoute.d.ts +3 -0
- package/dist/src/main/factories/makeRoute.js +2 -0
- package/dist/src/main/factories/makeRoute.spec.d.ts +1 -0
- package/dist/src/main/factories/makeRoute.spec.js +21 -0
- package/dist/src/main/factories/makeRouter.d.ts +2 -0
- package/dist/src/main/factories/makeRouter.js +7 -0
- package/dist/src/main/factories/makeRouter.spec.d.ts +1 -0
- package/dist/src/main/factories/makeRouter.spec.js +24 -0
- package/dist/src/main/factories/mixinProxy.d.ts +3 -0
- package/dist/src/main/factories/mixinProxy.js +41 -0
- package/dist/src/main/factories/mixinProxy.spec.d.ts +1 -0
- package/dist/src/main/factories/mixinProxy.spec.js +43 -0
- package/dist/src/main/routing/Routable.d.ts +12 -0
- package/dist/src/main/routing/Routable.js +16 -0
- package/dist/src/main/routing/Routable.spec.d.ts +1 -0
- package/dist/src/main/routing/Routable.spec.js +39 -0
- package/dist/src/main/routing/Route.d.ts +13 -0
- package/dist/src/main/routing/Route.js +51 -0
- package/dist/src/main/routing/Route.spec.d.ts +1 -0
- package/dist/src/main/routing/Route.spec.js +86 -0
- package/dist/src/main/routing/Router.d.ts +3 -0
- package/dist/src/main/routing/Router.js +1 -0
- package/dist/src/main/routing/RouterX.d.ts +6 -0
- package/dist/src/main/routing/RouterX.js +7 -0
- package/dist/src/main/routing/RouterX.spec.d.ts +1 -0
- package/dist/src/main/routing/RouterX.spec.js +15 -0
- package/dist/src/main/servicio.spec.js +4 -4
- package/dist/src/main/types.d.ts +5 -10
- package/package.json +2 -1
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { TBench } from '../../@spec/tbench/TBench.js';
|
|
11
|
+
import supertest from 'supertest';
|
|
12
|
+
import { svc } from '../../index.js';
|
|
13
|
+
import { BadRequestError, HttpStatus, InternalServerError, SuccessResponse } from '../../lib/http-meta/index.js';
|
|
14
|
+
/**
|
|
15
|
+
* Integration tests for Servicio app.
|
|
16
|
+
*/
|
|
17
|
+
describe('servicio', () => {
|
|
18
|
+
let bench, agent, app;
|
|
19
|
+
beforeEach(() => {
|
|
20
|
+
bench = new TBench();
|
|
21
|
+
app = svc.app();
|
|
22
|
+
agent = supertest.agent(app);
|
|
23
|
+
});
|
|
24
|
+
afterEach(() => {
|
|
25
|
+
bench.reset();
|
|
26
|
+
});
|
|
27
|
+
it('handles simple express like native route', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
28
|
+
app.get('/foo', (req, res) => {
|
|
29
|
+
res.status(200).send();
|
|
30
|
+
});
|
|
31
|
+
yield agent.get('/foo').expect(200);
|
|
32
|
+
}));
|
|
33
|
+
it('handles simple inline route', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
34
|
+
const response = new SuccessResponse({
|
|
35
|
+
status: HttpStatus.OK,
|
|
36
|
+
body: {
|
|
37
|
+
mary: 'Had a little lamb',
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
app.useRoute('GET /abc', () => response);
|
|
41
|
+
yield agent
|
|
42
|
+
.get('/abc')
|
|
43
|
+
.expect(HttpStatus.OK)
|
|
44
|
+
.expect(res => {
|
|
45
|
+
expect(res.body).toEqual(response.body);
|
|
46
|
+
});
|
|
47
|
+
}));
|
|
48
|
+
it('handles simple route with Route class', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
49
|
+
const response = new SuccessResponse({
|
|
50
|
+
status: HttpStatus.OK,
|
|
51
|
+
body: {
|
|
52
|
+
mary: 'Had a little lamb',
|
|
53
|
+
},
|
|
54
|
+
});
|
|
55
|
+
const route = svc.route('GET /xyz', () => response);
|
|
56
|
+
app.useRoute(route);
|
|
57
|
+
yield agent
|
|
58
|
+
.get('/xyz')
|
|
59
|
+
.expect(HttpStatus.OK)
|
|
60
|
+
.expect(res => {
|
|
61
|
+
expect(res.body).toEqual(response.body);
|
|
62
|
+
});
|
|
63
|
+
}));
|
|
64
|
+
it('handles router', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
65
|
+
const router = svc.router('/foo');
|
|
66
|
+
router.useRoute('GET /:id', () => new SuccessResponse());
|
|
67
|
+
app.useRouter(router);
|
|
68
|
+
yield agent
|
|
69
|
+
.get('/foo/123')
|
|
70
|
+
.expect(HttpStatus.OK);
|
|
71
|
+
}));
|
|
72
|
+
it('handles nested router', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
73
|
+
const router = svc.router('/bar');
|
|
74
|
+
router.useRoute('GET /:id', () => new SuccessResponse());
|
|
75
|
+
const itemsResponse = new SuccessResponse({
|
|
76
|
+
status: HttpStatus.OK,
|
|
77
|
+
body: { items: ['1', '2', '3'] },
|
|
78
|
+
}), nestedRouter = svc.router('/:id/items');
|
|
79
|
+
nestedRouter.useRoute('GET /', () => itemsResponse);
|
|
80
|
+
router.useRouter(nestedRouter);
|
|
81
|
+
app.useRouter(router);
|
|
82
|
+
yield agent
|
|
83
|
+
.get('/bar/123/items')
|
|
84
|
+
.expect(itemsResponse.status)
|
|
85
|
+
.expect(res => {
|
|
86
|
+
expect(res.body).toEqual(itemsResponse.body);
|
|
87
|
+
});
|
|
88
|
+
}));
|
|
89
|
+
it('handles http error with status by default (w/ express default handler)', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
90
|
+
const error = new BadRequestError();
|
|
91
|
+
app.useRoute('GET /xyz', () => {
|
|
92
|
+
throw error;
|
|
93
|
+
});
|
|
94
|
+
yield agent.get('/xyz').expect(error.status);
|
|
95
|
+
}));
|
|
96
|
+
it('handles http error with status by error handler', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
97
|
+
const error = new InternalServerError();
|
|
98
|
+
app.useRoute('GET /xyz', () => {
|
|
99
|
+
throw error;
|
|
100
|
+
});
|
|
101
|
+
app.useErrorHandler();
|
|
102
|
+
yield agent.get('/xyz').expect(error.status);
|
|
103
|
+
}));
|
|
104
|
+
it('handles http error with status and body by error handler', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
105
|
+
const error = new BadRequestError({
|
|
106
|
+
body: { message: 'Test error occurred' },
|
|
107
|
+
}), route = svc.route('GET /baz', () => {
|
|
108
|
+
throw error;
|
|
109
|
+
});
|
|
110
|
+
app.useRoute(route);
|
|
111
|
+
app.useErrorHandler();
|
|
112
|
+
yield agent
|
|
113
|
+
.get('/baz')
|
|
114
|
+
.expect(error.status)
|
|
115
|
+
.expect(res => {
|
|
116
|
+
expect(res.body).toEqual(error.body);
|
|
117
|
+
});
|
|
118
|
+
}));
|
|
119
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import express from 'express';
|
|
2
|
+
import { AppX } from '../app/AppX.js';
|
|
3
|
+
import { makeApp } from './makeApp.js';
|
|
4
|
+
import { mixinProxy } from './mixinProxy.js';
|
|
5
|
+
jest.mock('express', () => jest.fn());
|
|
6
|
+
jest.mock('./mixinProxy.js', () => ({ mixinProxy: jest.fn() }));
|
|
7
|
+
describe('makeApp()', () => {
|
|
8
|
+
beforeEach(() => {
|
|
9
|
+
jest.clearAllMocks();
|
|
10
|
+
});
|
|
11
|
+
it('creates an app by combining AppX with Express and returns the proxy', () => {
|
|
12
|
+
const expressApp = {}, app = {};
|
|
13
|
+
jest.mocked(express).mockReturnValue(expressApp);
|
|
14
|
+
jest.mocked(mixinProxy).mockReturnValue(app);
|
|
15
|
+
expect(makeApp()).toBe(app);
|
|
16
|
+
expect(mixinProxy).toHaveBeenCalledTimes(1);
|
|
17
|
+
expect(mixinProxy).toHaveBeenCalledWith(expect.any(AppX), expressApp);
|
|
18
|
+
});
|
|
19
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { HttpMethod, SuccessResponse } from '../../lib/http-meta/index.js';
|
|
2
|
+
import { Route } from '../routing/Route.js';
|
|
3
|
+
import { makeRoute } from './makeRoute.js';
|
|
4
|
+
jest.mock('../routing/Route.js', () => ({ Route: jest.fn() }));
|
|
5
|
+
describe('makeRoute()', () => {
|
|
6
|
+
beforeEach(() => {
|
|
7
|
+
jest.clearAllMocks();
|
|
8
|
+
});
|
|
9
|
+
it('calls the Route constructor with an endpoint object and action', () => {
|
|
10
|
+
const endpoint = { method: HttpMethod.GET, path: '/foo' }, action = () => new SuccessResponse();
|
|
11
|
+
makeRoute(endpoint, action);
|
|
12
|
+
expect(Route).toHaveBeenCalledTimes(1);
|
|
13
|
+
expect(Route).toHaveBeenCalledWith(endpoint, action);
|
|
14
|
+
});
|
|
15
|
+
it('calls the Route constructor with a string endpoint and action', () => {
|
|
16
|
+
const action = () => new SuccessResponse(), endpoint = 'POST /foo';
|
|
17
|
+
makeRoute(endpoint, action);
|
|
18
|
+
expect(Route).toHaveBeenCalledTimes(1);
|
|
19
|
+
expect(Route).toHaveBeenCalledWith(endpoint, action);
|
|
20
|
+
});
|
|
21
|
+
});
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import express from 'express';
|
|
2
|
+
import { RouterX } from '../routing/RouterX.js';
|
|
3
|
+
import { mixinProxy } from './mixinProxy.js';
|
|
4
|
+
export const makeRouter = (path) => {
|
|
5
|
+
const irouter = express.Router();
|
|
6
|
+
return mixinProxy(new RouterX(path, irouter), irouter);
|
|
7
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import express from 'express';
|
|
2
|
+
import { TBench } from '../../@spec/index.js';
|
|
3
|
+
import * as mixinProxyModule from './mixinProxy.js';
|
|
4
|
+
import { RouterX } from '../routing/RouterX.js';
|
|
5
|
+
import { makeRouter } from './makeRouter.js';
|
|
6
|
+
describe('makeRouter()', () => {
|
|
7
|
+
let bench;
|
|
8
|
+
beforeEach(() => {
|
|
9
|
+
bench = new TBench();
|
|
10
|
+
});
|
|
11
|
+
afterEach(() => {
|
|
12
|
+
jest.restoreAllMocks();
|
|
13
|
+
bench.reset();
|
|
14
|
+
});
|
|
15
|
+
it('creates a router by combining RouterX with Express and returns the proxy', () => {
|
|
16
|
+
const path = '/foo', irouter = bench.mock.express.getRoutable(), router = {};
|
|
17
|
+
const expressRouterMock = jest.spyOn(express, 'Router').mockReturnValue(irouter), mixinProxyMock = jest.spyOn(mixinProxyModule, 'mixinProxy').mockReturnValue(router);
|
|
18
|
+
expect(makeRouter(path)).toBe(router);
|
|
19
|
+
expect(expressRouterMock).toHaveBeenCalledTimes(1);
|
|
20
|
+
expect(mixinProxyMock).toHaveBeenCalledTimes(1);
|
|
21
|
+
expect(mixinProxyMock).toHaveBeenCalledWith(expect.any(RouterX), irouter);
|
|
22
|
+
expect(mixinProxyMock).toHaveBeenCalledWith(expect.objectContaining({ path }), irouter);
|
|
23
|
+
});
|
|
24
|
+
});
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
export const mixinProxy = (routable, irouter) => {
|
|
2
|
+
const routableRecord = routable, irouterRecord = irouter;
|
|
3
|
+
return new Proxy(irouterRecord, {
|
|
4
|
+
get(_, prop) {
|
|
5
|
+
if (prop in routableRecord) {
|
|
6
|
+
const value = routableRecord[prop];
|
|
7
|
+
const routableDescriptor = getPropertyDescriptor(routableRecord, prop);
|
|
8
|
+
// Bind only Routable methods to Routable
|
|
9
|
+
return typeof value === 'function' && routableDescriptor && 'value' in routableDescriptor
|
|
10
|
+
? value.bind(routableRecord)
|
|
11
|
+
: value;
|
|
12
|
+
}
|
|
13
|
+
const value = irouterRecord[prop];
|
|
14
|
+
return typeof value === 'function' ? value.bind(irouterRecord) : value;
|
|
15
|
+
},
|
|
16
|
+
set(_, prop, value) {
|
|
17
|
+
if (prop in routableRecord) {
|
|
18
|
+
routableRecord[prop] = value;
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
irouterRecord[prop] = value;
|
|
22
|
+
}
|
|
23
|
+
return true;
|
|
24
|
+
},
|
|
25
|
+
has(_, prop) {
|
|
26
|
+
return prop in routableRecord || prop in irouterRecord;
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
};
|
|
30
|
+
/*** Private ***/
|
|
31
|
+
function getPropertyDescriptor(target, prop) {
|
|
32
|
+
let current = target;
|
|
33
|
+
while (current) {
|
|
34
|
+
const descriptor = Object.getOwnPropertyDescriptor(current, prop);
|
|
35
|
+
if (descriptor) {
|
|
36
|
+
return descriptor;
|
|
37
|
+
}
|
|
38
|
+
current = Object.getPrototypeOf(current);
|
|
39
|
+
}
|
|
40
|
+
return undefined;
|
|
41
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import express from 'express';
|
|
2
|
+
import { TBench } from '../../@spec/index.js';
|
|
3
|
+
import { mixinProxy } from './mixinProxy.js';
|
|
4
|
+
import { RouterX } from '../routing/RouterX.js';
|
|
5
|
+
describe('mixinProxy()', () => {
|
|
6
|
+
let bench;
|
|
7
|
+
beforeEach(() => {
|
|
8
|
+
bench = new TBench();
|
|
9
|
+
});
|
|
10
|
+
afterEach(() => {
|
|
11
|
+
bench.reset();
|
|
12
|
+
});
|
|
13
|
+
it('prioritizes routable properties and binds methods to their owning objects', () => {
|
|
14
|
+
const irouterMock = bench.mock.express.getRoutable(), routable = new RouterX('/foo', irouterMock);
|
|
15
|
+
irouterMock.use.mockReturnValue('bar');
|
|
16
|
+
const mixin = mixinProxy(routable, irouterMock);
|
|
17
|
+
expect(mixin.path).toBe(routable.path);
|
|
18
|
+
expect(mixin.use()).toBe('bar');
|
|
19
|
+
});
|
|
20
|
+
it('does not bind callable values returned by Routable getters', () => {
|
|
21
|
+
const irouter = express.Router(), routable = new RouterX('/foo', irouter), mixin = mixinProxy(routable, irouter);
|
|
22
|
+
expect(mixin.irouter).toBe(irouter);
|
|
23
|
+
});
|
|
24
|
+
// TODO
|
|
25
|
+
// it('routes property assignments to the object that owns the property', () => {
|
|
26
|
+
// const
|
|
27
|
+
// target = { targetOnly: 'target' },
|
|
28
|
+
// mixin = { mixinOnly: 'mixin' },
|
|
29
|
+
// proxy = mixinProxy(target, mixin);
|
|
30
|
+
//
|
|
31
|
+
// proxy.targetOnly = 'updated target';
|
|
32
|
+
// proxy.mixinOnly = 'updated mixin';
|
|
33
|
+
//
|
|
34
|
+
// expect(target.targetOnly).toBe('updated target');
|
|
35
|
+
// expect(mixin.mixinOnly).toBe('updated mixin');
|
|
36
|
+
// });
|
|
37
|
+
it('checks properties on both objects', () => {
|
|
38
|
+
const irouterMock = bench.mock.express.getRoutable(), routable = new RouterX('/foo', irouterMock);
|
|
39
|
+
const mixin = mixinProxy(routable, irouterMock);
|
|
40
|
+
expect('path' in mixin).toBeTruthy();
|
|
41
|
+
expect('use' in mixin).toBeTruthy();
|
|
42
|
+
});
|
|
43
|
+
});
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type express from 'express';
|
|
2
|
+
import type { Route } from './Route.js';
|
|
3
|
+
import type { RouteAction, RouteEndpointParam } from '../types.js';
|
|
4
|
+
import type { Router } from './Router.js';
|
|
5
|
+
export type UseRouteParams = [Route] | [RouteEndpointParam, RouteAction];
|
|
6
|
+
export declare abstract class Routable<R extends express.IRouter> {
|
|
7
|
+
private readonly irouterInstance;
|
|
8
|
+
useRoute(...args: UseRouteParams): void;
|
|
9
|
+
useRouter(router: Router): void;
|
|
10
|
+
get irouter(): R;
|
|
11
|
+
protected constructor(irouterInstance: R);
|
|
12
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { makeRoute } from '../factories/makeRoute.js';
|
|
2
|
+
export class Routable {
|
|
3
|
+
useRoute(...args) {
|
|
4
|
+
const route = args.length === 1 ? args[0] : makeRoute(args[0], args[1]);
|
|
5
|
+
this.irouter[route.method](route.path, route.handler);
|
|
6
|
+
}
|
|
7
|
+
useRouter(router) {
|
|
8
|
+
this.irouter.use(router.path, router.irouter);
|
|
9
|
+
}
|
|
10
|
+
get irouter() {
|
|
11
|
+
return this.irouterInstance;
|
|
12
|
+
}
|
|
13
|
+
constructor(irouterInstance) {
|
|
14
|
+
this.irouterInstance = irouterInstance;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { TBench } from '../../@spec/index.js';
|
|
2
|
+
import { HttpMethod, SuccessResponse } from '../../lib/http-meta/index.js';
|
|
3
|
+
import * as makeRouteModule from '../factories/makeRoute.js';
|
|
4
|
+
import { Route } from './Route.js';
|
|
5
|
+
import { Routable } from './Routable.js';
|
|
6
|
+
import { RouterX } from './RouterX.js';
|
|
7
|
+
describe('Routable', () => {
|
|
8
|
+
let bench;
|
|
9
|
+
beforeEach(() => {
|
|
10
|
+
bench = new TBench();
|
|
11
|
+
});
|
|
12
|
+
afterEach(() => {
|
|
13
|
+
jest.restoreAllMocks();
|
|
14
|
+
bench.reset();
|
|
15
|
+
});
|
|
16
|
+
it.each(Object.values(HttpMethod))('useRoute() registers a %s route with its Express method', method => {
|
|
17
|
+
const register = jest.fn(), router = { [method.toLowerCase()]: register }, route = new Route({ method, path: '/foo' }, () => new SuccessResponse()), routable = new TestRoutable(router);
|
|
18
|
+
routable.useRoute(route);
|
|
19
|
+
expect(register).toHaveBeenCalledTimes(1);
|
|
20
|
+
expect(register).toHaveBeenCalledWith(route.path, route.handler);
|
|
21
|
+
});
|
|
22
|
+
it('creates and registers a route from inline parameters', () => {
|
|
23
|
+
const register = jest.fn(), router = { get: register }, endpoint = { method: HttpMethod.GET, path: '/foo' }, action = () => new SuccessResponse(), route = new Route(endpoint, action), routable = new TestRoutable(router), makeRouteMock = jest.spyOn(makeRouteModule, 'makeRoute').mockReturnValue(route);
|
|
24
|
+
routable.useRoute(endpoint, action);
|
|
25
|
+
expect(makeRouteMock).toHaveBeenCalledTimes(1);
|
|
26
|
+
expect(makeRouteMock).toHaveBeenCalledWith(endpoint, action);
|
|
27
|
+
expect(register).toHaveBeenCalledWith(route.path, route.handler);
|
|
28
|
+
});
|
|
29
|
+
it('useRouter() mounts a nested router at its path', () => {
|
|
30
|
+
const irouterMockParent = bench.mock.express.getRoutable('parent'), irouterMockChild = bench.mock.express.getRoutable('child'), routable = new TestRoutable(irouterMockParent), router = new RouterX('/foo', irouterMockChild);
|
|
31
|
+
routable.useRouter(router);
|
|
32
|
+
expect(irouterMockParent.use).toHaveBeenCalledWith(router.path, irouterMockChild);
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
class TestRoutable extends Routable {
|
|
36
|
+
constructor(router) {
|
|
37
|
+
super(router);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import express from 'express';
|
|
2
|
+
import { ExpressMethod } from '../../lib/express/types.js';
|
|
3
|
+
import { RouteAction, RouteEndpoint, RouteEndpointParam } from '../types.js';
|
|
4
|
+
export declare class Route {
|
|
5
|
+
constructor(endpoint: RouteEndpointParam, action: RouteAction);
|
|
6
|
+
readonly endpoint: RouteEndpoint;
|
|
7
|
+
readonly action: RouteAction;
|
|
8
|
+
get path(): string;
|
|
9
|
+
get method(): ExpressMethod;
|
|
10
|
+
get handler(): express.RequestHandler;
|
|
11
|
+
private requestHandler;
|
|
12
|
+
private parseEndpoint;
|
|
13
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { HttpMethod } from '../../lib/http-meta/index.js';
|
|
11
|
+
const httpMethodsList = Object.values(HttpMethod).join('|'), stringEndpointPattern = new RegExp(`^(${httpMethodsList})\\s(/.*)$`);
|
|
12
|
+
export class Route {
|
|
13
|
+
constructor(endpoint, action) {
|
|
14
|
+
this.requestHandler = null;
|
|
15
|
+
this.endpoint = typeof endpoint === 'string' ? this.parseEndpoint(endpoint) : endpoint;
|
|
16
|
+
this.action = action;
|
|
17
|
+
}
|
|
18
|
+
get path() {
|
|
19
|
+
return this.endpoint.path;
|
|
20
|
+
}
|
|
21
|
+
get method() {
|
|
22
|
+
return this.endpoint.method.toLowerCase();
|
|
23
|
+
}
|
|
24
|
+
get handler() {
|
|
25
|
+
if (this.requestHandler) {
|
|
26
|
+
return this.requestHandler;
|
|
27
|
+
}
|
|
28
|
+
this.requestHandler = (req, res) => __awaiter(this, void 0, void 0, function* () {
|
|
29
|
+
const response = yield this.action(req);
|
|
30
|
+
if (response.headers) {
|
|
31
|
+
res.set(response.headers);
|
|
32
|
+
}
|
|
33
|
+
res.status(response.status);
|
|
34
|
+
if (response.body === undefined) {
|
|
35
|
+
res.send();
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
res.send(response.body);
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
return this.requestHandler;
|
|
42
|
+
}
|
|
43
|
+
parseEndpoint(endpoint) {
|
|
44
|
+
const match = endpoint.match(stringEndpointPattern);
|
|
45
|
+
if (!match) {
|
|
46
|
+
throw new Error(`Invalid route endpoint: ${endpoint}`);
|
|
47
|
+
}
|
|
48
|
+
const [, method, path] = match;
|
|
49
|
+
return { method: method, path };
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { ExpressMethod } from '../../lib/express/types.js';
|
|
11
|
+
import { HttpMethod, HttpStatus, SuccessResponse } from '../../lib/http-meta/index.js';
|
|
12
|
+
import { TBench } from '../../@spec/index.js';
|
|
13
|
+
import { Route } from './Route.js';
|
|
14
|
+
describe('Route', () => {
|
|
15
|
+
let bench;
|
|
16
|
+
beforeEach(() => {
|
|
17
|
+
bench = new TBench();
|
|
18
|
+
});
|
|
19
|
+
afterEach(() => {
|
|
20
|
+
bench.reset();
|
|
21
|
+
});
|
|
22
|
+
it('constructs from an endpoint object and exposes endpoint properties', () => {
|
|
23
|
+
const endpoint = { method: HttpMethod.POST, path: '/foo' }, action = () => new SuccessResponse(), route = new Route(endpoint, action);
|
|
24
|
+
expect(route.endpoint).toBe(endpoint);
|
|
25
|
+
expect(route.action).toBe(action);
|
|
26
|
+
expect(route.path).toBe(endpoint.path);
|
|
27
|
+
expect(route.method).toEqual('post');
|
|
28
|
+
});
|
|
29
|
+
it('parses a string endpoint into method and path', () => {
|
|
30
|
+
const route = new Route('PUT /bar/:id', () => new SuccessResponse());
|
|
31
|
+
expect(route.endpoint).toEqual({ method: HttpMethod.PUT, path: '/bar/:id' });
|
|
32
|
+
expect(route.path).toBe('/bar/:id');
|
|
33
|
+
expect(route.method).toBe(ExpressMethod.PUT);
|
|
34
|
+
});
|
|
35
|
+
it('throws for invalid endpoint strings', () => {
|
|
36
|
+
['get /foo', 'POST bar', 'baz', ''].forEach(endpoint => {
|
|
37
|
+
expect(() => new Route(endpoint, () => new SuccessResponse()))
|
|
38
|
+
.toThrow(new Error(`Invalid route endpoint: ${endpoint}`));
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
it('invokes the action with the request', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
42
|
+
const action = jest.fn(() => Promise.resolve(new SuccessResponse())), route = new Route('GET /foo', action), { req } = bench.mock.express;
|
|
43
|
+
yield runHandler(route);
|
|
44
|
+
expect(action).toHaveBeenCalledWith(req);
|
|
45
|
+
}));
|
|
46
|
+
it('handler() wraps to send the response status', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
47
|
+
const response = new SuccessResponse({ status: HttpStatus.ACCEPTED }), route = new Route('GET /foo', () => response), res = yield runHandler(route);
|
|
48
|
+
expect(res.status).toHaveBeenCalledWith(response.status);
|
|
49
|
+
expect(res.send).toHaveBeenCalled();
|
|
50
|
+
expect(res.send).toHaveBeenCalledAfter(res.status);
|
|
51
|
+
}));
|
|
52
|
+
it('handler() wraps to send the response body', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
53
|
+
const body = { foo: 'bar' }, response = new SuccessResponse({ status: HttpStatus.OK, body }), route = new Route('GET /foo', () => Promise.resolve(response)), res = yield runHandler(route);
|
|
54
|
+
expect(res.status).toHaveBeenCalledWith(response.status);
|
|
55
|
+
expect(res.status).toHaveBeenCalledBefore(res.send);
|
|
56
|
+
expect(res.send).toHaveBeenCalledWith(body);
|
|
57
|
+
}));
|
|
58
|
+
it('handler() wraps to send the response headers', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
59
|
+
const headers = { 'x-header': 'abc' }, response = new SuccessResponse({ status: HttpStatus.OK, headers }), route = new Route('GET /foo', () => Promise.resolve(response)), res = yield runHandler(route);
|
|
60
|
+
expect(res.status).toHaveBeenCalledWith(response.status);
|
|
61
|
+
expect(res.set).toHaveBeenCalledWith(headers);
|
|
62
|
+
expect(res.set).toHaveBeenCalledBefore(res.send);
|
|
63
|
+
expect(res.send).toHaveBeenCalled();
|
|
64
|
+
}));
|
|
65
|
+
it('handler() wraps to send response status, body, and headers together', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
66
|
+
const headers = { 'x-header': 'abc' }, body = { ok: true }, response = new SuccessResponse({ status: HttpStatus.OK, headers, body }), route = new Route('GET /foo', () => Promise.resolve(response)), res = yield runHandler(route);
|
|
67
|
+
expect(res.status).toHaveBeenCalledWith(response.status);
|
|
68
|
+
expect(res.set).toHaveBeenCalledWith(headers);
|
|
69
|
+
expect(res.send).toHaveBeenCalledWith(body);
|
|
70
|
+
}));
|
|
71
|
+
it('handler() wraps to send without a body when the response has none', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
72
|
+
const response = new SuccessResponse(), route = new Route('GET /foo', () => response), res = yield runHandler(route);
|
|
73
|
+
expect(res.send).toHaveBeenCalledWith();
|
|
74
|
+
}));
|
|
75
|
+
it('caches and reuses the Express request handler', () => {
|
|
76
|
+
const route = new Route('GET /foo', () => new SuccessResponse());
|
|
77
|
+
expect(route.handler).toBe(route.handler);
|
|
78
|
+
});
|
|
79
|
+
function runHandler(route) {
|
|
80
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
81
|
+
const { req, res, next } = bench.mock.express;
|
|
82
|
+
yield route.handler(req, res, next);
|
|
83
|
+
return res;
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { RouterX } from './RouterX.js';
|
|
2
|
+
import { TBench } from '../../@spec/index.js';
|
|
3
|
+
describe('RouterX', () => {
|
|
4
|
+
let bench;
|
|
5
|
+
beforeEach(() => {
|
|
6
|
+
bench = new TBench();
|
|
7
|
+
});
|
|
8
|
+
afterEach(() => {
|
|
9
|
+
bench.reset();
|
|
10
|
+
});
|
|
11
|
+
it('exposes its path', () => {
|
|
12
|
+
const router = new RouterX('/foo', bench.mock.express.getRoutable());
|
|
13
|
+
expect(router.path).toBe('/foo');
|
|
14
|
+
});
|
|
15
|
+
});
|
|
@@ -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)
|