@@ -4,6 +4,7 @@ import { Response } from 'express';
44import { AbstractController } from '~/_common/abstracts/abstract.controller' ;
55import { Public } from './_common/decorators/public.decorator' ;
66import { ApiBearerAuth , ApiOperation , ApiQuery , ApiResponse } from '@nestjs/swagger' ;
7+ import LRU from 'lru-cache' ;
78
89interface GithubUpdate {
910 name ?: string ;
@@ -16,6 +17,8 @@ interface GithubUpdate {
1617 node_id ?: string ;
1718}
1819
20+ const storage = new LRU ( { ttl : 60 * 60 * 1000 } )
21+
1922@Public ( )
2023@Controller ( )
2124@ApiBearerAuth ( )
@@ -41,8 +44,21 @@ export class AppController extends AbstractController {
4144 @Param ( 'project' ) project ?: string ,
4245 @Query ( 'current' ) current ?: string ,
4346 ) : Promise < Response > {
44- const update = await fetch ( `https://api.github.com/repos/Libertech-FR/${ project } /tags` )
45- const data : GithubUpdate = await update . json ( )
47+ let data : GithubUpdate [ ] | object = { }
48+ console . log ( 'this.storage' , storage . get ( project ) )
49+ if ( storage . has ( project ) ) {
50+ this . logger . log ( `Fetching ${ project } tags from cache` )
51+ data = storage . get ( project ) as GithubUpdate [ ] | object
52+ } else {
53+ this . logger . log ( `Fetching ${ project } tags` )
54+ const update = await fetch ( `https://api.github.com/repos/Libertech-FR/${ project } /tags` )
55+ data = await update . json ( )
56+ storage . set ( project , data )
57+ console . log ( 'this.storage' , storage . get ( project ) )
58+ }
59+ if ( ! Array . isArray ( data ) ) {
60+ throw new BadRequestException ( `Invalid data from Github <${ JSON . stringify ( data ) } >` )
61+ }
4662 const lastVersion = data [ 0 ] . name . replace ( / ^ v / , '' )
4763 const pkgInfo = this . appService . getInfo ( )
4864 const currentVersion = current || pkgInfo . version
@@ -58,10 +74,12 @@ export class AppController extends AbstractController {
5874 const updateAvailable = currentMajor > lastMajor || currentMinor > lastMinor || currentPatch > lastPatch
5975
6076 return res . json ( {
61- project,
62- updateAvailable,
63- currentVersion,
64- lastVersion,
77+ data : {
78+ project,
79+ updateAvailable,
80+ currentVersion,
81+ lastVersion,
82+ } ,
6583 } ) ;
6684 }
6785}
0 commit comments