Example 1: Get information about the registry itself
// Example 1: Get information about the registry itself
async function example1WithFetch() {
const endpoint = "https://registry.npmjs.org/";
const res = await fetch(endpoint);
const data = await res.json();
console.log(data);
}
await example1WithFetch();
// Example 1: Get information about the registry itself
async function example1WithQueryRegistry() {
const data = await queryRegistry.getRegistryMetadata();
console.log(data);
}
await example1WithQueryRegistry();
Example 2: Get all available package metadata
// Example 2: Get all available package metadata
async function example2WithFetch(name) {
const endpoint = `https://registry.npmjs.org/${name}`;
const res = await fetch(endpoint);
const data = await res.json();
console.log(data);
}
await example2WithFetch("short-time-ago");
// Example 2: Get all available package metadata
async function example2WithQueryRegistry(name) {
const data = await queryRegistry.getPackument({ name });
console.log(data);
}
await example2WithQueryRegistry("short-time-ago");
Example 3: Get information about a specific version of a package
// Example 3: Get information about a specific version of a package
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);
}
await example3WithFetch("short-time-ago", "2.0.0");
// Example 3: Get information about a specific version of a package
async function example3WithQueryRegistry(name, version) {
const data = await queryRegistry.getPackageManifest({ name, version });
console.log(data);
}
await example3WithQueryRegistry("short-time-ago", "2.0.0");
Example 4: Search packages by text
// Example 4: Search packages by text
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);
}
await example4WithFetch("short time ago");
// Example 4: Search packages by text
async function example4WithQueryRegistry(text) {
const data = await queryRegistry.searchPackages({ query: { text } });
console.log(data);
}
await example4WithQueryRegistry("short time ago");
Example 5: Count downloads for a package
// Example 5: Count downloads for a package
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);
}
await example5WithFetch("react", "last-week");
// Example 5: Count downloads for a package
async function example5WithQueryRegistry(name, period) {
const data = await queryRegistry.getPackageDownloads({ name, period });
console.log(data);
}
await example5WithQueryRegistry("react", "last-week");
Bonus: Using a registry mirror
// Bonus: Using a registry mirror to get all available package metadata
async function bonusWithFetch(name) {
const endpoint = `https://registry.npmjs.cf/${name}`;
const res = await fetch(endpoint);
const data = await res.json();
console.log(data);
}
await bonusWithFetch("short-time-ago");
// Bonus: Using a registry mirror to get all available package metadata
async function bonusWithQueryRegistry(name, registry) {
const data = await queryRegistry.getPackument({ name, registry });
console.log(data);
}
await bonusWithQueryRegistry("short-time-ago", "https://registry.npmjs.cf");