-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
37 lines (29 loc) · 880 Bytes
/
index.js
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
import { NativeModules, NativeEventEmitter } from 'react-native';
import axios from 'axios'
const { RNSpotify } = NativeModules;
const spotifyEventEmitter = new NativeEventEmitter(RNSpotify);
let playbackAccessToken = null
RNSpotify.subscribe = (callback) => {
spotifyEventEmitter.addListener(
'PlaybackStateChanged',
(playbackState) => {
playbackAccessToken = playbackState.accessToken
callback(playbackState)
}
)
}
RNSpotify.unsubscribe = () => {
spotifyEventEmitter.removeAllListeners();
}
RNSpotify.webApiGet = async (endpoint, { accessToken, ...params }) => {
const result = await axios.create({
baseURL: 'https://api.spotify.com/v1/',
headers: {
common: {
Authorization: `Bearer ${playbackAccessToken || accessToken}`
}
}
}).get(endpoint, {params});
return result.data;
}
export default RNSpotify;