Skip to content

Commit

Permalink
Update openapi C3 template
Browse files Browse the repository at this point in the history
  • Loading branch information
G4brym committed Jan 16, 2025
1 parent f8c11d7 commit f25d621
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 36 deletions.
5 changes: 5 additions & 0 deletions .changeset/perfect-geckos-tell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"create-cloudflare": minor
---

Update openapi C3 template to include better ts types and lint command
1 change: 1 addition & 0 deletions packages/create-cloudflare/templates/openapi/ts/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,4 @@ dist
# wrangler project

.dev.vars
.wrangler/
30 changes: 30 additions & 0 deletions packages/create-cloudflare/templates/openapi/ts/biome.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"$schema": "https://biomejs.dev/schemas/1.9.4/schema.json",
"vcs": {
"enabled": false,
"clientKind": "git",
"useIgnoreFile": false
},
"files": {
"ignoreUnknown": false,
"ignore": []
},
"formatter": {
"enabled": true,
"indentStyle": "tab"
},
"organizeImports": {
"enabled": true
},
"linter": {
"enabled": true,
"rules": {
"recommended": true
}
},
"javascript": {
"formatter": {
"quoteStyle": "double"
}
}
}
15 changes: 9 additions & 6 deletions packages/create-cloudflare/templates/openapi/ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@
"deploy": "wrangler deploy",
"dev": "wrangler dev",
"start": "wrangler dev",
"lint": "npx @biomejs/biome check src/ || (npx @biomejs/biome check --write src/; exit 1)",
"cf-typegen": "wrangler types"
},
"dependencies": {
"chanfana": "^2.0.2",
"zod": "^3.23.8",
"hono": "^4.4.7"
"chanfana": "^2.6.1",
"hono": "^4.6.16",
"zod": "^3.24.1"
},
"devDependencies": {
"@types/node": "20.8.3",
"@types/service-worker-mock": "^2.0.1",
"wrangler": "^3.101.0"
"@biomejs/biome": "1.9.4",
"@cloudflare/workers-types": "^4.20250109.0",
"@types/node": "22.10.7",
"@types/service-worker-mock": "^2.0.4",
"wrangler": "^3.103.0"
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Bool, OpenAPIRoute } from "chanfana";
import { z } from "zod";
import { Task } from "../types";
import { type AppContext, Task } from "../types";

export class TaskCreate extends OpenAPIRoute {
schema = {
Expand Down Expand Up @@ -34,7 +34,7 @@ export class TaskCreate extends OpenAPIRoute {
},
};

async handle(c) {
async handle(c: AppContext) {
// Get validated data
const data = await this.getValidatedData<typeof this.schema>();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Bool, OpenAPIRoute, Str } from "chanfana";
import { z } from "zod";
import { Task } from "../types";
import { type AppContext, Task } from "../types";

export class TaskDelete extends OpenAPIRoute {
schema = {
Expand Down Expand Up @@ -30,7 +30,7 @@ export class TaskDelete extends OpenAPIRoute {
},
};

async handle(c) {
async handle(c: AppContext) {
// Get validated data
const data = await this.getValidatedData<typeof this.schema>();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Bool, OpenAPIRoute, Str } from "chanfana";
import { z } from "zod";
import { Task } from "../types";
import { type AppContext, Task } from "../types";

export class TaskFetch extends OpenAPIRoute {
schema = {
Expand Down Expand Up @@ -43,7 +43,7 @@ export class TaskFetch extends OpenAPIRoute {
},
};

async handle(c) {
async handle(c: AppContext) {
// Get validated data
const data = await this.getValidatedData<typeof this.schema>();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Bool, Num, OpenAPIRoute } from "chanfana";
import { z } from "zod";
import { Task } from "../types";
import { type AppContext, Task } from "../types";

export class TaskList extends OpenAPIRoute {
schema = {
Expand Down Expand Up @@ -37,7 +37,7 @@ export class TaskList extends OpenAPIRoute {
},
};

async handle(c) {
async handle(c: AppContext) {
// Get validated data
const data = await this.getValidatedData<typeof this.schema>();

Expand Down
5 changes: 4 additions & 1 deletion packages/create-cloudflare/templates/openapi/ts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { TaskFetch } from "./endpoints/taskFetch";
import { TaskList } from "./endpoints/taskList";

// Start a Hono app
const app = new Hono();
const app = new Hono<{ Bindings: Env }>();

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

// You may also register routes for non OpenAPI directly on Hono
// app.get('/test', (c) => c.text('Hono!'))

// Export the Hono app
export default app;
3 changes: 3 additions & 0 deletions packages/create-cloudflare/templates/openapi/ts/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { DateTime, Str } from "chanfana";
import type { Context } from "hono";
import { z } from "zod";

export type AppContext = Context<{ Bindings: Env }>;

export const Task = z.object({
name: Str({ example: "lorem" }),
slug: Str(),
Expand Down
40 changes: 19 additions & 21 deletions packages/create-cloudflare/templates/openapi/ts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,32 +1,30 @@
{
"compilerOptions": {
"allowJs": true,
"allowSyntheticDefaultImports": true,
"baseUrl": "src",
"declaration": true,
"sourceMap": true,
/* Base Options: */
"esModuleInterop": true,
"inlineSourceMap": false,
"lib": ["esnext"],
"listEmittedFiles": false,
"listFiles": false,
"moduleResolution": "node",
"noFallthroughCasesInSwitch": true,
"pretty": true,
"resolveJsonModule": true,
"rootDir": ".",
"skipLibCheck": true,
"strict": false,
"traceResolution": false,
"outDir": "",
"target": "esnext",
"module": "esnext",
"target": "es2022",
"verbatimModuleSyntax": false,
"allowJs": true,
"resolveJsonModule": true,
"moduleDetection": "force",
/* Strictness */
"noImplicitAny": false,
"noImplicitThis": true,
"strictNullChecks": false,
"strict": true,
"noUncheckedIndexedAccess": true,
/* If NOT transpiling with TypeScript: */
"moduleResolution": "Bundler",
"module": "es2022",
"noEmit": true,
/* If your code runs in the DOM: */
"lib": ["es2022"],
"types": [
"@types/node",
"@types/service-worker-mock",
"@cloudflare/workers-types"
]
},
"exclude": ["node_modules", "dist", "tests"],
"include": ["src", "scripts"]
"include": ["src", "worker-configuration.d.ts"]
}

0 comments on commit f25d621

Please sign in to comment.