-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresolver.ts
49 lines (40 loc) · 1.05 KB
/
resolver.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { getSongInfo, getSongUrl } from './netease/song';
export type Song = {
name: string;
artist: string;
url: string;
pic: string;
};
// The props for music player. We support both netease music and direct linked music.
export interface MusicPlayerProps {
netease?: number;
song?: Song;
}
const emptySong = { name: '', artist: '', url: '', pic: '' };
const song = async (props: MusicPlayerProps): Promise<Song> => {
const { netease, song } = props;
if (netease) {
const info = await getSongInfo(netease);
const url = await getSongUrl(netease);
// Check the return result.
return {
name: info[0].title,
artist: info[0].author,
url: url,
pic: info[0].pic,
};
}
if (song) {
return song;
}
console.error('No song information is provided, check your code.');
return emptySong;
};
export const resolveSong = async (props: MusicPlayerProps): Promise<Song> => {
// Try-catch for avoiding the unexpected errors.
try {
return await song(props);
} catch (e) {
return emptySong;
}
};