UNPKG

spotify-ts-wrapper

Version:

Spotify TypeScript wrapper.

81 lines (80 loc) 4.26 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.sectionParser = sectionParser; exports.fetchAlbum = fetchAlbum; const getSpotifyId_1 = require("../Utils/getSpotifyId"); function sectionParser(body) { var _a, _b, _c, _d, _e; const { data } = body; const browseSection = data === null || data === void 0 ? void 0 : data.browseSection; const sectionData = browseSection === null || browseSection === void 0 ? void 0 : browseSection.data; const title = (_a = sectionData === null || sectionData === void 0 ? void 0 : sectionData.title) === null || _a === void 0 ? void 0 : _a.transformedLabel; const pagingInfo = { nextOffSet: (_c = (_b = browseSection === null || browseSection === void 0 ? void 0 : browseSection.sectionItems) === null || _b === void 0 ? void 0 : _b.pagingInfo) === null || _c === void 0 ? void 0 : _c.nextOffset, }; const items = (_e = (_d = browseSection === null || browseSection === void 0 ? void 0 : browseSection.sectionItems) === null || _d === void 0 ? void 0 : _d.items) === null || _e === void 0 ? void 0 : _e.map((item) => { var _a; const content = (_a = item === null || item === void 0 ? void 0 : item.content) === null || _a === void 0 ? void 0 : _a.data; const title = content === null || content === void 0 ? void 0 : content.name; const type = content === null || content === void 0 ? void 0 : content.__typename; const playlists = []; const albums = []; if (type === "Playlist") { const playlist = fetchPlaylist(content); playlists.push(playlist); } else if (type === "Album") { const album = fetchAlbum(content); albums.push(album); } return { title, albums: albums[0], playlists: playlists[0], }; }); return { title, pagingInfo, items, }; } function fetchPlaylist(content) { var _a, _b, _c, _d, _e, _f, _g, _h; const title = content === null || content === void 0 ? void 0 : content.name; const description = content === null || content === void 0 ? void 0 : content.description; const images = (_d = (_c = (_b = (_a = content === null || content === void 0 ? void 0 : content.images) === null || _a === void 0 ? void 0 : _a.items[0]) === null || _b === void 0 ? void 0 : _b.sources) === null || _c === void 0 ? void 0 : _c.at(-1)) === null || _d === void 0 ? void 0 : _d.url; const format = content === null || content === void 0 ? void 0 : content.format; const owner = { name: (_f = (_e = content === null || content === void 0 ? void 0 : content.ownerV2) === null || _e === void 0 ? void 0 : _e.data) === null || _f === void 0 ? void 0 : _f.name, type: (_h = (_g = content === null || content === void 0 ? void 0 : content.ownerV2) === null || _g === void 0 ? void 0 : _g.data) === null || _h === void 0 ? void 0 : _h.__typename, }; const id = (0, getSpotifyId_1.extractSpotifyID)(content === null || content === void 0 ? void 0 : content.uri); return { description, images, format, owner, id, title, }; } function fetchAlbum(content) { var _a, _b, _c, _d, _e; const title = content === null || content === void 0 ? void 0 : content.name; const id = (0, getSpotifyId_1.extractSpotifyID)(content === null || content === void 0 ? void 0 : content.uri); const artwork = (_c = (_b = (_a = content === null || content === void 0 ? void 0 : content.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 = content === null || content === void 0 ? void 0 : content.artists) === null || _d === void 0 ? void 0 : _d.items) === null || _e === void 0 ? void 0 : _e.map((a) => { var _a; return ({ name: (_a = a.profile) === null || _a === void 0 ? void 0 : _a.name, id: (0, getSpotifyId_1.extractSpotifyID)(a === null || a === void 0 ? void 0 : a.uri), }); }); return { title, id, artists, artwork, }; }