Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cloudflare-pages): enable c.env.eventContext in handleMiddleware #3332

Merged
merged 1 commit into from
Sep 8, 2024

Conversation

yusukebe
Copy link
Member

@yusukebe yusukebe commented Aug 29, 2024

Fixes #3320

This is for the Cloudflare Pages adapter. With this PR, you can access EventContext object in hanldeMiddleware via c.env.

// functions/_middleware.ts
import { handleMiddleware } from 'hono/cloudflare-pages'

export const onRequest = [
  handleMiddleware(async (c, next) => {
    c.env.eventContext.data.user = 'Joe'
    await next()
  }),
]

Then, you can access the data value in the handler:

// functions/api/[[route]].ts
import { Hono } from 'hono'
import type { EventContext } from 'hono/cloudflare-pages'
import { handle } from 'hono/cloudflare-pages'

type Env = {
  Bindings: {
    eventContext: EventContext
  }
}

const app = new Hono<Env>().basePath('/api')

const routes = app.get('/hello', (c) => {
  return c.json({
    message: `Hello, ${c.env.eventContext.data.user}!`, // 'Joe'
  })
})

export const onRequest = handle(routes)

The author should do the following, if applicable

  • Add tests
  • Run tests
  • bun run format:fix && bun run lint:fix to format the code
  • Add TSDoc/JSDoc to document the code

Copy link

codecov bot commented Aug 29, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.76%. Comparing base (18f937d) to head (dfa0bf3).
Report is 14 commits behind head on next.

Additional details and impacted files
@@           Coverage Diff           @@
##             next    #3332   +/-   ##
=======================================
  Coverage   95.76%   95.76%           
=======================================
  Files         151      151           
  Lines        9166     9166           
  Branches     2726     2776   +50     
=======================================
  Hits         8778     8778           
  Misses        388      388           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@yusukebe
Copy link
Member Author

Hey @BarryThePenguin @zoubingwu !

I think this is good and will fix the issue #3320 . Can you review it?

Copy link
Contributor

@BarryThePenguin BarryThePenguin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice 👍🏻makes sense

Copy link

@zoubingwu zoubingwu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good job, lgtm

@yusukebe
Copy link
Member Author

@BarryThePenguin @zoubingwu Thanks!

@yusukebe yusukebe changed the base branch from main to next September 8, 2024 06:44
@yusukebe yusukebe merged commit 8e56989 into next Sep 8, 2024
14 checks passed
@yusukebe yusukebe deleted the feat/cloudflare-pages-event-context-data branch September 8, 2024 06:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

context c.set/get not working in cloudflare pages functions
3 participants