Skip to content

Commit d16f1c6

Browse files
feat(create-cloudflare): Update openapi template to include better ts types and lint command (#7796)
* Update openapi C3 template * Update .changeset/perfect-geckos-tell.md Co-authored-by: Pete Bacon Darwin <[email protected]> --------- Co-authored-by: Pete Bacon Darwin <[email protected]>
1 parent c409318 commit d16f1c6

File tree

10 files changed

+46
-35
lines changed

10 files changed

+46
-35
lines changed

.changeset/perfect-geckos-tell.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"create-cloudflare": patch
3+
---
4+
5+
Update openapi C3 template to include better ts types and lint command

packages/create-cloudflare/templates/openapi/ts/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,4 @@ dist
169169
# wrangler project
170170

171171
.dev.vars
172+
.wrangler/

packages/create-cloudflare/templates/openapi/ts/package.json

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@
99
"cf-typegen": "wrangler types"
1010
},
1111
"dependencies": {
12-
"chanfana": "^2.0.2",
13-
"zod": "^3.23.8",
14-
"hono": "^4.4.7"
12+
"chanfana": "^2.6.3",
13+
"hono": "^4.6.20",
14+
"zod": "^3.24.1"
1515
},
1616
"devDependencies": {
17-
"@types/node": "20.8.3",
18-
"@types/service-worker-mock": "^2.0.1",
19-
"wrangler": "^3.101.0"
17+
"@cloudflare/workers-types": "^4.20250129.0",
18+
"@types/node": "22.13.0",
19+
"@types/service-worker-mock": "^2.0.4",
20+
"wrangler": "^3.107.2"
2021
}
2122
}

packages/create-cloudflare/templates/openapi/ts/src/endpoints/taskCreate.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Bool, OpenAPIRoute } from "chanfana";
22
import { z } from "zod";
3-
import { Task } from "../types";
3+
import { type AppContext, Task } from "../types";
44

55
export class TaskCreate extends OpenAPIRoute {
66
schema = {
@@ -34,7 +34,7 @@ export class TaskCreate extends OpenAPIRoute {
3434
},
3535
};
3636

37-
async handle(c) {
37+
async handle(c: AppContext) {
3838
// Get validated data
3939
const data = await this.getValidatedData<typeof this.schema>();
4040

packages/create-cloudflare/templates/openapi/ts/src/endpoints/taskDelete.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Bool, OpenAPIRoute, Str } from "chanfana";
22
import { z } from "zod";
3-
import { Task } from "../types";
3+
import { type AppContext, Task } from "../types";
44

55
export class TaskDelete extends OpenAPIRoute {
66
schema = {
@@ -30,7 +30,7 @@ export class TaskDelete extends OpenAPIRoute {
3030
},
3131
};
3232

33-
async handle(c) {
33+
async handle(c: AppContext) {
3434
// Get validated data
3535
const data = await this.getValidatedData<typeof this.schema>();
3636

packages/create-cloudflare/templates/openapi/ts/src/endpoints/taskFetch.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Bool, OpenAPIRoute, Str } from "chanfana";
22
import { z } from "zod";
3-
import { Task } from "../types";
3+
import { type AppContext, Task } from "../types";
44

55
export class TaskFetch extends OpenAPIRoute {
66
schema = {
@@ -43,7 +43,7 @@ export class TaskFetch extends OpenAPIRoute {
4343
},
4444
};
4545

46-
async handle(c) {
46+
async handle(c: AppContext) {
4747
// Get validated data
4848
const data = await this.getValidatedData<typeof this.schema>();
4949

packages/create-cloudflare/templates/openapi/ts/src/endpoints/taskList.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Bool, Num, OpenAPIRoute } from "chanfana";
22
import { z } from "zod";
3-
import { Task } from "../types";
3+
import { type AppContext, Task } from "../types";
44

55
export class TaskList extends OpenAPIRoute {
66
schema = {
@@ -37,7 +37,7 @@ export class TaskList extends OpenAPIRoute {
3737
},
3838
};
3939

40-
async handle(c) {
40+
async handle(c: AppContext) {
4141
// Get validated data
4242
const data = await this.getValidatedData<typeof this.schema>();
4343

packages/create-cloudflare/templates/openapi/ts/src/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { TaskFetch } from "./endpoints/taskFetch";
66
import { TaskList } from "./endpoints/taskList";
77

88
// Start a Hono app
9-
const app = new Hono();
9+
const app = new Hono<{ Bindings: Env }>();
1010

1111
// Setup OpenAPI registry
1212
const openapi = fromHono(app, {
@@ -19,5 +19,8 @@ openapi.post("/api/tasks", TaskCreate);
1919
openapi.get("/api/tasks/:taskSlug", TaskFetch);
2020
openapi.delete("/api/tasks/:taskSlug", TaskDelete);
2121

22+
// You may also register routes for non OpenAPI directly on Hono
23+
// app.get('/test', (c) => c.text('Hono!'))
24+
2225
// Export the Hono app
2326
export default app;

packages/create-cloudflare/templates/openapi/ts/src/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { DateTime, Str } from "chanfana";
2+
import type { Context } from "hono";
23
import { z } from "zod";
34

5+
export type AppContext = Context<{ Bindings: Env }>;
6+
47
export const Task = z.object({
58
name: Str({ example: "lorem" }),
69
slug: Str(),
Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,30 @@
11
{
22
"compilerOptions": {
3-
"allowJs": true,
4-
"allowSyntheticDefaultImports": true,
5-
"baseUrl": "src",
6-
"declaration": true,
7-
"sourceMap": true,
3+
/* Base Options: */
84
"esModuleInterop": true,
9-
"inlineSourceMap": false,
10-
"lib": ["esnext"],
11-
"listEmittedFiles": false,
12-
"listFiles": false,
13-
"moduleResolution": "node",
14-
"noFallthroughCasesInSwitch": true,
15-
"pretty": true,
16-
"resolveJsonModule": true,
17-
"rootDir": ".",
185
"skipLibCheck": true,
19-
"strict": false,
20-
"traceResolution": false,
21-
"outDir": "",
22-
"target": "esnext",
23-
"module": "esnext",
6+
"target": "es2022",
7+
"verbatimModuleSyntax": false,
8+
"allowJs": true,
9+
"resolveJsonModule": true,
10+
"moduleDetection": "force",
11+
/* Strictness */
12+
"noImplicitAny": false,
13+
"noImplicitThis": true,
14+
"strictNullChecks": false,
15+
"strict": true,
16+
"noUncheckedIndexedAccess": true,
17+
/* If NOT transpiling with TypeScript: */
18+
"moduleResolution": "Bundler",
19+
"module": "es2022",
20+
"noEmit": true,
21+
"lib": ["es2022"],
2422
"types": [
2523
"@types/node",
2624
"@types/service-worker-mock",
2725
"@cloudflare/workers-types"
2826
]
2927
},
3028
"exclude": ["node_modules", "dist", "tests"],
31-
"include": ["src", "scripts"]
29+
"include": ["src", "worker-configuration.d.ts"]
3230
}

0 commit comments

Comments
 (0)