feat(core): send event props as McFly props

This commit is contained in:
Ayo Ayco 2025-06-28 09:08:32 +02:00
parent 1dacdccc26
commit 270a6ca76c
2 changed files with 16 additions and 14 deletions

View file

@ -2,16 +2,15 @@ import { ELEMENT_NODE, parse, renderSync, walkSync } from 'ultrahtml'
import { parseScript } from 'esprima' import { parseScript } from 'esprima'
import { consola } from 'consola' import { consola } from 'consola'
import type { BaseNode as JsNode } from 'estree' import type { BaseNode as JsNode } from 'estree'
import type { H3Event } from 'h3' import type { H3Event, HTTPMethod } from 'h3'
import { readBody, getQuery } from 'h3' import { readBody, getQuery } from 'h3'
const McFlyGlobal: { let McFlyGlobal: {
hello: string path?: string
event: any method?: HTTPMethod
} = { query?: any
hello: 'world', body?: any
event: undefined, } = {}
}
/** /**
* @typedef {import('estree').BaseNode} JsNode * @typedef {import('estree').BaseNode} JsNode
@ -30,7 +29,7 @@ export async function evaluateServerScripts(_html: string, event: H3Event) {
const body = await readBody(event).catch(() => {}) const body = await readBody(event).catch(() => {})
// Echo back request as response // Echo back request as response
McFlyGlobal.event = { McFlyGlobal = {
path: event.path, path: event.path,
method: event.method, method: event.method,
query, query,
@ -121,7 +120,9 @@ function evaluateServerScript(html: string) {
// stringify objects // stringify objects
finalValue = finalValue =
typeof finalValue === 'object' ? JSON.stringify(finalValue) : finalValue typeof finalValue === 'object'
? JSON.stringify(finalValue, null, 2)
: finalValue
html = html.replace(key, finalValue ?? '') html = html.replace(key, finalValue ?? '')
regex.lastIndex = -1 regex.lastIndex = -1

View file

@ -4,18 +4,19 @@
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Cozy</title> <title>Cozy</title>
<link rel="stylesheet" href="/prism.css" />
<script server:setup> <script server:setup>
const url = McFly.event.query.url const url = McFly.query.url
</script> </script>
</head> </head>
<body> <body>
<h1>Cozy</h1> <h1>Cozy</h1>
<form> <form method="get">
<input required type="url" name="url" id="url" value={{ url }} /> <input required type="url" name="url" id="url" value={{ url }} />
<button type="submit">Extract</button> <button type="submit">Extract</button>
</form> </form>
<p> <code-block language="js">
{{ McFly }} {{ McFly }}
</p> </code-block>
</body> </body>
</html> </html>