Skip to content

Commit 020a8c5

Browse files
committed
feat: use next(e) rather than returning an error in Express
1 parent 4e99cad commit 020a8c5

3 files changed

Lines changed: 40 additions & 16 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@ts-rest/express': minor
3+
---
4+
5+
Allow optional [ts-rest] ... logging for express endpoints

.changeset/smart-parents-yell.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@ts-rest/express': minor
3+
---
4+
5+
Pass express errors to next() rather than catching them and handling with ts-rest

libs/ts-rest/express/src/lib/ts-rest-express.ts

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { IRouter, Request, Response } from 'express';
1+
import { IRouter, NextFunction, Request, Response } from 'express';
22
import { IncomingHttpHeaders } from 'http';
33
import {
44
AppRoute,
@@ -103,7 +103,7 @@ const transformAppRouteQueryImplementation = (
103103
console.log(`[ts-rest] Initialized ${schema.method} ${schema.path}`);
104104
}
105105

106-
app.get(schema.path, async (req, res) => {
106+
app.get(schema.path, async (req, res, next) => {
107107
const queryResult = checkZodSchema(req.query, schema.query);
108108

109109
if (!queryResult.success) {
@@ -118,14 +118,18 @@ const transformAppRouteQueryImplementation = (
118118
return res.status(400).send(paramsResult.error);
119119
}
120120

121-
const result = await route({
122-
params: paramsResult.data,
123-
query: queryResult.data,
124-
headers: req.headers,
125-
req: req,
126-
});
121+
try {
122+
const result = await route({
123+
params: paramsResult.data,
124+
query: queryResult.data,
125+
headers: req.headers,
126+
req: req,
127+
});
127128

128-
return res.status(Number(result.status)).json(result.body);
129+
return res.status(Number(result.status)).json(result.body);
130+
} catch (e) {
131+
return next(e);
132+
}
129133
});
130134
};
131135

@@ -143,6 +147,7 @@ const transformAppRouteMutationImplementation = (
143147

144148
const method = schema.method;
145149

150+
const callback = async (req: Request, res: Response, next: NextFunction) => {
146151
const queryResult = checkZodSchema(req.query, schema.query);
147152

148153
if (!queryResult.success) {
@@ -163,6 +168,7 @@ const transformAppRouteMutationImplementation = (
163168
return res.status(400).send(paramsResult.error);
164169
}
165170

171+
try {
166172
const result = await route({
167173
params: paramsResult.data,
168174
body: bodyResult.data,
@@ -177,8 +183,7 @@ const transformAppRouteMutationImplementation = (
177183

178184
return res.status(Number(result.status)).json(result.body);
179185
} catch (e) {
180-
console.error(`[ts-rest] Error on ${method} ${schema.path}`, e);
181-
return res.status(500).send('Internal Server Error');
186+
return next(e);
182187
}
183188
};
184189

@@ -204,7 +209,10 @@ export const createExpressEndpoints = <
204209
>(
205210
schema: TRouter,
206211
router: T,
207-
app: IRouter
212+
app: IRouter,
213+
options = {
214+
logInitialization: true,
215+
}
208216
) => {
209217
recursivelyApplyExpressRouter(router, [], (route, path) => {
210218
const routerViaPath = getValue(schema, path.join('.'));
@@ -214,13 +222,19 @@ export const createExpressEndpoints = <
214222
}
215223

216224
if (isAppRoute(routerViaPath)) {
217-
if (routerViaPath.method !== 'GET') {
218-
transformAppRouteMutationImplementation(route, routerViaPath, app);
219-
} else {
225+
if (routerViaPath.method === 'GET') {
220226
transformAppRouteQueryImplementation(
221227
route as AppRouteQueryImplementation<any>,
222228
routerViaPath,
223-
app
229+
app,
230+
options
231+
);
232+
} else {
233+
transformAppRouteMutationImplementation(
234+
route,
235+
routerViaPath,
236+
app,
237+
options
224238
);
225239
}
226240
} else {

0 commit comments

Comments
 (0)