Skip to content

Commit 047efcb

Browse files
committed
feat(rest): set status code to 204 when body is undefined
1 parent 1762765 commit 047efcb

File tree

2 files changed

+41
-29
lines changed

2 files changed

+41
-29
lines changed

packages/rest/src/writer.ts

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,34 +19,37 @@ export function writeResultToResponse(
1919
// result returned back from invoking controller method
2020
result: OperationRetval,
2121
): void {
22-
if (result) {
23-
if (result instanceof Readable || typeof result.pipe === 'function') {
24-
response.setHeader('Content-Type', 'application/octet-stream');
25-
// Stream
26-
result.pipe(response);
27-
return;
28-
}
29-
switch (typeof result) {
30-
case 'object':
31-
case 'boolean':
32-
case 'number':
33-
if (Buffer.isBuffer(result)) {
34-
// Buffer for binary data
35-
response.setHeader('Content-Type', 'application/octet-stream');
36-
} else {
37-
// TODO(ritch) remove this, should be configurable
38-
// See https://github.com/strongloop/loopback-next/issues/436
39-
response.setHeader('Content-Type', 'application/json');
40-
// TODO(bajtos) handle errors - JSON.stringify can throw
41-
result = JSON.stringify(result);
42-
}
43-
break;
44-
default:
45-
response.setHeader('Content-Type', 'text/plain');
46-
result = result.toString();
47-
break;
48-
}
49-
response.write(result);
22+
if (!result) {
23+
response.statusCode = 204;
24+
response.end();
25+
return;
5026
}
51-
response.end();
27+
28+
if (result instanceof Readable || typeof result.pipe === 'function') {
29+
response.setHeader('Content-Type', 'application/octet-stream');
30+
// Stream
31+
result.pipe(response);
32+
return;
33+
}
34+
switch (typeof result) {
35+
case 'object':
36+
case 'boolean':
37+
case 'number':
38+
if (Buffer.isBuffer(result)) {
39+
// Buffer for binary data
40+
response.setHeader('Content-Type', 'application/octet-stream');
41+
} else {
42+
// TODO(ritch) remove this, should be configurable
43+
// See https://github.com/strongloop/loopback-next/issues/436
44+
response.setHeader('Content-Type', 'application/json');
45+
// TODO(bajtos) handle errors - JSON.stringify can throw
46+
result = JSON.stringify(result);
47+
}
48+
break;
49+
default:
50+
response.setHeader('Content-Type', 'text/plain');
51+
result = result.toString();
52+
break;
53+
}
54+
response.end(result);
5255
}

packages/rest/test/unit/writer.unit.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,15 @@ describe('writer', () => {
6868
expect(result.payload).to.equal('ABC123');
6969
});
7070

71+
it('sends 204 No Content when the response is undefined', async () => {
72+
writeResultToResponse(response, undefined);
73+
const result = await observedResponse;
74+
75+
expect(result.statusCode).to.equal(204);
76+
expect(result.headers).to.not.have.property('content-type');
77+
expect(result.payload).to.equal('');
78+
});
79+
7180
function setupResponseMock() {
7281
const responseMock = stubExpressContext();
7382
response = responseMock.response;

0 commit comments

Comments
 (0)