Skip to content

Commit

Permalink
fix(api): return empty stats object if font exists but no stats (#939)
Browse files Browse the repository at this point in the history
* fix(api): return empty stats object if font exists but no stats

* Create wise-dolls-wait.md
  • Loading branch information
ayuhito committed Feb 11, 2024
1 parent 328a354 commit 0b94dd6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .changeset/wise-dolls-wait.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"cdn": patch
"api": patch
---

fix(api): return empty stats object if font exists but no stats
3 changes: 3 additions & 0 deletions api/cdn/wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@ services = [
logpush = true
workers_dev = false

[triggers]
crons = ["0 0 * * *"]

[miniflare.mounts]
api = "../api/"
25 changes: 23 additions & 2 deletions api/metadata/src/stats/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,30 @@ router.get('/v1/stats/:id', withParams, async (request, env, ctx) => {
throw new StatusError(404, 'Not found. Font does not exist.');
}

const stats = await getPackageStat(id, env, ctx);
let stats = await getPackageStat(id, env, ctx);
if (!stats) {
throw new StatusError(500, 'Internal Server Error. Stats list empty.');
stats = {
total: {
npmDownloadMonthly: 0,
npmDownloadTotal: 0,
jsDelivrHitsMonthly: 0,
jsDelivrHitsTotal: 0,
},
static: {
npmDownloadMonthly: 0,
npmDownloadTotal: 0,
jsDelivrHitsMonthly: 0,
jsDelivrHitsTotal: 0,
},
variable: metadata.variable
? {
npmDownloadMonthly: 0,
npmDownloadTotal: 0,
jsDelivrHitsMonthly: 0,
jsDelivrHitsTotal: 0,
}
: undefined,
};
}

response = json(stats, {
Expand Down

0 comments on commit 0b94dd6

Please sign in to comment.