@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
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
|
+
## 0.5.0 (2026-12-05)
|
|
2
|
+
|
|
3
|
+
* refaactor: Refactor complete approach to use namespace solution ([4006cd3](https://github.com/sepo27/servicio/commit/4006cd3))
|
|
4
|
+
* feat: Setup Router logic ([1e90ae4](https://github.com/sepo27/servicio/commit/1e90ae4))
|
|
5
|
+
* feat: Add Router support ([7d15d8b](https://github.com/sepo27/servicio/commit/7d15d8b))
|
|
6
|
+
* chore(agents): Add TBench agent skills ([5fc23b4](https://github.com/sepo27/servicio/commit/5fc23b4))
|
|
7
|
+
* chore: Setup basic agent skills ([23f50bb](https://github.com/sepo27/servicio/commit/23f50bb))
|
|
8
|
+
* chore: Add eslint rule to omit interface delimiters ([1d3b2f9](https://github.com/sepo27/servicio/commit/1d3b2f9))
|
|
9
|
+
* test: Reorg spec tbench ([fc0b007](https://github.com/sepo27/servicio/commit/fc0b007))
|
|
10
|
+
|
|
11
|
+
## 0.4.0 (2026-11-26)
|
|
12
|
+
|
|
13
|
+
* feat: Refactor addRoute() to useRoute() ([3258451](https://github.com/sepo27/servicio/commit/3258451))
|
|
14
|
+
|
|
1
15
|
## <small>0.3.2 (2026-11-25)</small>
|
|
2
16
|
|
|
3
17
|
* docs: Update README ([189f2d0](https://github.com/sepo27/servicio/commit/189f2d0))
|
package/README.md
CHANGED
|
@@ -2,15 +2,15 @@
|
|
|
2
2
|
|
|
3
3
|
**! ACTIVE DEVELOPMENT !**
|
|
4
4
|
|
|
5
|
-
#
|
|
5
|
+
# What
|
|
6
6
|
|
|
7
7
|
Backend Service Frame.
|
|
8
|
-
*(
|
|
8
|
+
*(Extension of express JS)*
|
|
9
9
|
|
|
10
10
|
**Features:**
|
|
11
11
|
- lightweight *(as express JS itself)*
|
|
12
12
|
- reusable routes abstraction
|
|
13
|
-
-
|
|
13
|
+
- simplified router actions (with returning responses)
|
|
14
14
|
|
|
15
15
|
# Quick Start
|
|
16
16
|
|
|
@@ -23,11 +23,11 @@ pnpm install @sepo27/servicio
|
|
|
23
23
|
|
|
24
24
|
Use
|
|
25
25
|
```typescript
|
|
26
|
-
import {
|
|
26
|
+
import { svc, HttpStatus, SuccessResponse } from '@sepo27/servicio';
|
|
27
27
|
|
|
28
|
-
const app =
|
|
28
|
+
const app: svc.App = svc.app();
|
|
29
29
|
|
|
30
|
-
|
|
30
|
+
app.useRoute('GET /foo', () => {
|
|
31
31
|
return new SuccessResponse({
|
|
32
32
|
status: HttpStatus.OK,
|
|
33
33
|
body: {
|
|
@@ -36,8 +36,6 @@ const fooRoute = new Route('GET /foo', () => {
|
|
|
36
36
|
});
|
|
37
37
|
});
|
|
38
38
|
|
|
39
|
-
app.addRoute(fooRoute);
|
|
40
|
-
|
|
41
39
|
app.listen(3000, () => {
|
|
42
40
|
console.log('=== Servicio listening on port 3000');
|
|
43
41
|
});
|
|
@@ -45,9 +43,72 @@ app.listen(3000, () => {
|
|
|
45
43
|
|
|
46
44
|
# Usage
|
|
47
45
|
|
|
46
|
+
## Common cases
|
|
47
|
+
|
|
48
|
+
Reusable routes
|
|
49
|
+
|
|
50
|
+
```typescript
|
|
51
|
+
import { svc, SuccessResponse } from '@sepo27/servicio';
|
|
52
|
+
|
|
53
|
+
const app: svc.App = svc.app()
|
|
54
|
+
|
|
55
|
+
const fooRoute: svc.Route = svc.route('GET /foo', () => new SuccessResponse())
|
|
56
|
+
|
|
57
|
+
app.useRoute(fooRoute)
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
Router
|
|
61
|
+
|
|
62
|
+
```typescript
|
|
63
|
+
import { svc, SuccessResponse, HttpStatus } from '@sepo27/servicio';
|
|
64
|
+
|
|
65
|
+
const app: svc.App = svc.app()
|
|
66
|
+
|
|
67
|
+
const router: svc.Router = svc.router('/foo')
|
|
68
|
+
router.useRoute('GET /', () => new SuccessResponse({
|
|
69
|
+
status: HttpStatus.OK,
|
|
70
|
+
body: {
|
|
71
|
+
items: ['foo'],
|
|
72
|
+
},
|
|
73
|
+
}))
|
|
74
|
+
|
|
75
|
+
app.useRouter(router)
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
Nested Router
|
|
79
|
+
|
|
80
|
+
```typescript
|
|
81
|
+
import { svc, SuccessResponse, HttpStatus } from '@sepo27/servicio';
|
|
82
|
+
|
|
83
|
+
const app: svc.App = svc.app()
|
|
84
|
+
|
|
85
|
+
const router: svc.Router = svc.router('/foo')
|
|
86
|
+
router.useRoute('GET /', () => new SuccessResponse({
|
|
87
|
+
status: HttpStatus.OK,
|
|
88
|
+
body: {
|
|
89
|
+
items: ['foo'],
|
|
90
|
+
},
|
|
91
|
+
}))
|
|
92
|
+
|
|
93
|
+
const fooItemsRouter: svc.Router = svc.router('/:id/items')
|
|
94
|
+
fooItemsRouter.useRoute('GET /', () => new SuccessResponse({
|
|
95
|
+
status: HttpStatus.OK,
|
|
96
|
+
body: {
|
|
97
|
+
items: ['bar']
|
|
98
|
+
},
|
|
99
|
+
}))
|
|
100
|
+
|
|
101
|
+
router.useRouter(fooItemsRouter)
|
|
102
|
+
|
|
103
|
+
app.useRouter(router)
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
**:warning: A bug is present where path params are missing in nested routers' actions / handlers**
|
|
107
|
+
**:warning: Presently "mixin" objects' methods (`App` / `Router`) do not support chaining**
|
|
108
|
+
|
|
48
109
|
## Express JS wrapper
|
|
49
110
|
|
|
50
|
-
`
|
|
111
|
+
`svc.app()` factory returns an object instance "mixin" of `AppX` and `Express` classes.
|
|
51
112
|
So all express functionality is preserved as is.
|
|
52
113
|
|
|
53
114
|
## Http Meta
|
|
@@ -63,7 +124,7 @@ If instance of `HttpError` is thrown with optional `headers` it will be caught &
|
|
|
63
124
|
However, body isn't handled by default.
|
|
64
125
|
Use servicio default error handler instead to fully support `HttpError`s:
|
|
65
126
|
```typescript
|
|
66
|
-
const app =
|
|
127
|
+
const app = svc.app();
|
|
67
128
|
|
|
68
129
|
// Add routes here ...
|
|
69
130
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export
|
|
1
|
+
export * from './tbench/index.js';
|
package/dist/src/@spec/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export
|
|
1
|
+
export * from './tbench/index.js';
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { ExpressMethod } from '../../lib/express/types.js';
|
|
2
|
+
export class TMock {
|
|
3
|
+
constructor() {
|
|
4
|
+
/*** Public ***/
|
|
5
|
+
/*** Private ***/
|
|
6
|
+
this.mockCache = {};
|
|
7
|
+
}
|
|
8
|
+
get express() {
|
|
9
|
+
const tmock = this;
|
|
10
|
+
return {
|
|
11
|
+
get req() {
|
|
12
|
+
return {};
|
|
13
|
+
},
|
|
14
|
+
get res() {
|
|
15
|
+
return {
|
|
16
|
+
status: jest.fn().mockReturnThis(),
|
|
17
|
+
set: jest.fn().mockReturnThis(),
|
|
18
|
+
send: jest.fn().mockReturnThis(),
|
|
19
|
+
};
|
|
20
|
+
},
|
|
21
|
+
get next() {
|
|
22
|
+
return tmock.getMock('express.next', () => jest.fn());
|
|
23
|
+
},
|
|
24
|
+
getRoutable(key = 'express.routable') {
|
|
25
|
+
const mock = Object.values(ExpressMethod).reduce((acc, method) => Object.assign(acc, {
|
|
26
|
+
[method]: jest.fn(),
|
|
27
|
+
}), {});
|
|
28
|
+
return tmock.makeStructMock(key, mock);
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
reset() {
|
|
33
|
+
this.mockCache = {};
|
|
34
|
+
jest.resetAllMocks();
|
|
35
|
+
}
|
|
36
|
+
makeStructMock(key, struct) {
|
|
37
|
+
const self = this;
|
|
38
|
+
return Object.keys(struct).reduce((acc, prop) => Object.defineProperty(acc, prop, {
|
|
39
|
+
get() {
|
|
40
|
+
const propKey = [key, prop].join('.');
|
|
41
|
+
return self.getMock(propKey, () => struct[prop]);
|
|
42
|
+
},
|
|
43
|
+
}), {});
|
|
44
|
+
}
|
|
45
|
+
getMock(key, factory) {
|
|
46
|
+
if (key in this.mockCache) {
|
|
47
|
+
return this.mockCache[key];
|
|
48
|
+
}
|
|
49
|
+
const mock = factory();
|
|
50
|
+
this.mockCache[key] = mock;
|
|
51
|
+
return mock;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import express from 'express';
|
|
2
|
+
import { ExpressMethod } from '../../lib/express/types.js';
|
|
3
|
+
export type ExpressRequestMock = express.Request & {};
|
|
4
|
+
export type ExpressResponseMock = express.Response & {
|
|
5
|
+
status: jest.Mock;
|
|
6
|
+
set: jest.Mock;
|
|
7
|
+
send: jest.Mock;
|
|
8
|
+
};
|
|
9
|
+
export type ExpressRoutableMock = express.IRouter & Record<ExpressMethod, jest.Mock>;
|
|
10
|
+
export interface ExpressMock {
|
|
11
|
+
get req(): ExpressRequestMock;
|
|
12
|
+
get res(): ExpressResponseMock;
|
|
13
|
+
get next(): express.NextFunction;
|
|
14
|
+
getRoutable(key?: string): ExpressRoutableMock;
|
|
15
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/src/index.d.ts
CHANGED
|
@@ -1,3 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import type { App as AppType } from './main/app/App.js';
|
|
2
|
+
import { Route as RouteClass } from './main/routing/Route.js';
|
|
3
|
+
import type { Router as RouterType } from './main/routing/Router.js';
|
|
4
|
+
export declare namespace svc {
|
|
5
|
+
const app: () => AppType;
|
|
6
|
+
const route: (endpoint: import("./main/types.js").RouteEndpointParam, action: import("./main/types.js").RouteAction) => RouteClass;
|
|
7
|
+
const Route: typeof RouteClass;
|
|
8
|
+
const router: (path: string) => RouterType;
|
|
9
|
+
type App = AppType;
|
|
10
|
+
type Route = RouteClass;
|
|
11
|
+
type Router = RouterType;
|
|
12
|
+
}
|
|
3
13
|
export * from './lib/http-meta/index.js';
|
package/dist/src/index.js
CHANGED
|
@@ -1,3 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { makeApp } from './main/factories/makeApp.js';
|
|
2
|
+
import { makeRoute } from './main/factories/makeRoute.js';
|
|
3
|
+
import { makeRouter } from './main/factories/makeRouter.js';
|
|
4
|
+
import { Route as RouteClass } from './main/routing/Route.js';
|
|
5
|
+
export var svc;
|
|
6
|
+
(function (svc) {
|
|
7
|
+
// Logic
|
|
8
|
+
svc.app = makeApp;
|
|
9
|
+
svc.route = makeRoute;
|
|
10
|
+
svc.Route = RouteClass;
|
|
11
|
+
svc.router = makeRouter;
|
|
12
|
+
})(svc || (svc = {}));
|
|
3
13
|
export * from './lib/http-meta/index.js';
|
|
@@ -1,38 +1,39 @@
|
|
|
1
|
-
export var
|
|
2
|
-
(function (
|
|
1
|
+
export var ExpressMethod;
|
|
2
|
+
(function (ExpressMethod) {
|
|
3
3
|
// ACL = 'acl',
|
|
4
4
|
// BIND = 'bind',
|
|
5
5
|
// CHECKOUT = 'checkout',
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
6
|
+
ExpressMethod["USE"] = "use";
|
|
7
|
+
ExpressMethod["CONNECT"] = "connect";
|
|
8
|
+
ExpressMethod["COPY"] = "copy";
|
|
9
|
+
ExpressMethod["DELETE"] = "delete";
|
|
10
|
+
ExpressMethod["GET"] = "get";
|
|
11
|
+
ExpressMethod["HEAD"] = "head";
|
|
12
|
+
ExpressMethod["LINK"] = "link";
|
|
13
|
+
ExpressMethod["LOCK"] = "lock";
|
|
13
14
|
// M_SEARCH = 'm-search',
|
|
14
15
|
// MERGE = 'merge',
|
|
15
16
|
// MKACTIVITY = 'mkactivity',
|
|
16
17
|
// MKCALENDAR = 'mkcalendar',
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
18
|
+
ExpressMethod["MKCOL"] = "mkcol";
|
|
19
|
+
ExpressMethod["MOVE"] = "move";
|
|
20
|
+
ExpressMethod["NOTIFY"] = "notify";
|
|
21
|
+
ExpressMethod["OPTIONS"] = "options";
|
|
22
|
+
ExpressMethod["PATCH"] = "patch";
|
|
23
|
+
ExpressMethod["POST"] = "post";
|
|
24
|
+
ExpressMethod["PROPFIND"] = "propfind";
|
|
25
|
+
ExpressMethod["PROPPATCH"] = "proppatch";
|
|
26
|
+
ExpressMethod["PURGE"] = "purge";
|
|
27
|
+
ExpressMethod["PUT"] = "put";
|
|
28
|
+
ExpressMethod["QUERY"] = "query";
|
|
28
29
|
// REBIND = 'rebind',
|
|
29
|
-
|
|
30
|
-
|
|
30
|
+
ExpressMethod["REPORT"] = "report";
|
|
31
|
+
ExpressMethod["SEARCH"] = "search";
|
|
31
32
|
// SOURCE = 'source',
|
|
32
|
-
|
|
33
|
-
|
|
33
|
+
ExpressMethod["SUBSCRIBE"] = "subscribe";
|
|
34
|
+
ExpressMethod["TRACE"] = "trace";
|
|
34
35
|
// UNBIND = 'unbind',
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
})(
|
|
36
|
+
ExpressMethod["UNLINK"] = "unlink";
|
|
37
|
+
ExpressMethod["UNLOCK"] = "unlock";
|
|
38
|
+
ExpressMethod["UNSUBSCRIBE"] = "unsubscribe";
|
|
39
|
+
})(ExpressMethod || (ExpressMethod = {}));
|
|
@@ -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
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import express from 'express';
|
|
2
|
+
import { Routable } from '../routing/Routable.js';
|
|
3
|
+
export declare class AppX extends Routable<express.Express> {
|
|
4
|
+
constructor(expressApp: express.Express);
|
|
5
|
+
/**
|
|
6
|
+
* Use default Servicio error handler.
|
|
7
|
+
*/
|
|
8
|
+
useErrorHandler(): this;
|
|
9
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Routable } from '../routing/Routable.js';
|
|
2
|
+
import { errorHandler } from './errorHandler.js';
|
|
3
|
+
export class AppX extends Routable {
|
|
4
|
+
constructor(expressApp) {
|
|
5
|
+
super(expressApp);
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Use default Servicio error handler.
|
|
9
|
+
*/
|
|
10
|
+
useErrorHandler() {
|
|
11
|
+
this.irouter.use(errorHandler);
|
|
12
|
+
return this;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { TBench } from '../../@spec/index.js';
|
|
2
|
+
import { errorHandler } from './errorHandler.js';
|
|
3
|
+
import { AppX } from './AppX.js';
|
|
4
|
+
describe('AppX', () => {
|
|
5
|
+
let bench;
|
|
6
|
+
beforeEach(() => {
|
|
7
|
+
bench = new TBench();
|
|
8
|
+
});
|
|
9
|
+
afterEach(() => {
|
|
10
|
+
bench.reset();
|
|
11
|
+
});
|
|
12
|
+
it('useErrorHandler() registers the default error handler', () => {
|
|
13
|
+
const expressApp = bench.mock.express.getRoutable('app'), app = new AppX(expressApp);
|
|
14
|
+
expect(app.useErrorHandler()).toBe(app);
|
|
15
|
+
expect(expressApp.use).toHaveBeenCalledWith(errorHandler);
|
|
16
|
+
});
|
|
17
|
+
});
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import express from 'express';
|
|
2
|
+
/**
|
|
3
|
+
* Default error handler to send errors from HttpError instances.
|
|
4
|
+
*
|
|
5
|
+
* @param err
|
|
6
|
+
* @param req
|
|
7
|
+
* @param res
|
|
8
|
+
* @param next
|
|
9
|
+
*/
|
|
10
|
+
export declare const errorHandler: (err: Error, req: express.Request, res: express.Response, next: express.NextFunction) => void;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { HttpError } from '../../lib/http-meta/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* Default error handler to send errors from HttpError instances.
|
|
4
|
+
*
|
|
5
|
+
* @param err
|
|
6
|
+
* @param req
|
|
7
|
+
* @param res
|
|
8
|
+
* @param next
|
|
9
|
+
*/
|
|
10
|
+
export const errorHandler = (err, req, res, next) => {
|
|
11
|
+
if (err instanceof HttpError) {
|
|
12
|
+
if (err.headers) {
|
|
13
|
+
res.set(err.headers);
|
|
14
|
+
}
|
|
15
|
+
const sendArgs = err.body ? [err.body] : [];
|
|
16
|
+
res.status(err.status).send(...sendArgs);
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
next(err);
|
|
20
|
+
}
|
|
21
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { BadRequestError } from '../../lib/http-meta/index.js';
|
|
2
|
+
import { TBench } from '../../@spec/index.js';
|
|
3
|
+
import { errorHandler } from './errorHandler.js';
|
|
4
|
+
describe('errorHandler()', () => {
|
|
5
|
+
let bench;
|
|
6
|
+
beforeEach(() => {
|
|
7
|
+
bench = new TBench();
|
|
8
|
+
});
|
|
9
|
+
afterEach(() => {
|
|
10
|
+
bench.reset();
|
|
11
|
+
});
|
|
12
|
+
it('sends status of http error', () => {
|
|
13
|
+
const err = new BadRequestError(), resMock = bench.mock.express.res;
|
|
14
|
+
errorHandler(err, bench.mock.express.req, resMock, bench.mock.express.next);
|
|
15
|
+
expect(resMock.status).toHaveBeenCalledWith(err.status);
|
|
16
|
+
expect(resMock.send).toHaveBeenCalledWith();
|
|
17
|
+
expect(resMock.send).toHaveBeenCalledAfter(resMock.status);
|
|
18
|
+
expect(bench.mock.express.next).not.toHaveBeenCalled();
|
|
19
|
+
});
|
|
20
|
+
it('sends status and headers of http error', () => {
|
|
21
|
+
const errHeaders = { 'x-app': 'test' }, err = new BadRequestError({ headers: errHeaders }), resMock = bench.mock.express.res;
|
|
22
|
+
errorHandler(err, bench.mock.express.req, resMock, bench.mock.express.next);
|
|
23
|
+
expect(resMock.set).toHaveBeenCalledWith(err.headers);
|
|
24
|
+
expect(resMock.set).toHaveBeenCalledBefore(resMock.status);
|
|
25
|
+
expect(resMock.status).toHaveBeenCalledWith(err.status);
|
|
26
|
+
expect(resMock.send).toHaveBeenCalledWith();
|
|
27
|
+
expect(resMock.send).toHaveBeenCalledAfter(resMock.status);
|
|
28
|
+
expect(bench.mock.express.next).not.toHaveBeenCalled();
|
|
29
|
+
});
|
|
30
|
+
it('sends status and body of http error', () => {
|
|
31
|
+
const errBody = { error: 'Test error' }, err = new BadRequestError({ body: errBody }), resMock = bench.mock.express.res;
|
|
32
|
+
errorHandler(err, bench.mock.express.req, resMock, bench.mock.express.next);
|
|
33
|
+
expect(resMock.status).toHaveBeenCalledWith(err.status);
|
|
34
|
+
expect(resMock.send).toHaveBeenCalledWith(errBody);
|
|
35
|
+
expect(resMock.send).toHaveBeenCalledAfter(resMock.status);
|
|
36
|
+
expect(bench.mock.express.next).not.toHaveBeenCalled();
|
|
37
|
+
});
|
|
38
|
+
it('sends status, headers and body of http error', () => {
|
|
39
|
+
const errHeaders = { 'x-app': 'test' }, errBody = { error: 'Test error' }, err = new BadRequestError({ headers: errHeaders, body: errBody }), resMock = bench.mock.express.res;
|
|
40
|
+
errorHandler(err, bench.mock.express.req, resMock, bench.mock.express.next);
|
|
41
|
+
expect(resMock.set).toHaveBeenCalledWith(err.headers);
|
|
42
|
+
expect(resMock.set).toHaveBeenCalledBefore(resMock.status);
|
|
43
|
+
expect(resMock.status).toHaveBeenCalledWith(err.status);
|
|
44
|
+
expect(resMock.send).toHaveBeenCalledWith(errBody);
|
|
45
|
+
expect(resMock.send).toHaveBeenCalledAfter(resMock.status);
|
|
46
|
+
expect(bench.mock.express.next).not.toHaveBeenCalled();
|
|
47
|
+
});
|
|
48
|
+
it('passes through non-http error', () => {
|
|
49
|
+
const err = new Error('Unknown test error'), resMock = bench.mock.express.res;
|
|
50
|
+
errorHandler(err, bench.mock.express.req, resMock, bench.mock.express.next);
|
|
51
|
+
expect(resMock.set).not.toHaveBeenCalled();
|
|
52
|
+
expect(resMock.status).not.toHaveBeenCalled();
|
|
53
|
+
expect(resMock.send).not.toHaveBeenCalled();
|
|
54
|
+
expect(bench.mock.express.next).toHaveBeenCalledWith(err);
|
|
55
|
+
});
|
|
56
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|