-
Notifications
You must be signed in to change notification settings - Fork 779
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 allow users to pick either Hono or itty…
…-router
- Loading branch information
Showing
42 changed files
with
1,370 additions
and
382 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 | ||
--- | ||
|
||
fix: Update C3 OpenAPI template to allow users to pick either Hono or itty-router |
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,48 @@ | ||
import { processArgument } from "@cloudflare/cli/args"; | ||
import type { C3Context } from "types"; | ||
|
||
const frameworkConfig = [ | ||
{ | ||
label: "Hono", | ||
value: "hono", | ||
}, | ||
{ | ||
label: "itty-router", | ||
value: "itty-router", | ||
}, | ||
]; | ||
|
||
export default { | ||
configVersion: 1, | ||
id: "openapi", | ||
displayName: "API starter (OpenAPI compliant)", | ||
platform: "workers", | ||
copyFiles: { | ||
path: "./ts", | ||
async selectVariant(ctx: C3Context) { | ||
const framework = await processArgument<string>(ctx.args, "framework", { | ||
type: "select", | ||
label: "framework", | ||
question: "Which framework do you want to use?", | ||
options: frameworkConfig, | ||
defaultValue: frameworkConfig[0].value, | ||
validate: (value) => { | ||
if ( | ||
!frameworkConfig.map((obj) => obj.value).includes(String(value)) | ||
) { | ||
return `Invalid framework \`${value}\`. Please choose one of the following: ${frameworkConfig.map((obj) => obj.value).join(", ")}.`; | ||
} | ||
}, | ||
}); | ||
|
||
return framework; | ||
}, | ||
variants: { | ||
hono: { | ||
path: "./hono", | ||
}, | ||
"itty-router": { | ||
path: "./itty-router", | ||
}, | ||
}, | ||
}, | ||
}; |
File renamed without changes.
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
...es/openapi/ts/src/endpoints/taskCreate.ts → .../openapi/hono/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
...es/openapi/ts/src/endpoints/taskDelete.ts → .../openapi/hono/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
81 changes: 81 additions & 0 deletions
81
packages/create-cloudflare/templates/openapi/hono/src/endpoints/taskFetch.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 |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { Bool, OpenAPIRoute, Str } from "chanfana"; | ||
import { z } from "zod"; | ||
import { Task } from "../types"; | ||
|
||
export class TaskFetch extends OpenAPIRoute { | ||
schema = { | ||
tags: ["Tasks"], | ||
summary: "Get a single Task by slug", | ||
request: { | ||
params: z.object({ | ||
taskSlug: Str({ description: "Task slug" }), | ||
}), | ||
}, | ||
responses: { | ||
"200": { | ||
description: "Returns a single task if found", | ||
content: { | ||
"application/json": { | ||
schema: z.object({ | ||
series: z.object({ | ||
success: Bool(), | ||
result: z.object({ | ||
task: Task, | ||
}), | ||
}), | ||
}), | ||
}, | ||
}, | ||
}, | ||
"404": { | ||
description: "Task not found", | ||
content: { | ||
"application/json": { | ||
schema: z.object({ | ||
series: z.object({ | ||
success: Bool(), | ||
error: Str(), | ||
}), | ||
}), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
async handle(c) { | ||
// Get validated data | ||
const data = await this.getValidatedData<typeof this.schema>(); | ||
|
||
// Retrieve the validated slug | ||
const { taskSlug } = data.params; | ||
|
||
// Implement your own object fetch here | ||
|
||
const exists = true; | ||
|
||
// @ts-ignore: check if the object exists | ||
if (exists === false) { | ||
return Response.json( | ||
{ | ||
success: false, | ||
error: "Object not found", | ||
}, | ||
{ | ||
status: 404, | ||
}, | ||
); | ||
} | ||
|
||
return { | ||
success: true, | ||
task: { | ||
name: "my task", | ||
slug: taskSlug, | ||
description: "this needs to be done", | ||
completed: false, | ||
due_date: new Date().toISOString().slice(0, 10), | ||
}, | ||
}; | ||
} | ||
} |
51 changes: 27 additions & 24 deletions
51
...ates/openapi/ts/src/endpoints/taskList.ts → ...es/openapi/hono/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
23 changes: 23 additions & 0 deletions
23
packages/create-cloudflare/templates/openapi/hono/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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
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"; | ||
|
||
// Start a Hono app | ||
const app = new Hono(); | ||
|
||
// Setup OpenAPI registry | ||
const openapi = fromHono(app, { | ||
docs_url: "/", | ||
}); | ||
|
||
// 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 the Hono app | ||
export default app; |
Oops, something went wrong.