-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
V2.4.8 Add searchTrain by headsign function from SearchManager class
- Loading branch information
1 parent
cc634a6
commit 3bd14dc
Showing
8 changed files
with
320 additions
and
160 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters