# Response Filter

Ts.ED response filter provide a ResponseFilter decorator to decorate a class and handle data returned by the endpoint before sending it to your consumer. The decorator take a Content-Type value to define when the class must be used to transform data to the expected result.

It's the right place to implement your own serialization logic. For example, you can define a Response filter to transform data to an XML content or wrap the data into another generic object.

With this decorator, you are able to define multiple Response Filter (but only by Content-Type). Ts.ED will choose the better Response Filter according to the Accept HTTP header on the request object, when present, or fallback to the default Response filter.

# Xml Filter

By using the appropriate Content-Type, you can define a Xml serializer as following:

    WARNING

    Don't forget to register your Response Filter by adding your class to responseFilters field on the server configuration.

    # Wrap responses

    One of the usage of the Response Filter could be to wrap all returned data into a generic object. To doing that, use the application/json Content-Type with the ResponseFilter decorator to wrap data to the expected result:

      WARNING

      The wrapper won't be documented in your generated swagger.json!

      # Handle all responses

      By using the */* Content-Type value given to the ResponseFilter you can intercept all data.

      import {ResponseFilter, Context, ResponseFilterMethods} from "@tsed/common";
      
      @ResponseFilter("*/*")
      export class AnyResponseFilter implements ResponseFilterMethods {
        transform(data: any, ctx: Context) {
          // do something
          return data;
        }
      }
      
      1
      2
      3
      4
      5
      6
      7
      8
      9

      # Pagination

      The following advanced example will show you how you can combine the different Ts.ED features to describe Pagination. The used features are the following:

        Last Updated: 3/19/2024, 7:27:06 AM

        Other topics