Friday Fun Post: fetch lyrics from a public API

Functional Javascript - Aug 28 '20 - - Dev Community

A simple api fetch idiom you can apply for any api.

For this particular api, there is no api key required that you must sign up for, for example like the wordnik api.

import fetchByAxiosObj from "@root/fetch/src/fetchByAxiosObj";

/*
@func
fetch lyrics by artist name (band name) and song title

@typedef {{lyrics: string}} lyricsObj
@param {string} artist
@param {string} song
@return {Promise<lyricsObj>}
*/
const fetchLyrics = async (artist, song) => await fetchByAxiosObj(`https://api.lyrics.ovh/v1/${artist}/${song}`);

Test it out

import { lpromise } from "@root/libs/src/loggers/consolelog";

//@tests
lpromise(fetchLyrics("Coldplay", "Adventure of a Lifetime"));
/*
@output
{
  lyrics: 'Turn your magic on\n' +
    "Umi she'd say\n" +
    "Everything you want's a dream away\n" +
    'And we are legends every day\n' +
    "That's what she told me\n" +
    '\n' +
    'Turn your magic on\n' +
    "To me she'd say\n" +
    "Everything you want's a dream away\n" +
    'Under this pressure, under this weight\n' +
    'We are diamonds\n' +
    '\n' +
    'I feel my heart beating\n' +
    'I feel my heart underneath my skin\n' +
    'I feel my heart beating\n' +
    'Oh, you make me feel\n' +
    "Like I'm alive again\n" +
    '(Alive again)\n' +
    'Oh, you make me feel\n' +
    "Like I'm alive again\n" +
    '\n' +
    "Said I can't go on, not in this way\n" +
    "I'm a dream that died by light of day\n" +
    'Gonna hold up half the sky and say\n' +
    'Only I own me\n' +
    '\n' +
    'I feel my heart beating\n' +
    'I feel my heart underneath my skin\n' +
    'Oh, I can feel my heart beating\n' +
    "'Cause you make me feel\n" +
    "Like I'm alive again\n" +
    '(Alive again)\n' +
    'Oh, you make me feel\n' +
    "Like I'm alive again\n" +
    '\n' +
    'Turn your magic on\n' +
    "Umi she'd say\n" +
    "Everything you want's a dream away\n" +
    'Under this pressure, under this weight\n' +
    'We are diamonds taking shape\n' +
    'We are diamonds taking shape\n' +
    '\n' +
    'Woo hoo\n' +
    'Woo hoo\n' +
    '\n' +
    "If we've only got this life\n" +
    'This adventure, oh, then I\n' +
    "And if we've only got this life\n" +
    'You get me through, ah\n' +
    "And if we've only got this life\n" +
    'In this adventure, oh, then I\n' +
    'Want to share it with you\n' +
    'With you\n' +
    'With you\n' +
    'I see you, I see you\n' +
    '\n' +
    'Woo hoo (Woo hoo)\n' +
    'Woo hoo (Woo hoo)\n' +
    'Woo hoo (Woo hoo)\n' +
    'Woo hoo (Woo hoo)\n' +
    '\n' +
    'Woo hoo (Woo hoo)\n' +
    'Woo hoo (Woo hoo)\n' +
    'Woo hoo (Woo hoo)\n' +
    'Woo hoo (Woo hoo)\n' +
    '\n' +
    'Woo hoo (Woo hoo)\n' +
    'Woo hoo (Woo hoo)\n' +
    'Woo hoo (Woo hoo)\n' +
    'Woo hoo (Woo hoo)'
}
*/

Notes

a.

The fetchByAxiosObj func is a simple strongly-typed wrapper around the axios fetch library.
I always wrap third-party npm libraries to enforce constraints.

b.

the lpromise func is a simple wrapper that logs the result of a promise.

Finale

If you have any questions on the pros and cons of the architectural design of this let me know.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player