Public
HTTP (deprecated)
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Readme

Val license badge generator

Generates readme badge from SPDX-License-Identifier comment.

![](https://vladimyr-licensebadge.web.val.run/v/vladimyr/licenseBadge)

Usage

https://vladimyr-licensebadge.web.val.run/v/<author>/<val>

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// SPDX-License-Identifier: 0BSD
import { fetchVal } from "https://esm.town/v/vladimyr/fetchVal";
import { formatLicenseInfo, parseLicenseInfo } from "https://esm.town/v/vladimyr/parseLicense";
import { serveReadme } from "https://esm.town/v/vladimyr/serveReadme";
import { toHonoHandler } from "https://esm.town/v/vladimyr/toHonoHandler";
import { badgen as createBadge, type BadgenOptions } from "npm:badgen";
import { Hono } from "npm:hono";
const router = new Hono();
router.get("/", toHonoHandler(serveReadme(import.meta.url)));
router.get("/v/:author/:name", async (c) => {
const { author, name } = c.req.param();
let badgeOptions: BadgenOptions = { label: "license", status: undefined };
try {
const { code } = await fetchVal(author, name);
try {
const info = parseLicenseInfo(code);
badgeOptions.status = formatLicenseInfo(info);
} catch {
badgeOptions.color = "gray";
badgeOptions.status = "invalid";
}
} catch (err) {
badgeOptions.color = "red";
badgeOptions.status = err?.code ?? "error";
}
return c.newResponse(
createBadge(badgeOptions),
{ headers: { "content-type": "image/svg+xml" } },
);
});
export default router.fetch;
vladimyr-licensebadge.web.val.run
March 8, 2024