File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import { Get , JsonController } from "routing-controllers" ;
2+
3+ import { AppVersionService } from "../../services/AppVersionService" ;
4+
5+ @JsonController ( "version/" )
6+ export class VersionController {
7+ private appVersionService : AppVersionService ;
8+
9+ constructor ( appVersionService : AppVersionService ) {
10+ this . appVersionService = appVersionService ;
11+ }
12+
13+ @Get ( )
14+ async get ( ) : Promise < { version : string } > {
15+ const version = await this . appVersionService . fetchIosAppStoreVersion ( ) ;
16+ return { version } ;
17+ }
18+ }
19+
Original file line number Diff line number Diff line change @@ -13,6 +13,7 @@ import { TransactionReviewController } from './TransactionReviewController';
1313import { ChatController } from './ChatController' ;
1414import { AuthTokenController } from './AuthToken' ;
1515import { SearchController } from './SearchController' ;
16+ import { VersionController } from './VersionController' ;
1617
1718export const controllers = [
1819 AuthController ,
@@ -26,6 +27,7 @@ export const controllers = [
2627 RequestController ,
2728 ReportController ,
2829 SearchController ,
30+ VersionController ,
2931 UserController ,
3032 UserReviewController ,
3133 TransactionController ,
Original file line number Diff line number Diff line change 1+ export const ITUNES_LOOKUP_URL =
2+ "https://itunes.apple.com/lookup?bundleId=com.cornellappdev.resell" ;
3+
Original file line number Diff line number Diff line change 1+ import { Service } from "typedi" ;
2+ import { HttpError } from "routing-controllers" ;
3+ import fetch from "node-fetch" ;
4+
5+ import { ITUNES_LOOKUP_URL } from "../constants" ;
6+
7+ interface ITunesLookupResult {
8+ resultCount : number ;
9+ results : Array < { version ?: string } > ;
10+ }
11+
12+ @Service ( )
13+ export class AppVersionService {
14+ public async fetchIosAppStoreVersion ( ) : Promise < string > {
15+ let response : fetch . Response ;
16+ try {
17+ response = await fetch ( ITUNES_LOOKUP_URL ) ;
18+ } catch ( _err ) {
19+ throw new HttpError ( 502 , "Failed to fetch app version from App Store" ) ;
20+ }
21+
22+ let data : ITunesLookupResult ;
23+ try {
24+ data = ( await response . json ( ) ) as ITunesLookupResult ;
25+ } catch ( _err ) {
26+ throw new HttpError ( 502 , "Failed to parse App Store response" ) ;
27+ }
28+
29+ if ( ! response . ok || ! data . results ?. length ) {
30+ throw new HttpError ( 502 , "Failed to fetch app version from App Store" ) ;
31+ }
32+
33+ const version = data . results [ 0 ] ?. version ;
34+ if ( ! version ) {
35+ throw new HttpError ( 502 , "Version not found in App Store response" ) ;
36+ }
37+
38+ return version ;
39+ }
40+ }
41+
You can’t perform that action at this time.
0 commit comments