Skip to content

Commit

Permalink
feat: add handlers and read env
Browse files Browse the repository at this point in the history
  • Loading branch information
windchime-yk committed Sep 23, 2024
1 parent d1324c0 commit 05052cd
Show file tree
Hide file tree
Showing 11 changed files with 531 additions and 109 deletions.
8 changes: 8 additions & 0 deletions .example.dev.vars
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
DEVELOP = "1"
X_API_KEY = "Example_0000"
DB_HOST = ""
DB_USERNAME = ""
DEV_DB_USERNAME = ""
DB_PASSWORD = ""
DEV_DB_PASSWORD = ""
DB_NAME = ""
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ Cloudflare Workers以前では正常に反映されていたため、Cloudflare
- [Web標準のバックエンドアプリのテスト](https://zenn.dev/yusukebe/articles/9a6335ed793c43#hono%E3%82%A2%E3%83%97%E3%83%AA%E3%81%AE%E3%83%86%E3%82%B9%E3%83%88)
- [How to run the Node built-in testrunner for TypeScript files inside a specific directory? · Issue #3902 · nodejs/help](https://github.com/nodejs/help/issues/3902)
- [Factory Helper - Hono](https://hono.dev/docs/helpers/factory#createmiddleware)
- [Vitest integration | Cloudflare Workers docs](https://developers.cloudflare.com/workers/testing/vitest-integration/)
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
},
"packageManager": "[email protected]",
"dependencies": {
"hono": "^4.6.1",
"vitest": "^2.1.1"
"hono": "^4.6.1"
},
"devDependencies": {
"@biomejs/biome": "^1.8.3",
"@cloudflare/vitest-pool-workers": "^0.5.2",
"@cloudflare/workers-types": "^4.20240529.0",
"wrangler": "^3.57.2"
"vitest": "^2.0.5",
"wrangler": "^3.78.7"
}
}
400 changes: 344 additions & 56 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

52 changes: 52 additions & 0 deletions src/handlers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { createFactory } from "hono/factory";
import { configMiddleware } from "../middlewares/config";
import type { Env } from "../types";

const factory = createFactory<Env>();

export const getCommonResponse = (
method: string,
apiKey: string | undefined,
) => {
return {
status: 200,
method,
text: "OK!",
apiKey: apiKey ?? "",
};
};

export const getHandler = factory.createHandlers(
configMiddleware,
async (c) => {
return c.json({
...getCommonResponse(c.req.method, c.var.api_key),
});
},
);
export const postHandler = factory.createHandlers(
configMiddleware,
async (c) => {
return c.json({
...getCommonResponse(c.req.method, c.var.api_key),
});
},
);

export const patchHandler = factory.createHandlers(
configMiddleware,
async (c) => {
return c.json({
...getCommonResponse(c.req.method, c.var.api_key),
});
},
);

export const deleteHandler = factory.createHandlers(
configMiddleware,
async (c) => {
return c.json({
...getCommonResponse(c.req.method, c.var.api_key),
});
},
);
72 changes: 52 additions & 20 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,53 +1,85 @@
import { describe, expect, it } from "vitest";
import app from "./index";
import type { DOT_ENV } from "./types";

const testResponse = {
status: 200,
method: "GET",
text: "OK!",
apiKey: "Example_0000",
};
type TestResponse = typeof testResponse;

// TODO: dotenvが使えれば、環境変数を取得できるようになるはず
const MOCK_ENV = {
DEVELOP: "1",
X_API_KEY: "Example_0000",
DB_HOST: "",
DB_USERNAME: "",
DEV_DB_USERNAME: "",
DB_PASSWORD: "",
DEV_DB_PASSWORD: "",
DB_NAME: "",
GH_PAT: "",
} satisfies DOT_ENV;

describe("レスポンス確認", () => {
it("GET", async () => {
const testGetResponse = structuredClone(testResponse);
const res = await app.request("/", {
method: "GET",
});
console.log(res);
const json = await res.json();
const res = await app.request(
"/",
{
method: "GET",
},
MOCK_ENV,
);
expect(res.status).toBe(200);
const json = await res.json<TestResponse>();
expect(json).toStrictEqual(testGetResponse);
});

it("POST", async () => {
const testPostResponse = structuredClone(testResponse);
testPostResponse.method = "POST";
const res = await app.request("/", {
method: "POST",
});
console.log(res);
const json = await res.json();
const res = await app.request(
"/",
{
method: "POST",
},
MOCK_ENV,
);
expect(res.status).toBe(200);
const json = await res.json<TestResponse>();
expect(json).toStrictEqual(testPostResponse);
});

it("PATCH", async () => {
const testPatchResponse = structuredClone(testResponse);
testPatchResponse.method = "PATCH";
const res = await app.request("/", {
method: "PATCH",
});
console.log(res);
const json = await res.json();
const res = await app.request(
"/",
{
method: "PATCH",
},
MOCK_ENV,
);
expect(res.status).toBe(200);
const json = await res.json<TestResponse>();
expect(json).toStrictEqual(testPatchResponse);
});

it("DELETE", async () => {
const testDeleteResponse = structuredClone(testResponse);
testDeleteResponse.method = "DELETE";
const res = await app.request("/", {
method: "DELETE",
});
console.log(res);
const json = await res.json();
const res = await app.request(
"/",
{
method: "DELETE",
},
MOCK_ENV,
);
expect(res.status).toBe(200);
const json = await res.json<TestResponse>();
expect(json).toStrictEqual(testDeleteResponse);
});
});
41 changes: 12 additions & 29 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,18 @@
import { Hono } from "hono";
import {
deleteHandler,
getHandler,
patchHandler,
postHandler,
} from "./handlers";
import type { Env } from "./types";

const app = new Hono();

const getCommonResponse = (method: string) => {
return {
status: 200,
method,
text: "OK!",
};
};
const app = new Hono<Env>();

app
.get("/", async (c) => {
return c.json({
...getCommonResponse(c.req.method),
});
})
.post(async (c) => {
return c.json({
...getCommonResponse(c.req.method),
});
})
.patch(async (c) => {
return c.json({
...getCommonResponse(c.req.method),
});
})
.delete(async (c) => {
return c.json({
...getCommonResponse(c.req.method),
});
});
.get("/", ...getHandler)
.post(...postHandler)
.patch(...patchHandler)
.delete(...deleteHandler);

export default app;
22 changes: 22 additions & 0 deletions src/middlewares/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { Context } from "hono";
import { env } from "hono/adapter";
import { createMiddleware } from "hono/factory";
import type { DOT_ENV, Env } from "../types";

export const configMiddleware = createMiddleware<Env>(async (ctx, next) => {
const envConfig = env<DOT_ENV, Context<Env>>(ctx);

ctx.set("api_key", envConfig.X_API_KEY);
ctx.set("db_host", envConfig.DB_HOST);
ctx.set(
"db_username",
envConfig.DEVELOP ? envConfig.DEV_DB_USERNAME : envConfig.DB_USERNAME,
);
ctx.set(
"db_password",
envConfig.DEVELOP ? envConfig.DEV_DB_PASSWORD : envConfig.DB_PASSWORD,
);
ctx.set("db_name", envConfig.DB_NAME);

await next();
});
22 changes: 22 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export type Env = {
Variables: {
api_key: string;
db_host: string;
db_username: string;
db_password: string;
db_name: string;
gh_pat: string;
};
};

export type DOT_ENV = {
DEVELOP: string;
X_API_KEY: string;
DB_HOST: string;
DB_USERNAME: string;
DEV_DB_USERNAME: string;
DB_PASSWORD: string;
DEV_DB_PASSWORD: string;
DB_NAME: string;
GH_PAT: string;
};
13 changes: 13 additions & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { defineWorkersConfig } from "@cloudflare/vitest-pool-workers/config";

export default defineWorkersConfig({
test: {
poolOptions: {
workers: {
wrangler: {
configPath: "wrangler.toml",
},
},
},
},
});
2 changes: 1 addition & 1 deletion wrangler.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
name = "verify-cfw-drizzle-crud"
compatibility_date = "2024-09-14"
compatibility_date = "2024-09-09"
compatibility_flags = [ "nodejs_compat" ]

0 comments on commit 05052cd

Please sign in to comment.