Skip to content

Commit 0ceccc2

Browse files
authored
feat: add item to queue (#66)
1 parent 6a10586 commit 0ceccc2

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

Diff for: src/actions.ts

+25
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
PlaySpecificList,
99
PlaySpecificTracks,
1010
PreviousSong,
11+
QueueItem,
1112
SeekPosition,
1213
SkipSong,
1314
TransferPlayback,
@@ -20,6 +21,7 @@ export enum ActionId {
2021
PlaySpecificList = 'playSpecificList',
2122
PlaySpecificTracks = 'playSpecificTracks',
2223
Pause = 'pause',
24+
QueueItem = 'queueItem',
2325
VolumeUp = 'volumeUp',
2426
VolumeDown = 'volumeDown',
2527
VolumeSpecific = 'volumeSpecific',
@@ -347,6 +349,29 @@ export function GetActionsList(executeAction: (fcn: DoAction) => Promise<void>):
347349
})
348350
},
349351
},
352+
[ActionId.QueueItem]: {
353+
name: 'Add Track to Queue',
354+
options: [
355+
{
356+
tooltip: 'Provide the ID for the track',
357+
required: true,
358+
type: 'textinput',
359+
label: 'Track ID',
360+
id: 'context_uri',
361+
useVariables: true,
362+
},
363+
],
364+
callback: async (action, context) => {
365+
if (typeof action.options.context_uri === 'string') {
366+
const context_uri_portion = await context.parseVariablesInString(String(action.options.context_uri))
367+
const context_uri = `spotify:track:${context_uri_portion}`
368+
369+
await executeAction(async (instance, deviceId) => {
370+
if (deviceId) await QueueItem(instance, deviceId, context_uri)
371+
})
372+
}
373+
},
374+
},
350375
}
351376

352377
return actions

Diff for: src/api/playback.ts

+13
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,19 @@ import {
99
BodyParameters,
1010
} from './util.js'
1111

12+
export async function addItemToQueue(
13+
reqOptions: RequestOptionsBase,
14+
context_uri: string,
15+
options?: DeviceOptions,
16+
): Promise<Response<void>> {
17+
const params: QueryParameters = {
18+
uri: context_uri,
19+
}
20+
if (options && 'deviceId' in options) params.device_id = options.deviceId
21+
22+
return doPostRequest(reqOptions, '/v1/me/player/queue', params)
23+
}
24+
1225
export async function getMyCurrentPlaybackState(
1326
reqOptions: RequestOptionsBase,
1427
): Promise<Response<SpotifyApi.CurrentPlaybackResponse | undefined>> {

Diff for: src/helpers.ts

+22
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { getMyDevices, setVolume } from './api/device.js'
22
import {
3+
addItemToQueue,
34
getMyCurrentPlaybackState,
45
pause,
56
play,
@@ -53,6 +54,27 @@ export async function ChangeVolume(
5354
}
5455
}
5556

57+
export async function QueueItem(
58+
instance: SpotifyInstanceBase,
59+
deviceId: string,
60+
context_uri: string,
61+
attempt = 0,
62+
): Promise<void> {
63+
const reqOptions = instance.getRequestOptionsBase()
64+
if (!reqOptions) return
65+
66+
try {
67+
await addItemToQueue(reqOptions, context_uri, { deviceId })
68+
} catch (err) {
69+
const retry = await instance.checkIfApiErrorShouldRetry(err)
70+
if (retry && attempt <= MAX_ATTEMPTS) {
71+
return QueueItem(instance, deviceId, context_uri, attempt + 1)
72+
} else {
73+
throw err
74+
}
75+
}
76+
}
77+
5678
export async function SkipSong(instance: SpotifyInstanceBase, deviceId: string, attempt = 0): Promise<void> {
5779
const reqOptions = instance.getRequestOptionsBase()
5880
if (!reqOptions) return

0 commit comments

Comments
 (0)