New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

amtrak

Package Overview
Dependencies
Maintainers
1
Versions
51
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

amtrak - npm Package Compare versions

Comparing version 3.0.1 to 3.0.2

test/index.ts

12

dist/index.d.ts

@@ -1,7 +0,7 @@

import { Train, StationMeta, TrainResponse, StationResponse } from "./types";
declare const fetchTrain: (trainId: string) => Promise<Train>;
declare const fetchAllTrains: () => Promise<TrainResponse>;
declare const fetchStation: (stationId: string) => Promise<StationMeta>;
declare const fetchAllStations: () => Promise<StationResponse>;
export { fetchTrain, fetchAllTrains, fetchStation, fetchAllStations };
declare const fetchTrain: (trainId: string) => Promise<import("axios").AxiosResponse<any, any>>;
declare const fetchAllTrains: () => Promise<import("axios").AxiosResponse<any, any>>;
declare const fetchStation: (stationId: string) => Promise<import("axios").AxiosResponse<any, any>>;
declare const fetchAllStations: () => Promise<import("axios").AxiosResponse<any, any>>;
declare const fetchStaleStatus: () => Promise<import("axios").AxiosResponse<any, any>>;
export { fetchTrain, fetchAllTrains, fetchStation, fetchAllStations, fetchStaleStatus, };
//# sourceMappingURL=index.d.ts.map
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.fetchAllStations = exports.fetchStation = exports.fetchAllTrains = exports.fetchTrain = void 0;
const node_fetch_1 = require("node-fetch");
exports.fetchStaleStatus = exports.fetchAllStations = exports.fetchStation = exports.fetchAllTrains = exports.fetchTrain = void 0;
const axios_1 = require("axios");
const fetchTrain = async (trainId) => {
const res = await (0, node_fetch_1.default)(`https://api-v3.amtraker.com/v3/trains/${trainId}`);
const data = await res.json();
return data;
return axios_1.default.get(`https://api-v3.amtraker.com/v3/trains/${trainId}`);
};
exports.fetchTrain = fetchTrain;
const fetchAllTrains = async () => {
const res = await (0, node_fetch_1.default)("https://api-v3.amtraker.com/v3/trains");
const data = await res.json();
return data;
return axios_1.default.get("https://api-v3.amtraker.com/v3/trains");
};
exports.fetchAllTrains = fetchAllTrains;
const fetchStation = async (stationId) => {
const res = await (0, node_fetch_1.default)(`https://api-v3.amtraker.com/v3/stations/${stationId}`);
const data = await res.json();
return data;
return axios_1.default.get(`https://api-v3.amtraker.com/v3/stations/${stationId}`);
};
exports.fetchStation = fetchStation;
const fetchAllStations = async () => {
const res = await (0, node_fetch_1.default)("https://api-v3.amtraker.com/v3/stations");
const data = await res.json();
return data;
return axios_1.default.get("https://api-v3.amtraker.com/v3/stations");
};
exports.fetchAllStations = fetchAllStations;
const fetchStaleStatus = async () => {
return axios_1.default.get("https://api-v3.amtraker.com/v3/stale");
};
exports.fetchStaleStatus = fetchStaleStatus;
//# sourceMappingURL=index.js.map

@@ -27,2 +27,3 @@ "use strict";

})(StationStatus = exports.StationStatus || (exports.StationStatus = {}));
;
//# sourceMappingURL=types.js.map
{
"name": "amtrak",
"version": "3.0.1",
"version": "3.0.2",
"description": "A simple and easy way to parse data from Amtrak's train tracking API.",

@@ -33,5 +33,5 @@ "main": "dist/index.js",

"dependencies": {
"node-fetch": "^3.3.0",
"axios": "^1.3.3",
"nodemon": "^2.0.20"
}
}

@@ -6,12 +6,21 @@ # Amtrak.js

