0.1.0 • Published 9 months ago

api-railway v0.1.0

Weekly downloads
-
License
ISC
Repository
github
Last release
9 months ago

RailwayAPI NodeJS client

This is an official NPM package that wraps railwayapi API for NodeJS with first class Typescript support.

An example project using this package can be found on Railway Frontend Demo

Example 1 Example 1

Installation

Install this package using npm

npm install api-railway

Getting started

Import the module and create a new client. You can pass different base URL, API version, protocol if you want to use different server.

import Client from 'api-railway'

const client1 = new Client()

const client2 = new Client({
  BASE_URL: 'example.com',
  API_VRSION: 'v1',
  DEFAULT_PROTOCOL: 'https',
})

Every REST method returns a Promise, making this library async await ready. Following examples will use the await form.

Each REST endpoint returns an object with type ApiReturnType

type ApiRetrunType<T extends ApiRetrunDataType, U extends ErrorObj = ErrorObj, V = {}> = {
  httpStatusCode: number
  httpStatusText: string
  responseType: ResponseType
  ok: boolean
  url: string
  responseHeaders: Headers
  data: T[] | undefined
  errors: U[] | undefined
  raw: any
  extra?: V
}

where ApiRetrunDataType is defined as

type ApiRetrunDataType =
  | ScheduleInfo
  | ScheduleRow
  | State
  | StationGeneralInfo
  | StationInfo
  | TrainGeneralInfo
  | TrainInfo
  | TrainsBtwStationsType
  | TrainType
  | Zone

and ErrorObj is defined as

type ErrorObj = {
  httpCode: number
  type: string
  code: string
  title: string
  description: string
  href: string
  path: string
  [key: string]: any
}

Table of Contents

Init

ParamTypeRequiredDefaultInfo
API_VERSIONStringfalsev1API version to use
API_KEYStringfalse
BASE_URLStringfalseapi.railwayapi.site/api
API_TIMEOUTNumberfalse15000
PROTOCOL'http' or 'https'false'https'

Public REST Endpoints

Trains

Get specific train info

ParamTypeRequiredDefaultInfo
trainNumberStringtrueTrain number of train to be searched

Return type

ApiRetrunType<TrainInfo, ErrorObj>

Where TrainInfo is

type TrainInfo = {
  id: number
  trainNumber: string
  trainName: string
  trainFullName: string
  stationFrom: { stationCode: string; id: number; stationName: string }
  stationTo: { stationCode: string; id: number; stationName: string }
  departureTime: string
  arrivalTime: string
  duration: string
  trainRunsOn: TrainRunsOnType
  numberOfStops: number
  trainTypeCode: string
  distance: number
  availableClasses: string[]
  avgSpeed: number
  hasPantry: boolean
  returnTrainNumber: string
  updatedAt: string
}

Example:

const response = await client.trains.getTrains('12279')

Get list of train

ParamTypeRequiredDefaultInfo
qStringtrueQuery
limitNumberfalse10Number of train to be returned

Return type

ApiRetrunType<TrainGeneralInfo, ErrorObj>

Where TrainGeneralInfo is

export type TrainGeneralInfo = {
  id: number
  trainNumber: string
  trainName: string
}

Example:

const response1 = await client.trains.getTrainsGeneral('122')
const response2 = await client.trains.getTrainsGeneral('122', 20)

Schedule

ParamTypeRequiredDefaultInfo
trainNumberStringtrueTrain number of train to be searched

Return type

ApiRetrunType<ScheduleInfo, ErrorObj>

Where ScheduleInfo is

type ScheduleRow = {
  srNo: string
  stationId: number
  stationName: string
  stationCode: string
  arrivalTime: number | null
  departureTime: string | null
  distance: string
  haltTime: string | null
  dayCount: number
  platform: string | null
  boardingDisabled: boolean
  speed: string
}

Example:

const response = await client.schedules.getSchedules('12279')

Stations

Get station

ParamTypeRequiredDefaultInfo
stationCodeStringtrueStation code of station to be searched

Return type

ApiRetrunType<StationInfo, ErrorObj>

Where StationInfo is

type StationInfo = {
  id: number
  stationCode: string
  stationName: string
  stateName: string
  stationType: string
  numberOfPlatforms: number
  hindiStationName: string
  zones: { zoneName: string; zoneCode: string }
  latitude: string
  longitude: string
  updatedAt: string
}

Example:

const response = await client.stations.getStations('NZM')

Get station list

ParamTypeRequiredDefaultInfo
qStringtrueQuery
limitNumberfalse10

Return type

ApiRetrunType<StationGeneralInfo, ErrorObj>

Where StationGeneralInfo is

type StationGeneralInfo = {
  id: number
  stationCode: string
  stationName: string
}

Example:

const response = await client.stations.getStationsGeneral('N')

Trains-between-stations

ParamTypeRequiredDefaultInfo
fromStationCodeStringtrueFrom station code
toStationCodeStringtrueTo station code
dateStringfalse

Return type

ApiRetrunType<TrainsBtwStationsType, ErrorObj, TrainsBtwStationsExtraType>

Where TrainsBtwStationsType, TrainsBtwStationsExtraType, and StationInfo is

type TrainsBtwStationsType = {
  id: number
  trainNumber: string
  trainName: string
  trainFullName: string
  trainRunsOn: TrainRunsOnType
  availableClasses: string[]
  hasPantry: boolean
  trainType: string
  returnTrainNumber: string
  stationFrom: StationStop
  stationTo: StationStop
  updatedAt: string
  distance: number
  duration: string
}

type TrainsBtwStationsExtraType = {
  stationsInfo: StationInfo[]
}

type StationInfo = {
  id: number
  stationCode: string
  stationName: string
  stateName: string
  zoneCode: string
  stationType: string
  hindiStationName: string
}

Example:

const response = await client.trainsBtwStations.getTrainsBtweenStations('NZM', 'GWL')
0.1.0

9 months ago

0.0.11

9 months ago

0.0.10

9 months ago

0.0.9

9 months ago

0.0.8

9 months ago

0.0.7

9 months ago

0.0.6

9 months ago

0.0.5

9 months ago

0.0.4

9 months ago

0.0.3

10 months ago

0.0.2

10 months ago

0.0.1

10 months ago