hono icon indicating copy to clipboard operation
hono copied to clipboard

[Discussion] What middleware do you want?

Open yusukebe opened this issue 2 years ago • 43 comments

From v2.0.0, we can create 3rd party middleware. So far, the following 3rd-party middleware has been created.

  • GraphQL Middleware
  • Firebase Auth Middleware
  • Validator Middleware
  • Sentry Middleware

GraphQL Middleware was originally provided as Built-in Middleware, which I also use. Firebase Auth make more authentication options for Cloudflare Workers. Validator Middleware is my creation, and I like it. Sentry Middleware will be very helpful in catching errors at the edge.

Next, What middleware do you want?

If there is any Middleware you want, please comment here, even if you don't make it yourself. Someone may make it. You may also get other ideas by seeing the ideas here.

yusukebe avatar Aug 25 '22 00:08 yusukebe

Error Handler. We should be able to throw exceptions anywhere in the code and the middleware should catch it, so that it can in turn handle it as a 500 error page, 404 error page or anything else, based on the error type.

rishavs avatar Aug 26 '22 12:08 rishavs

Hi @rishavs !

Thank you for your comment. Are app.notFound and app.onError not enough? Indeed, Hono does not have the original Error objects.

// https://honojs.dev/docs/api/hono/#not-found
app.notFound((c) => {
  return c.text('Custom 404 Message', 404)
})

// https://honojs.dev/docs/api/hono/#error-handling
app.onError((err, c) => {
  console.error(`${err}`)
  return c.text('Custom Error Message', 500)
})

yusukebe avatar Aug 26 '22 12:08 yusukebe

Let me try them. I don't think they are documented anywhere right now.

The way I was handling errors was something like this;

// deno-lint-ignore-file
import { Hono } from 'https://deno.land/x/hono/mod.ts'
import {render} from "./../views/render.js"
import {postsList} from "./../views/pages/postsList.js"

const showPostsListPage = new Hono()
showPostsListPage.get('/', async (c) => {
    try {
        const page = await postsList()
        var props = {
            title: "Posts List",
            description: "This here is the description of the post list page",
            page : page
        } 
        const view = render(props)
        return c.html(view)    
    } catch (err) {
        return c.html ("500 ERROR")
    }
})

export {showPostsListPage}

and I had to put the try catch on every single route. but app.onError seems to be exactly what I wanted. Thanks.

rishavs avatar Aug 26 '22 12:08 rishavs

I don't think they are documented anywhere right now.

Yes, it is not well documented. We have to write better, cc: @ThatOneBro :)

yusukebe avatar Aug 26 '22 13:08 yusukebe

Some other middlewares that would be great to have are;

  1. Helmet
  2. body parser
  3. cookie parser
  4. timeout

rishavs avatar Aug 26 '22 19:08 rishavs

@rishavs

Helmet would be nice.

Body parser and cookie parser are already included in the core.

https://honojs.dev/docs/api/context/#creq

// Parse cookie
app.get('/entry/:id', (c) => {
  const value = c.req.cookie('name')
  ...
})

// Parse Request body
 app.post('', async (c) => {
   const body = await c.req.parseBody()
   ...
 })

These are not documented well too.

yusukebe avatar Aug 26 '22 23:08 yusukebe

Opentracing or similar (https://github.com/openzipkin/b3-propagation/blob/master/README.md)!

jbergstroem avatar Aug 28 '22 05:08 jbergstroem

I'd love some non-3rd-party authentication middleware(s)! Just some simple register/signup that stores data in a database (bun's built in sqlite db's for example)

It could also extend to other best practices like GDPR compliance, session storage, forgot password, etc. It would save a lot of time, and the need for another completely unrelated library. (Featherjs in my situation, which also comes with its own http server)

SwatDoge avatar Aug 29 '22 11:08 SwatDoge

Cloudflare R2 Bucket CRUD :)

Also more convenience around Durable Objects & WebSockets

matthewrobb avatar Sep 09 '22 21:09 matthewrobb

Cloudflare R2 Bucket CRUD :)

Also a R2 pass-through middleware would be interesting, to serve and cache a R2 bucket as a static site. Something in the lines of kotx/render.

stefanmaric avatar Sep 29 '22 20:09 stefanmaric

Datadog!

yusukebe avatar Oct 14 '22 22:10 yusukebe

Hi @icrayix !

Thank you for using Hono.

