Skip to content

Commit

Permalink
V2.4.8 Add searchTrain by headsign function from SearchManager class
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexis06030631 committed May 3, 2024
1 parent cc634a6 commit 3bd14dc
Show file tree
Hide file tree
Showing 8 changed files with 320 additions and 160 deletions.
401 changes: 247 additions & 154 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sncf.js",
"version": "2.4.5",
"version": "2.4.8",
"description": "An easy to use javascript module to get all the information in real time about the trains ©SNCF (only in France)",
"scripts": {
"test": "nodemon test.js --watch test.js --watch dist",
Expand All @@ -26,11 +26,11 @@
"author": "Alexis06030631",
"license": "ISC",
"dependencies": {
"axios": "^0.26.1"
"axios": "1.6.0"
},
"devDependencies": {
"@types/node": "^20.1.0",
"nodemon": "^2.0.22",
"nodemon": "^3.1.0",
"typescript": "^5.0.4",
"vitepress": "^1.0.0-beta.3"
},
Expand Down
7 changes: 6 additions & 1 deletion src/client/Client.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {BaseClient} from './BaseClient';
import {SncfjsError, ErrorCodes} from "../errors";
import {RequestManager} from "../request";
import {PlacesManager} from "../managers";
import {PlacesManager, SearchManager} from "../managers";
import Status from "./../util/Status";
import {DisruptionManager} from "../managers";
import {LineManager} from "../managers";
Expand Down Expand Up @@ -33,6 +33,10 @@ export class Client extends BaseClient {
* PlacesManager class
*/
place: PlacesManager;
/**
* SearchManager: It is used to search for a train by headsign ID and other search queries
*/
searchManager: SearchManager;
/**
* DisruptionManager class
*/
Expand Down Expand Up @@ -77,6 +81,7 @@ export class Client extends BaseClient {
this.request = this.requestManager.request
this.status = Status.Disconnected;
this.place = new PlacesManager(this);
this.searchManager = new SearchManager(this);
this.disruption = new DisruptionManager(this);
this.line = new LineManager(this);
this.journey = new JourneyManager(this);
Expand Down
1 change: 1 addition & 0 deletions src/errors/ErrorCodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const keys: string[] = [
'NetworkError',
'InvalidId',
'NoResultFound',
'HeadSignNotFound',

// Navitia errors codes
'unable_to_parse',
Expand Down
1 change: 1 addition & 0 deletions src/errors/Messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const Messages = {
[ErrorCodes.NetworkError]: (details:any) => `The request at ${details.host} was failed with the code ${details.code}. Please verify it and try again.`,
[ErrorCodes.InvalidId]: (id:string) => `The id "${id}" is not valid. Please provide a valid id.`,
[ErrorCodes.NoResultFound]: (param:string) => `No result found for ${param}.`,
[ErrorCodes.HeadSignNotFound]: (param:string) => `The headsign "${param}" was not found. Please provide a valid headsign or check the date.`,

// Navitia errors codes
[ErrorCodes.unable_to_parse]: 'Unable to parse the data from the API. Please try again later or change the parameters.',
Expand Down
54 changes: 54 additions & 0 deletions src/managers/SearchManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import {CachedManager} from "./";
import {Vehicle} from "../structures";
import {ErrorCodes, SncfjsError} from "../errors";
import {dateToNavitiaDate} from "../util";
export class SearchManager extends CachedManager {
/**
* @internal
*/
client: any;
constructor(client:any) {
super()
Object.defineProperty(this, "client", {value: client})
}


/**
* Search a train by headsign ID
* @param headsign - The headsign of the line
* @param date - Not required, but highly recommended for effective results
*
* @example
* This example shows how to search a line by name
* ```javascript
* const vehicle = await sncf.searchManager.searchTrain('123456', date)
* console.log(vehicle)
* ```
* */
async searchTrain(headsign:string, date?:Date):Promise<Vehicle> {
return new Promise(async (resolve, reject) => {
const params:any = {headsign: headsign, count:1}
if(date && date instanceof Date) {
date.setHours(0)
date.setMinutes(0)
params.since = dateToNavitiaDate(date)
date.setDate(date.getDate()+1)
params.until = dateToNavitiaDate(date)
}
const request = await this.client.requestManager.request(`vehicle_journeys`, params)
if(request.error) {
return reject(request.error)
}else {
if(!request?.vehicle_journeys.length) return reject(new SncfjsError(ErrorCodes.HeadSignNotFound, headsign))
const vehicle:any = (request.vehicle_journeys)[0]
vehicle.disruptions.forEach((disruption:any) => {
if(request.disruptions.map((disruption:any) => disruption.id).includes(disruption.id)) {
vehicle.disruptions[vehicle.disruptions.indexOf(disruption)] = request.disruptions
.find((disruption2:any) => disruption2.id === disruption.id)
}
})
return resolve(new Vehicle(this.client, vehicle))
}
})
}
}
1 change: 1 addition & 0 deletions src/managers/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from "./CachedManager";
export * from "./DisruptionManager";
export * from "./JourneyManager";
export * from "./SearchManager";
export * from "./LineManager";
export * from "./PlaceManager";
9 changes: 7 additions & 2 deletions src/util/Converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,20 @@ export function navitiaDateToDate (date:string): Date {
* @param date - The date to transform
*/
export function dateToNavitiaDate (date:Date|string):string {
// Get date utc
let initDate = date;
// Check if the date is a string date or a Date object
if (typeof date === 'string') date = new Date(date);
if (!(date instanceof Date) || isNaN(date.getTime())) {
throw new SncfjsError(ErrorCodes.InvalidDate, initDate);
}
const options:any = {
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
};
const dateLocale:string = date.toLocaleDateString('fr-FR').replace(/\//g, '')
return dateLocale.substring(4, 8) + dateLocale.substring(2, 4) + dateLocale.substring(0, 2) + 'T' + date.toLocaleTimeString('fr-FR').replace(/\//g, '')
const timeLocale:string = date.toLocaleTimeString('fr-FR', options)
return dateLocale.substring(4, 8) + dateLocale.substring(2, 4) + dateLocale.substring(0, 2) + 'T' + timeLocale.replace(/:/g, '')
}

/**
Expand Down

0 comments on commit 3bd14dc

Please sign in to comment.