expose request in ribbit

This commit is contained in:
Chris Wanstrath 2025-11-05 15:02:42 -08:00
parent 68cd61227b
commit 0556efb41f
2 changed files with 12 additions and 11 deletions

View File

@ -53,6 +53,15 @@ for (const name of HTML5_TAGS) {
; (ribbitGlobals as any)[name].tagName = name ; (ribbitGlobals as any)[name].tagName = name
} }
const tag = async (tagName: string, atNamed = {}, ...args: any[]) => {
if (typeof args[0] === 'function')
await tagBlock(tagName, atNamed, args[0])
else
tagCall(tagName, atNamed, ...args)
return TAG_TOKEN
}
const tagBlock = async (tagName: string, props = {}, fn: Function) => { const tagBlock = async (tagName: string, props = {}, fn: Function) => {
const attrs = Object.entries(props).map(([key, value]) => `${key}="${value}"`) const attrs = Object.entries(props).map(([key, value]) => `${key}="${value}"`)
const space = attrs.length ? ' ' : '' const space = attrs.length ? ' ' : ''
@ -77,13 +86,4 @@ const tagCall = (tagName: string, atNamed = {}, ...args: any[]) => {
buffer.push(`<${tagName}${space}${attrs.join(' ')} />`) buffer.push(`<${tagName}${space}${attrs.join(' ')} />`)
else else
buffer.push(`<${tagName}${space}${attrs.join(' ')}>${children}</${tagName}>`) buffer.push(`<${tagName}${space}${attrs.join(' ')}>${children}</${tagName}>`)
} }
const tag = async (tagName: string, atNamed = {}, ...args: any[]) => {
if (typeof args[0] === 'function')
await tagBlock(tagName, atNamed, args[0])
else
tagCall(tagName, atNamed, ...args)
return TAG_TOKEN
}

View File

@ -25,6 +25,7 @@ export function startWeb(rootPath: string) {
if (page === 'layout') return c.text('404 Not Found', 404) if (page === 'layout') return c.text('404 Not Found', 404)
const params = c.req.query() const params = c.req.query()
const request = { method: c.req.method }
if (c.req.method === 'POST') { if (c.req.method === 'POST') {
const formData = await c.req.formData() const formData = await c.req.formData()
for (const [key, value] of formData.entries()) { for (const [key, value] of formData.entries()) {
@ -39,7 +40,7 @@ export function startWeb(rootPath: string) {
const layoutFile = Bun.file(layoutPath) const layoutFile = Bun.file(layoutPath)
let layoutCode = await layoutFile.exists() ? await layoutFile.text() : '' let layoutCode = await layoutFile.exists() ? await layoutFile.text() : ''
const vm = new Shrimp(Object.assign({}, ribbitGlobals, { params })) const vm = new Shrimp(Object.assign({}, ribbitGlobals, { params, request }))
if (await file.exists()) { if (await file.exists()) {
let content = '' let content = ''