0.1.0 • Published 4 years ago

trafikverket-nodejs v0.1.0

Weekly downloads
-
License
MIT
Repository
gitlab
Last release
4 years ago

@openrailse/trafikverket

npm (scoped with tag) NPM Libraries.io dependency status for specific release, scoped npm package

API wrapper for Trafikverket Öppet API writen in TypeScript using built in Node.JS modules where possible to minimise dependencies. This package also aims to implement all available schema versions with correct typings so that future changes to the API wont break existing code.

Table of Contents

Available Implementations

Currently this package only implements the following Trafikverket Öppet API endpoints:

  • Railroad Infomation
    • TrainAnnouncement Provides timetable information corresponding to specific trains
    • TrainMessage Traffic announcements including rail works and rail faults
    • TrainStation Information about stations

If you require any of the other implementations, raise an issue to have it implemented or raise an issue and PR with the implementation. To see a full list of what is available to be implemnted see the Trafikverket Öppet API - API Model page.

Instalation

To install this package using npm run:

npm install @openrailse/trafikverket

or if yarn is more your thing:

yarn add @openrailse/trafikverket

Usage

There are a couple of different options when using this package including Single shot and streaming. All the examples below will show both TypeScript usage and JavaScript usage.

Single shot

This method requests data once and that is the end of the flow, you will receive an array of data containing the query result and can continue with the rest of your application. The example below shows usign the TrainAnnouncement schema 1.5 api to find all announcements at the Lund (Lu) station.

TypeScript:

import {
  TrafikverketAPI,
  TrainAnnoucementQuery,
  TrainAnnoucement,
  Filter,
  DataField
} from '@openrailse/trafikverket';

const trafikverket: TrafikverketAPI = new TrafikverketAPI('someSuperSecretPrivateAPIKey');

const query: TrainAnnoucementQuery<'1.5'> = new TrainAnnoucementQuery<'1.5'>('1.5')
  .filter(Filter.equal(DataField.LocationSignature, 'Lu'))
  .once('message', (messageData: TrainAnnouncement<'1.5'>[]): void => {
    console.log('messagedata', messageData);
  })
  .once('error', (err: Error): void => {
    console.error(err);
  })
  .request(trafikverket);

JavaScript:

import {
  TrafikverketAPI,
  TrainAnnoucementQuery,
  Filter,
  DataField
} from '@openrailse/trafikverket';

const trafikverket = new TrafikverketAPI('someSuperSecretPrivateAPIKey');

const query = new TrainAnnoucementQuery('1.5')
  .filter(Filter.equal(DataField.LocationSignature, 'Lu'))
  .once('message', (messageData) => {
    console.log('messagedata', messageData);
  })
  .once('error', (err) => {
    console.error(err);
  })
  .request(trafikverket);

Streaming

This methoid works very similar to single shot except will continue to publish messages after the first query is complete. This is done by enabling Server Side Events by passing in a true value as a second parameter onto the query class and using .on() instead of .once():

TypeScript:

import {
  TrafikverketAPI,
  TrainAnnoucementQuery,
  TrainAnnoucement,
  Filter,
  DataField
} from '@openrailse/trafikverket';

const trafikverket: TrafikverketAPI = new TrafikverketAPI('someSuperSecretPrivateAPIKey');

const query: TrainAnnoucementQuery<'1.5'> = new TrainAnnoucementQuery<'1.5'>('1.5', true)
  .filter(Filter.equal(DataField.LocationSignature, 'Lu'))
  .on('message', (messageData: TrainAnnouncement<'1.5'>[]): void => {
    console.log('messagedata', messageData);
  })
  .on('close', (): void => {
    console.warn('Connection was closed. re-run the query again');
  })
  .on('error', (err: Error): void => {
    console.error(err);
  })
  .request(trafikverket);

JavaScript:

import {
  TrafikverketAPI,
  TrainAnnoucementQuery,
  Filter,
  DataField
} from '@openrailse/trafikverket';

const trafikverket = new TrafikverketAPI('someSuperSecretPrivateAPIKey');

const query = new TrainAnnoucementQuery('1.5', true)
  .filter(Filter.equal(DataField.LocationSignature, 'Lu'))
  .on('message', (messageData) => {
    console.log('messagedata', messageData);
  })
  .on('close', () => {
    console.warn('Connection was closed. re-run the query again');
  })
  .on('error', (err) => {
    console.error(err);
  })
  .request(trafikverket);