github-profile-trophy-reiyu.../index.ts
Spenser Black ac3f80b5cb
Provide descriptive error when username missing
Adds more text to the error to hopefully inform users that they failed
to provide a proper URL, and not that the site is broken.

Error changed to generic 400 since the problem is that the user didn't
make a good request, not that the page itself is missing.
2022-02-10 11:04:05 -05:00

92 lines
2.7 KiB
TypeScript

import { ServerRequest } from "./deps.ts";
import { GithubAPIClient } from "./src/github_api_client.ts";
import { Card } from "./src/card.ts";
import { CONSTANTS, parseParams } from "./src/utils.ts";
import { COLORS, Theme } from "./src/theme.ts";
import { Error400 } from './src/error_page.ts';
import "https://deno.land/x/dotenv@v0.5.0/load.ts";
const client = new GithubAPIClient();
export default async (req: ServerRequest) => {
const params = parseParams(req);
const username = params.get("username");
const row = params.getNumberValue("row", CONSTANTS.DEFAULT_MAX_ROW);
const column = params.getNumberValue("column", CONSTANTS.DEFAULT_MAX_COLUMN);
const themeParam: string = params.getStringValue("theme", "default");
let theme: Theme = COLORS.default;
if (Object.keys(COLORS).includes(themeParam)) {
theme = COLORS[themeParam];
}
const marginWidth = params.getNumberValue(
"margin-w",
CONSTANTS.DEFAULT_MARGIN_W,
);
const paddingHeight = params.getNumberValue(
"margin-h",
CONSTANTS.DEFAULT_MARGIN_H,
);
const noBackground = params.getBooleanValue(
"no-bg",
CONSTANTS.DEFAULT_NO_BACKGROUND,
);
const noFrame = params.getBooleanValue(
"no-frame",
CONSTANTS.DEFAULT_NO_FRAME,
);
const titles: Array<string> = params.getAll("title").flatMap((r) =>
r.split(",")
).map((r) => r.trim());
const ranks: Array<string> = params.getAll("rank").flatMap((r) =>
r.split(",")
).map((r) => r.trim());
if (username === null) {
const [base] = req.url.split("?");
const error = new Error400(`<h2><code>username</code> is a required query parameter</h2>
<p>The URL should look like <code>${base}?username=USERNAME</code>, where
<code>USERNAME</code> is <em>your GitHub username.</em>`);
req.respond(
{
body: error.render(),
status: error.status,
headers: new Headers({ "Content-Type": "text" }),
},
);
return;
}
const token = Deno.env.get("GITHUB_TOKEN");
const userInfo = await client.requestUserInfo(token, username);
if (userInfo === null) {
req.respond(
{
body: "Can not find a user with userID: " + username,
status: 404,
headers: new Headers({ "Content-Type": "text" }),
},
);
return;
}
// Success Response
req.respond(
{
body: new Card(
titles,
ranks,
column,
row,
CONSTANTS.DEFAULT_PANEL_SIZE,
marginWidth,
paddingHeight,
noBackground,
noFrame,
).render(userInfo, theme),
headers: new Headers(
{
"Content-Type": "image/svg+xml",
"Cache-Control": `public, max-age=${CONSTANTS.CACHE_MAX_AGE}`,
},
),
},
);
};