44 * Liked songs, top played, recently played/added operations.
55 */
66
7- import { ApiError , invoke , request } from './shared.js' ;
7+ import { ApiError , request , tauriInvoke } from './shared.js' ;
88
99export const favorites = {
1010 /**
@@ -15,17 +15,11 @@ export const favorites = {
1515 * @returns {Promise<{tracks: Array, total: number, limit: number, offset: number}> }
1616 */
1717 async get ( params = { } ) {
18- if ( invoke ) {
19- try {
20- return await invoke ( 'favorites_get' , {
21- limit : params . limit ?? null ,
22- offset : params . offset ?? null ,
23- } ) ;
24- } catch ( error ) {
25- console . error ( '[api.favorites.get] Tauri error:' , error ) ;
26- throw new ApiError ( 500 , error . toString ( ) ) ;
27- }
28- }
18+ const result = await tauriInvoke ( 'favorites_get' , {
19+ limit : params . limit ?? null ,
20+ offset : params . offset ?? null ,
21+ } ) ;
22+ if ( result !== null ) return result ;
2923 // Fallback to HTTP
3024 const query = new URLSearchParams ( ) ;
3125 if ( params . limit ) query . set ( 'limit' , params . limit . toString ( ) ) ;
@@ -40,14 +34,8 @@ export const favorites = {
4034 * @returns {Promise<{is_favorite: boolean, favorited_date: string|null}> }
4135 */
4236 async check ( trackId ) {
43- if ( invoke ) {
44- try {
45- return await invoke ( 'favorites_check' , { trackId } ) ;
46- } catch ( error ) {
47- console . error ( '[api.favorites.check] Tauri error:' , error ) ;
48- throw new ApiError ( 500 , error . toString ( ) ) ;
49- }
50- }
37+ const result = await tauriInvoke ( 'favorites_check' , { trackId } ) ;
38+ if ( result !== null ) return result ;
5139 return request ( `/favorites/${ encodeURIComponent ( trackId ) } ` ) ;
5240 } ,
5341
@@ -57,20 +45,17 @@ export const favorites = {
5745 * @returns {Promise<{success: boolean, favorited_date: string}> }
5846 */
5947 async add ( trackId ) {
60- if ( invoke ) {
61- try {
62- return await invoke ( 'favorites_add' , { trackId } ) ;
63- } catch ( error ) {
64- console . error ( '[api.favorites.add] Tauri error:' , error ) ;
65- // Check for specific error messages
66- if ( error . toString ( ) . includes ( 'already favorited' ) ) {
67- throw new ApiError ( 409 , 'Track is already favorited' ) ;
68- }
69- if ( error . toString ( ) . includes ( 'not found' ) ) {
70- throw new ApiError ( 404 , error . toString ( ) ) ;
71- }
72- throw new ApiError ( 500 , error . toString ( ) ) ;
48+ try {
49+ const result = await tauriInvoke ( 'favorites_add' , { trackId } ) ;
50+ if ( result !== null ) return result ;
51+ } catch ( error ) {
52+ if ( error . message . includes ( 'already favorited' ) ) {
53+ throw new ApiError ( 409 , 'Track is already favorited' ) ;
54+ }
55+ if ( error . message . includes ( 'not found' ) ) {
56+ throw new ApiError ( 404 , error . message ) ;
7357 }
58+ throw error ;
7459 }
7560 return request ( `/favorites/${ encodeURIComponent ( trackId ) } ` , {
7661 method : 'POST' ,
@@ -83,16 +68,14 @@ export const favorites = {
8368 * @returns {Promise<void> }
8469 */
8570 async remove ( trackId ) {
86- if ( invoke ) {
87- try {
88- return await invoke ( 'favorites_remove' , { trackId } ) ;
89- } catch ( error ) {
90- console . error ( '[api.favorites.remove] Tauri error:' , error ) ;
91- if ( error . toString ( ) . includes ( 'not in favorites' ) ) {
92- throw new ApiError ( 404 , error . toString ( ) ) ;
93- }
94- throw new ApiError ( 500 , error . toString ( ) ) ;
71+ try {
72+ const result = await tauriInvoke ( 'favorites_remove' , { trackId } ) ;
73+ if ( result !== null ) return result ;
74+ } catch ( error ) {
75+ if ( error . message . includes ( 'not in favorites' ) ) {
76+ throw new ApiError ( 404 , error . message ) ;
9577 }
78+ throw error ;
9679 }
9780 return request ( `/favorites/${ encodeURIComponent ( trackId ) } ` , {
9881 method : 'DELETE' ,
@@ -104,14 +87,8 @@ export const favorites = {
10487 * @returns {Promise<{tracks: Array}> }
10588 */
10689 async getTop25 ( ) {
107- if ( invoke ) {
108- try {
109- return await invoke ( 'favorites_get_top25' ) ;
110- } catch ( error ) {
111- console . error ( '[api.favorites.getTop25] Tauri error:' , error ) ;
112- throw new ApiError ( 500 , error . toString ( ) ) ;
113- }
114- }
90+ const result = await tauriInvoke ( 'favorites_get_top25' ) ;
91+ if ( result !== null ) return result ;
11592 return request ( '/favorites/top25' ) ;
11693 } ,
11794
@@ -123,17 +100,11 @@ export const favorites = {
123100 * @returns {Promise<{tracks: Array, days: number}> }
124101 */
125102 async getRecentlyPlayed ( params = { } ) {
126- if ( invoke ) {
127- try {
128- return await invoke ( 'favorites_get_recently_played' , {
129- days : params . days ?? null ,
130- limit : params . limit ?? null ,
131- } ) ;
132- } catch ( error ) {
133- console . error ( '[api.favorites.getRecentlyPlayed] Tauri error:' , error ) ;
134- throw new ApiError ( 500 , error . toString ( ) ) ;
135- }
136- }
103+ const result = await tauriInvoke ( 'favorites_get_recently_played' , {
104+ days : params . days ?? null ,
105+ limit : params . limit ?? null ,
106+ } ) ;
107+ if ( result !== null ) return result ;
137108 // Fallback to HTTP
138109 const query = new URLSearchParams ( ) ;
139110 if ( params . days ) query . set ( 'days' , params . days . toString ( ) ) ;
@@ -150,17 +121,11 @@ export const favorites = {
150121 * @returns {Promise<{tracks: Array, days: number}> }
151122 */
152123 async getRecentlyAdded ( params = { } ) {
153- if ( invoke ) {
154- try {
155- return await invoke ( 'favorites_get_recently_added' , {
156- days : params . days ?? null ,
157- limit : params . limit ?? null ,
158- } ) ;
159- } catch ( error ) {
160- console . error ( '[api.favorites.getRecentlyAdded] Tauri error:' , error ) ;
161- throw new ApiError ( 500 , error . toString ( ) ) ;
162- }
163- }
124+ const result = await tauriInvoke ( 'favorites_get_recently_added' , {
125+ days : params . days ?? null ,
126+ limit : params . limit ?? null ,
127+ } ) ;
128+ if ( result !== null ) return result ;
164129 // Fallback to HTTP
165130 const query = new URLSearchParams ( ) ;
166131 if ( params . days ) query . set ( 'days' , params . days . toString ( ) ) ;
0 commit comments