Skip to content

How do i create a default response for all incoming request? #3840

Answered by EdamAme-x
flixyudh asked this question in Q&A
Discussion options

You must be logged in to vote

Hey @flixyudh
Please try to use this

import { Hono } from "hono"

const app = new Hono<{
    Variables: {
        createResponse: (data: unknown) => Response
    }
}>()

app.use(async (c, next) => {
    c.set("createResponse", (data: unknown) => c.json({
        success: true,
        data
    }, 200))
    await next()
})

app.get((c) => {
    return c.var.createResponse({
        name: "Hono is cool!"
    })
})

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@flixyudh
Comment options

@EdamAme-x
Comment options

@EdamAme-x
Comment options

Answer selected by flixyudh
@flixyudh
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants