mirror of
https://github.com/reiyua/github-profile-trophy-reiyua-mirror.git
synced 2026-11-17 06:02:09 +11:00
Merge branch 'pr-125'
This commit is contained in:
commit
4ed7c7309f
7 changed files with 57 additions and 33 deletions
7
debug.ts
7
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 });
|
||||
|
|
|
|||
1
deps.ts
1
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";
|
||||
|
|
|
|||
33
index.ts
33
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,31 +41,37 @@ export default async (req: ServerRequest) => {
|
|||
).map((r) => r.trim());
|
||||
|
||||
if (username === null) {
|
||||
req.respond(
|
||||
const [base] = req.url.split("?");
|
||||
const error = new Error400(
|
||||
`<h2>"username" 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>`,
|
||||
);
|
||||
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(
|
||||
{
|
||||
body: new Card(
|
||||
return new Response(
|
||||
new Card(
|
||||
titles,
|
||||
ranks,
|
||||
column,
|
||||
|
|
@ -76,6 +82,7 @@ export default async (req: ServerRequest) => {
|
|||
noBackground,
|
||||
noFrame,
|
||||
).render(userInfo, theme),
|
||||
{
|
||||
headers: new Headers(
|
||||
{
|
||||
"Content-Type": "image/svg+xml",
|
||||
|
|
|
|||
20
src/error_page.ts
Normal file
20
src/error_page.ts
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
abstract class BaseError {
|
||||
readonly status!: number;
|
||||
readonly message!: string;
|
||||
constructor(readonly content?: string) {}
|
||||
render(): string {
|
||||
return `<!DOCTYPE html><html><body><h1>${this.status} - ${this.message}</h1>${
|
||||
this.content ?? ""
|
||||
}</body></html>`;
|
||||
}
|
||||
}
|
||||
|
||||
export class Error400 extends BaseError {
|
||||
readonly status = 400;
|
||||
readonly message = "Bad Reuquest";
|
||||
}
|
||||
|
||||
export class Error404 extends BaseError {
|
||||
readonly status = 404;
|
||||
readonly message = "Not Found";
|
||||
}
|
||||
|
|
@ -21,6 +21,9 @@ export class GithubAPIClient {
|
|||
this.requestUserPullRequest(token, username),
|
||||
this.requestUserRepository(token, username),
|
||||
]);
|
||||
if (results.some((r) => r == null)) {
|
||||
return null;
|
||||
}
|
||||
return new UserInfo(results[0]!, results[1]!, results[2]!, results[3]!);
|
||||
}
|
||||
private async requestUserActivity(
|
||||
|
|
|
|||
|
|
@ -1,5 +1,3 @@
|
|||
import { ServerRequest } from ".././deps.ts";
|
||||
|
||||
export class CustomURLSearchParams extends URLSearchParams {
|
||||
constructor(
|
||||
init?: string[][] | Record<string, string> | string | URLSearchParams,
|
||||
|
|
@ -31,13 +29,13 @@ export class CustomURLSearchParams extends URLSearchParams {
|
|||
getBooleanValue(key: string, defaultValue: boolean): boolean {
|
||||
if (super.has(key)) {
|
||||
const param = super.get(key);
|
||||
return param !== null && param.toString() === 'true';
|
||||
return param !== null && param.toString() === "true";
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
export function parseParams(req: ServerRequest): CustomURLSearchParams {
|
||||
export function parseParams(req: Request): CustomURLSearchParams {
|
||||
const splittedURL = req.url.split("?");
|
||||
if (splittedURL.length < 2) {
|
||||
return new CustomURLSearchParams();
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"builds":[{
|
||||
"src": "**/index.ts",
|
||||
"use": "vercel-deno@0.7.10"
|
||||
"use": "vercel-deno@1.1.1"
|
||||
}],
|
||||
"routes": [{ "src": "/.*", "dest": "/" }]
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue