npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-stib

v1.0.1

Published

A set of Node.js helpers to fetch the Brussels Public Transport APIs (STIB)

Downloads

2

Readme

Node-STIB

A set of Node.js helpers to fetch the Brussels Public Transport APIs (STIB).

1. Installation

npm i node-stib

You'll need valid STIB API credentials to use this library. For more information, check out the official developers page.

Please note that this library is NOT an official project from STIB and it is maintained voluntarily under MIT license.

2. Examples

2.1. Basic authentication

Authenticate with consumer key & consumer secret

The most straightforward way to authenticate is using a pair of consumer key and consumer secret. This ensures you to always have valid connection to the API. This will generate an expirable token for further communications with the API.

const { Stib } = require('node-stib')

async const fetchMetroLine5Description = () => {
  const stib = new Stib()
  const token = await stib.getToken('consumerKey', 'consumerSecret')

  console.log(token)

  // Once authenticated you can use the API
  return await stib.getLineDescription([5])
}

Authenticate with a token

Although not reccomended you can also directly connect to the API using a pre-generated token.

const { Stib } = require('node-stib')

async const fetchMetroLine5Description = () => {
  const stib = new Stib('tokentokentoken')

  return await stib.getLineDescription([5])
}

2.2. Get all stops coordinates for a given line

const { Stib } = require('node-stib')

async const fetchStopCoordinates = () => {
  const stib = new Stib()
  await stib.getToken('consumerKey', 'consumerSecret')

  const lines = await stib.getLineDescription([61, 92, 93])
  const points = lines
                 .map(line => [ ...line.points ])
                 .flat()
                 .map(point => point.id)

  const stopsCoordinates = await stib.getStopDescription(points)

  return stopsCoordinates
}

2.3. Count all moving vehicles

const { Stib } = require('node-stib')

async const countAllMovingVehicles = () => {
  const stib = new Stib()
  await stib.getToken('consumerKey', 'consumerSecret')

  const lines = []
  for(let i=1;i<100;i++) lines.push(i)

  const movingVehicles = await stib.getVehiclePosition(lines)

  return movingVehicles
         .reduce((ac, cv) => ac += cv.vehiclePositions.length, 0)
}

3. API documentation

3.1. constructor()

new Stib(?token) initialize a new Stib object.

Arguments:

  • token (string) optional : A temporary token, if no token is provided you should generate one using the .getToken method

Usage:

const { Stib } = require("node-stib");

const stib = new Stib("tokentokentoken");

3.2. getToken()

await getToken(consumerKey, consumerSecret) try to create a new temporary token, if a valid token has already been generated in the past hour returns the active valid token.

Arguments:

  • consumerKey (string) : Your personal consumer key
  • consumerSecret (string) : Your personal consumer secret

Returns a promise containing the an object with an access token and its expiration time.

3.3. getLineDescription()

await getLineDescription(lines) get the description for a set of given lines.

Arguments :

  • lines (array) : An array containing line numbers

Returns a promise containing an array of line descriptions

3.4. getStopDescription()

await getStopDescription(stops) get the description for a set of given stop ids.

Arguments :

  • stops (array) : An array of stop ids

Returns a promise containing an array of stop descriptions.

3.5. getVehiclePosition()

await getVehiclePosition(lines) get the realtime positions of vehicle for a set of given lines

Arguments :

  • lines (array) : An array containing line numbers

Returns a promise containing an array of realtime positions per line

3.6. getMessageByLine()

await getMessageByLine(lines) get a series of maintenance messages for a given set of lines

Arguments :

  • lines (array) : An array containing line numbers

Returns a promise containing an array of maintenance message per line

3.7. getWaitingTime()

await getWaitingTime(stops) get all incoming vehicles for a list of given stops.

Arguments :

  • stops (array) : An array of stop ids

Returns a promise containing an array of waiting times for each line of the given stops

4. Rate Limiting

This library is aware of STIB API's rate limitation and has some builtin helpers to help you prevent hitting the limit too fast.

  • Queries are dispatched so you can query up to 200 resources (lines or points) in a single call.
  • An array containing multiple instance of the same resource will only be fetched once.