@kkbox/kkbox-js-sdk
Version:
KKBOX Open API developer SDK for JavaScript. Use it to easily access KKBOX open API to get various metadata about KKBOX's tracks, albums, artists, playlists and stations.
226 lines (183 loc) • 9.47 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _Endpoint = require("../Endpoint");
var _Fetcher2 = _interopRequireDefault(require("./Fetcher"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }
function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
/**
* Search API.
* @see https://docs-en.kkbox.codes/v1.1/reference#search
*/
var SearchFetcher =
/*#__PURE__*/
function (_Fetcher) {
_inherits(SearchFetcher, _Fetcher);
/**
* @ignore
*/
function SearchFetcher(http) {
var _this;
var territory = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'TW';
_classCallCheck(this, SearchFetcher);
_this = _possibleConstructorReturn(this, _getPrototypeOf(SearchFetcher).call(this, http, territory));
/**
* @ignore
*/
_this.filterConditions = undefined;
/**
* @ignore
*/
_this.q = undefined;
/**
* @ignore
*/
_this.type = undefined;
return _this;
}
/**
* Filter what you don't want when search.
*
* @param {Object} [conditions] - search conditions.
* @param {string} conditions.track - track's name.
* @param {string} conditions.album - album's name.
* @param {string} conditions.artist - artist's name.
* @param {string} conditions.playlist - playlist's title.
* @param {string} conditions.availableTerritory - tracks and albums available territory.
* @return {Search}
* @example
* api.searchFetcher
* .setSearchCriteria('五月天 好好')
* .filter({artist: '五月天'})
* .fetchSearchResult();
*/
_createClass(SearchFetcher, [{
key: "filter",
value: function filter() {
var conditions = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
this.filterConditions = conditions;
return this;
}
/**
* Init the search fetcher for the artist, album, track or playlist.
*
* @param {string} q - The keyword to be searched.
* @param {string} [type] - ['artist', 'album', 'track', 'playlist'] The type of search. Default to search all types. If you want to use multiple type at the same time, you may use ',' to separate them.
* @return {Search}
* @see https://docs-en.kkbox.codes/v1.1/reference#search_1
*/
}, {
key: "setSearchCriteria",
value: function setSearchCriteria(q) {
var type = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : undefined;
this.q = q;
this.type = type;
return this;
}
/**
* Fetch the search result.
*
* @param {number} [limit] - The size of one page.
* @param {number} [offset] - The offset index for first element.
* @return {Promise}
* @example
* api.searchFetcher
* .setSearchCriteria('五月天 好好')
* .fetchSearchResult();
* @see https://docs-en.kkbox.codes/v1.1/reference#search_1
*/
}, {
key: "fetchSearchResult",
value: function fetchSearchResult() {
var limit = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : undefined;
var offset = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : undefined;
return this.http.get(_Endpoint.SEARCH, {
q: this.q,
type: this.type,
territory: this.territory,
limit: limit,
offset: offset
}).then(doFilter.bind(this));
}
}]);
return SearchFetcher;
}(_Fetcher2["default"]);
exports["default"] = SearchFetcher;
function doFilter(response) {
var _this2 = this;
if (this.filterConditions !== undefined) {
var data = Object.keys(response.data).map(function (key) {
switch (key) {
case 'tracks':
return _defineProperty({}, key, Object.assign(response.data[key], {
data: response.data[key].data.filter(function (track) {
if (_this2.filterConditions.availableTerritory !== undefined && !track.available_territories.includes(_this2.filterConditions.availableTerritory)) {
return false;
}
if (_this2.filterConditions.track !== undefined && !new RegExp('.*' + _this2.filterConditions.track + '.*').test(track.name)) {
return false;
}
if (_this2.filterConditions.album !== undefined && !new RegExp('.*' + _this2.filterConditions.album + '.*').test(track.album.name)) {
return false;
}
return !(_this2.filterConditions.artist !== undefined && !new RegExp('.*' + _this2.filterConditions.artist + '.*').test(track.album.artist.name));
})
}));
case 'albums':
return _defineProperty({}, key, Object.assign(response.data[key], {
data: response.data[key].data.filter(function (album) {
if (_this2.filterConditions.availableTerritory !== undefined && !album.available_territories.includes(_this2.filterConditions.availableTerritory)) {
return false;
}
if (_this2.filterConditions.album !== undefined && !new RegExp('.*' + _this2.filterConditions.album + '.*').test(album.name)) {
return false;
}
return !(_this2.filterConditions.artist !== undefined && !new RegExp('.*' + _this2.filterConditions.artist + '.*').test(album.artist.name));
})
}));
case 'artists':
return _defineProperty({}, key, Object.assign(response.data[key], {
data: response.data[key].data.filter(function (artist) {
if (_this2.filterConditions.artist === undefined) {
return true;
} else {
return new RegExp('.*' + _this2.filterConditions.artist + '.*').test(artist.name);
}
})
}));
case 'playlists':
return _defineProperty({}, key, Object.assign(response.data[key], {
data: response.data[key].data.filter(function (playlist) {
if (_this2.filterConditions.playlist === undefined) {
return true;
} else {
return new RegExp('.*' + _this2.filterConditions.playlist + '.*').test(playlist.title);
}
})
}));
default:
return _defineProperty({}, key, response.data[key]);
}
});
return Object.assign(response, {
data: Object.assign.apply(Object, _toConsumableArray(data))
});
} else {
return response;
}
}