New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@node-ts/bus-class-serializer

Package Overview
Dependencies
Maintainers
1
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@node-ts/bus-class-serializer

A JSON serializer for @node-ts/bus based on class-transformer for strong marshalling of messages

  • 1.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

@node-ts/bus-class-serializer

A JSON-based serializer for @node-ts/bus that deserializes into strong types. This is a preferred alternative to the default serializer that does not support this.

Installation

Install this package and its dependencies

npm i reflect-metadata class-transformer @node-ts/bus-class-serializer

Configure your bus to use the serializer:

import { ClassSerializer } from '@node-ts/bus-class-serializer'

await Bus.configure().withSerializer(new ClassSerializer()).initialize()

Note This package relies on class transformer that requires reflect-metadata to be installed and called at the start of your application before any other imports. Please follow their guides on how to configure your app and contracts to serialize correctly.

Why?

Consider the following message:

class Update extends Command {
  constructor(readonly date: Date) {}
}

When serialized/deserialized this will become a plain object with no date functions on the property:

const receivedMessage = JSON.parse(JSON.stringify(new Update(new Date())))
receivedMessage.getDate() // Error - getDate is undefined

Although we could manually fix and assign the date in the handle, this is tedious and adds unecessary code. Instead messages should be holistically deserialized to strong type at the boundaries of the system.

Using this serializer, a minor change to the contract is made:

class Update extends Command {
  @Type(() => Date) readonly date: Date
  constructor(date: Date) {
    this.date = date
  }
}

When this is deserialized, the date should be an actual Date:

const serializer = new ClassSerializer()
const receivedMessage = serializer.deserialize(
  serializer.serialize(new Update(new Date())),
  Update
)
receivedMessage.getDate() // Success - a date value

Keywords

FAQs

Package last updated on 06 Feb 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc