Select Git revision
fanart_api.js
-
Janne Mareike Koschinski authoredJanne Mareike Koschinski authored
fanart_api.js 1.29 KiB
import fetch from 'node-fetch';
class FanartApi {
apiKey;
baseUrl;
constructor(apiKey, baseUrl) {
this.apiKey = apiKey;
this.baseUrl = baseUrl || "https://webservice.fanart.tv/v3/";
}
request(path, options) {
const url = new URL(path, this.baseUrl);
const params = new URLSearchParams(options || {});
params.append("api_key", this.apiKey);
url.search = params.toString();
return fetch(url.href, {
options: {
timeout: 2000,
},
}).then((response) => {
if (response.status === 404) {
return null;
} else {
return response.text().then(text => {
return {
ok: response.ok,
body: text,
}
});
}
}).then((data) => {
if (data === null) {
return null;
} else {
const {ok, body} = data;
if (!ok) {
throw new Error(`${url}: ${body}`);
}
return JSON.parse(body);
}
}).catch(err => {
console.error(err);
return null;
});
}
}
export default FanartApi;