query-registry
Advanced tools
Comparing version 2.0.0-3 to 2.0.0-4
@@ -13,2 +13,3 @@ # Changelog | ||
- Separate functions to query the npm registry | ||
- getDailyPackageDownloads() | ||
@@ -25,2 +26,6 @@ - getDailyRegistryDownloads() | ||
- Now the package can be used in a browser environment | ||
- BREAKING CHANGE: Added export map to `package.json` | ||
### Changed | ||
@@ -27,0 +32,0 @@ |
@@ -118,4 +118,5 @@ 'use strict'; | ||
}) { | ||
if (cached && cache.has(url)) { | ||
const cachedJSON = cache.get(url); | ||
const cachedJSON = cache.get(url); | ||
if (cached && cachedJSON) { | ||
log('fetch: returning cached response: %O', { | ||
@@ -122,0 +123,0 @@ url, |
@@ -1,2 +0,2 @@ | ||
"use strict";function r(r){return r&&"object"==typeof r&&"default"in r?r.default:r}Object.defineProperty(exports,"__esModule",{value:!0});var e=r(require("validate-npm-package-name")),t=require("make-error"),n=r(t),a=r(require("debug")),s=r(require("isomorphic-unfetch")),i=r(require("tiny-lru")),o=r(require("git-url-parse"));const c=["https://registry.npmjs.cf","https://registry.yarnpkg.com"];class u extends t.BaseError{constructor(r,e){super(`fetch: request to ${r} failed with status ${e.statusText}`),this.url=r,this.response=e}}const d=n("InvalidPackageNameError"),p=n("InvalidPackageVersionError"),g=a("query-registry");function y({name:r}){const{validForOldPackages:t,validForNewPackages:n}=e(r);if(!t&&!n)throw g("assertValidPackageName: invalid package name: %O",{name:r}),new d(`invalid package name: '${r}'`)}const l=i(250,3e5);async function m({url:r,cached:e=!0}){if(e&&l.has(r)){const e=l.get(r);return g("fetch: returning cached response: %O",{url:r,cachedJSON:e}),e}const t=await s(r);if(!t.ok)throw g("fetch: request failed: %O",{url:r,status:t.statusText,response:t}),new u(r,t);const n=await t.json();return e&&l.set(r,n),g("fetch: returning fresh response: %O",{url:r,json:n}),n}async function f({endpoint:r,query:e,registry:t="https://registry.npmjs.org",mirrors:n=c,cached:a}){const s=[t,...n].map(t=>{const n=new URL(r,t);return n.search=null!=e?e:"",n.href});let i;for(const r of s)try{return await m({url:r,cached:a})}catch(r){i=r}throw g("fetchFromRegistry: cannot retrieve data from registry or mirrors: %O",{endpoint:r,query:e,registry:t,mirrors:n,lastError:i}),i}async function h({endpoint:r,registryDownloadsAPI:e="https://api.npmjs.org",cached:t}){return f({endpoint:r,registry:e,mirrors:[],cached:t})}function w({rawDownloadPeriod:r="last-week"}){if("string"==typeof r)return r;if(r instanceof Date)return P(r);const{start:e,end:t}=r;return`${P(e)}:${P(t)}`}function P(r){return r.toISOString().split("T")[0]}function k({rawPackument:r,version:e="latest"}){var t;const{name:n,"dist-tags":a,versions:s}=r,i=s[null!=(t=a[e])?t:e];if(!i)throw g("getPackageManifest: invalid package version: %O",{name:n,version:e}),new p(`invalid package version: '${n}@${e}'`);return i}async function v({name:r,registry:e,mirrors:t,cached:n}){return y({name:r}),f({endpoint:"/"+r,registry:e,mirrors:t,cached:n})}async function x({name:r,version:e,registry:t,mirrors:n,cached:a}){return k({rawPackument:await v({name:r,registry:t,mirrors:n,cached:a}),version:e})}function D({rawLicense:r}){if(r&&"string"==typeof r)return r}function $({rawRepository:r}){return function(r){return r&&"object"==typeof r&&"string"==typeof r.url&&["string","undefined"].includes(typeof r.type)&&["string","undefined"].includes(typeof r.directory)}(r)?R({rawRepository:r}):"string"==typeof r?R({rawRepository:{url:r}}):void 0}function R({rawRepository:r}){const{url:e,directory:t}=r,n=function({url:r}){let e;try{e=o(r)}catch{}return e}({url:e});if(!n)return;const{source:a,full_name:s,filepath:i}=n;return{type:"git",url:`https://${a.replace(/^$/,"github.com").replace(/^github$/,"github.com").replace(/^gitlab$/,"gitlab.com").replace(/^bitbucket$/,"bitbucket.org")}/${s}`,directory:null!=t?t:""!==i?i:void 0}}exports.FetchError=u,exports.InvalidPackageNameError=d,exports.InvalidPackageVersionError=p,exports.cloudflareRegistry="https://registry.npmjs.cf",exports.getDailyPackageDownloads=async function({name:r,period:e,registryDownloadsAPI:t,cached:n}){return y({name:r}),h({endpoint:`/downloads/range/${w({rawDownloadPeriod:e})}/${r}`,registryDownloadsAPI:t,cached:n})},exports.getDailyRegistryDownloads=async function({period:r,registryDownloadsAPI:e,cached:t}={}){return h({endpoint:"/downloads/range/"+w({rawDownloadPeriod:r}),registryDownloadsAPI:e,cached:t})},exports.getPackageDownloads=async function({name:r,period:e,registryDownloadsAPI:t,cached:n}){return y({name:r}),h({endpoint:`/downloads/point/${w({rawDownloadPeriod:e})}/${r}`,registryDownloadsAPI:t,cached:n})},exports.getPackageManifest=async function({name:r,version:e,registry:t,mirrors:n,cached:a}){const s=await v({name:r,registry:t,mirrors:n,cached:a}),i=k({rawPackument:s,version:e});return await async function({rawPackageManifest:r,rawPackument:e,registry:t,mirrors:n,cached:a}){const{_id:s,name:i,version:o,license:c,repository:u,_npmUser:d}=r,p=e.time[o],g=D({rawLicense:c}),y=$({rawRepository:u}),l=await async function({rawPackageManifest:r,registry:e,mirrors:t,cached:n}){const{name:a,types:s,typings:i}=r,o=function({name:r}){return r.startsWith("@types/")?r:"@types/"+r.replace("@","").replace("/","__")}({name:a});if(a===o||s||i)return;let c=!1;try{const{deprecated:r}=await x({name:o,registry:e,mirrors:t,cached:n});c=void 0===r}catch{}return c?o:void 0}({rawPackageManifest:r,registry:t,mirrors:n,cached:a}),m=function({name:r}){if(!r.startsWith("@types/"))return;const[e,t]=r.replace("@types/","").split("__");return t?`@${e}/${t}`:e}({name:i});return{...r,id:s,createdAt:p,publisher:d,license:g,gitRepository:y,definitelyTypedName:l,untypedName:m}}({rawPackageManifest:i,rawPackument:s,registry:t,mirrors:n,cached:a})},exports.getPackument=async function({name:r,registry:e,mirrors:t,cached:n}){return function({rawPackument:r}){const{_id:e,"dist-tags":t,time:{...n},license:a,repository:s}=r,i=D({rawLicense:a}),o=$({rawRepository:s});return{...r,id:e,distTags:t,versionsToTimestamps:n,license:i,gitRepository:o}}({rawPackument:await v({name:r,registry:e,mirrors:t,cached:n})})},exports.getRawPackageManifest=x,exports.getRawPackument=v,exports.getRegistryDownloads=async function({period:r,registryDownloadsAPI:e,cached:t}={}){return h({endpoint:"/downloads/point/"+w({rawDownloadPeriod:r}),registryDownloadsAPI:e,cached:t})},exports.getRegistryMetadata=async function({registry:r,cached:e}={}){return f({registry:r,mirrors:[],endpoint:"/",cached:e})},exports.npmRegistry="https://registry.npmjs.org",exports.npmRegistryDownloadsAPI="https://api.npmjs.org",exports.npmRegistryMirrors=c,exports.searchPackages=async function({query:r,registry:e,mirrors:t,cached:n}){return f({endpoint:"/-/v1/search",query:function({rawSearchCriteria:r}){return Object.entries(r).filter(([,r])=>["string","number"].includes(typeof r)).map(([r,e])=>`${r}=${e}`).join("&")}({rawSearchCriteria:r}),registry:e,mirrors:t,cached:n})},exports.yarnRegistry="https://registry.yarnpkg.com"; | ||
"use strict";function r(r){return r&&"object"==typeof r&&"default"in r?r.default:r}Object.defineProperty(exports,"__esModule",{value:!0});var e=r(require("validate-npm-package-name")),t=require("make-error"),n=r(t),a=r(require("debug")),s=r(require("isomorphic-unfetch")),i=r(require("tiny-lru")),o=r(require("git-url-parse"));const c=["https://registry.npmjs.cf","https://registry.yarnpkg.com"];class u extends t.BaseError{constructor(r,e){super(`fetch: request to ${r} failed with status ${e.statusText}`),this.url=r,this.response=e}}const d=n("InvalidPackageNameError"),p=n("InvalidPackageVersionError"),g=a("query-registry");function y({name:r}){const{validForOldPackages:t,validForNewPackages:n}=e(r);if(!t&&!n)throw g("assertValidPackageName: invalid package name: %O",{name:r}),new d(`invalid package name: '${r}'`)}const l=i(250,3e5);async function m({url:r,cached:e=!0}){const t=l.get(r);if(e&&t)return g("fetch: returning cached response: %O",{url:r,cachedJSON:t}),t;const n=await s(r);if(!n.ok)throw g("fetch: request failed: %O",{url:r,status:n.statusText,response:n}),new u(r,n);const a=await n.json();return e&&l.set(r,a),g("fetch: returning fresh response: %O",{url:r,json:a}),a}async function f({endpoint:r,query:e,registry:t="https://registry.npmjs.org",mirrors:n=c,cached:a}){const s=[t,...n].map(t=>{const n=new URL(r,t);return n.search=null!=e?e:"",n.href});let i;for(const r of s)try{return await m({url:r,cached:a})}catch(r){i=r}throw g("fetchFromRegistry: cannot retrieve data from registry or mirrors: %O",{endpoint:r,query:e,registry:t,mirrors:n,lastError:i}),i}async function h({endpoint:r,registryDownloadsAPI:e="https://api.npmjs.org",cached:t}){return f({endpoint:r,registry:e,mirrors:[],cached:t})}function w({rawDownloadPeriod:r="last-week"}){if("string"==typeof r)return r;if(r instanceof Date)return P(r);const{start:e,end:t}=r;return`${P(e)}:${P(t)}`}function P(r){return r.toISOString().split("T")[0]}function k({rawPackument:r,version:e="latest"}){var t;const{name:n,"dist-tags":a,versions:s}=r,i=s[null!=(t=a[e])?t:e];if(!i)throw g("getPackageManifest: invalid package version: %O",{name:n,version:e}),new p(`invalid package version: '${n}@${e}'`);return i}async function v({name:r,registry:e,mirrors:t,cached:n}){return y({name:r}),f({endpoint:"/"+r,registry:e,mirrors:t,cached:n})}async function x({name:r,version:e,registry:t,mirrors:n,cached:a}){return k({rawPackument:await v({name:r,registry:t,mirrors:n,cached:a}),version:e})}function D({rawLicense:r}){if(r&&"string"==typeof r)return r}function $({rawRepository:r}){return function(r){return r&&"object"==typeof r&&"string"==typeof r.url&&["string","undefined"].includes(typeof r.type)&&["string","undefined"].includes(typeof r.directory)}(r)?R({rawRepository:r}):"string"==typeof r?R({rawRepository:{url:r}}):void 0}function R({rawRepository:r}){const{url:e,directory:t}=r,n=function({url:r}){let e;try{e=o(r)}catch{}return e}({url:e});if(!n)return;const{source:a,full_name:s,filepath:i}=n;return{type:"git",url:`https://${a.replace(/^$/,"github.com").replace(/^github$/,"github.com").replace(/^gitlab$/,"gitlab.com").replace(/^bitbucket$/,"bitbucket.org")}/${s}`,directory:null!=t?t:""!==i?i:void 0}}exports.FetchError=u,exports.InvalidPackageNameError=d,exports.InvalidPackageVersionError=p,exports.cloudflareRegistry="https://registry.npmjs.cf",exports.getDailyPackageDownloads=async function({name:r,period:e,registryDownloadsAPI:t,cached:n}){return y({name:r}),h({endpoint:`/downloads/range/${w({rawDownloadPeriod:e})}/${r}`,registryDownloadsAPI:t,cached:n})},exports.getDailyRegistryDownloads=async function({period:r,registryDownloadsAPI:e,cached:t}={}){return h({endpoint:"/downloads/range/"+w({rawDownloadPeriod:r}),registryDownloadsAPI:e,cached:t})},exports.getPackageDownloads=async function({name:r,period:e,registryDownloadsAPI:t,cached:n}){return y({name:r}),h({endpoint:`/downloads/point/${w({rawDownloadPeriod:e})}/${r}`,registryDownloadsAPI:t,cached:n})},exports.getPackageManifest=async function({name:r,version:e,registry:t,mirrors:n,cached:a}){const s=await v({name:r,registry:t,mirrors:n,cached:a}),i=k({rawPackument:s,version:e});return await async function({rawPackageManifest:r,rawPackument:e,registry:t,mirrors:n,cached:a}){const{_id:s,name:i,version:o,license:c,repository:u,_npmUser:d}=r,p=e.time[o],g=D({rawLicense:c}),y=$({rawRepository:u}),l=await async function({rawPackageManifest:r,registry:e,mirrors:t,cached:n}){const{name:a,types:s,typings:i}=r,o=function({name:r}){return r.startsWith("@types/")?r:"@types/"+r.replace("@","").replace("/","__")}({name:a});if(a===o||s||i)return;let c=!1;try{const{deprecated:r}=await x({name:o,registry:e,mirrors:t,cached:n});c=void 0===r}catch{}return c?o:void 0}({rawPackageManifest:r,registry:t,mirrors:n,cached:a}),m=function({name:r}){if(!r.startsWith("@types/"))return;const[e,t]=r.replace("@types/","").split("__");return t?`@${e}/${t}`:e}({name:i});return{...r,id:s,createdAt:p,publisher:d,license:g,gitRepository:y,definitelyTypedName:l,untypedName:m}}({rawPackageManifest:i,rawPackument:s,registry:t,mirrors:n,cached:a})},exports.getPackument=async function({name:r,registry:e,mirrors:t,cached:n}){return function({rawPackument:r}){const{_id:e,"dist-tags":t,time:{...n},license:a,repository:s}=r,i=D({rawLicense:a}),o=$({rawRepository:s});return{...r,id:e,distTags:t,versionsToTimestamps:n,license:i,gitRepository:o}}({rawPackument:await v({name:r,registry:e,mirrors:t,cached:n})})},exports.getRawPackageManifest=x,exports.getRawPackument=v,exports.getRegistryDownloads=async function({period:r,registryDownloadsAPI:e,cached:t}={}){return h({endpoint:"/downloads/point/"+w({rawDownloadPeriod:r}),registryDownloadsAPI:e,cached:t})},exports.getRegistryMetadata=async function({registry:r,cached:e}={}){return f({registry:r,mirrors:[],endpoint:"/",cached:e})},exports.npmRegistry="https://registry.npmjs.org",exports.npmRegistryDownloadsAPI="https://api.npmjs.org",exports.npmRegistryMirrors=c,exports.searchPackages=async function({query:r,registry:e,mirrors:t,cached:n}){return f({endpoint:"/-/v1/search",query:function({rawSearchCriteria:r}){return Object.entries(r).filter(([,r])=>["string","number"].includes(typeof r)).map(([r,e])=>`${r}=${e}`).join("&")}({rawSearchCriteria:r}),registry:e,mirrors:t,cached:n})},exports.yarnRegistry="https://registry.yarnpkg.com"; | ||
//# sourceMappingURL=query-registry.cjs.production.min.js.map |
@@ -13,4 +13,5 @@ import { FetchError } from './errors.esm.js'; | ||
}) { | ||
if (cached && cache.has(url)) { | ||
const cachedJSON = cache.get(url); | ||
const cachedJSON = cache.get(url); | ||
if (cached && cachedJSON) { | ||
log('fetch: returning cached response: %O', { | ||
@@ -17,0 +18,0 @@ url, |
{ | ||
"name": "query-registry", | ||
"description": "Query the npm registry for packuments, manifests, packages and download counts", | ||
"version": "2.0.0-3", | ||
"version": "2.0.0-4", | ||
"author": "velut", | ||
@@ -103,3 +103,3 @@ "license": "MIT", | ||
"nock": "13.0.7", | ||
"np": "7.2.0", | ||
"np": "7.3.0", | ||
"prettier": "2.2.1", | ||
@@ -109,5 +109,5 @@ "setup-polly-jest": "0.9.1", | ||
"tslib": "2.1.0", | ||
"typescript": "4.1.3" | ||
"typescript": "4.1.5" | ||
}, | ||
"peerDependencies": {} | ||
} |
@@ -17,4 +17,4 @@ import unfetch from 'isomorphic-unfetch'; | ||
}): Promise<any> { | ||
if (cached && cache.has(url)) { | ||
const cachedJSON = cache.get(url); | ||
const cachedJSON = cache.get(url); | ||
if (cached && cachedJSON) { | ||
log('fetch: returning cached response: %O', { url, cachedJSON }); | ||
@@ -21,0 +21,0 @@ return cachedJSON; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
348919