pozil / pub-sub-api-node-client

A node client for the Salesforce Pub/Sub API
Creative Commons Zero v1.0 Universal
70 stars 37 forks source link

Add logic to simplify Avro decoding and field access #9

Closed pozil closed 1 year ago

pozil commented 1 year ago

Event payloads looks like this after being decoded by Avro:

{
    Name: { string : "My name" },
    LastModifiedDate: { long: 1675168622000 }
}

It would be nice to strip the intermediate type properties and produce something like this:

{
    Name: "My name",
    LastModifiedDate: 1675168622000
}
soileaud commented 1 year ago

This was an issue for us, and I came up with this solution. I'm curious what others are doing. This worked for Platform Events, haven't tested change data capture since we don't use it.

Object.entries(payload).forEach(([key, value]) => {
    if (value && typeof value === 'object') {
        payload[key] = value[Object.keys(value)[0])
    }
pozil commented 1 year ago

Thanks for the lead @soileaud. The implementation turned out to be a bit more complex because of compound types but it's now part of v3.0.0.