diff --git a/debug.ts b/debug.ts index cf1ef32..ab64cc8 100644 --- a/debug.ts +++ b/debug.ts @@ -1,7 +1,4 @@ -import { serve } from "https://deno.land/std@0.66.0/http/server.ts"; +import { serve } from "https://deno.land/std@0.125.0/http/server.ts"; import requestHandler from "./index.ts"; -const s = serve({ port: 8080 }); -for await (const req of s) { - requestHandler(req); -} +serve(requestHandler, { port: 8080 }); diff --git a/deps.ts b/deps.ts index aa99f08..5d6175a 100644 --- a/deps.ts +++ b/deps.ts @@ -1,2 +1 @@ -export { ServerRequest } from "https://deno.land/std@0.66.0/http/server.ts"; export { soxa } from "https://deno.land/x/soxa@1.4/mod.ts"; diff --git a/index.ts b/index.ts index c4b1d16..a1ab91a 100644 --- a/index.ts +++ b/index.ts @@ -1,13 +1,13 @@ -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, Error404 } 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) => { +export default async (req: Request) => { const params = parseParams(req); const username = params.get("username"); const row = params.getNumberValue("row", CONSTANTS.DEFAULT_MAX_ROW); @@ -41,41 +41,48 @@ export default async (req: ServerRequest) => { ).map((r) => r.trim()); if (username === null) { - req.respond( + const [base] = req.url.split("?"); + const error = new Error400( + `
The URL should look like ${base}?username=USERNAME, where
+USERNAME is your GitHub username.`,
+ );
+ return new Response(
+ error.render(),
{
- body: "Can not find a query parameter: username",
- status: 404,
+ 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(
+ const error = new Error404(
+ "Can not find a user with username: " + username,
+ );
+ return new Response(
+ error.render(),
{
- body: "Can not find a user with userID: " + username,
- status: 404,
+ status: error.status,
headers: new Headers({ "Content-Type": "text" }),
},
);
- return;
}
// Success Response
- req.respond(
+ return new Response(
+ new Card(
+ titles,
+ ranks,
+ column,
+ row,
+ CONSTANTS.DEFAULT_PANEL_SIZE,
+ marginWidth,
+ paddingHeight,
+ noBackground,
+ noFrame,
+ ).render(userInfo, theme),
{
- 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",
diff --git a/src/error_page.ts b/src/error_page.ts
new file mode 100644
index 0000000..f2e0ce4
--- /dev/null
+++ b/src/error_page.ts
@@ -0,0 +1,20 @@
+abstract class BaseError {
+ readonly status!: number;
+ readonly message!: string;
+ constructor(readonly content?: string) {}
+ render(): string {
+ return `