MCPcopy
hub / github.com/nestjs/nest / reply

Method reply

packages/platform-express/adapters/express-adapter.ts:103–132  ·  view source on GitHub ↗
(response: any, body: any, statusCode?: number)

Source from the content-addressed store, hash-verified

101 }
102
103 public reply(response: any, body: any, statusCode?: number) {
104 if (statusCode) {
105 response.status(statusCode);
106 }
107 if (isNil(body)) {
108 return response.send();
109 }
110 if (body instanceof StreamableFile) {
111 this.applyStreamHeaders(response, body);
112 const stream = body.getStream();
113 stream.once('error', err => {
114 body.errorHandler(err, response);
115 });
116 return stream
117 .pipe<Writable>(response)
118 .on('error', (err: Error) => body.errorLogger(err));
119 }
120 const responseContentType = response.getHeader('Content-Type');
121 if (
122 typeof responseContentType === 'string' &&
123 !responseContentType.startsWith('application/json') &&
124 body?.statusCode >= HttpStatus.BAD_REQUEST
125 ) {
126 this.logger.warn(
127 "Content-Type doesn't match Reply body, you might need a custom ExceptionFilter for non-JSON responses",
128 );
129 response.setHeader('Content-Type', 'application/json');
130 }
131 return isObject(body) ? response.json(body) : response.send(String(body));
132 }
133
134 public status(response: any, statusCode: number) {
135 return response.status(statusCode);

Callers

nothing calls this directly

Calls 13

applyStreamHeadersMethod · 0.95
isNilFunction · 0.90
isObjectFunction · 0.90
getStreamMethod · 0.80
errorLoggerMethod · 0.80
statusMethod · 0.65
onMethod · 0.65
warnMethod · 0.65
setHeaderMethod · 0.65
sendMethod · 0.45
errorHandlerMethod · 0.45
pipeMethod · 0.45

Tested by

no test coverage detected