1
- import { NotFoundError , ParseError } from 'elysia' ;
1
+ import { ParseError } from 'elysia' ;
2
2
3
3
import { SPOTIFY_LINK_REGEX , YOUTUBE_LINK_REGEX } from '~/config/constants' ;
4
4
import { ServiceType } from '~/config/enum' ;
5
+ import { getSourceFromId } from '~/utils/encoding' ;
5
6
6
7
import { logger } from '~/utils/logger' ;
7
- import { cacheSearchService , getCachedSearchService } from '~/services/cache' ;
8
8
9
9
export type SearchService = {
10
10
id : string ;
@@ -13,41 +13,40 @@ export type SearchService = {
13
13
} ;
14
14
15
15
export const getSearchService = async ( link ?: string , searchId ?: string ) => {
16
- const cached = searchId ? await getCachedSearchService ( searchId ) : null ;
17
- if ( cached ) {
18
- logger . info ( `[${ getSearchService . name } ] (${ searchId } ) cache hit` ) ;
19
- return cached ;
20
- }
16
+ const decodedSource = searchId ? getSourceFromId ( searchId ) : undefined ;
17
+
18
+ let source = link ;
21
19
22
- if ( ! link && searchId ) {
23
- throw new NotFoundError ( 'SearchId does not exist' ) ;
20
+ if ( searchId && decodedSource ) {
21
+ logger . info (
22
+ `[${ getSearchService . name } ] (${ searchId } ) source decoded: ${ decodedSource } `
23
+ ) ;
24
+ source = decodedSource ;
24
25
}
25
26
26
27
let id , type ;
27
28
28
- const spotifyId = link ! . match ( SPOTIFY_LINK_REGEX ) ?. [ 3 ] ;
29
+ const spotifyId = source ! . match ( SPOTIFY_LINK_REGEX ) ?. [ 3 ] ;
29
30
if ( spotifyId ) {
30
31
id = spotifyId ;
31
32
type = ServiceType . Spotify ;
32
33
}
33
34
34
- const youtubeId = link ! . match ( YOUTUBE_LINK_REGEX ) ?. [ 1 ] ;
35
+ const youtubeId = source ! . match ( YOUTUBE_LINK_REGEX ) ?. [ 1 ] ;
35
36
if ( youtubeId ) {
36
37
id = youtubeId ;
37
38
type = ServiceType . YouTube ;
38
39
}
39
40
40
41
if ( ! id || ! type ) {
41
- throw new ParseError ( 'Service id could not be extracted from link .' ) ;
42
+ throw new ParseError ( 'Service id could not be extracted from source .' ) ;
42
43
}
43
44
44
45
const searchService = {
45
46
id,
46
47
type,
47
- source : link ,
48
+ source,
48
49
} as SearchService ;
49
50
50
- await cacheSearchService ( id , searchService ) ;
51
-
52
51
return searchService ;
53
52
} ;
0 commit comments