Merge pull request 'fix(transformers)!: answer 204 when a handler returns undefined (GitHub #38)' (#172) from fix/undefined-return-204 into develop
CI / dco (push) Has been skipped
CI / runtimes (bun) (push) Successful in 20s
CI / build (push) Successful in 32s
CI / runtimes (edge) (push) Successful in 20s
CI / runtimes (deno) (push) Successful in 32s
Promote → GitHub / push-github (push) Successful in 6s

This commit was merged in pull request #172.
This commit is contained in:
chrnx committed 2026-10-25 03:51:58 +00:00
commit dd3a74afcd
3 files changed
+62 -6

No files matched your search

+9
View File
@@ -126,6 +126,15 @@ npm treats versions as semver and semver forbids leading zeros.
migrate, remove the token from the node's `needs`, or move the work that produces it into a
separate provider or step the node needs.
- **A handler that returns `undefined` answers `204 No Content`** (GitHub #38). The default JSON
transformer passed the value to `JSON.stringify`, which returns `undefined` rather than a string:
the Node server sent an empty `200` with `content-type: application/json`, and the fetch adapter
threw a `TypeError`, so Deno, Bun and the edge answered `500` for a handler that did nothing
wrong. Every runtime now sends `204` with no body and no `content-type`. A handler that returns
`null` still gets `200` with `null`. A client that checked for `200` after a handler with no
`return` sees `204` instead; return a value to keep a `200`. A route with its own `@Transformer`
is not affected.
### Added
- **Health probes: `createApp({ health: true })`** (#115). Off by default. `GET`/`HEAD /__live__`
+8 -6
View File
@@ -24,12 +24,14 @@ export interface ErrorRequest {
*/
export type ErrorRenderer = (error: unknown, req: ErrorRequest) => ErrorResponse | undefined | void;
/** Default transformer: serialize a handler's return value as a JSON `200` response. */
export const JsonTransformer: TransformerFn = (value) => ({
status: 200,
headers: { 'content-type': 'application/json' },
body: JSON.stringify(value),
});
/**
* Default transformer: serialize a handler's return value as a JSON `200` response. A handler that
* returns `undefined` (no `return`) answers `204 No Content` with no body, on every runtime (GitHub #38).
*/
export const JsonTransformer: TransformerFn = (value) => {
if (value === undefined) return { status: 204, body: '' };
return { status: 200, headers: { 'content-type': 'application/json' }, body: JSON.stringify(value) };
};
/**
* Map a thrown value to a JSON error response (422 for validation, `HttpError` status, else 500).
+45
View File
@@ -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');
});
});