lyric-karaoke-cli
Version:
A CLI application for displaying song lyrics in a karaoke-style format
62 lines (53 loc) • 1.66 kB
JavaScript
/**
* API module for communication with music APIs
*/
const axios = require('axios');
const { getApiKey, apiBaseUrl } = require('../config');
const { logger } = require('../utils');
// API base URLs and endpoints will vary based on which music API is chosen
const BASE_URL = apiBaseUrl || 'https://api.example.com/v1'; // Use config or fallback
/**
* Search for songs by query
* @param {string} query - Search query (song title, artist, etc.)
* @returns {Promise<Array>} - List of matching songs
*/
async function searchSongs(query) {
try {
const response = await axios.get(`${BASE_URL}/search`, {
params: {
q: query,
type: 'track',
limit: 10
},
headers: {
'Authorization': `Bearer ${getApiKey()}`
}
});
return response.data.results || [];
} catch (error) {
logger.error('Error searching for songs:', error.message);
throw new Error(`Failed to search for songs: ${error.message}`);
}
}
/**
* Get song details including lyrics
* @param {string} songId - ID of the song
* @returns {Promise<Object>} - Song details including lyrics
*/
async function getSongDetails(songId) {
try {
const response = await axios.get(`${BASE_URL}/songs/${songId}`, {
headers: {
'Authorization': `Bearer ${getApiKey()}`
}
});
return response.data;
} catch (error) {
logger.error(`Error fetching song details for ID ${songId}:`, error.message);
throw new Error(`Failed to fetch song details: ${error.message}`);
}
}
module.exports = {
searchSongs,
getSongDetails
};