@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.cjs
CHANGED
|
@@ -56,6 +56,7 @@ module.exports = __toCommonJS(index_exports);
|
|
|
56
56
|
var import_node_crypto = require("node:crypto");
|
|
57
57
|
|
|
58
58
|
// src/errors.js
|
|
59
|
+
var import_node_process = require("node:process");
|
|
59
60
|
var ApiError = class _ApiError extends Error {
|
|
60
61
|
/**
|
|
61
62
|
* Create an API _error
|
|
@@ -162,7 +163,7 @@ function createErrorHandler() {
|
|
|
162
163
|
if (_error.details) {
|
|
163
164
|
response.details = _error.details;
|
|
164
165
|
}
|
|
165
|
-
if (
|
|
166
|
+
if (import_node_process.env.NODE_ENV === "development") {
|
|
166
167
|
response.stack = _error.stack;
|
|
167
168
|
}
|
|
168
169
|
res.status(response.statusCode).json(response);
|
|
@@ -1578,6 +1579,49 @@ var SimpleRouter = class {
|
|
|
1578
1579
|
head(path, handler, options = {}) {
|
|
1579
1580
|
return this.addRoute("HEAD", path, handler, options);
|
|
1580
1581
|
}
|
|
1582
|
+
/**
|
|
1583
|
+
* Convert to Express Router middleware
|
|
1584
|
+
*
|
|
1585
|
+
* @param {Object} express - Express module (required)
|
|
1586
|
+
* @returns {Function} Express-compatible router middleware
|
|
1587
|
+
*
|
|
1588
|
+
* @example
|
|
1589
|
+
* import express from 'express';
|
|
1590
|
+
* const router = createRouter();
|
|
1591
|
+
* router.get('/users', handler);
|
|
1592
|
+
* app.use('/api', router.toExpressRouter(express));
|
|
1593
|
+
*/
|
|
1594
|
+
toExpressRouter(express) {
|
|
1595
|
+
if (!express || typeof express.Router !== "function") {
|
|
1596
|
+
throw new Error("Express is required for toExpressRouter(). Pass the express module as argument: router.toExpressRouter(express)");
|
|
1597
|
+
}
|
|
1598
|
+
const expressRouter = express.Router();
|
|
1599
|
+
for (const route of this.routes) {
|
|
1600
|
+
const method = route.method.toLowerCase();
|
|
1601
|
+
const path = route.path;
|
|
1602
|
+
const handler = route.handler;
|
|
1603
|
+
const middleware = route.middleware || [];
|
|
1604
|
+
const expressHandler = async (req, res, next) => {
|
|
1605
|
+
try {
|
|
1606
|
+
for (const mw of middleware) {
|
|
1607
|
+
await new Promise((resolve, reject) => {
|
|
1608
|
+
mw(req, res, (err) => err ? reject(err) : resolve());
|
|
1609
|
+
});
|
|
1610
|
+
}
|
|
1611
|
+
const result = await handler(req, res);
|
|
1612
|
+
if (result !== void 0 && !res.headersSent) {
|
|
1613
|
+
res.json(result);
|
|
1614
|
+
}
|
|
1615
|
+
} catch (error) {
|
|
1616
|
+
next(error);
|
|
1617
|
+
}
|
|
1618
|
+
};
|
|
1619
|
+
if (typeof expressRouter[method] === "function") {
|
|
1620
|
+
expressRouter[method](path, expressHandler);
|
|
1621
|
+
}
|
|
1622
|
+
}
|
|
1623
|
+
return expressRouter;
|
|
1624
|
+
}
|
|
1581
1625
|
};
|
|
1582
1626
|
function createRouter(routeConfig, options = {}) {
|
|
1583
1627
|
const router = new SimpleRouter(options);
|