-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
55 lines (51 loc) · 1.47 KB
/
index.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
50
51
52
53
54
55
import fetch from 'isomorphic-unfetch'
interface Results {
error?: {
message: string
}
mini: string
small: string
medium: string
largest: string
}
/**
* Returns album artworks
* @example
* const artwork = await albumArt('MK', '17')
* console.log(artwork.mini)
*/
const albumArt = (artist: string, album: string): PromiseLike<Results> => {
return new Promise((resolve, reject) => {
artist = artist.replace(/\s/g, '+').replace(/\&/g, '+')
album = album.replace(/\s/g, '+').replace(/\&/g, '+')
let resultJson: Results
fetch(`https://itunes.apple.com/search?term=${artist}+${album}&limit=1&entity=song`)
.then(res => res.json())
.then(res => {
if (res.resultCount === 0 || res.resultCount === '0') {
// reject(new Error('iTunes Store returned with 0 results'))
reject({
error: {
message: 'iTunes Store returned with 0 results'
}
})
} else {
resultJson = {
mini: res.results[0].artworkUrl100,
small: res.results[0].artworkUrl100.replace(/100x100/, '600x600'),
medium: res.results[0].artworkUrl100.replace(/100x100/, '900x900'),
largest: res.results[0].artworkUrl100.replace(/100x100/, '2000x2000')
}
resolve(resultJson)
}
})
.catch(err => {
reject('iTunes Store returned with 0 results')
throw err
})
})
}
export default albumArt
export {
Results
}