0.0.18 • Published 3 years ago

h3-worker v0.0.18

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

h3-worker

Use h3 in a (Cloudflare) worker!

This little package uses some helpers from h3 and unenv to make h3 apps work in a Cloudflare worker environment.

Install

# Using npm
npm install h3-worker

# Using yarn
yarn add h3-worker

Usage

  • Set up your Cloudflare worker project - I recommend worker-typescript-template
  • Check out the h3 docs
  • Write some routes and use the handleEvent method from h3-worker to respondWith
import { createApp, handleEvent } from 'h3-worker'

// Create the app
const app = createApp()

// Add some routes (no need to worry about order)
app.use('/', () => 'Hello world')
app.use('/json', () => ({ hello: 'JSON' }))

// Type the body if you like
app.use<{ firstName: string }>('/first-name', (req) => {
  const { body } = req
  return `Hi ${body.firstName}`
})

// Add the event listener
addEventListener('fetch', (event) => {
  // Respond with a handleEvent() call, passing in the event and your app. Options are optional
  event.respondWith(handleEvent(event, app, { basePath: '', sortStack: true }))
})

TaDa!

0.0.10

3 years ago

0.0.11

3 years ago

0.0.12

3 years ago

0.0.13

3 years ago

0.0.14

3 years ago

0.0.15

3 years ago

0.0.9

3 years ago

0.0.16

3 years ago

0.0.8

3 years ago

0.0.17

3 years ago

0.0.18

3 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago