forked from badges/shields
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Crates] Added crate size badge (badges#10421)
* add some basic tests for size badge * add crate size to crates fetch * add crate size service * change test to reflect base-10 counting instead * fix downloads tests to work with new crate_size scheme addition * move handling of unknown size to handle
- Loading branch information
1 parent
1a09e9a
commit bdf84f9
Showing
4 changed files
with
86 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import prettyBytes from 'pretty-bytes' | ||
import { InvalidResponse, pathParams } from '../index.js' | ||
import { BaseCratesService, description } from './crates-base.js' | ||
|
||
export default class CratesSize extends BaseCratesService { | ||
static category = 'size' | ||
static route = { | ||
base: 'crates/size', | ||
pattern: ':crate/:version?', | ||
} | ||
|
||
static openApi = { | ||
'/crates/size/{crate}': { | ||
get: { | ||
summary: 'Crates.io Size', | ||
description, | ||
parameters: pathParams({ | ||
name: 'crate', | ||
example: 'rustc-serialize', | ||
}), | ||
}, | ||
}, | ||
'/crates/size/{crate}/{version}': { | ||
get: { | ||
summary: 'Crates.io Size (version)', | ||
description, | ||
parameters: pathParams( | ||
{ | ||
name: 'crate', | ||
example: 'rustc-serialize', | ||
}, | ||
{ | ||
name: 'version', | ||
example: '0.3.24', | ||
}, | ||
), | ||
}, | ||
}, | ||
} | ||
|
||
render({ size }) { | ||
return { | ||
label: 'size', | ||
message: prettyBytes(size), | ||
color: 'blue', | ||
} | ||
} | ||
|
||
async handle({ crate, version }) { | ||
const json = await this.fetch({ crate, version }) | ||
const size = this.constructor.getVersionObj(json).crate_size | ||
|
||
if (size == null) { | ||
throw new InvalidResponse({ prettyMessage: 'unknown' }) | ||
} | ||
|
||
return this.render({ size }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { createServiceTester } from '../tester.js' | ||
import { isFileSize } from '../test-validators.js' | ||
export const t = await createServiceTester() | ||
|
||
t.create('size') | ||
.get('/tokio.json') | ||
.expectBadge({ label: 'size', message: isFileSize }) | ||
|
||
t.create('size (with version)') | ||
.get('/tokio/1.32.0.json') | ||
.expectBadge({ label: 'size', message: '725 kB' }) | ||
|
||
t.create('size (with version where version doesnt have size)') | ||
.get('/tokio/0.1.6.json') | ||
.expectBadge({ label: 'crates.io', message: 'unknown' }) | ||
|
||
t.create('size (not found)') | ||
.get('/not-a-crate.json') | ||
.expectBadge({ label: 'crates.io', message: 'not found' }) |