radiobrowser-api-client
Version:
Client for Radio Browser API
375 lines (374 loc) • 19 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RadioBrowserClient = void 0;
const axios_1 = __importDefault(require("axios"));
const data_1 = require("./types/data");
const queries_1 = require("./types/queries");
const typescript_json_serializer_1 = require("typescript-json-serializer");
const axios_retry_1 = __importDefault(require("axios-retry"));
class RadioBrowserClient {
axios;
jsonSerializer;
/**
* Constructor for RadioBrowserClient class.
* @param appName - Name of your application
* @param appVersion - Version of your application
*/
constructor(appName, appVersion) {
this.axios = axios_1.default.create({
headers: appName && appVersion ? { 'User-Agent': `${appName}/${appVersion}` } : {},
transformResponse: [(data) => data],
});
(0, axios_retry_1.default)(this.axios, { retries: 3, retryDelay: (retryCount) => retryCount * 1000 });
this.jsonSerializer = new typescript_json_serializer_1.JsonSerializer();
}
/**
* Sends request to the Radio Browser API.
* @param endpoint - The endpoint of the API
* @param outputFormat - The format of the server response
* @param params - The parameters for the request
* @param baseURL - The base URL of the server (if axios.defaults.baseURL is set, it is not required)
* @returns The server response
*/
async sendRequest(endpoint, outputFormat, params, baseURL) {
if (!this.axios.defaults.baseURL && !baseURL) {
const servers = await this.getServers();
if (!servers.length) {
throw new Error('No servers');
}
this.setServer(servers[0].name);
}
const response = await this.axios.get(baseURL ? `${baseURL}/${outputFormat}/${endpoint}` : `/${outputFormat}/${endpoint}`, {
params,
paramsSerializer: {
indexes: null,
},
});
return response;
}
/**
* Fetches the list of available servers from the Radio Browser API.
* @param outputFormat - The output format for the list of servers (default is JSON)
* @see {@link https://all.api.radio-browser.info/#Server_mirrors} for more information on the API endpoint
* @returns A promise that resolves to the list of servers
*/
async fetchServers(outputFormat = 'json') {
const { data } = await this.sendRequest('servers', outputFormat, {}, !this.axios.defaults.baseURL ? 'https://all.api.radio-browser.info' : undefined);
return data;
}
/**
* Fetches and parses the list of available servers from the Radio Browser API.
* @see {@link https://all.api.radio-browser.info/#Server_mirrors} for more information on the API endpoint
* @returns A promise that resolves to the list of servers
*/
async getServers() {
const data = await this.fetchServers();
return this.jsonSerializer.deserialize(JSON.parse(data), data_1.Server);
}
/**
* Sets the server to be used for subsequent requests.
* @param serverName - The name of the server
*/
setServer(serverName) {
this.axios.defaults.baseURL = `https://${serverName}`;
}
/**
* Fetches stations from the Radio Browser API.
* @param params - The search parameters
* @param outputFormat - The output format for the list of stations (default is JSON)
* @see {@link https://all.api.radio-browser.info/#Advanced_station_search} for more information on the API endpoint
* @returns A promise that resolves to the list of stations
*/
async fetchStations(params, outputFormat = 'json') {
const { data } = await this.sendRequest('stations/search', outputFormat, this.jsonSerializer.serialize(new queries_1.ISearchStation(params)));
return data;
}
/**
* Fetches and parses stations from the Radio Browser API.
* @param params - The search parameters
* @returns A promise that resolves to the list of stations
*/
async searchStations(params) {
const data = await this.fetchStations(params);
return this.jsonSerializer.deserialize(JSON.parse(data), data_1.Station);
}
/**
* Fetches stations from the Radio Browser API by given UUIDs.
* @param params - The search parameters
* @param outputFormat - The output format for the list of stations (default is JSON)
* @see {@link https://all.api.radio-browser.info/#Search_radio_stations_by_uuid} for more information on the API endpoint
* @returns A promise that resolves to the list of stations by given UUIDs
*/
async fetchStationsByUUIDs(params, outputFormat = 'json') {
const { data } = await this.sendRequest('stations/byuuid', outputFormat, { uuids: params.stationUUIDs.join(',') });
return data;
}
/**
* Fetches and parses stations from the Radio Browser API by given UUIDs.
* @param params - The search parameters
* @see {@link https://all.api.radio-browser.info/#Search_radio_stations_by_uuid} for more information on the API endpoint
* @returns A promise that resolves to the list of stations by given UUIDs
*/
async getStationsByUUIDs(params) {
const data = await this.fetchStationsByUUIDs(params);
return this.jsonSerializer.deserialize(JSON.parse(data), data_1.Station);
}
/**
* Fetches countries from the Radio Browser API.
* @param params - The search parameters
* @param query - The search query
* @param outputFormat - The output format for the list of countires (default is JSON)
* @see {@link https://all.api.radio-browser.info/#List_of_countries} for more information on the API endpoint
* @returns A promise that resolves to the list of countires
*/
async fetchCountries(params, query = '', outputFormat = 'json') {
const { data } = await this.sendRequest(`countries/${query}`, outputFormat, this.jsonSerializer.serialize(new queries_1.ICountries(params)));
return data;
}
/**
* Fetches and parses list of countries from the Radio Browser API.
* @param params - The search parameters
* @param query - The search query
* @see {@link https://all.api.radio-browser.info/#List_of_countries} for more information on the API endpoint
* @returns A promise that resolves to the list of countires
*/
async getCountries(params, query = '') {
const data = await this.fetchCountries(params, query);
return this.jsonSerializer.deserialize(JSON.parse(data), data_1.Country);
}
/**
* Fetches codecs from the Radio Browser API.
* @param params - The search parameters
* @param query - The search query
* @param outputFormat - The output format for the list of codecs (default is JSON)
* @see {@link https://all.api.radio-browser.info/#List_of_codecs} for more information on the API endpoint
* @returns A promise that resolves to the list of codecs
*/
async fetchCodecs(params, query = '', outputFormat = 'json') {
const { data } = await this.sendRequest(`codecs/${query}`, outputFormat, this.jsonSerializer.serialize(new queries_1.ICodecs(params)));
return data;
}
/**
* Fetches and parses codecs from the Radio Browser API.
* @param params - The search parameters
* @param query - The search query
* @see {@link https://all.api.radio-browser.info/#List_of_codecs} for more information on the API endpoint
* @returns A promise that resolves to the list of codecs
*/
async getCodecs(params, query = '') {
const data = await this.fetchCodecs(params, query);
return this.jsonSerializer.deserialize(JSON.parse(data), data_1.Codec);
}
/**
* Fetches states from the Radio Browser API.
* @param params - The search parameters
* @param query - The search query
* @param outputFormat - The output format for the list of states (default is JSON)
* @see {@link https://all.api.radio-browser.info/#List_of_states} for more information on the API endpoint
* @returns A promise that resolves to the list of states
*/
async fetchStates(params, query = '', outputFormat = 'json') {
const { data } = await this.sendRequest(`states/${query}`, outputFormat, this.jsonSerializer.serialize(new queries_1.IStates(params)));
return data;
}
/**
* Fetches and parses states from the Radio Browser API.
* @param params - The search parameters
* @param query - The search query
* @see {@link https://all.api.radio-browser.info/#List_of_states} for more information on the API endpoint
* @returns A promise that resolves to the list of states
*/
async getStates(params, query = '') {
const data = await this.fetchStates(params, query);
return this.jsonSerializer.deserialize(JSON.parse(data), data_1.State);
}
/**
* Fetches languages from the Radio Browser API.
* @param params - The search parameters
* @param query - The search query
* @param outputFormat - The output format for the list of languages (default is JSON)
* @see {@link https://all.api.radio-browser.info/#List_of_languages} for more information on the API endpoint
* @returns A promise that resolves to the list of languages
*/
async fetchLanguages(params, query = '', outputFormat = 'json') {
const { data } = await this.sendRequest(`languages/${query}`, outputFormat, this.jsonSerializer.serialize(new queries_1.ILanguages(params)));
return data;
}
/**
* Fetches and parses languages from the Radio Browser API.
* @param params - The search parameters
* @param query - The search query
* @see {@link https://all.api.radio-browser.info/#List_of_languages} for more information on the API endpoint
* @returns A promise that resolves to the list of languages
*/
async getLanguages(params, query = '') {
const data = await this.fetchLanguages(params, query);
return this.jsonSerializer.deserialize(JSON.parse(data), data_1.Language);
}
/**
* Fetches tags from the Radio Browser API.
* @param params - The search parameters
* @param query - The search query
* @param outputFormat - The output format for the list of tags (default is JSON)
* @see {@link https://all.api.radio-browser.info/#List_of_tags} for more information on the API endpoint
* @returns A promise that resolves to the list of tags
*/
async fetchTags(params, query = '', outputFormat = 'json') {
const { data } = await this.sendRequest(`tags/${query}`, outputFormat, this.jsonSerializer.serialize(new queries_1.ITags(params)));
return data;
}
/**
* Fetches and parses tags from the Radio Browser API.
* @param params - The search parameters
* @param query - The search query
* @see {@link https://all.api.radio-browser.info/#List_of_tags} for more information on the API endpoint
* @returns A promise that resolves to the list of tags
*/
async getTags(params, query = '') {
const data = await this.fetchTags(params, query);
return this.jsonSerializer.deserialize(JSON.parse(data), data_1.Tag);
}
/**
* Fetches station checks from the Radio Browser API.
* @param params - The search parameters
* @param outputFormat - The output format for the list of station checks (default is JSON)
* @see {@link https://all.api.radio-browser.info/#List_of_station_check_results} for more information on the API endpoint
* @returns A promise that resolves to the list of station checks
*/
async fetchStationChecks(params, outputFormat = 'json') {
const { data } = await this.sendRequest('checks', outputFormat, this.jsonSerializer.serialize(new queries_1.IStationChecks(params)));
return data;
}
/**
* Fetches and parses station checks from the Radio Browser API.
* @param params - The search parameters
* @see {@link https://all.api.radio-browser.info/#List_of_station_check_results} for more information on the API endpoint
* @returns A promise that resolves to the list of station checks
*/
async getStationChecks(params) {
const data = await this.fetchStationChecks(params);
return this.jsonSerializer.deserialize(JSON.parse(data), data_1.StationCheck);
}
/**
* Fetches station clicks from the Radio Browser API.
* @param params - The search parameters
* @param outputFormat - The output format for the list of station clicks (default is JSON)
* @see {@link https://all.api.radio-browser.info/#List_of_station_clicks} for more information on the API endpoint
* @returns A promise that resolves to the list of station clicks
*/
async fetchStationClicks(params, outputFormat = 'json') {
const { data } = await this.sendRequest('clicks', outputFormat, this.jsonSerializer.serialize(new queries_1.IStationClicks(params)));
return data;
}
/**
* Fetches and parses station clicks from the Radio Browser API.
* @param params - The search parameters
* @see {@link https://all.api.radio-browser.info/#List_of_station_clicks} for more information on the API endpoint
* @returns A promise that resolves to the list of station clicks
*/
async getStationClicks(params) {
const data = await this.fetchStationClicks(params);
return this.jsonSerializer.deserialize(JSON.parse(data), data_1.StationClick);
}
/**
* Fetches station check steps from the Radio Browser API.
* @param params - The search parameters
* @param outputFormat - The output format for the list of station check steps (default is JSON)
* @see {@link https://all.api.radio-browser.info/#List_of_station_check_steps} for more information on the API endpoint
* @returns A promise that resolves to the list of station check steps
*/
async fetchStationCheckSteps(params, outputFormat = 'json') {
const { data } = await this.sendRequest('checksteps', outputFormat, this.jsonSerializer.serialize(new queries_1.IStationCheckSteps(params)));
return data;
}
/**
* Fetches and parses station check steps from the Radio Browser API.
* @param params - The search parameters
* @param outputFormat - The output format for the list of station check steps (default is JSON)
* @see {@link https://all.api.radio-browser.info/#List_of_station_check_steps} for more information on the API endpoint
* @returns A promise that resolves to the list of station check steps
*/
async getStationCheckSteps(params) {
const data = await this.fetchStationCheckSteps(params);
return this.jsonSerializer.deserialize(JSON.parse(data), data_1.StationCheckStep);
}
/**
* Fetches old versions of station from the Radio Browser API.
* @param params - The search parameters
* @param outputFormat - The output format for the list of station old versions (default is JSON)
* @see {@link https://all.api.radio-browser.info/#Old_versions_of_stations} for more information on the API endpoint
* @returns A promise that resolves to the list of station old versions
*/
async fetchStationOldVersion(params, stationUUID = '', outputFormat = 'json') {
const { data } = await this.sendRequest(stationUUID !== '' ? `stations/changed/${stationUUID}` : 'stations/changed', outputFormat, this.jsonSerializer.serialize(new queries_1.IStationOldVersion(params)));
return data;
}
/**
* Fetches and parses old versions of station from the Radio Browser API.
* @param params - The search parameters
* @see {@link https://all.api.radio-browser.info/#Old_versions_of_stations} for more information on the API endpoint
* @returns A promise that resolves to the list of station old versions
*/
async getStationOldVersion(params, stationUUID = '') {
const data = await this.fetchStationOldVersion(params, stationUUID);
return this.jsonSerializer.deserialize(JSON.parse(data), data_1.StationOldVersion);
}
/**
* Fetches server stats from the Radio Browser API.
* @param outputFormat - The output format for the server stats (default is JSON)
* @see {@link https://all.api.radio-browser.info/#Server_stats} for more information on the API endpoint
* @returns A promise that resolves to the server stats
*/
async fetchServerStats(outputFormat = 'json') {
const { data } = await this.sendRequest('stats', outputFormat, {});
return data;
}
/**
* Fetches and parses server stats from the Radio Browser API.
* @see {@link https://all.api.radio-browser.info/#Server_stats} for more information on the API endpoint
* @returns A promise that resolves to the server stats
*/
async getServerStats() {
const data = await this.fetchServerStats();
return this.jsonSerializer.deserialize(JSON.parse(data), data_1.ServerStats);
}
/**
* Fetches server config from the Radio Browser API.
* @param outputFormat - The output format for the server config (default is JSON)
* @see {@link https://all.api.radio-browser.info/#Server_config} for more information on the API endpoint
* @returns A promise that resolves to the server config
*/
async fetchServerConfig(outputFormat = 'json') {
const { data } = await this.sendRequest('config', outputFormat, {});
return data;
}
/**
* Fetches server config from the Radio Browser API.
* @see {@link https://all.api.radio-browser.info/#Server_config} for more information on the API endpoint
* @returns A promise that resolves to the server config
*/
async getServerConfig() {
const data = await this.fetchServerConfig();
return this.jsonSerializer.deserialize(JSON.parse(data), data_1.ServerConfig);
}
/**
* Votes for the station by given UUID.
* @see {@link https://all.api.radio-browser.info/#Vote_for_station} for more information on the API endpoint
* @param stationUUID - The UUID of the station to vote for
*/
async voteForStation(stationUUID) {
await this.sendRequest(`vote/${stationUUID}`, 'json', {});
}
/**
* Logs a click for the station by given UUID.
* @see {@link https://all.api.radio-browser.info/#Count_station_click} for more information on the API endpoint
* @param stationUUID - The UUID of the station for which the click is logged
*/
async click(stationUUID) {
await this.sendRequest(`url/${stationUUID}`, 'json', {});
}
}
exports.RadioBrowserClient = RadioBrowserClient;