Skip to content

Commit f6a3a11

Browse files
committed
fix: downloads/point endpoint raise server error when a package has no downloads
1 parent b5ca720 commit f6a3a11

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

src/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,17 @@ export default class VerdaccioMiddlewarePlugin implements IPluginMiddleware<Cust
6969
});
7070
// this.logger.debug(`[install-counts] metadata: ${JSON.stringify(metadata)}`);
7171
const results = await getPackageDownloadTimeSeriesResults(self.redisClient, startDate, endDate, packageName);
72+
const hasResults = results !== null && results.length > 0;
73+
this.logger.debug(`[install-counts] results: ${JSON.stringify(results)}`);
7274
let totalDownloads = 0;
73-
if (results !== null) {
75+
if (hasResults) {
7476
for (const [timestamp, count] of results) {
7577
totalDownloads += Number(count);
7678
}
7779
}
7880
// Return the total downloads for the package in the period
79-
const realStartDate = results === null ? startDate : new Date(Number(results[0][0]));
80-
const realEndDate = results === null ? endDate : new Date(Number(results[results.length - 1][0]));
81+
const realStartDate = hasResults ? new Date(Number(results[0][0])) : startDate;
82+
const realEndDate = hasResults ? new Date(Number(results[results.length - 1][0])) : endDate;
8183
const response = {
8284
downloads: totalDownloads,
8385
start: realStartDate.toISOString().substring(0, 10),

0 commit comments

Comments
 (0)