Skip to content

Commit

Permalink
refactor(ts): Make deep file structures (#36)
Browse files Browse the repository at this point in the history
Separate files by their roles, and merge tests.
  • Loading branch information
5ouma authored Aug 10, 2024
1 parent f9d3976 commit 71210e2
Show file tree
Hide file tree
Showing 15 changed files with 35 additions and 37 deletions.
5 changes: 1 addition & 4 deletions deno.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"$schema": "https://deno.land/x/deno/cli/schemas/config-file.v1.json",
"fmt": { "exclude": ["LICENSE", ".github/**/*.md"] },
"test": { "include": ["src/", "test/"] },
"tasks": {
"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",
Expand All @@ -10,15 +9,13 @@
"cov": "deno task test --coverage && deno coverage --lcov > coverage.lcov"
},
"imports": {
"@5ouma/reproxy/libs": "./src/libs/mod.ts",
"@5ouma/reproxy/types": "./src/types/mod.ts",
"@oak/oak": "jsr:@oak/oak@16.1.0",
"@octokit/rest": "https://esm.sh/@octokit/rest@21.0.1",
"@std/assert": "jsr:@std/assert@1.0.2",
"@std/fmt": "jsr:@std/fmt@1.0.0",
"@std/http": "jsr:@std/http@1.0.2",
"@std/http/user-agent": "jsr:@std/http@~0.223/user-agent",
"@std/url": "jsr:@std/url@0.225.0"
"@std/path": "jsr:@std/path@1.0.2"
},
"deploy": {
"project": "reproxy",
Expand Down
11 changes: 2 additions & 9 deletions deno.lock

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

3 changes: 2 additions & 1 deletion src/libs/content.ts → src/libs/middlewares/content.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { RouterContext } from "@oak/oak";
import { Octokit } from "@octokit/rest";
import { getRepository, githubToken } from "./env.ts";

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

export async function getContent<R extends string>(
ctx: RouterContext<R>,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { type RouterContext, testing } from "@oak/oak";
import { assertEquals, assertStringIncludes } from "@std/assert";
import { STATUS_CODE } from "@std/http/status";
import { getContent } from "@5ouma/reproxy/libs";
import { exportRepo, testRef, testRepo, unknownRepo } from "../utils.ts";

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

Deno.test("Get Content", async <R extends string>(t: Deno.TestContext) => {
const ctx: RouterContext<R> = testing.createMockContext();
Expand Down
21 changes: 12 additions & 9 deletions src/libs/redirect.ts → src/libs/middlewares/redirect.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
import type { RouterContext } from "@oak/oak";
import { STATUS_CODE } from "@std/http/status";
import { UserAgent } from "@std/http/user-agent";
import { join } from "@std/url";
import { getRepository } from "./env.ts";
import { join } from "@std/path";

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

export function redirect<R extends string>(
ctx: RouterContext<R>,
userAgent: UserAgent,
ref: string = "master",
): boolean {
const repository = getRepository();
const url = join(
new URL("https://github.com"),
repository.owner,
repository.name,
"blob",
ref,
repository.path,
const url = new URL(
join(
repository.owner,
repository.name,
"blob",
ref,
repository.path,
),
"https://github.com",
);

if (!userAgent?.browser.name) return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { type RouterContext, testing } from "@oak/oak";
import { assertEquals } from "@std/assert";
import { STATUS_CODE } from "@std/http/status";
import { UserAgent } from "@std/http/user-agent";
import { redirect } from "@5ouma/reproxy/libs";
import { exportRepo, testRef, testRepo } from "../utils.ts";

import { redirect } from "./redirect.ts";
import { exportRepo, testRef, testRepo } from "../test_utils.ts";

Deno.test("Redirect Detection", async <R extends string>(t: Deno.TestContext) => {
const ctx: RouterContext<R> = testing.createMockContext();
Expand Down
4 changes: 2 additions & 2 deletions src/libs/mod.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from "./content.ts";
export * from "./redirect.ts";
export * from "./middlewares/content.ts";
export * from "./middlewares/redirect.ts";
2 changes: 1 addition & 1 deletion test/utils.ts → src/libs/test_utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Repository } from "@5ouma/reproxy/types";
import type { Repository } from "./types.ts";

export const testRepo: Repository = {
owner: "denoland",
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/libs/env.ts → src/libs/utils/env.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Repository } from "@5ouma/reproxy/types";
import type { Repository } from "../types.ts";

export function getRepository(): Repository {
const repository: Repository = {
Expand Down
5 changes: 3 additions & 2 deletions test/libs/env_test.ts → src/libs/utils/env_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { assertEquals } from "@std/assert";
import { getRepository } from "../../src/libs/env.ts";
import { clearRepo, exportRepo, testRepo } from "../utils.ts";

import { getRepository } from "./env.ts";
import { clearRepo, exportRepo, testRepo } from "../test_utils.ts";

Deno.test("Get Repository Env", async (t: Deno.TestContext) => {
await t.step("Normal", () => {
Expand Down
2 changes: 1 addition & 1 deletion src/router.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Router, type RouterContext } from "@oak/oak";
import { getContent, redirect } from "@5ouma/reproxy/libs";
import { getContent, redirect } from "./libs/mod.ts";

export const router = new Router();
router
Expand Down
5 changes: 3 additions & 2 deletions test/router_test.ts → src/router_test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { type RouterContext, testing } from "@oak/oak";
import { assertEquals } from "@std/assert";
import { STATUS_CODE } from "@std/http/status";
import { router } from "../src/router.ts";
import { exportRepo, testRef, testRepo } from "./utils.ts";

import { router } from "./router.ts";
import { exportRepo, testRef, testRepo } from "./libs/test_utils.ts";

Deno.test("Serve", async <R extends string>(t: Deno.TestContext) => {
await t.step("/", async () => {
Expand Down
1 change: 1 addition & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Application } from "@oak/oak";
import type { ApplicationListenEvent } from "@oak/oak/application";
import { yellow } from "@std/fmt/colors";

import { router } from "./router.ts";

const app = new Application();
Expand Down
1 change: 0 additions & 1 deletion src/types/mod.ts

This file was deleted.

0 comments on commit 71210e2

Please sign in to comment.