-
Notifications
You must be signed in to change notification settings - Fork 760
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update c3 openapi template to use chanfana and Hono (#6106)
- Loading branch information
Showing
23 changed files
with
660 additions
and
370 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,6 @@ | ||
--- | ||
"create-cloudflare": minor | ||
"@cloudflare/prerelease-registry": patch | ||
--- | ||
|
||
Update c3 openapi template to use chanfana and Hono |
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
42 changes: 26 additions & 16 deletions
42
packages/create-cloudflare/templates/openapi/ts/src/endpoints/taskCreate.ts
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
39 changes: 20 additions & 19 deletions
39
packages/create-cloudflare/templates/openapi/ts/src/endpoints/taskDelete.ts
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
51 changes: 27 additions & 24 deletions
51
packages/create-cloudflare/templates/openapi/ts/src/endpoints/taskList.ts
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
34 changes: 14 additions & 20 deletions
34
packages/create-cloudflare/templates/openapi/ts/src/index.ts
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,29 +1,23 @@ | ||
import { OpenAPIRouter } from "@cloudflare/itty-router-openapi"; | ||
import { fromHono } from "chanfana"; | ||
import { Hono } from "hono"; | ||
import { TaskCreate } from "./endpoints/taskCreate"; | ||
import { TaskDelete } from "./endpoints/taskDelete"; | ||
import { TaskFetch } from "./endpoints/taskFetch"; | ||
import { TaskList } from "./endpoints/taskList"; | ||
|
||
export const router = OpenAPIRouter({ | ||
// Start a Hono app | ||
const app = new Hono(); | ||
|
||
// Setup OpenAPI registry | ||
const openapi = fromHono(app, { | ||
docs_url: "/", | ||
}); | ||
|
||
router.get("/api/tasks/", TaskList); | ||
router.post("/api/tasks/", TaskCreate); | ||
router.get("/api/tasks/:taskSlug/", TaskFetch); | ||
router.delete("/api/tasks/:taskSlug/", TaskDelete); | ||
|
||
// 404 for everything else | ||
router.all("*", () => | ||
Response.json( | ||
{ | ||
success: false, | ||
error: "Route not found", | ||
}, | ||
{ status: 404 } | ||
) | ||
); | ||
// Register OpenAPI endpoints | ||
openapi.get("/api/tasks", TaskList); | ||
openapi.post("/api/tasks", TaskCreate); | ||
openapi.get("/api/tasks/:taskSlug", TaskFetch); | ||
openapi.delete("/api/tasks/:taskSlug", TaskDelete); | ||
|
||
export default { | ||
fetch: router.handle, | ||
} satisfies ExportedHandler; | ||
// Export the Hono app | ||
export default app; |
17 changes: 9 additions & 8 deletions
17
packages/create-cloudflare/templates/openapi/ts/src/types.ts
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,9 +1,10 @@ | ||
import { DateTime, Str } from "@cloudflare/itty-router-openapi"; | ||
import { DateTime, Str } from "chanfana"; | ||
import { z } from "zod"; | ||
|
||
export const Task = { | ||
name: new Str({ example: "lorem" }), | ||
slug: String, | ||
description: new Str({ required: false }), | ||
completed: Boolean, | ||
due_date: new DateTime(), | ||
}; | ||
export const Task = z.object({ | ||
name: Str({ example: "lorem" }), | ||
slug: Str(), | ||
description: Str({ required: false }), | ||
completed: z.boolean().default(false), | ||
due_date: DateTime(), | ||
}); |
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 |
---|---|---|
|
@@ -2,5 +2,5 @@ export const repos = [ | |
"workers-sdk", | ||
"next-on-pages", | ||
"pages-plugins", | ||
"itty-router-openapi", | ||
"chanfana", | ||
]; |
Oops, something went wrong.