fix(transformers)!: answer 204 when a handler returns undefined (GitHub #38)
JsonTransformer passed the handler's value to JSON.stringify, which returns undefined for undefined, while TransformerFn promises a string body. The Node server wrote that as an empty 200 with content-type application/json; the fetch adapter's toBodyInit read `body.buffer` on it and threw, so Deno, Bun and the edge answered 500 for a handler with no `return`. JsonTransformer now returns status 204 and an empty body (no headers) for undefined. outcomeToResponse already sends a null body for 204 and the Node server ends with the empty string, so all four runtimes give the same answer. null is a JSON value and still renders as 200 `null`. Routes with their own @Transformer are unchanged. BREAKING CHANGE: a handler with no return value now answers 204 instead of an empty 200 on Node (and instead of 500 elsewhere). Return a value to keep a 200. test/undefined-return.test.ts covers app.fetch and the Node server (both fail without the fix) and pins null to 200. Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
3 files changed
+62
-6
No files matched your search
@@ -0,0 +1,45 @@
|
||||
// GitHub #38: `JSON.stringify(undefined)` is `undefined`, so a handler with no `return` gave an
|
||||
// empty 200 on Node and a 500 (a TypeError in the fetch adapter) on Deno, Bun and the edge. It now
|
||||
// answers 204 with no body everywhere.
|
||||
import 'reflect-metadata';
|
||||
import { afterEach, describe, expect, it } from 'vitest';
|
||||
import type { AddressInfo } from 'node:net';
|
||||
import type { Server } from 'node:http';
|
||||
import { createApp, Get, Module, Route } from '../src/index';
|
||||
|
||||
@Route('/')
|
||||
class Ctl {
|
||||
@Get('/none') none() {}
|
||||
@Get('/null') nul() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Module({ mountpoint: '/', controllers: [Ctl] })
|
||||
class M {}
|
||||
|
||||
describe('a handler that returns undefined answers 204 (GitHub #38)', () => {
|
||||
let server: Server | undefined;
|
||||
afterEach(() => new Promise<void>((resolve) => (server ? server.close(() => resolve()) : resolve())));
|
||||
|
||||
it('on app.fetch', async () => {
|
||||
const r = await createApp({ modules: [M] }).fetch(new Request('http://x/none'));
|
||||
expect(r.status).toBe(204);
|
||||
expect(r.headers.get('content-type')).toBeNull();
|
||||
expect(await r.text()).toBe('');
|
||||
});
|
||||
|
||||
it('on the Node server', async () => {
|
||||
server = await createApp({ modules: [M] }).listen(0);
|
||||
const r = await fetch(`http://127.0.0.1:${(server.address() as AddressInfo).port}/none`);
|
||||
expect(r.status).toBe(204);
|
||||
expect(r.headers.get('content-type')).toBeNull();
|
||||
expect(await r.text()).toBe('');
|
||||
});
|
||||
|
||||
it('null is still a JSON value: 200 with `null`', async () => {
|
||||
const r = await createApp({ modules: [M] }).fetch(new Request('http://x/null'));
|
||||
expect(r.status).toBe(200);
|
||||
expect(await r.text()).toBe('null');
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user