UNPKG

spotify-ts-wrapper

Version:

Spotify TypeScript wrapper.

92 lines (91 loc) 5.11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.genreParser = genreParser; const getSpotifyId_1 = require("../Utils/getSpotifyId"); function genreParser(body) { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k; const { data } = body; const { browse } = data; const header = { title: (_b = (_a = browse === null || browse === void 0 ? void 0 : browse.header) === null || _a === void 0 ? void 0 : _a.title) === null || _b === void 0 ? void 0 : _b.transformedLabel, color: (_d = (_c = browse === null || browse === void 0 ? void 0 : browse.header) === null || _c === void 0 ? void 0 : _c.color) === null || _d === void 0 ? void 0 : _d.hex, image: (_h = (_g = (_f = (_e = browse === null || browse === void 0 ? void 0 : browse.header) === null || _e === void 0 ? void 0 : _e.backgroundImage) === null || _f === void 0 ? void 0 : _f.sources) === null || _g === void 0 ? void 0 : _g.at(-1)) === null || _h === void 0 ? void 0 : _h.url, }; const items = (_k = (_j = browse === null || browse === void 0 ? void 0 : browse.sections) === null || _j === void 0 ? void 0 : _j.items) === null || _k === void 0 ? void 0 : _k.map((item, index) => { var _a, _b, _c, _d; const title = (_b = (_a = item === null || item === void 0 ? void 0 : item.data) === null || _a === void 0 ? void 0 : _a.title) === null || _b === void 0 ? void 0 : _b.transformedLabel; const id = (0, getSpotifyId_1.extractSpotifyID)(item === null || item === void 0 ? void 0 : item.uri); const sectionItems = (_d = (_c = item === null || item === void 0 ? void 0 : item.sectionItems) === null || _c === void 0 ? void 0 : _c.items) === null || _d === void 0 ? void 0 : _d.map((_sectionItem) => { const content = _sectionItem === null || _sectionItem === void 0 ? void 0 : _sectionItem.content; const data = content === null || content === void 0 ? void 0 : content.data; const typename = data === null || data === void 0 ? void 0 : data.__typename; const albums = []; const playlists = []; if (typename === "Album") { const album = fetchAlbumSection(data); albums.push(album); } else { const playlist = fetchPlaylist(data); playlists.push(playlist); } return { playlist: playlists[0], album: albums[0], }; }); return { title, id, sections: sectionItems, }; }); return { header, items, }; } function fetchAlbumSection(body) { var _a, _b, _c, _d, _e; const type = body === null || body === void 0 ? void 0 : body.__typename; const title = body === null || body === void 0 ? void 0 : body.name; const id = (0, getSpotifyId_1.extractSpotifyID)(body === null || body === void 0 ? void 0 : body.uri); const image = (_c = (_b = (_a = body === null || body === void 0 ? void 0 : body.coverArt) === null || _a === void 0 ? void 0 : _a.sources) === null || _b === void 0 ? void 0 : _b.at(-1)) === null || _c === void 0 ? void 0 : _c.url; const artists = (_e = (_d = body === null || body === void 0 ? void 0 : body.artists) === null || _d === void 0 ? void 0 : _d.items) === null || _e === void 0 ? void 0 : _e.map((artist) => { var _a; return ({ name: (_a = artist.profile) === null || _a === void 0 ? void 0 : _a.name, id: (0, getSpotifyId_1.extractSpotifyID)(artist === null || artist === void 0 ? void 0 : artist.uri), }); }); return { type, title, id, image, artists, }; } function fetchPlaylist(body) { var _a, _b, _c, _d, _e, _f, _g, _h; const type = body === null || body === void 0 ? void 0 : body.__typename; const description = body === null || body === void 0 ? void 0 : body.description; const artwork = (_d = (_c = (_b = (_a = body === null || body === void 0 ? void 0 : body.images) === null || _a === void 0 ? void 0 : _a.items) === null || _b === void 0 ? void 0 : _b.at(-1)) === null || _c === void 0 ? void 0 : _c.sources[0]) === null || _d === void 0 ? void 0 : _d.url; if (typeof artwork === "undefined") return; const title = body === null || body === void 0 ? void 0 : body.name; const format = body === null || body === void 0 ? void 0 : body.format; const id = (0, getSpotifyId_1.extractSpotifyID)(body === null || body === void 0 ? void 0 : body.uri); const owner = { name: (_f = (_e = body === null || body === void 0 ? void 0 : body.ownerV2) === null || _e === void 0 ? void 0 : _e.data) === null || _f === void 0 ? void 0 : _f.name, type: (_h = (_g = body === null || body === void 0 ? void 0 : body.ownerV2) === null || _g === void 0 ? void 0 : _g.data) === null || _h === void 0 ? void 0 : _h.__typename, }; return { title, owner, format, id, description, type, }; }