Skip to content

Commit

Permalink
refactor(server): Rewrite with @hono/hono (#41)
Browse files Browse the repository at this point in the history
Use Hono, a more useful web framework.
  • Loading branch information
5ouma authored Aug 24, 2024
1 parent bdbb129 commit 6687934
Show file tree
Hide file tree
Showing 16 changed files with 206 additions and 290 deletions.
6 changes: 2 additions & 4 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@
"run": "deno run --env='.env' --allow-env='REPOSITORY_OWNER,REPOSITORY_NAME,REPOSITORY_PATH,GITHUB_TOKEN' --allow-net='0.0.0.0,api.github.com'",
"start": "deno task run ./src/server.ts",
"dev": "deno task run --watch ./src/server.ts",
"test": "deno test --allow-env='REPOSITORY_OWNER,REPOSITORY_NAME,REPOSITORY_PATH,GITHUB_TOKEN' --allow-net='api.github.com' --parallel --shuffle",
"test": "deno test --allow-env='REPOSITORY_OWNER,REPOSITORY_NAME,REPOSITORY_PATH,GITHUB_TOKEN' --allow-net='0.0.0.0,api.github.com' --parallel --shuffle",
"cov": "deno task test --coverage && deno coverage --lcov > coverage.lcov"
},
"imports": {
"@oak/oak": "jsr:@oak/[email protected]",
"@hono/hono": "jsr:@hono/[email protected]",
"@octokit/rest": "https://esm.sh/@octokit/[email protected]",
"@std/assert": "jsr:@std/[email protected]",
"@std/fmt": "jsr:@std/[email protected]",
"@std/http": "jsr:@std/[email protected]",
"@std/http/user-agent": "jsr:@std/http@~0.223/user-agent",
"@std/path": "jsr:@std/[email protected]"
},
"deploy": {
Expand Down
146 changes: 31 additions & 115 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import type { RouterContext } from "@oak/oak";
import { Octokit } from "@octokit/rest";
import type { StatusCode } from "@std/http";

import { getRepository, githubToken } from "../utils/env.ts";

export async function getContent<R extends string>(
ctx: RouterContext<R>,
export async function getContent(
ref: string | undefined = undefined,
): Promise<void> {
): Promise<[string, StatusCode]> {
const octokit = new Octokit({ auth: githubToken });
const repository = getRepository();

Expand All @@ -19,10 +18,8 @@ export async function getContent<R extends string>(
ref: ref,
});

ctx.response.status = status;
ctx.response.body = data;
return [data.toString(), status];
} catch (error) {
ctx.response.status = error.status;
ctx.response.body = `⚠️ ${ctx.response.status}: ${error.message}`;
return [`⚠️ ${error.status}: ${error.message}`, error.status];
}
}
31 changes: 31 additions & 0 deletions src/libs/middleware/content_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { assertEquals, assertExists, assertStringIncludes } from "@std/assert";
import { STATUS_CODE } from "@std/http/status";

import { getContent } from "./content.ts";
import { exportRepo, testRef, testRepo, unknownRepo } from "../test_utils.ts";

Deno.test("Get Content", async (t: Deno.TestContext) => {
await t.step("normal", async () => {
exportRepo(testRepo);
const [data, status] = await getContent();

assertExists(data);
assertEquals(status, STATUS_CODE.OK);
});

await t.step("with ref", async () => {
exportRepo(testRepo);
const [data, status] = await getContent(testRef);

assertExists(data);
assertEquals(status, STATUS_CODE.OK);
});

await t.step("not found", async () => {
exportRepo(unknownRepo);
const [data, status] = await getContent();

assertStringIncludes(data, `⚠️ ${STATUS_CODE.NotFound}:`);
assertEquals(status, STATUS_CODE.NotFound);
});
});
12 changes: 12 additions & 0 deletions src/libs/middleware/redirect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { UserAgent } from "@std/http/user-agent";

import { getGitHubUrl, getRepository } from "../utils/env.ts";

export function redirect(
userAgent: UserAgent,
ref: string = "master",
): URL | null {
const url = getGitHubUrl(getRepository(), ref);

return userAgent?.browser.name ? url : null;
}
21 changes: 21 additions & 0 deletions src/libs/middleware/redirect_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { assertEquals } from "@std/assert";

import { redirect } from "./redirect.ts";
import { exportRepo, testRef, testRepo, testUserAgent } from "../test_utils.ts";
import { getGitHubUrl } from "../utils/env.ts";

Deno.test("Redirect Detection", async (t: Deno.TestContext) => {
await t.step("normal", () => {
exportRepo(testRepo);
const url: URL | null = redirect(testUserAgent);

assertEquals(url, getGitHubUrl(testRepo));
});

await t.step("with ref", () => {
exportRepo(testRepo);
const url: URL | null = redirect(testUserAgent, testRef);

assertEquals(url, getGitHubUrl(testRepo, testRef));
});
});
35 changes: 0 additions & 35 deletions src/libs/middlewares/content_test.ts

This file was deleted.

30 changes: 0 additions & 30 deletions src/libs/middlewares/redirect.ts

This file was deleted.

Loading

0 comments on commit 6687934

Please sign in to comment.