feat(core): send request as global
This commit is contained in:
parent
af02d1dbad
commit
012db337ae
2 changed files with 27 additions and 6 deletions
|
@ -3,6 +3,7 @@ import { parseScript } from 'esprima'
|
|||
import { consola } from 'consola'
|
||||
import type { BaseNode as JsNode } from 'estree'
|
||||
import type { H3Event } from 'h3'
|
||||
import { readBody, getQuery } from 'h3'
|
||||
|
||||
const McFlyGlobal: {
|
||||
hello: string
|
||||
|
@ -21,13 +22,26 @@ const McFlyGlobal: {
|
|||
* @param {string} _html
|
||||
* @returns {string}
|
||||
*/
|
||||
export function evaluateServerScripts(_html: string, event: H3Event) {
|
||||
export async function evaluateServerScripts(_html: string, event: H3Event) {
|
||||
// Parse query params
|
||||
const query = getQuery(event)
|
||||
|
||||
// Try to read request body
|
||||
const body = await readBody(event).catch(() => {})
|
||||
|
||||
// Echo back request as response
|
||||
McFlyGlobal.event = {
|
||||
path: event.path,
|
||||
method: event.method,
|
||||
query,
|
||||
body,
|
||||
url: event.node.req.url,
|
||||
method: event.node.req.method,
|
||||
statusCode: event.node.req.statusCode,
|
||||
statusMessage: event.node.req.statusMessage,
|
||||
}
|
||||
|
||||
// console.log('>>> Event (from core)', McFlyGlobal)
|
||||
|
||||
let html = evaluateServerScript(_html)
|
||||
html = deleteServerScripts(html)
|
||||
return html
|
||||
|
|
|
@ -6,15 +6,22 @@
|
|||
<title>Cozy (McFly)</title>
|
||||
<script server:setup>
|
||||
const appName = "Cozy"
|
||||
console.log('>>> Event (from app)', McFly.event)
|
||||
console.log('>>> Event (from app)', McFly.event.body)
|
||||
</script>
|
||||
</my-head>
|
||||
<body>
|
||||
<h1>{{ appName }} - {{McFly.hello}}</h1>
|
||||
<form>
|
||||
<input id="url" name="url" />
|
||||
<form method="post">
|
||||
<input required type="url" id="url" name="url" />
|
||||
<button type="submit">Extract</button>
|
||||
<p>{{ McFly.event.url }}</p>
|
||||
<ul>
|
||||
<li>
|
||||
URL: {{ McFly.event.body.url }}
|
||||
</li>
|
||||
<li>
|
||||
Method: {{ McFly.event.method }}
|
||||
</li>
|
||||
</ul>
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in a new issue