Skip to content

Commit 198785e

Browse files
committed
refactor: convert Express route to Hono handler with error handling and type definitions
1 parent 3e9d075 commit 198785e

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

templates/server/route.js.template

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
11
/**
22
* {{entityName}} routes
33
*/
4-
import express from 'express';
5-
import * as {{controllerName}} from '../controllers/{{controllerFileName}}';
4+
import { {{controllerName}} } from '../controllers/{{controllerFileName}}';
65

7-
const router = express.Router();
8-
9-
// {{methodUpper}} {{routePath}}
10-
router.{{methodLower}}('{{routePath}}', {{controllerName}}.{{methodName}});
6+
/**
7+
* Handler for {{methodUpper}} {{routePath}}
8+
* @param {Object} c - Hono context
9+
* @returns {Response} - Response object
10+
*/
11+
export async function {{methodName}}Handler(c) {
12+
try {
13+
return await {{controllerName}}.{{methodName}}(c);
14+
} catch (error) {
15+
console.error(`Error in {{methodName}}:`, error);
16+
return c.json({ error: error.message || 'An unexpected error occurred' }, 500);
17+
}
18+
}
1119

12-
export default router;
20+
/**
21+
* Route configuration for {{entityName}} endpoint
22+
*/
23+
export const {{entityName}}Route = {
24+
method: '{{methodUpper}}',
25+
path: '{{routePath}}',
26+
handler: {{methodName}}Handler
27+
};

0 commit comments

Comments
 (0)