|
| 1 | +const { |
| 2 | + flux: { |
| 3 | + stores: { SpotifyStore } |
| 4 | + }, |
| 5 | + util: { awaitDispatch } |
| 6 | +} = shelter; |
| 7 | + |
| 8 | +export const URL_REGEX = |
| 9 | + /^https:\/\/open\.spotify\.com\/(track|album|playlist|episode|show|artist|user)(?:\/)([a-z0-9]+).*$/i; |
| 10 | + |
| 11 | +function getDeviceAndSocket() { |
| 12 | + if (!SpotifyStore.hasConnectedAccount()) { |
| 13 | + throw new Error( |
| 14 | + "No account found. Have you connected your Spotify account yet?" |
| 15 | + ); |
| 16 | + } |
| 17 | + |
| 18 | + const deviceAndSocket = SpotifyStore.getActiveSocketAndDevice(); |
| 19 | + if (!deviceAndSocket) { |
| 20 | + throw new Error("No device found. Start Spotify and try again.."); |
| 21 | + } |
| 22 | + |
| 23 | + return deviceAndSocket; |
| 24 | +} |
| 25 | + |
| 26 | +async function refreshAccessToken() { |
| 27 | + getDeviceAndSocket().socket.handleDeviceStateChange(); |
| 28 | + return Promise.race([ |
| 29 | + awaitDispatch("SPOTIFY_SET_DEVICES"), |
| 30 | + new Promise((_, reject) => |
| 31 | + setTimeout( |
| 32 | + () => reject(new Error("Couldn't refresh Access Token!")), |
| 33 | + 3_000 |
| 34 | + ) |
| 35 | + ) |
| 36 | + ]); |
| 37 | +} |
| 38 | + |
| 39 | +async function spotifyRequest( |
| 40 | + method, |
| 41 | + path, |
| 42 | + searchParams, |
| 43 | + body, |
| 44 | + isRetry = false |
| 45 | +) { |
| 46 | + return new Promise((resolve, reject) => { |
| 47 | + const { device, socket } = getDeviceAndSocket(); |
| 48 | + |
| 49 | + const token = socket.accessToken; |
| 50 | + |
| 51 | + const url = new URL(`https://api.spotify.com/v1/me/player/${path}`); |
| 52 | + |
| 53 | + url.search = new URLSearchParams({ |
| 54 | + device_id: device.id, |
| 55 | + ...searchParams |
| 56 | + }); |
| 57 | + |
| 58 | + const options = { |
| 59 | + method, |
| 60 | + headers: { |
| 61 | + "Content-Type": "application/json", |
| 62 | + Authorization: `Bearer ${token}` |
| 63 | + }, |
| 64 | + body: JSON.stringify(body) |
| 65 | + }; |
| 66 | + fetch(url, options).then(async (res) => { |
| 67 | + if (res.ok) return resolve(); |
| 68 | + if (res.status === 401 && !isRetry) { |
| 69 | + refreshAccessToken() |
| 70 | + .then(() => spotifyRequest(...arguments, true)) |
| 71 | + .then(resolve, reject); |
| 72 | + return; |
| 73 | + } |
| 74 | + reject(new Error(`Spotify API request failed with ${res.status}!`)); |
| 75 | + }); |
| 76 | + }); |
| 77 | +} |
| 78 | + |
| 79 | +export function extractTypeAndId(url) { |
| 80 | + const match = URL_REGEX.exec(url); |
| 81 | + if (!match) return; |
| 82 | + return match.slice(1); |
| 83 | +} |
| 84 | + |
| 85 | +export async function open({ url: urlString }) { |
| 86 | + const url = new URL(urlString); |
| 87 | + url.searchParams.delete("si"); // remove tracking parameter |
| 88 | + window.open("spotify:/" + url.pathname + url.search); |
| 89 | +} |
| 90 | + |
| 91 | +export async function play({ type, id }) { |
| 92 | + const body = |
| 93 | + type === "track" |
| 94 | + ? { uris: [`spotify:${type}:${id}`] } |
| 95 | + : { context_uri: `spotify:${type}:${id}` }; // playlist, album, artist |
| 96 | + |
| 97 | + body.position_ms = 0; |
| 98 | + return spotifyRequest("PUT", "play", {}, body); |
| 99 | +} |
| 100 | + |
| 101 | +export async function queue({ type, id }) { |
| 102 | + return spotifyRequest("POST", "queue", { |
| 103 | + uri: `spotify:${type}:${id}` |
| 104 | + }); |
| 105 | +} |
0 commit comments