-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
writer.ts
63 lines (59 loc) · 1.9 KB
/
writer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Copyright IBM Corp. 2017,2018. All Rights Reserved.
// Node module: @loopback/rest
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
import {OperationRetval, Response} from './types';
import {Readable} from 'stream';
/**
* Writes the result from Application controller method
* into the HTTP response
*
* @param response - HTTP Response
* @param result - Result from the API to write into HTTP Response
*/
export function writeResultToResponse(
// not needed and responsibility should be in the sequence.send
response: Response,
// result returned back from invoking controller method
result: OperationRetval,
): void {
// Bypass response writing if the controller method returns `response` itself
// or the response headers have been sent
if (result === response || response.headersSent) {
return;
}
if (result === undefined) {
response.statusCode = 204;
response.end();
return;
}
const isStream =
result instanceof Readable || typeof (result && result.pipe) === 'function';
if (isStream) {
response.setHeader('Content-Type', 'application/octet-stream');
// Stream
result.pipe(response);
return;
}
switch (typeof result) {
case 'object':
case 'boolean':
case 'number':
if (Buffer.isBuffer(result)) {
// Buffer for binary data
response.setHeader('Content-Type', 'application/octet-stream');
} else {
// TODO(ritch) remove this, should be configurable
// See https://github.com/strongloop/loopback-next/issues/436
response.setHeader('Content-Type', 'application/json');
// TODO(bajtos) handle errors - JSON.stringify can throw
result = JSON.stringify(result);
}
break;
default:
response.setHeader('Content-Type', 'text/plain');
result = result.toString();
break;
}
response.end(result);
}