stellar / js-stellar-sdk

Main Stellar client library for the JavaScript language.
https://stellar.github.io/js-stellar-sdk/
Apache License 2.0
615 stars 296 forks source link

ledger stream onMessage: returns different type than expected #972

Open acharb opened 1 month ago

acharb commented 1 month ago

Describe the bug

When streaming ledgers with typescript the sdk says to expect CollectionPage, however the value received is just the ledger record

Makes it so have to type assert to LedgeRecord in order to use the value properly

What version are you on?

@stellar/stellar-sdk@^12.0.0-rc.2"

To Reproduce

import { Horizon } from "@stellar/stellar-sdk";

const streamTest = async () => {
  const horizon = new Horizon.Server("https://horizon-testnet.stellar.org");
  horizon
    .ledgers()
    .cursor("now")
    .limit(200)
    .stream({
      onmessage: async (
        msg: Horizon.ServerApi.CollectionPage<Horizon.ServerApi.LedgerRecord>,
      ) => {
        console.log(msg.closed_at); // throws error that .closed_at does not exist
      },
    });
};

streamTest();

In order to resolve have to do:

const streamTest = async () => {
  const horizon = new Horizon.Server("https://horizon-testnet.stellar.org");
  horizon
    .ledgers()
    .cursor("now")
    .limit(200)
    .stream({
      onmessage: async (msg: any) => {
        console.log((msg as Horizon.ServerApi.LedgerRecord).closed_at);
      },
    });
};

streamTest();

Trying to declare the param as LedgerRecord like so:

msg: Horizon.ServerApi.LedgerRecord

throws an error at compile time

Expected behavior

The onMessage value type should be LedgerRecord