Skip to content

Latest commit

 

History

History
56 lines (42 loc) · 1.6 KB

README.md

File metadata and controls

56 lines (42 loc) · 1.6 KB

H3 Adapter (Wooks Composables)

!!! This is work-in-progress library, breaking changes are expected !!!


🔥 Get power of @wooksjs/event-http in your h3 project!

Install

npm install @wooksjs/h3-adapter @wooksjs/event-http

Usage

import { WooksError, useRequest} from '@wooksjs/event-http'
import { useRouteParams} from '@wooksjs/event-core'
import { useBody } from '@wooksjs/http-body'
import { useProxy } from '@wooksjs/http-proxy'
import { createServer } from 'http'
import { createApp, toNodeListener, createRouter  } from 'h3'
import { eventHandler } from '@wooksjs/h3-adapter'

const router = createRouter()

router.get('/test/:param', eventHandler(() => {
    const { get } = useRouteParams()
    return { message: 'it works', param: get('param'), url: useRequest().url }
}))

router.post('/post', eventHandler(async () => {
    const { parseBody, rawBody } = useBody()
    const raw = await rawBody()
    return { parsed: await parseBody(), raw  }
}))

router.get('/error', eventHandler(() => {
    throw new WooksError(400, 'test error')
}))

router.get('/proxy', eventHandler(() => {
    const proxy = useProxy()
    return proxy('http://localhost:3000/test/proxy')
}))

const app = createApp()
app.use(router)

createServer(toNodeListener(app))
    .listen(process.env.PORT || 3000, () => console.log('listening 3000'))