apple-music-web-api
Advanced tools
Comparing version 0.1.4 to 0.2.0
import { Song, ListResponse } from "./Song"; | ||
import { Playlist } from "./Playlist"; | ||
export declare const initializeAppleMusicApi: (developerToken: string) => AppleMusicApiInterface; | ||
declare type TrackPayload = { | ||
id: string; | ||
type: string; | ||
}; | ||
declare type AppleMusicApiInterface = { | ||
fetchSong: (id: string) => Promise<Song>; | ||
fetchSongs: (ids: string[]) => Promise<ListResponse<Song>>; | ||
fetchLibraryPlaylists: (userToken: string) => AsyncIterableIterator<Playlist>; | ||
createPlaylist: (userToken: string, attributes: { | ||
name: string; | ||
description: string; | ||
}, initialTracks: TrackPayload[]) => Promise<void>; | ||
addTracksToPlaylist: (userToken: string, playlistId: string, tracks: TrackPayload[]) => Promise<ListResponse<any>>; | ||
}; | ||
export {}; |
113
lib/index.js
"use strict"; | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
@@ -38,14 +49,52 @@ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
}; | ||
var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); } | ||
var __asyncValues = (this && this.__asyncValues) || function (o) { | ||
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); | ||
var m = o[Symbol.asyncIterator], i; | ||
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i); | ||
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; } | ||
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); } | ||
}; | ||
var __asyncDelegator = (this && this.__asyncDelegator) || function (o) { | ||
var i, p; | ||
return i = {}, verb("next"), verb("throw", function (e) { throw e; }), verb("return"), i[Symbol.iterator] = function () { return this; }, i; | ||
function verb(n, f) { i[n] = o[n] ? function (v) { return (p = !p) ? { value: __await(o[n](v)), done: n === "return" } : f ? f(v) : v; } : f; } | ||
}; | ||
var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) { | ||
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); | ||
var g = generator.apply(thisArg, _arguments || []), i, q = []; | ||
return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i; | ||
function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; } | ||
function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } } | ||
function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); } | ||
function fulfill(value) { resume("next", value); } | ||
function reject(value) { resume("throw", value); } | ||
function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); } | ||
}; | ||
var __values = (this && this.__values) || function(o) { | ||
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; | ||
if (m) return m.call(o); | ||
if (o && typeof o.length === "number") return { | ||
next: function () { | ||
if (o && i >= o.length) o = void 0; | ||
return { value: o && o[i++], done: !o }; | ||
} | ||
}; | ||
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var axios_1 = require("axios"); | ||
exports.initializeAppleMusicApi = function (developerToken) { | ||
var callApi = function (endpoint) { return __awaiter(void 0, void 0, void 0, function () { | ||
var callApi = function (endpoint, options) { return __awaiter(void 0, void 0, void 0, function () { | ||
var response; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
var _a; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: return [4 /*yield*/, axios_1.default.get("https://api.music.apple.com" + endpoint, { | ||
headers: { Authorization: "Bearer " + developerToken } | ||
method: (_a = options === null || options === void 0 ? void 0 : options.method) !== null && _a !== void 0 ? _a : "GET", | ||
data: options === null || options === void 0 ? void 0 : options.data, | ||
headers: __assign({ Authorization: "Bearer " + developerToken }, ((options === null || options === void 0 ? void 0 : options.userToken) && { "Music-User-Token": options.userToken })) | ||
})]; | ||
case 1: | ||
response = _a.sent(); | ||
response = _b.sent(); | ||
return [2 /*return*/, response.data]; | ||
@@ -55,6 +104,27 @@ } | ||
}); }; | ||
return appleMusicApi(callApi); | ||
var pageApi = function (endpoint, options) { | ||
return __asyncGenerator(this, arguments, function () { | ||
var page; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, __await(callApi(endpoint, options))]; | ||
case 1: | ||
page = _a.sent(); | ||
return [5 /*yield**/, __values(__asyncDelegator(__asyncValues(page.data)))]; | ||
case 2: return [4 /*yield*/, __await.apply(void 0, [_a.sent()])]; | ||
case 3: | ||
_a.sent(); | ||
_a.label = 4; | ||
case 4: | ||
if (page.next) return [3 /*break*/, 0]; | ||
_a.label = 5; | ||
case 5: return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
}; | ||
return appleMusicApi(callApi, pageApi); | ||
}; | ||
var storefront = "us"; | ||
var appleMusicApi = function (callApi) { return ({ | ||
var appleMusicApi = function (callApi, pageApi) { return ({ | ||
fetchSong: function (id) { return callApi("/v1/catalog/" + storefront + "/songs/" + id); }, | ||
@@ -65,3 +135,32 @@ fetchSongs: function (ids) { | ||
.join(",")); | ||
}, | ||
fetchLibraryPlaylists: function (userToken) { | ||
return pageApi("/v1/me/library/playlists", { method: "GET", userToken: userToken }); | ||
}, | ||
createPlaylist: function (userToken, attributes, initialTracks) { | ||
return callApi("/v1/me/library/playlists", { | ||
method: "POST", | ||
userToken: userToken, | ||
data: { | ||
attributes: attributes, | ||
relationships: { | ||
tracks: { | ||
data: initialTracks | ||
} | ||
} | ||
} | ||
}); | ||
}, | ||
addTracksToPlaylist: function (userToken, playlistId, tracks) { | ||
return callApi("/v1/me/library/playlists/" + encodeURIComponent(playlistId) + "/tracks", { | ||
method: "POST", | ||
userToken: userToken, | ||
data: { | ||
data: tracks.map(function (_a) { | ||
var id = _a.id, type = _a.type; | ||
return ({ id: id, type: type }); | ||
}) | ||
} | ||
}); | ||
} | ||
}); }; |
{ | ||
"name": "apple-music-web-api", | ||
"version": "0.1.4", | ||
"version": "0.2.0", | ||
"description": "Apple Music API SDK for JS", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
47445
9
277