MobilityData/gtfs-realtime-bindings

any support for typescript types?

Closed this issue ยท 19 comments

It seems like i can't access the subfields of feed.entity.

request(requestSettings, (error: any, response: { statusCode: number; }, body: any) => {
  if (!error && response.statusCode == 200) {
    var feed = GtfsRealtimeBindings.transit_realtime.FeedMessage.decode(body);
    feed.entity.forEach((entity: any) => {
      if (entity.TripUpdate) {
        console.log(entity.TripUpdate);
      }
    //   console.log(entity.trip_update);
    //   console.log(entity);
    
    });
  }
});

Apparently
if (entity.TripUpdate) never passes because entity is of type any, but it is of a specific realtime transit type, so i need to install it first.

wouldn't work either when i do

feed.entity.forEach((entity: { TripUpdate: any; }) => {
      if (entity.TripUpdate) {
        console.log(entity.TripUpdate);
      }

entity.TripUpdate is printed as undefined

@Joe-ChenZ I think you may be running into issues because of the case of entity.TripUpdate vs entity.tripUpdate (https://github.com/MobilityData/gtfs-realtime-bindings/blob/master/nodejs/gtfs-realtime.js#L589-L595)

I was able to pull from the (NY) MTA's API with the following:

const GtfsRealtimeBindings = require("gtfs-realtime-bindings");
const https = require("https");

https
  .get(
    "https://api-endpoint.mta.info/Dataservice/mtagtfsfeeds/nyct%2Fgtfs-ace",
    {
      headers: {
        "x-api-key": "<redacted>",
      },
    },
    (resp) => {
      const buffers = [];

      // A chunk of data has been received.
      resp.on("data", (chunk) => {
        buffers.push(chunk);
      });

      // The whole response has been received.
      resp.on("end", () => {
        const data = Buffer.concat(buffers);
        const feed =
          GtfsRealtimeBindings.transit_realtime.FeedMessage.decode(data);
        feed.entity.forEach(function (entity) {
          if (entity.tripUpdate) {
            console.log(JSON.stringify(entity.tripUpdate));
          }
        });
      });
    }
  )
  .on("error", (err) => {
    console.log("Error: " + err.message);
  });

also, it should be pretty straight-forward to add TS types. i can help to do that soon once i (hopefully) get my first improvements for the JS bindings merged (#103)

I'm also interested in TS types and can also help

@gauthier-th nice! i already have a local branch which adds the TS types, so will probably put that up once #103 is merged (the approach is fairly trivial, just using pbts to generate a .d.ts file and then specifying that file as types in the package.json). if you have feedback on that approach, please let me know!

perhaps something helpful here that you might be able to help with is writing a test / example(s) using TS?

yep, i did the same on my side too.
sure! i could write this.

#105 is up now! @gauthier-th feel free to either propose changes onto that PR (or you can put up your own PR of tests / example(s) after this one is merged)

I think we should already solve #104 and #105 before adding more documentation / examples for TS

@isabelle-dr once you publish the newest node package version to NPM, you should be able to close this issue!

@jameslinjl Any idea when the latest package will be published to NPM?

@sambencivengo - that's in MobilityData folks' hands - poke @isabelle-dr :) hopefully sometime this week?

Hi @isabelle-dr ! Do you have any news on this?

@gauthier-th I'll check in with Isabelle in the new year. I wouldn't expect much motion here until the holidays are behind us, since most work stuff in North America comes to a standstill between Christmas and New Years.

Any news here?

@gauthier-th yeah, i chatted with isabelle. she's on vacation and then heading to a transportation conference over the next couple weeks. once she's back, she said she'll get all this published

ETA 1/17

Ok, thanks!

@sambencivengo @gauthier-th NPM packages have been published! I have QA'ed 1.1.1 and it lgtm

@isabelle-dr If we don't hear any complaints over a couple days, then we can close out this issue

Awesome, thanks for making this happen!