generated from whyk-pg/learn-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d1324c0
commit 05052cd
Showing
11 changed files
with
531 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = "" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
} | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
}); | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" ] |