Skip to content

Use the unpkg instead of the npm registry? #7

Description

@tunnckoCore

Because it may be slow, it may crash and what not. While developing Badgen.net Service, we concluded to just switch to https://unpkg.com/${name}@${tag}/package.json.

Not to mention how confusing this npm API can be. It's total madness, seriously. This comment in the current master of get-pkg is one of many things. With Unpkg we everything is a whole lot easier and faster - you have scoped support, versions for all scoped/non-scoped, versions for tagged/non-tagged and etc.

Examples:

@foo/bar
@foo/bar@1.2.3
@foo/bar@next
bar
bar@1.2.3
bar@next

Or at least, we can allow it through some option, so it will unpkg and fallback to yarn.

I end up with this in few minutes (because non of the other packages does it well or are by Sindre, or are too old), before even realizing that get-pkg exist (i started to forget this good ol' packages - you are everywhere 🤣 ).

const parsePackage = require('parse-package-name')
const axios = require('axios')

// packageName: String, endpoint: (name: String, tag: String = 'latest'): String => {}
export default async function packageJson(packageName, endpoint) {
  const { name, version } = parsePackage(packageName)
  const tag = version === '' ? 'latest' : version
  const url = typeof endpoint === 'function'
    ? endpoint(name, tag)
    : `https://unpkg.com/${name}@${tag}/package.json`

  return axios.get(url).then((res) => res.data)
}

/cc @jonschlinkert @doowb

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions