UNPKG

lunify.js

Version:

A basic api wrapper for the spotify api covering the oauth routes.

236 lines 8 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __exportStar = (this && this.__exportStar) || function(m, exports) { for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Player = void 0; const CurrentPlayback_1 = require("./CurrentPlayback"); const DeviceManager_1 = require("./DeviceManager"); __exportStar(require("./Device"), exports); __exportStar(require("./DeviceManager"), exports); class Player { constructor(client, user) { this.client = client; this.user = user; this.devices = new DeviceManager_1.PlayerDeviceManager(this.client, this); } /** * Get current plackback (⚠️ This data is fetched from the api, not from a websocket, consider rate limits) * @example ```ts * const playing = player.now(); * console.log(playing) * ``` */ now() { return __awaiter(this, void 0, void 0, function* () { const res = yield this.client.rest.get('/me/player', { headers: { Authorization: yield this.user.oauth.getAuthorization() } }); if (!res) return null; return new CurrentPlayback_1.CurrentPlayback(this.client, this.user, res); }); } /** * Start playing a track on users current device * @param {string | string[]} track - A spotify track id or a list of spotify track ids * @example ```ts * player.start('6IRdLKIyS4p7XNiP8r6rsx'); * ``` */ play(trackId) { return __awaiter(this, void 0, void 0, function* () { const finalTracks = []; if (typeof trackId !== 'string') { for (const t of trackId) finalTracks.push('spotify:track:' + t); } else { finalTracks.push('spotify:track:' + trackId); } yield this.client.rest.put('/me/player/play', { headers: { Authorization: yield this.user.oauth.getAuthorization() }, body: { uris: finalTracks } }); return true; }); } /** * Resume playback on users current device with paused track * @example ```ts * player.resume(); * ``` * Use `.play(...)` to start playing a new track */ resume() { return __awaiter(this, void 0, void 0, function* () { yield this.client.rest.put('/me/player/play', { headers: { Authorization: yield this.user.oauth.getAuthorization() } }); return true; }); } /** * Stop playback on users current device * @example ```ts * player.stop(); * ``` */ stop() { return __awaiter(this, void 0, void 0, function* () { yield this.client.rest.put('/me/player/pause', { headers: { Authorization: yield this.user.oauth.getAuthorization() } }); return true; }); } /** * Skip the current track and start playing the next one * @example ```ts * player.skip(); * ``` */ skip() { return __awaiter(this, void 0, void 0, function* () { yield this.client.rest.post('/me/player/next', { headers: { Authorization: yield this.user.oauth.getAuthorization() } }); return true; }); } /** * Start playing the track before the current one (starts playing previous track) * @example ```ts * player.rewind(); * ``` */ rewind() { return __awaiter(this, void 0, void 0, function* () { yield this.client.rest.post('/me/player/previous', { headers: { Authorization: yield this.user.oauth.getAuthorization() } }); return true; }); } /** * Seek to any position in the current playback * @param {number} position - position in milliseconds (seconds x1000) * @example ```ts * player.seek(16 * 1000); // 16 seconds * ``` */ seekTo(position) { return __awaiter(this, void 0, void 0, function* () { yield this.client.rest.put('/me/player/seek', { headers: { Authorization: yield this.user.oauth.getAuthorization() }, query: { position_ms: position } }); return true; }); } /** * Set the repeat mode for the current playback * @param {'track' | 'context' | false} mode - track, context, false * - track: repeat the current track * - context: repeat the current context * - false: turn repeat off * @example ```ts * player.repeat('track'); * player.repeat('context'); * player.repeat(false); * ``` */ repeat(mode) { return __awaiter(this, void 0, void 0, function* () { yield this.client.rest.put('/me/player/repeat', { headers: { Authorization: yield this.user.oauth.getAuthorization() }, query: { state: mode || 'off' } }); return true; }); } /** * Set the volume for the current playback device * @param {number} percentage - volume in percent % * @example ```ts * player.volumeTo(69); // 69% * ``` */ volumeTo(percentage) { return __awaiter(this, void 0, void 0, function* () { yield this.client.rest.put('/me/player/volume', { headers: { Authorization: yield this.user.oauth.getAuthorization() }, query: { volume_percent: percentage } }); return true; }); } /** * Toggle shuffle on or off for playback * @param {number} state - volume in percent % * @example ```ts * player.shuffle(true); * ``` */ shuffle(state) { return __awaiter(this, void 0, void 0, function* () { yield this.client.rest.put('/me/player/shuffle', { headers: { Authorization: yield this.user.oauth.getAuthorization() }, query: { state } }); return true; }); } } exports.Player = Player; //# sourceMappingURL=index.js.map