@aaronlcj/use-last-fm
Version:
Get data from last.fm as a React hook.
39 lines • 1.4 kB
JavaScript
import { parseSong } from './lib';
import useSWR from 'swr';
/**
* Use Last.fm
* @param method The search type and params used to construct the request endpoint
* @param token Your API token
* @param interval Optional, this is the internal between each request
* @param imageSize The size of the image
*/
var useLastFM = /** @class */ (function () {
function useLastFM(token) {
this.token = token;
}
useLastFM.prototype.get = function (method, interval, imageSize) {
if (interval === void 0) { interval = 15 * 1000; }
if (imageSize === void 0) { imageSize = 'extralarge'; }
var endpoint = "//ws.audioscrobbler.com/2.0/?method=" + method.query.type + "." + method.query.node + "&" + method.query.type + "=" + method.param + "&api_key=" + this.token + "&format=json&limit=1";
var _a = useSWR(endpoint, { refreshInterval: interval }), _b = _a.data, track = _b === void 0 ? null : _b, error = _a.error;
if (error) {
return {
status: 'error',
song: null
};
}
try {
return parseSong(track, imageSize);
}
catch (e) {
return {
status: 'error',
song: null
};
}
};
return useLastFM;
}());
export default useLastFM;
export * from './types';
//# sourceMappingURL=index.js.map