iann838 / workery

Workery is a Modern, OpenAPI based, fast to code, fully typed, web framework for building APIs on Cloudflare Workers.
MIT License
10 stars 0 forks source link

workery vs chanfana #2

Open pyoner opened 3 weeks ago

pyoner commented 3 weeks ago

Hi @iann838!

The project looks good, but can you compare your project with chanfana

Thanks!

iann838 commented 3 weeks ago

Hi @pyoner

Thanks, chanfana provides a framework agnostic wrapper for creating APIs. However, workery is a framework on its own. In fact this project came as a result of trying to fix the chanfana's predecessor (itty-router-openapi) https://github.com/cloudflare/chanfana/issues/139.

Chanfana provides class-based API definitions with a single entry for all middleware. Workery is built with APIs like how FastAPI is implemented in Python, supporting dependency injection on top of middleware.

For example, here is a comparison of how APIs are defined.

Chanfana (as how they show it on their README).

import { fromHono, OpenAPIRoute } from 'chanfana'
import { Hono } from 'hono'
import { z } from 'zod'

export class GetPageNumber extends OpenAPIRoute {
  schema = {
    request: {
      params: z.object({
        id: z.string().min(2).max(10),
      }),
      query: z.object({
        page: z.number().int().min(0).max(20),
      }),
    },
  }

  async handle(c) {
    const data = await this.getValidatedData<typeof this.schema>()

    return c.json({
      id: data.params.id,
      page: data.query.page,
    })
  }
}

// Star a Hono app
const app = new Hono()

// Setup OpenAPI registry
const openapi = fromHono(app)

// Register OpenAPI endpoints
openapi.get('/entry/:id', GetPageNumber)

// Export the Hono app
export default app

Workery equivalent of the Chanfana version:

import { App } from "workery"
import { Path, Query } from "workery/parameters"
import z from "zod"

const app = new App<Env>({})

app.get("/entry/{id}", {
    parameters: {
        id: Path(z.string().min(2).max(10)),
        page: Query(z.number().int().min(0).max(20))
    },
    handle ({ id, page }) {
        return { id, page }
    }
})

export default app

Sadly this framework is unlikely to get much traction as Chanfana is the official recommended package for building APIs on Workers, and I am no marketing expert neither.

Currently this package is stable and I have been using it myself for my own production projects, including ones with complex auth flows and other logic that is better and more controllable using dependency injection. https://workery.iann838.com/guides/dependencies.html

pyoner commented 2 weeks ago

@iann838 thanks for the quick reply! Do you plan to implement file based routing like honox does?

iann838 commented 2 weeks ago

@pyoner I can, and it sounds great as a feature. I would picture something like this:

All routes are placed as files under one directory and in index.ts:

const app = new App<Env>({})

app.fromDir(dirName)

export default app

But currently, there isn't much traction for this repo compared to chanfana or hono. I sent a post about this package on the Cloudflare Discord a few days back, but it went silent. However, I will continue to maintain this package as multiple critical production projects in our team are using this framework as the main dependency.