forked from ryo-ma/github-profile-trophy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
65 lines (61 loc) · 1.76 KB
/
index.ts
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { ServerRequest } from "./deps.ts";
import { GithubAPIClient } from "./src/github_api_client.ts";
import { Card } from "./src/card.ts";
import { parseParams, CONSTANTS } from "./src/utils.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");
let row = CONSTANTS.DEFAULT_MAX_ROW;
let column = CONSTANTS.DEFAULT_MAX_COLUMN;
let titles: Array<string> = params.getAll("title").flatMap((r) =>
r.split(",")
).map((r) => r.trim());
let ranks: Array<string> = params.getAll("rank").flatMap((r) => r.split(","))
.map((r) => r.trim());
if (params.has("row")) {
const param = params.get("row");
if (param != null) {
row = parseInt(param);
}
}
if (params.has("column")) {
const param = params.get("column");
if (param != null) {
column = parseInt(param);
}
}
if (username != null) {
const userInfo = await client.requestUserInfo(username);
if (userInfo !== null) {
req.respond(
{
body: new Card(titles, ranks, column, row).render(userInfo),
headers: new Headers(
{
"Content-Type": "image/svg+xml",
"Cache-Control": `public, max-age: ${CONSTANTS.CACHE_MAX_AGE}`,
},
),
},
);
} else {
req.respond(
{
body: "Can not find a user",
status: 404,
headers: new Headers({ "Content-Type": "text" }),
},
);
}
} else {
req.respond(
{
body: "Can not find a query parameter: username",
status: 404,
headers: new Headers({ "Content-Type": "text" }),
},
);
}
};