@coherent.js/api 1.0.0-beta.5 → 1.0.0-beta.6
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 +45 -1
- package/dist/index.cjs.map +2 -2
- package/dist/index.js +45 -1
- package/dist/index.js.map +2 -2
- package/package.json +3 -3
- package/types/index.d.ts +188 -38
package/dist/index.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
import { createHash, randomBytes } from "node:crypto";
|
|
3
3
|
|
|
4
4
|
// src/errors.js
|
|
5
|
+
import { env } from "node:process";
|
|
5
6
|
var ApiError = class _ApiError extends Error {
|
|
6
7
|
/**
|
|
7
8
|
* Create an API _error
|
|
@@ -108,7 +109,7 @@ function createErrorHandler() {
|
|
|
108
109
|
if (_error.details) {
|
|
109
110
|
response.details = _error.details;
|
|
110
111
|
}
|
|
111
|
-
if (
|
|
112
|
+
if (env.NODE_ENV === "development") {
|
|
112
113
|
response.stack = _error.stack;
|
|
113
114
|
}
|
|
114
115
|
res.status(response.statusCode).json(response);
|
|
@@ -1524,6 +1525,49 @@ var SimpleRouter = class {
|
|
|
1524
1525
|
head(path, handler, options = {}) {
|
|
1525
1526
|
return this.addRoute("HEAD", path, handler, options);
|
|
1526
1527
|
}
|
|
1528
|
+
/**
|
|
1529
|
+
* Convert to Express Router middleware
|
|
1530
|
+
*
|
|
1531
|
+
* @param {Object} express - Express module (required)
|
|
1532
|
+
* @returns {Function} Express-compatible router middleware
|
|
1533
|
+
*
|
|
1534
|
+
* @example
|
|
1535
|
+
* import express from 'express';
|
|
1536
|
+
* const router = createRouter();
|
|
1537
|
+
* router.get('/users', handler);
|
|
1538
|
+
* app.use('/api', router.toExpressRouter(express));
|
|
1539
|
+
*/
|
|
1540
|
+
toExpressRouter(express) {
|
|
1541
|
+
if (!express || typeof express.Router !== "function") {
|
|
1542
|
+
throw new Error("Express is required for toExpressRouter(). Pass the express module as argument: router.toExpressRouter(express)");
|
|
1543
|
+
}
|
|
1544
|
+
const expressRouter = express.Router();
|
|
1545
|
+
for (const route of this.routes) {
|
|
1546
|
+
const method = route.method.toLowerCase();
|
|
1547
|
+
const path = route.path;
|
|
1548
|
+
const handler = route.handler;
|
|
1549
|
+
const middleware = route.middleware || [];
|
|
1550
|
+
const expressHandler = async (req, res, next) => {
|
|
1551
|
+
try {
|
|
1552
|
+
for (const mw of middleware) {
|
|
1553
|
+
await new Promise((resolve, reject) => {
|
|
1554
|
+
mw(req, res, (err) => err ? reject(err) : resolve());
|
|
1555
|
+
});
|
|
1556
|
+
}
|
|
1557
|
+
const result = await handler(req, res);
|
|
1558
|
+
if (result !== void 0 && !res.headersSent) {
|
|
1559
|
+
res.json(result);
|
|
1560
|
+
}
|
|
1561
|
+
} catch (error) {
|
|
1562
|
+
next(error);
|
|
1563
|
+
}
|
|
1564
|
+
};
|
|
1565
|
+
if (typeof expressRouter[method] === "function") {
|
|
1566
|
+
expressRouter[method](path, expressHandler);
|
|
1567
|
+
}
|
|
1568
|
+
}
|
|
1569
|
+
return expressRouter;
|
|
1570
|
+
}
|
|
1527
1571
|
};
|
|
1528
1572
|
function createRouter(routeConfig, options = {}) {
|
|
1529
1573
|
const router = new SimpleRouter(options);
|