get-release npm module Solved, question with improved code
I've created a simple npm module and CLI program to get the latest GitHub and Bitbucket release from their APIs using Node.js. Please tell me if there's anything to improve!
Module code:
#!/usr/bin/env node
const fetch = require(&quot;node-fetch&quot;);

/**
 * @param {string} string
 * @return {string}
 */
const normalize = (string) =&gt; {
    try {
        return string.toUpperCase().trim()
    } catch (e) {
        return string
    }
}

/**
 * @param {string} provider
 * @param {string} user
 * @param {string} repo
 * @param {string} part
 */
module.exports.getRelease = async ({provider, user, repo, part = &quot;&quot;}) =&gt; {
    if (normalize(provider) === normalize(&quot;github&quot;)) {
        let json = (await (await fetch(`https://api.github.com/repos/${user}/${repo}/releases/latest`)).json())
        if (json.message === &quot;Not Found&quot;) throw &quot;Invalid repository&quot;
        if (!(&quot;assets&quot; in json)) throw &quot;Rate limit exceeded&quot;
        let browser_download_urls = json.assets.map(asset =&gt; asset.browser_download_url)
        return browser_download_urls.filter(url =&gt; url.includes(part))
    } else if (normalize(provider) === normalize(&quot;bitbucket&quot;)) {
        let json = (await (await fetch(`https://api.bitbucket.org/2.0/repositories/${user}/${repo}/downloads/`)).json())
        if (json.type === &quot;error&quot;) throw &quot;Invalid repository&quot;
        let links = json.values.map(value =&gt; value.links.self.href)
        return links.filter(url =&gt; url.includes(part))
    } else {
        throw &quot;Invalid provider&quot;
    }
}

const usage = _ =&gt; {
    console.log(
        `Usage: get-release (github|bitbucket) user repo [partofreleasefile]
   Ex: get-release github phhusson treble_experimentations
       get-release github phhusson treble_experimentations arm64-ab-gapps
       get-release bitbucket JesusFreke smali
       get-release bitbucket JesusFreke smali baksmali`
    )
    process.exit(1)
}

// If via CLI
if (require.main === module) {
    let args = process.argv.slice(2)
    if (args.length !== 3 &amp;&amp; args.length !== 4) {
        usage()
    }
    module.exports.getRelease({
        provider: args[0],
        user: args[1],
        repo: args[2],
        part: args[3]
    }).then(result =&gt; {
        if (result.length !== 1) {
            console.log(result)
        } else {
            console.log(result[0])
        }
    }).catch(error =&gt; {
        console.log(error)
        usage()
        process.exit(1)
    })
}

Called using:
const { getRelease } = require(&quot;get-release&quot;)
;(async _ =&gt; {
  let url = await getRelease(
      {
          provider: &quot;github&quot;,
          user: &quot;phhusson&quot;,
          repo: &quot;treble_experimentations&quot;,
          part: &quot;arm64-ab-gapps&quot;
      }
  )
  console.log(url[0])
})()
Consider using a hashmap of provider -&gt; process. This will enable you to register new providers with relative ease.
async function githubRelease({user, repo, part = &quot;&quot;}) {
  let json = (await (await fetch(`https://api.github.com/repos/${user}/${repo}/releases/latest`)).json())
  if (json.message === &quot;Not Found&quot;) throw &quot;Invalid repository&quot;
  if (!(&quot;assets&quot; in json)) throw &quot;Rate limit exceeded&quot;
  let browser_download_urls = json.assets.map(asset =&gt; asset.browser_download_url)
  return browser_download_urls.filter(url =&gt; url.includes(part))
}

It will then be mapped inside the getRelease function as follows:
module.exports.getRelease = async ({provider, user, repo, part = &quot;&quot;}) =&gt; {
    if !(providerMethods[normalise(provider)]) {
        throw &quot;Invalid provider&quot;
    }
    return providerMethods[normalise(provider)](user, repo, part)
}

where providerMethods would be:
let providerMethods = {
    normalise(&quot;github&quot;): githubReleases
}

This will enable users of your module to register new (or private) registry providers. They can add a new provider to the providerMethods object (gitlab etc) and don't have to wait for you to update the library first for supporting any extra providers (you can add providers if popular).
The await (await fetch()).json() can be extracted to another function. Although, I'd prefer using the promise.then(r =&gt; r.json()).catch() chain over multiple awaits, it is entirely dependent on your comfort with the language.