MobilityData / gtfs-realtime-bindings

Language bindings generated from the GTFS Realtime protocol buffer spec for popular languages.
Apache License 2.0
370 stars 127 forks source link

any support for typescript types? #99

Closed Joe-ChenZ closed 1 year ago

Joe-ChenZ commented 2 years ago

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

jameslinjl commented 1 year ago

@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);
  });
jameslinjl commented 1 year ago

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 (https://github.com/MobilityData/gtfs-realtime-bindings/pull/103)

gauthier-th commented 1 year ago

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

jameslinjl commented 1 year ago

@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?

gauthier-th commented 1 year ago

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

jameslinjl commented 1 year ago

https://github.com/MobilityData/gtfs-realtime-bindings/pull/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)

gauthier-th commented 1 year ago

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

jameslinjl commented 1 year ago

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

sambencivengo commented 1 year ago

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

jameslinjl commented 1 year ago

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

gauthier-th commented 1 year ago

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

jameslinjl commented 1 year ago

@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.

gauthier-th commented 1 year ago

Any news here?

jameslinjl commented 1 year ago

@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

jameslinjl commented 1 year ago

ETA 1/17

gauthier-th commented 1 year ago

Ok, thanks!

jameslinjl commented 1 year ago

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

jameslinjl commented 1 year ago

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

isabelle-dr commented 1 year ago

Awesome, thanks for making this happen!