@prosopo/api-express-router 2.5.5 → 2.6.1
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
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# @prosopo/api-express-router
|
|
2
|
+
|
|
3
|
+
## 2.6.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Updated dependencies [04cc7ee]
|
|
8
|
+
- @prosopo/common@2.6.1
|
|
9
|
+
- @prosopo/api-route@2.6.1
|
|
10
|
+
|
|
11
|
+
## 2.6.0
|
|
12
|
+
|
|
13
|
+
### Minor Changes
|
|
14
|
+
|
|
15
|
+
- a0bfc8a: bump all pkg versions since independent versioning applied
|
|
16
|
+
|
|
17
|
+
### Patch Changes
|
|
18
|
+
|
|
19
|
+
- Updated dependencies [a0bfc8a]
|
|
20
|
+
- @prosopo/api-route@2.6.0
|
|
21
|
+
- @prosopo/common@2.6.0
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const express = require("express");
|
|
4
|
+
const errorHandler = require("./errorHandler.cjs");
|
|
5
|
+
class ApiExpressRouterFactory {
|
|
6
|
+
createRouter(routersProvider, apiEndpointAdapter) {
|
|
7
|
+
const router = express.Router();
|
|
8
|
+
const apiRoutes = routersProvider.getRoutes();
|
|
9
|
+
this.registerRoutes(router, apiRoutes, apiEndpointAdapter);
|
|
10
|
+
router.use(errorHandler.handleErrors);
|
|
11
|
+
return router;
|
|
12
|
+
}
|
|
13
|
+
registerRoutes(router, routes, apiEndpointAdapter) {
|
|
14
|
+
for (const route of routes) {
|
|
15
|
+
router.post(
|
|
16
|
+
route.path,
|
|
17
|
+
async (request, response, next) => {
|
|
18
|
+
return await apiEndpointAdapter.handleRequest(
|
|
19
|
+
route.endpoint,
|
|
20
|
+
request,
|
|
21
|
+
response,
|
|
22
|
+
next
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
exports.ApiExpressRouterFactory = ApiExpressRouterFactory;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const common = require("@prosopo/common");
|
|
4
|
+
class ApiExpressDefaultEndpointAdapter {
|
|
5
|
+
constructor(logLevel, errorStatusCode) {
|
|
6
|
+
this.logLevel = logLevel;
|
|
7
|
+
this.errorStatusCode = errorStatusCode;
|
|
8
|
+
}
|
|
9
|
+
async handleRequest(endpoint, request, response, next) {
|
|
10
|
+
let args;
|
|
11
|
+
try {
|
|
12
|
+
args = endpoint.getRequestArgsSchema()?.parse(request.body);
|
|
13
|
+
} catch (error) {
|
|
14
|
+
return next(
|
|
15
|
+
new common.ProsopoApiError("API.PARSE_ERROR", {
|
|
16
|
+
context: { code: 400, error }
|
|
17
|
+
})
|
|
18
|
+
);
|
|
19
|
+
}
|
|
20
|
+
try {
|
|
21
|
+
const apiEndpointResponse = await endpoint.processRequest(
|
|
22
|
+
args,
|
|
23
|
+
request.logger
|
|
24
|
+
);
|
|
25
|
+
response.json(apiEndpointResponse);
|
|
26
|
+
} catch (error) {
|
|
27
|
+
request.logger.error(error.message);
|
|
28
|
+
response.status(500).send("An internal server error occurred.");
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
exports.ApiExpressDefaultEndpointAdapter = ApiExpressDefaultEndpointAdapter;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const common = require("@prosopo/common");
|
|
4
|
+
const handleErrors = (err, request, response, next) => {
|
|
5
|
+
const { code, statusMessage, jsonError } = common.unwrapError(err, request.i18n);
|
|
6
|
+
response.statusMessage = statusMessage;
|
|
7
|
+
response.set("content-type", "application/json");
|
|
8
|
+
response.status(code);
|
|
9
|
+
response.send({ error: jsonError });
|
|
10
|
+
response.end();
|
|
11
|
+
};
|
|
12
|
+
exports.handleErrors = handleErrors;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const apiExpressRouterFactory$1 = require("./apiExpressRouterFactory.cjs");
|
|
4
|
+
const apiExpressDefaultEndpointAdapter = require("./endpointAdapter/apiExpressDefaultEndpointAdapter.cjs");
|
|
5
|
+
const errorHandler = require("./errorHandler.cjs");
|
|
6
|
+
const apiExpressRouterFactory = new apiExpressRouterFactory$1.ApiExpressRouterFactory();
|
|
7
|
+
const createApiExpressDefaultEndpointAdapter = (logLevel, errorStatusCode = 500) => {
|
|
8
|
+
return new apiExpressDefaultEndpointAdapter.ApiExpressDefaultEndpointAdapter(logLevel, errorStatusCode);
|
|
9
|
+
};
|
|
10
|
+
exports.handleErrors = errorHandler.handleErrors;
|
|
11
|
+
exports.apiExpressRouterFactory = apiExpressRouterFactory;
|
|
12
|
+
exports.createApiExpressDefaultEndpointAdapter = createApiExpressDefaultEndpointAdapter;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prosopo/api-express-router",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.6.1",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -20,12 +20,12 @@
|
|
|
20
20
|
"build:cjs": "npx vite --config vite.cjs.config.ts build"
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@prosopo/api-route": "2.
|
|
24
|
-
"@prosopo/common": "2.
|
|
23
|
+
"@prosopo/api-route": "2.6.1",
|
|
24
|
+
"@prosopo/common": "2.6.1",
|
|
25
25
|
"zod": "3.23.8"
|
|
26
26
|
},
|
|
27
27
|
"devDependencies": {
|
|
28
|
-
"@prosopo/config": "2.
|
|
28
|
+
"@prosopo/config": "2.6.0"
|
|
29
29
|
},
|
|
30
30
|
"author": "PROSOPO LIMITED <info@prosopo.io>",
|
|
31
31
|
"license": "Apache-2.0",
|