## Disclaimer
This library and its creator have no relation to Amtrak. Amtrak and the Amtrak Logo are trademarks of The National Railroad Passenger Corporation (NRPC). The API endpoint used is originally intended for with [Amtrak's Train Tracking map](https://www.amtrak.com/track-your-train.html).
This library and its creator have no relation to Amtrak. Amtrak and the Amtrak
Logo are trademarks of The National Railroad Passenger Corporation (NRPC). The
API endpoint used is originally intended for with
[Amtrak's Train Tracking map](https://www.amtrak.com/track-your-train.html).
## Note
Any version below 3.0.0 **WILL NO LONGER WORK** due to:
- Amtrak blocking all requests from any Node.js REST client, or at least the ones that I have tried.
- Amtrak blocking all requests from any Node.js REST client, or at least the
ones that I have tried.
- The deprecation of the Amtraker v1 and v2 APIs.
While v3 code syntax is the same, the resulting types are different, please check the [docs](#docs) section for more information.
While v3 code syntax is the same, the resulting types are different, please
check the [docs](#docs) section for more information.
## Installation
Using NPM:

@@ -29,64 +38,62 @@

## Docs
## Functions
## TS Examples
As this library was written in TypeScript, it is naturally easy to use it:
### Fetching All Data
```ts
import { fetchTrainData } from 'amtrak';
Amtrak.js has a pretty basic schema with 4 different functions:
//fetches data, cleans it, and then prints to terminal
fetchTrainData().then((trainData) => {
console.dir(trainData, { depth: null })
})
```
### Fetching All Trains
```ts
import { fetchAllTrains } from 'amtrak';
- `fetchAllTrains()`: Fetches all active Amtrak trains.
- `fetchTrain(trainId: string)`: Fetches a train by its number or ID.
- `fetchAllStations()`: Fetches metadata for all Amtrak stations.
- `fetchStation(stationId: string)`: Fetches metadata for a station by its ID.
//fetches all trains and then prints to terminal
fetchAllTrains().then((trainData) => {
console.dir(trainData, { depth: null })
})
```
### Fetching Single Trains
```ts
import { fetchTrain } from 'amtrak';
### `fetchAllTrains()`
//fetches all trains with the number 20 (crescent) and then prints to terminal
fetchTrain(20).then((trainData) => {
console.dir(trainData, { depth: null })
})
```
### Fetching All Stations
```ts
import { fetchAllStations } from 'amtrak';
- Fetches all active Amtrak trains.
- Returns `Promise<TrainResponse>` where each key is a train number and the
value is a list of `Train` objects.
//fetches all stations and then prints to terminal
fetchAllStations().then((stationData) => {
console.dir(stationData, { depth: null })
})
```
### Fetching Single Stations
#### Example
```ts
import { fetchStation } from 'amtrak';
const { fetchAllTrains } = require("amtrak"); // CommonJS
import { fetchAllTrains } from "amtrak"; // ES6
//fetches all station data for CHI (chicago) and then prints to terminal
fetchStation('CHI').then((stationData) => {
console.dir(stationData, { depth: null })
})
```
## JS Example
And of course, as the TS is compliled to JS, you can use that as well. The only real difference between the two is how you (generally) import, so everything else is the same. The only difference here is that you can import *from* in ts, but you require the whole module is js.
```js
const amtrak = require("amtrak");
amtrak.fetchTrainData().then((trainData) => {
console.dir(trainData, { depth: null })
// JS
fetchAllTrains().then((trains) => {
console.log(trains);
});
// TS
```
(wait they're like the same lmao)
### `fetchTrain(trainId: string)`
## Contributing
I don't currently have any official contributing templates, but please make sure to add some testing code to `testing/src/testAll.ts`. Anything you add should take one of the existing types and return a modified version of it or a new type which you define in `src/types/types.ts`.
- Fetches a train by its number or ID.
- Returns `Promise<TrainResponse>` with a single key (the train number) and the
value is a list of `Train` objects.
- If a valid Train ID is provided, the value will be a list of length 1.
- If the train number/ID is not found, the promise will resolve with an empty
array.
- A train ID is comprised of the train number and the day of the month the train
originated.
- For example, a California Zephyr train (train #5) that originated on
02/09/2023 would have an ID of `5-9`;
### `fetchAllStations()`
- Fetches metadata for all Amtrak stations.
- Returns `Promise<StationResponse>` where each key is a station ID and the
value is a `StationMeta` object.
### `fetchStation(stationId: string)`
- Fetches metadata for a station by its ID.
- Returns `Promise<StationResponse>` with a single key (the station ID) and the
value is a `StationMeta` object.
- If the station ID is not found, the promise will resolve with an empty object.
### `fetchStaleStatus()`
- Fetches info on the state of the Amtraker API.
- Returns `Promise<StaleStatusResponse>`.

@@ -1,30 +0,30 @@

import { Train, StationMeta, TrainResponse, StationResponse } from "./types";
import fetch from "node-fetch";
import * as AmtrakerTypes from "./types";
import axios from "axios";
const fetchTrain = async (trainId: string) => {
const res = await fetch(`https://api-v3.amtraker.com/v3/trains/${trainId}`);
const data = await res.json();
return data as Train;
return axios.get(`https://api-v3.amtraker.com/v3/trains/${trainId}`);
};
const fetchAllTrains = async () => {
const res = await fetch("https://api-v3.amtraker.com/v3/trains");
const data = await res.json();
return data as TrainResponse;
return axios.get("https://api-v3.amtraker.com/v3/trains");
};
const fetchStation = async (stationId: string) => {
const res = await fetch(
`https://api-v3.amtraker.com/v3/stations/${stationId}`
);
const data = await res.json();
return data as StationMeta;
return axios.get(`https://api-v3.amtraker.com/v3/stations/${stationId}`);
};
const fetchAllStations = async () => {
const res = await fetch("https://api-v3.amtraker.com/v3/stations");
const data = await res.json();
return data as StationResponse;
return axios.get("https://api-v3.amtraker.com/v3/stations");
};
export { fetchTrain, fetchAllTrains, fetchStation, fetchAllStations };
const fetchStaleStatus = async () => {
return axios.get("https://api-v3.amtraker.com/v3/stale");
};
export {
fetchTrain,
fetchAllTrains,
fetchStation,
fetchAllStations,
fetchStaleStatus,
};

@@ -91,2 +91,2 @@ export interface Train {

stale: boolean;
}
};

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc