# 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;
}
}
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:
- Generics
- Function programming to declare models
- For decorator to declare a custom model for JsonSchema, OS2 or OS3.
- Response Filter to manage paginated response.
Last Updated: 10/24/2024, 6:33:40 AM
Other topics
- Session & cookies
- Passport.js
- Keycloak
- Prisma
- TypeORM
- MikroORM
- Mongoose
- GraphQL
- GraphQL WS
- Apollo
- TypeGraphQL
- GraphQL Nexus
- Socket.io
- Swagger
- AJV
- Multer
- Serve static files
- Templating
- Serverless HTTP
- Seq
- OIDC
- Stripe
- Agenda
- Terminus
- Serverless
- Server-sent events
- IORedis
- Vike
- Jest
- Vitest
- Controllers
- Providers
- Model
- JsonMapper
- Middlewares
- Pipes
- Interceptors
- Authentication
- Hooks
- Exceptions
- Throw HTTP Exceptions
- Cache
- Command
- Response Filter
- Injection scopes
- Custom providers
- Lazy-loading provider
- Custom endpoint decorator
- Testing
- Customize 404