@congruent-stack/congruent-api-express 0.7.0 → 0.10.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/dist/index.cjs +82 -4
- package/dist/index.mjs +83 -5
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -7,19 +7,96 @@ function createExpressRegistry(app, diContainer, apiContract) {
|
|
|
7
7
|
handlerRegisteredCallback: (entry) => {
|
|
8
8
|
const { genericPath } = entry.methodEndpoint;
|
|
9
9
|
const method = entry.methodEndpoint.method.toLowerCase();
|
|
10
|
-
app[method]
|
|
10
|
+
if (typeof app[method] !== "function") {
|
|
11
|
+
throw new Error(`Unsupported HTTP method: ${method}`);
|
|
12
|
+
}
|
|
13
|
+
const routeHandlers = [];
|
|
14
|
+
entry.decoratorFactories.forEach((decoratorFactory) => {
|
|
15
|
+
routeHandlers.push(async (req, res, next) => {
|
|
16
|
+
if (!res.locals.diScope) {
|
|
17
|
+
res.locals.diScope = entry.dicontainer.createScope();
|
|
18
|
+
}
|
|
19
|
+
req.pathParams = req.params;
|
|
20
|
+
const decorator = decoratorFactory(res.locals.diScope);
|
|
21
|
+
const haltResult = await congruentApi.triggerEndpointDecoratorNoStaticTypeCheck(
|
|
22
|
+
entry.methodEndpoint,
|
|
23
|
+
decorator,
|
|
24
|
+
req,
|
|
25
|
+
{
|
|
26
|
+
next,
|
|
27
|
+
originalRequest: req
|
|
28
|
+
}
|
|
29
|
+
);
|
|
30
|
+
if (haltResult && congruentApi.isHttpResponseObject(haltResult)) {
|
|
31
|
+
const haltResultHeaders = new Map(
|
|
32
|
+
Object.entries(haltResult.headers || {})
|
|
33
|
+
);
|
|
34
|
+
res.status(haltResult.code).setHeaders(haltResultHeaders).json(haltResult.body);
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
routeHandlers.push(async (req, res) => {
|
|
39
|
+
if (!res.locals.diScope) {
|
|
40
|
+
res.locals.diScope = entry.dicontainer.createScope();
|
|
41
|
+
}
|
|
11
42
|
req.pathParams = req.params;
|
|
12
|
-
const result = await entry.
|
|
43
|
+
const result = await entry.triggerNoStaticTypeCheck(
|
|
44
|
+
res.locals.diScope,
|
|
45
|
+
req,
|
|
46
|
+
{
|
|
47
|
+
originalRequest: req
|
|
48
|
+
}
|
|
49
|
+
);
|
|
13
50
|
const resultHeaders = new Map(
|
|
14
51
|
Object.entries(result.headers || {})
|
|
15
52
|
);
|
|
16
53
|
res.status(result.code).setHeaders(resultHeaders).json(result.body);
|
|
17
54
|
});
|
|
55
|
+
app[method](genericPath, ...routeHandlers);
|
|
18
56
|
},
|
|
19
57
|
middlewareHandlerRegisteredCallback: (entry) => {
|
|
20
|
-
|
|
58
|
+
const {
|
|
59
|
+
/*TODO: method,*/
|
|
60
|
+
genericPath
|
|
61
|
+
} = entry;
|
|
62
|
+
const middlewareHandlers = [];
|
|
63
|
+
entry.decoratorFactories.forEach((decoratorFactory) => {
|
|
64
|
+
middlewareHandlers.push(async (req, res, next) => {
|
|
65
|
+
if (!res.locals.diScope) {
|
|
66
|
+
res.locals.diScope = entry.dicontainer.createScope();
|
|
67
|
+
}
|
|
68
|
+
req.pathParams = req.params;
|
|
69
|
+
const decorator = decoratorFactory(res.locals.diScope);
|
|
70
|
+
const haltResult = await congruentApi.triggerMiddlewareDecoratorNoStaticTypeCheck(
|
|
71
|
+
entry,
|
|
72
|
+
decorator,
|
|
73
|
+
req,
|
|
74
|
+
{
|
|
75
|
+
next,
|
|
76
|
+
originalRequest: req
|
|
77
|
+
}
|
|
78
|
+
);
|
|
79
|
+
if (haltResult && congruentApi.isHttpResponseObject(haltResult)) {
|
|
80
|
+
const haltResultHeaders = new Map(
|
|
81
|
+
Object.entries(haltResult.headers || {})
|
|
82
|
+
);
|
|
83
|
+
res.status(haltResult.code).setHeaders(haltResultHeaders).json(haltResult.body);
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
middlewareHandlers.push(async (req, res, next) => {
|
|
88
|
+
if (!res.locals.diScope) {
|
|
89
|
+
res.locals.diScope = entry.dicontainer.createScope();
|
|
90
|
+
}
|
|
21
91
|
req.pathParams = req.params;
|
|
22
|
-
const haltResult = await entry.
|
|
92
|
+
const haltResult = await entry.triggerNoStaticTypeCheck(
|
|
93
|
+
res.locals.diScope,
|
|
94
|
+
req,
|
|
95
|
+
{
|
|
96
|
+
next,
|
|
97
|
+
originalRequest: req
|
|
98
|
+
}
|
|
99
|
+
);
|
|
23
100
|
if (haltResult && congruentApi.isHttpResponseObject(haltResult)) {
|
|
24
101
|
const haltResultHeaders = new Map(
|
|
25
102
|
Object.entries(haltResult.headers || {})
|
|
@@ -27,6 +104,7 @@ function createExpressRegistry(app, diContainer, apiContract) {
|
|
|
27
104
|
res.status(haltResult.code).setHeaders(haltResultHeaders).json(haltResult.body);
|
|
28
105
|
}
|
|
29
106
|
});
|
|
107
|
+
app.use(genericPath, ...middlewareHandlers);
|
|
30
108
|
}
|
|
31
109
|
});
|
|
32
110
|
return registry;
|
package/dist/index.mjs
CHANGED
|
@@ -1,23 +1,100 @@
|
|
|
1
|
-
import { createRegistry, isHttpResponseObject } from '@congruent-stack/congruent-api';
|
|
1
|
+
import { createRegistry, triggerMiddlewareDecoratorNoStaticTypeCheck, isHttpResponseObject, triggerEndpointDecoratorNoStaticTypeCheck } from '@congruent-stack/congruent-api';
|
|
2
2
|
|
|
3
3
|
function createExpressRegistry(app, diContainer, apiContract) {
|
|
4
4
|
const registry = createRegistry(diContainer, apiContract, {
|
|
5
5
|
handlerRegisteredCallback: (entry) => {
|
|
6
6
|
const { genericPath } = entry.methodEndpoint;
|
|
7
7
|
const method = entry.methodEndpoint.method.toLowerCase();
|
|
8
|
-
app[method]
|
|
8
|
+
if (typeof app[method] !== "function") {
|
|
9
|
+
throw new Error(`Unsupported HTTP method: ${method}`);
|
|
10
|
+
}
|
|
11
|
+
const routeHandlers = [];
|
|
12
|
+
entry.decoratorFactories.forEach((decoratorFactory) => {
|
|
13
|
+
routeHandlers.push(async (req, res, next) => {
|
|
14
|
+
if (!res.locals.diScope) {
|
|
15
|
+
res.locals.diScope = entry.dicontainer.createScope();
|
|
16
|
+
}
|
|
17
|
+
req.pathParams = req.params;
|
|
18
|
+
const decorator = decoratorFactory(res.locals.diScope);
|
|
19
|
+
const haltResult = await triggerEndpointDecoratorNoStaticTypeCheck(
|
|
20
|
+
entry.methodEndpoint,
|
|
21
|
+
decorator,
|
|
22
|
+
req,
|
|
23
|
+
{
|
|
24
|
+
next,
|
|
25
|
+
originalRequest: req
|
|
26
|
+
}
|
|
27
|
+
);
|
|
28
|
+
if (haltResult && isHttpResponseObject(haltResult)) {
|
|
29
|
+
const haltResultHeaders = new Map(
|
|
30
|
+
Object.entries(haltResult.headers || {})
|
|
31
|
+
);
|
|
32
|
+
res.status(haltResult.code).setHeaders(haltResultHeaders).json(haltResult.body);
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
routeHandlers.push(async (req, res) => {
|
|
37
|
+
if (!res.locals.diScope) {
|
|
38
|
+
res.locals.diScope = entry.dicontainer.createScope();
|
|
39
|
+
}
|
|
9
40
|
req.pathParams = req.params;
|
|
10
|
-
const result = await entry.
|
|
41
|
+
const result = await entry.triggerNoStaticTypeCheck(
|
|
42
|
+
res.locals.diScope,
|
|
43
|
+
req,
|
|
44
|
+
{
|
|
45
|
+
originalRequest: req
|
|
46
|
+
}
|
|
47
|
+
);
|
|
11
48
|
const resultHeaders = new Map(
|
|
12
49
|
Object.entries(result.headers || {})
|
|
13
50
|
);
|
|
14
51
|
res.status(result.code).setHeaders(resultHeaders).json(result.body);
|
|
15
52
|
});
|
|
53
|
+
app[method](genericPath, ...routeHandlers);
|
|
16
54
|
},
|
|
17
55
|
middlewareHandlerRegisteredCallback: (entry) => {
|
|
18
|
-
|
|
56
|
+
const {
|
|
57
|
+
/*TODO: method,*/
|
|
58
|
+
genericPath
|
|
59
|
+
} = entry;
|
|
60
|
+
const middlewareHandlers = [];
|
|
61
|
+
entry.decoratorFactories.forEach((decoratorFactory) => {
|
|
62
|
+
middlewareHandlers.push(async (req, res, next) => {
|
|
63
|
+
if (!res.locals.diScope) {
|
|
64
|
+
res.locals.diScope = entry.dicontainer.createScope();
|
|
65
|
+
}
|
|
66
|
+
req.pathParams = req.params;
|
|
67
|
+
const decorator = decoratorFactory(res.locals.diScope);
|
|
68
|
+
const haltResult = await triggerMiddlewareDecoratorNoStaticTypeCheck(
|
|
69
|
+
entry,
|
|
70
|
+
decorator,
|
|
71
|
+
req,
|
|
72
|
+
{
|
|
73
|
+
next,
|
|
74
|
+
originalRequest: req
|
|
75
|
+
}
|
|
76
|
+
);
|
|
77
|
+
if (haltResult && isHttpResponseObject(haltResult)) {
|
|
78
|
+
const haltResultHeaders = new Map(
|
|
79
|
+
Object.entries(haltResult.headers || {})
|
|
80
|
+
);
|
|
81
|
+
res.status(haltResult.code).setHeaders(haltResultHeaders).json(haltResult.body);
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
middlewareHandlers.push(async (req, res, next) => {
|
|
86
|
+
if (!res.locals.diScope) {
|
|
87
|
+
res.locals.diScope = entry.dicontainer.createScope();
|
|
88
|
+
}
|
|
19
89
|
req.pathParams = req.params;
|
|
20
|
-
const haltResult = await entry.
|
|
90
|
+
const haltResult = await entry.triggerNoStaticTypeCheck(
|
|
91
|
+
res.locals.diScope,
|
|
92
|
+
req,
|
|
93
|
+
{
|
|
94
|
+
next,
|
|
95
|
+
originalRequest: req
|
|
96
|
+
}
|
|
97
|
+
);
|
|
21
98
|
if (haltResult && isHttpResponseObject(haltResult)) {
|
|
22
99
|
const haltResultHeaders = new Map(
|
|
23
100
|
Object.entries(haltResult.headers || {})
|
|
@@ -25,6 +102,7 @@ function createExpressRegistry(app, diContainer, apiContract) {
|
|
|
25
102
|
res.status(haltResult.code).setHeaders(haltResultHeaders).json(haltResult.body);
|
|
26
103
|
}
|
|
27
104
|
});
|
|
105
|
+
app.use(genericPath, ...middlewareHandlers);
|
|
28
106
|
}
|
|
29
107
|
});
|
|
30
108
|
return registry;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@congruent-stack/congruent-api-express",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.10.0",
|
|
5
5
|
"description": "Typescript schema-first tooling for agnostic REST APIs.",
|
|
6
6
|
"keywords": [],
|
|
7
7
|
"author": "congruent-stack",
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
"peerDependencies": {
|
|
23
23
|
"@types/express": "5.0.3",
|
|
24
24
|
"express": "5.1.0",
|
|
25
|
-
"@congruent-stack/congruent-api": "0.
|
|
25
|
+
"@congruent-stack/congruent-api": "0.10.0"
|
|
26
26
|
},
|
|
27
27
|
"devDependencies": {
|
|
28
28
|
"@types/express": "5.0.3",
|