# Override Send response

There is the current implementation of the :

import {isBoolean, isNumber, isStream, isString} from "@tsed/core";
import {ConverterService} from "../../converters";
import {Middleware} from "../../mvc/decorators/class/middleware";
import {Req} from "../../mvc/decorators/params/request";
import {Res} from "../../mvc/decorators/params/response";
import {IMiddleware} from "../../mvc/interfaces";

/**
 * See example to override SendResponseMiddleware [here](/docs/middlewares/override/send-response.md).
 * @middleware
 */
@Middleware()
export class SendResponseMiddleware implements IMiddleware {
  constructor(protected converterService: ConverterService) {}

  public use(@Req() req: Req, @Res() response: Res) {
    const {data, endpoint} = req.ctx;
    if (data === undefined) {
      return response.send();
    }

    if (this.shouldBeStreamed(data)) {
      data.pipe(response);

      return response;
    }

    if (this.shouldBeSent(data)) {
      return response.send(data);
    }

    return response.json(this.converterService.serialize(data, {type: endpoint.type, withIgnoredProps: false}));
  }

  protected shouldBeSent(data: any) {
    return Buffer.isBuffer(data) || isBoolean(data) || isNumber(data) || isString(data) || data === null;
  }

  protected shouldBeStreamed(data: any) {
    return isStream(data);
  }
}
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

But for some reason, this implementation isn't enough to meet your needs.

With it's possible to change the default implementation like this:

import {ConverterService, OverrideProvider, Req, Res, SendResponseMiddleware} from "@tsed/common";
import {isStream} from "@tsed/core/src";

@OverrideProvider(SendResponseMiddleware)
export class MySendResponseMiddleware {
  constructor(private converterService: ConverterService) {
  }

  public use(@Req() request: Req, @Res() response: Res): any {
    const {ctx: {data, endpoint}} = request;

    if (data === undefined) {
      return response.send();
    }

    if (isStream(data)) {
      data.pipe(response);

      return response;
    }

    const payload = {
      data: this.converterService.serialize(data, {type: endpoint.type}),
      errors: []
    };

    return response.json(payload);
  }
}
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

WARNING

It's not preferable to use inheritance when you override SendResponse middleware. The use method signature may change in future and create a breaking change for your own implementation.

TIP

By default, the server import automatically your middlewares matching with this rules ${rootDir}/middlewares/**/*.ts (See componentScan configuration).

.
├── src
│   ├── controllers
│   ├── services
│   ├── middlewares
│   └── Server.ts
└── package.json
1
2
3
4
5
6
7

If not, just import your middleware in your server or edit the componentScan configuration.

import {ServerLoader, ServerSettings} from "@tsed/common";
import "./src/other/directory/MySendResponseMiddleware";

@ServerSettings({
    ...
})
export class Server extends ServerLoader {}
1
2
3
4
5
6
7