Skip to content

Commit d4e31e1

Browse files
committed
feat(DepInjection, and Places): Move to dep injection to allow for scoped access to the API along with pulling stop details from a Go Transit Stop
1 parent 952271e commit d4e31e1

9 files changed

Lines changed: 199 additions & 41 deletions

File tree

package-lock.json

Lines changed: 12 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"class-validator": "^0.14.0",
4444
"got": "^12.6.0",
4545
"icloudjs": "^1.4.0",
46-
"reflect-metadata": "^0.1.13"
46+
"reflect-metadata": "^0.1.13",
47+
"typedi": "^0.10.0"
4748
}
4849
}

src/Modules/GoTransit/Stop.ts

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/Modules/GoTransit/Stop/Place.ts

Whitespace-only changes.
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
// src/Modules/GoTransit/Stop/StopDetails.ts
2+
3+
import { Type } from 'class-transformer';
4+
import { IsString } from 'class-validator';
5+
import { Service } from 'typedi';
6+
7+
@Service()
8+
export class GoTransitPlaceStop {
9+
@Type(() => String)
10+
public Code: string;
11+
12+
@Type(() => String)
13+
public Name: string;
14+
15+
@Type(() => String)
16+
public NameFr: string;
17+
}
18+
19+
@Service()
20+
export class GoTransitPlaceStops {
21+
@Type(() => GoTransitPlaceStop)
22+
public Stop?: GoTransitPlaceStop[];
23+
}
24+
25+
@Service()
26+
export class GoTransitPlace {
27+
@Type(() => String)
28+
public Code: string;
29+
30+
@Type(() => String)
31+
public Name: string;
32+
33+
@Type(() => Number)
34+
public Longitude: number;
35+
36+
@Type(() => Number)
37+
public Latitude: number;
38+
39+
@Type(() => Number)
40+
public Radius: number;
41+
42+
@Type(() => GoTransitPlaceStops)
43+
public Stops?: GoTransitPlaceStops;
44+
}
45+
46+
export class GoTransitStopFacilities {
47+
@Type(() => String)
48+
@IsString()
49+
/**
50+
* TODO Figure these codes out to an ENUM
51+
*/
52+
public Code: string;
53+
54+
@Type(() => String)
55+
@IsString()
56+
public Description: string;
57+
58+
@Type(() => String)
59+
@IsString()
60+
public DescriptionFr: string;
61+
}
62+
63+
@Service()
64+
export class GoTransitStopDetails {
65+
@Type(() => String)
66+
public ZoneCode: string;
67+
68+
@Type(() => String)
69+
public StreetNumber: string;
70+
71+
@Type(() => String)
72+
public Intersection: string;
73+
74+
@Type(() => String)
75+
public City: string;
76+
77+
@Type(() => String)
78+
public Code: string;
79+
80+
@Type(() => String)
81+
public StopName: string;
82+
83+
@Type(() => String)
84+
public StopNameFr: string;
85+
86+
@Type(() => Boolean)
87+
public IsBus: boolean;
88+
89+
@Type(() => Boolean)
90+
public IsTrain: boolean;
91+
92+
@Type(() => Number)
93+
public Longitude: number;
94+
95+
@Type(() => Number)
96+
public Latitude: number;
97+
98+
@Type(() => String)
99+
public DrivingDirections?: string;
100+
101+
@Type(() => String)
102+
public DrivingDirectionsFr?: string;
103+
104+
public BoardingInfo?: string;
105+
106+
public BoardingInfoFr?: string;
107+
108+
@Type(() => GoTransitStopFacilities)
109+
public Facilities: GoTransitStopFacilities[];
110+
111+
public Parkings?: any[];
112+
113+
@Type(() => GoTransitPlace)
114+
public Place: GoTransitPlace;
115+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// src/Modules/GoTransit/Stops.ts
2+
import { TransformPlainToInstance, Type } from 'class-transformer';
3+
import { IsString } from 'class-validator';
4+
import { Container, Service } from 'typedi';
5+
import { APIResponse, GoTransit } from '..';
6+
import { GoTransitStopDetails } from './StopDetails';
7+
8+
export enum GoTransitStopType {
9+
BUS = 'Bus Stop',
10+
TRAIN = 'Train Station',
11+
TRAINBUS = 'Train & Bus Station',
12+
BUSTERMINAL = 'Bus Terminal',
13+
PARKNRIDE = 'Park & Ride',
14+
}
15+
16+
@Service()
17+
export class GoTransitStop {
18+
@Type(() => String)
19+
@IsString()
20+
public LocationCode: string;
21+
22+
@Type(() => String)
23+
@IsString()
24+
public LocationName: string;
25+
26+
@Type(() => String)
27+
@IsString()
28+
public PublicStopId: string;
29+
30+
public LocationType: GoTransitStopType;
31+
32+
@TransformPlainToInstance(GoTransitStopDetails)
33+
public async getStopDetails(): Promise<GoTransitStopDetails> {
34+
const goAPI = Container.get<GoTransit>('transitAPI');
35+
36+
const { Stop } = await goAPI.makeRequest<
37+
APIResponse & { Stop: GoTransitStopDetails }
38+
>(`Stop/Details/${this.LocationCode}`);
39+
40+
return Stop;
41+
}
42+
}

src/Modules/GoTransit/index.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
// src/Modules/GoTransit/index.ts
2-
import { plainToInstance } from 'class-transformer';
2+
import { plainToInstance, TransformPlainToInstance } from 'class-transformer';
33
import got, { OptionsOfJSONResponseBody } from 'got';
4+
import { Service } from 'typedi';
45
import { CONFIG } from '../../Library/Config';
56
import { logger, LogMode } from '../../Library/Logger';
6-
import { GoTransitStop, GoTransitStopType } from './Stop';
7+
import { GoTransitStop } from './Stop';
78
import { GoTransitTrainTrip } from './TrainTrip';
89
import { GoTransitUnionDepartureTrip } from './UnionDeparture';
910

@@ -21,10 +22,11 @@ interface APIMetadata {
2122
ErrorMessage: string;
2223
}
2324

24-
interface APIResponse {
25+
export interface APIResponse {
2526
Metadata: APIMetadata;
2627
}
2728

29+
@Service()
2830
export class GoTransit {
2931
private config: GoTransitConfig = {
3032
apiToken: CONFIG.token,
@@ -45,7 +47,7 @@ export class GoTransit {
4547
},
4648
});
4749

48-
private async makeRequest<ResponseType extends APIResponse>(
50+
public async makeRequest<ResponseType extends APIResponse>(
4951
url: string,
5052
options?: OptionsOfJSONResponseBody,
5153
): Promise<ResponseType> {
@@ -66,7 +68,7 @@ export class GoTransit {
6668
/**
6769
* Gets all the trains soon arriving at Union and the platform allocation
6870
* if provided/same as the departure board/go tracker.
69-
*
71+
*
7072
* @returns array of GoTransitUnionDepartureTrip
7173
*/
7274
public async unionDepartures(): Promise<GoTransitUnionDepartureTrip[]> {
@@ -80,7 +82,6 @@ export class GoTransit {
8082
);
8183
}
8284

83-
8485
/**
8586
* Function to get all active trains within the Go Transit network.
8687
*
@@ -96,15 +97,16 @@ export class GoTransit {
9697

9798
/**
9899
* Get all Go Transit Stops within the network
99-
*
100-
* @returns Array of all Go Transit Bus Stops,
100+
*
101+
* @returns Array of all Go Transit Bus Stops,
101102
* Bus Terminals, Bus & Train Stations, and Park & Ride Stops
102103
*/
104+
@TransformPlainToInstance(GoTransitStop)
103105
public async getAllStops(): Promise<GoTransitStop[]> {
104106
const response = await this.makeRequest<
105107
APIResponse & { Stations: { Station: GoTransitStop[] } }
106108
>('Stop/All');
107109

108-
return plainToInstance(GoTransitStop, response.Stations.Station);
110+
return response.Stations.Station;
109111
}
110112
}

src/Modules/User/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ export class User {
3030
* Sends notification to user via her Apple Watch Ultra
3131
*/
3232
public async notifyUser(options: { subject?: string; text?: string }) {
33-
await sendAlert(this.deviceID, options);
33+
return sendAlert(this.deviceID, options);
3434
}
3535
}

src/index.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
// src/index.ts
22
import 'reflect-metadata';
3+
import { Container } from 'typedi';
4+
import { useContainer } from 'class-validator';
5+
36
import { CONFIG } from './Library/Config';
47
import { logger, LogMode } from './Library/Logger';
58
import { GoTransit } from './Modules/GoTransit';
@@ -21,12 +24,15 @@ logger.log(LogMode.INFO, `Starting TransitRouting`);
2124
// text: 'Platform 6 & 7'
2225
// })
2326

24-
const goAPI = new GoTransit({
25-
apiURL: CONFIG.apiURL,
27+
useContainer(Container);
2628

29+
const goAPI = new GoTransit({
2730
apiToken: CONFIG.token,
31+
apiURL: CONFIG.apiURL,
2832
});
2933

34+
Container.set('transitAPI', goAPI);
35+
3036
logger.log(LogMode.INFO, `Making request`);
3137

3238
// const unionDepartures = await goAPI.unionDepartures();
@@ -51,6 +57,14 @@ logger.log(LogMode.INFO, `Making request`);
5157
// }
5258
// }
5359

60+
const stops = await goAPI.getAllStops();
61+
62+
for (const stop of stops) {
63+
const stopDetails = await stop.getStopDetails();
64+
65+
logger.log(LogMode.INFO, `Stop Details`, stopDetails);
66+
}
67+
5468
await sayHello('K-FOSS');
5569

5670
export {};

0 commit comments

Comments
 (0)