apart from the kind of insufficient docs LOL

Ha, Ha. We have to write more good documents.

The thing I wanted to ask is the state of the already created 3rd-party middleware. I've already found the corresponding repo but that seems pretty empty.

There are some middleware managed in every independent repository:

  • https://github.com/honojs/graphql-server
  • https://github.com/honojs/sentry
  • https://github.com/honojs/firebase-auth

We want to move these repositories to monorepo, this repository:

https://github.com/honojs/middleware

We can't do that yet. Nevertheless, the above monorepo has a minimal setup, so adding new middleware may be easy if we want to do so.

So yeah just wanted to know if any of the above-mentioned middleware has already been created?

No, not yet. Some of them are still in the idea stage, but others like tRPC could be implemented already

yusukebe avatar Nov 29 '22 19:11 yusukebe

Just wondering, why is that?

Mainly the main maintainer (me) should do that, but I don't have time. I'm not a full-time open-source developer.

So is that repo actually open for contributions?

Yes, it's open. You can create PR for the repository.

yusukebe avatar Nov 29 '22 23:11 yusukebe

WebSocket support.

samal-rasmussen avatar Nov 30 '22 11:11 samal-rasmussen

It might sound strange, but I would prefer not to see more middleware. Since it's for workers which interprets code on the fly, it's IMHO better to be minimalistic rather than risking the bloat of having too many features. I find the current list of middleware really hits the sweet spot, and I would really think twice about adding something. I think it is very wise to keep the more exotic ones (especially connections to 3rd party services) in separate repositories like you did.

If there was one thing I'd say I miss, it's generating an OpenAPI spec out of the box, or through a middleware. Although I don't use them currently, I can see websocket support as a nice addition too.

Regarding the validation, that's one thing I'm not really a fan of. I prefer to validate in the endpoint itself using a more powerful lib like https://github.com/grantila/suretype where you get both the validation, the typing, can produce schemas, etc.

dagnelies avatar Dec 02 '22 09:12 dagnelies

OpenAPI / Swagger support 🙏

silvioprog avatar Jan 27 '23 00:01 silvioprog

TRPC support would be great.

a-eid avatar Jan 31 '23 13:01 a-eid

Hi!

Released "tRPC Server Middleware" now. Check it out!

https://github.com/honojs/middleware/tree/main/packages/trpc-server

yusukebe avatar Feb 04 '23 15:02 yusukebe

Proxy would be a great addition. Something as simple as this: https://deno.land/x/[email protected]

finalclass avatar Feb 16 '23 07:02 finalclass

Proxy would be a great addition. Something as simple as this: deno.land/x/[email protected]

..or reflare! https://github.com/xiaoyang-sde/reflare

jbergstroem avatar Mar 05 '23 22:03 jbergstroem

session controller and MySql(MariaDB)

ruizyi avatar Mar 31 '23 02:03 ruizyi

storage : awe-s3

zuohuadong avatar May 04 '23 01:05 zuohuadong

Datadog for logs or some other 3rd party logging! 🙏

donferi avatar Jun 06 '23 17:06 donferi

HMAC signature validation for verifying webhooks akin to https://github.com/gr2m/cloudflare-worker-github-app-example/blob/dee1c7766065058fde2a8bf366f2f2b729ea2d3f/worker.js

Durable Objects tooling akin to hyper-durable

mpint avatar Jun 23 '23 13:06 mpint

Websockets, Reverse Proxy, Prometheus.

mikestopcontinues avatar Jul 15 '23 12:07 mikestopcontinues

OpenAPI/Swagger support

ryoppippi avatar Jul 21 '23 15:07 ryoppippi

openapi is really very important

masterbater avatar Aug 14 '23 03:08 masterbater

Passwordless.dev

alex8bitw avatar Sep 11 '23 06:09 alex8bitw

Proxy would be a great addition. Something as simple as this: deno.land/x/[email protected]

..or reflare! https://github.com/xiaoyang-sde/reflare Now that reflare is gone, something needs to fill that space.

alex8bitw avatar Sep 11 '23 07:09 alex8bitw

Proxy would be a great addition. Something as simple as this: deno.land/x/[email protected]

..or reflare! https://github.com/xiaoyang-sde/reflare

Now that reflare is gone, something needs to fill that space.

h3 can proxy requests.

jbergstroem avatar Sep 11 '23 11:09 jbergstroem