Class serializer
@node-ts/bus-class-serializer leverages class transformer to provide serialization/deserialization to class instances. This means that class based properties of messages like javascript Dates can be consumed directly without having to do additional custom deserialization in each handler.
Installation
Install @node-ts/bus-class-serializer and its dependencies
npm install @node-ts/bus-class-serializer reflect-metadata class-transformer
Configure bus to use the serializer
import { Bus } from '@node-ts/bus-core'
import { ClassSerializer } from '@node-ts/bus-class-serializer'
Bus
.configure()
.withSerializer(new ClassSerializer())
.initialize()
Strongly typed messages
Once the @node-ts/bus-class-serializer has been installed and configured, you can consume class transformer notation for object properties of your messages.
For example
import { Command } from '@node-ts/bus-messages'
import { Type } from 'class-transformer'
class Update extends Command {
// Provide a @Type hint so that class-transformer can deserialize this at runtime
@Type(() => Date) readonly date: Date
constructor (
date: Date
) {
this.date = date
}
}
Last updated
Was this helpful?