24
loading...
This website collects cookies to deliver better user experience
npm
refers both to:npm
CLI tool installed by default with Node.js on your machinenpm install react
command in your Javascript project, you are downloading the react
package from the online npm registry.isomorphic-unfetch
and query-registry
packages as follows:npm install isomorphic-unfetch query-registry
GET
request to the /
endpoint, that is https://registry.npmjs.org/
.async function example1WithFetch() {
const endpoint = "https://registry.npmjs.org/";
const res = await fetch(endpoint);
const data = await res.json();
console.log(data);
}
async function example1WithQueryRegistry() {
const data = await queryRegistry.getRegistryMetadata();
console.log(data);
}
{
"db_name":"registry",
"engine":"couch_bt_engine",
"doc_count":2226548,
"doc_del_count":334,
"update_seq":5769731,
"purge_seq":0,
"compact_running":false,
"sizes":{
"active":57693928578,
"external":132154863659,
"file":58937123056
},
"disk_size":58937123056,
"data_size":57693928578,
"other":{
"data_size":132154863659
},
"instance_start_time":"1624686290809498",
"disk_format_version":7,
"committed_update_seq":5769731,
"compacted_seq":5729968,
"uuid":"964c127ddcbbd59982db296a0f9e8a56"
}
GET
request to the /<package>
endpoint, for example https://registry.npmjs.org/react or https://registry.npmjs.org/@types/node.async function example2WithFetch(name) {
const endpoint = `https://registry.npmjs.org/${name}`;
const res = await fetch(endpoint);
const data = await res.json();
console.log(data);
}
async function example2WithQueryRegistry(name) {
const data = await queryRegistry.getPackument({ name });
console.log(data);
}
{
"_id": "react",
"_rev": "1684-29eba7dd741dee3c165b86b7e4f63461",
"name": "react",
"description": "React is a JavaScript library for building user interfaces.",
"dist-tags": {…},
"versions": {…},
"maintainers": […],
"time": {…},
"repository": {…},
"readme": "",
"readmeFilename": "",
"homepage": "https://reactjs.org/",
"keywords": […],
"bugs": {…},
"users": {…},
"license": "MIT"
}
[email protected]
or @types/[email protected]
, we can send a GET
request to the /<package>/<version>
endpoint, for example https://registry.npmjs.org/react/17.0.2 or https://registry.npmjs.org/@types/node/15.14.0.async function example3WithFetch(name, version) {
const endpoint = `https://registry.npmjs.org/${name}/${version}`;
const res = await fetch(endpoint);
const data = await res.json();
console.log(data);
}
async function example3WithQueryRegistry(name, version) {
const data = await queryRegistry.getPackageManifest({ name, version });
console.log(data);
}
package.json
at publishing time plus some additional attributes added by the registry.{
"name": "react",
"description": "React is a JavaScript library for building user interfaces.",
"keywords": […],
"version": "17.0.2",
"homepage": "https://reactjs.org/",
"bugs": {…},
"license": "MIT",
"main": "index.js",
"repository": {…},
"engines": {…},
"dependencies": {…},
"browserify": {…},
"_id": "[email protected]",
"_nodeVersion": "15.11.0",
"_npmVersion": "7.6.0",
"dist": {…},
"_npmUser": {…},
"directories": {},
"maintainers": […],
"_npmOperationalInternal": {…},
"_hasShrinkwrap": false,
}
GET
request to the /-/v1/search?text=<some query>
endpoint, for example https://registry.npmjs.org/-/v1/search?text=react.author:velut
keyword parameter like this: https://registry.npmjs.org/-/v1/search?text=author:velut.async function example4WithFetch(text) {
const endpoint = `https://registry.npmjs.org/-/v1/search?text=${text}`;
const res = await fetch(endpoint);
const data = await res.json();
console.log(data);
}
async function example4WithQueryRegistry(text) {
const data = await queryRegistry.searchPackages({ query: { text } });
console.log(data);
}
objects
attribute. Each package comes with a small number of important attributes, including name
and version
, plus some score values for the package itself and for its relevance to our query.{
"objects": [
{
"package": {
"name": "react",
"scope": "unscoped",
"version": "17.0.2",
"description": "React is a JavaScript library for building user interfaces.",
"keywords": ["react"],
"date": "2021-03-22T21:56:19.536Z",
"links": {
"npm": "https://www.npmjs.com/package/react",
"homepage": "https://reactjs.org/",
"repository": "https://github.com/facebook/react",
"bugs": "https://github.com/facebook/react/issues"
},
"publisher": {
"username": "…",
"email": "…"
},
"maintainers": [
{ "username": "…", "email": "…" },
{ "username": "…", "email": "…" }
]
},
"score": {
"final": 0.5866665170132767,
"detail": {
"quality": 0.5246016720020373,
"popularity": 0.8931981392742823,
"maintenance": 0.3333333333333333
}
},
"searchScore": 100000.63
}
],
"total": 164637,
"time": "Fri Jul 02 2021 13:13:14 GMT+0000 (Coordinated Universal Time)"
}
GET
request to a slightly different API endpoint at https://api.npmjs.org/downloads/point/<period>/<package>
, for example https://api.npmjs.org/downloads/point/last-week/react. Supported time periods include last-day
, last-week
, last-month
and last-year
.async function example5WithFetch(name, period) {
const endpoint = `https://api.npmjs.org/downloads/point/${period}/${name}`;
const res = await fetch(endpoint);
const data = await res.json();
console.log(data);
}
async function example5WithQueryRegistry(name, period) {
const data = await queryRegistry.getPackageDownloads({ name, period });
console.log(data);
}
{
"downloads": 10889040,
"start": "2021-06-25",
"end": "2021-07-01",
"package": "react"
}
fetch("https://registry.npmjs.org/react").then(res => res.json()).then(console.log)
fetch("https://registry.npmjs.cf/react").then(res => res.json()).then(console.log)
async function bonusWithFetch(name) {
const endpoint = `https://registry.npmjs.cf/${name}`;
const res = await fetch(endpoint);
const data = await res.json();
console.log(data);
}
async function bonusWithQueryRegistry(name, registry) {
const data = await queryRegistry.getPackument({ name, registry });
console.log(data);
}