UNPKG

npm-registry-sdk

Version:

A fully type-safe client library for the npm registry API.

35 lines 1.37 kB
import { RegistryError } from "../error.js"; export async function request(registry, options) { const { params, endpoint, ...request } = options; const url = new URL(registry + endpoint); if (params) { for (const [key, value] of Object.entries(params)) { if (value) url.searchParams.append(key, String(value)); } } let urlString = url.origin + url.pathname; if (url.search) { // Base path; path segments like %2F remain encoded. // url.search is the raw, percent-encoded query string (e.g., "?q=scope%3Atypes") or empty. // This is needed to make API happy with the qualifiers, // as it expects special characters (e.g., ':') in query values to be unencoded. // decodeURIComponent("?q=scope%3Atypes") results in "?q=scope:types". urlString += decodeURIComponent(url.search); } const response = await fetch(urlString, { method: 'GET', signal: request.signal, headers: { 'Content-Type': 'application/json', Accept: 'application/json', ...request.headers, }, }); const responseData = await response.json(); if (!response.ok) { throw new RegistryError(responseData.error, responseData.code); } return responseData; } //# sourceMappingURL=request.js.map