Skip to content

Commit 48cf61e

Browse files
authored
fix(server): forward DELETE custom route bodies (#16857)
1 parent f769225 commit 48cf61e

3 files changed

Lines changed: 58 additions & 1 deletion

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@mastra/server': patch
3+
---
4+
5+
Fix DELETE custom routes forwarding JSON request bodies.

packages/server/src/server/server-adapter/index.test.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,19 @@ class TestMastraServer extends MastraServer<any, any, any> {
1414
registerContextMiddleware = vi.fn();
1515
registerAuthMiddleware = vi.fn();
1616
registerHttpLoggingMiddleware = vi.fn();
17+
18+
buildCustomRouteHandlerForTest() {
19+
return this.buildCustomRouteHandler();
20+
}
21+
22+
handleCustomRouteRequestForTest(
23+
url: string,
24+
method: string,
25+
headers: Record<string, string | string[] | undefined>,
26+
body: unknown,
27+
) {
28+
return this.handleCustomRouteRequest(url, method, headers, body);
29+
}
1730
}
1831

1932
function createMockFGAProvider(authorized = true): IFGAProvider {
@@ -24,6 +37,45 @@ function createMockFGAProvider(authorized = true): IFGAProvider {
2437
};
2538
}
2639

40+
describe('custom route forwarding', () => {
41+
it('should forward DELETE JSON bodies to custom routes', async () => {
42+
const mastra = new Mastra({
43+
logger: false,
44+
server: {
45+
apiRoutes: [
46+
{
47+
path: '/items/:id',
48+
method: 'DELETE',
49+
handler: async c => {
50+
const body = await c.req.json();
51+
const { id } = c.req.param();
52+
53+
return c.json({ deleted: id, reason: body.reason });
54+
},
55+
},
56+
],
57+
},
58+
});
59+
const adapter = new TestMastraServer({ app: {}, mastra });
60+
61+
await expect(adapter.buildCustomRouteHandlerForTest()).resolves.toBe(true);
62+
63+
const response = await adapter.handleCustomRouteRequestForTest(
64+
'http://localhost/items/123',
65+
'DELETE',
66+
{ 'content-type': 'application/json' },
67+
{ reason: 'no longer needed' },
68+
);
69+
70+
expect(response).not.toBeNull();
71+
expect(response!.status).toBe(200);
72+
await expect(response!.json()).resolves.toEqual({
73+
deleted: '123',
74+
reason: 'no longer needed',
75+
});
76+
});
77+
});
78+
2779
describe('FGA Middleware - checkRouteFGA', () => {
2880
let checkRouteFGA: (
2981
mastra: any,

packages/server/src/server/server-adapter/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -791,7 +791,7 @@ export abstract class MastraServer<TApp, TRequest, TResponse> extends MastraServ
791791
}
792792

793793
const init: RequestInit = { method, headers: fetchHeaders };
794-
if (['POST', 'PUT', 'PATCH'].includes(method) && body !== undefined) {
794+
if (['POST', 'PUT', 'PATCH', 'DELETE'].includes(method) && body !== undefined) {
795795
if (body instanceof ArrayBuffer || body instanceof Uint8Array || body instanceof ReadableStream) {
796796
init.body = body as any;
797797
if (body instanceof ReadableStream) {

0 commit comments

Comments
 (0)