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 { 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 } from 'h3'
|
||||||
|
import { readBody, getQuery } from 'h3'
|
||||||
|
|
||||||
const McFlyGlobal: {
|
const McFlyGlobal: {
|
||||||
hello: string
|
hello: string
|
||||||
|
@ -21,13 +22,26 @@ const McFlyGlobal: {
|
||||||
* @param {string} _html
|
* @param {string} _html
|
||||||
* @returns {string}
|
* @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 = {
|
McFlyGlobal.event = {
|
||||||
|
path: event.path,
|
||||||
|
method: event.method,
|
||||||
|
query,
|
||||||
|
body,
|
||||||
url: event.node.req.url,
|
url: event.node.req.url,
|
||||||
method: event.node.req.method,
|
|
||||||
statusCode: event.node.req.statusCode,
|
statusCode: event.node.req.statusCode,
|
||||||
statusMessage: event.node.req.statusMessage,
|
statusMessage: event.node.req.statusMessage,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// console.log('>>> Event (from core)', McFlyGlobal)
|
||||||
|
|
||||||
let html = evaluateServerScript(_html)
|
let html = evaluateServerScript(_html)
|
||||||
html = deleteServerScripts(html)
|
html = deleteServerScripts(html)
|
||||||
return html
|
return html
|
||||||
|
|
|
@ -6,15 +6,22 @@
|
||||||
<title>Cozy (McFly)</title>
|
<title>Cozy (McFly)</title>
|
||||||
<script server:setup>
|
<script server:setup>
|
||||||
const appName = "Cozy"
|
const appName = "Cozy"
|
||||||
console.log('>>> Event (from app)', McFly.event)
|
console.log('>>> Event (from app)', McFly.event.body)
|
||||||
</script>
|
</script>
|
||||||
</my-head>
|
</my-head>
|
||||||
<body>
|
<body>
|
||||||
<h1>{{ appName }} - {{McFly.hello}}</h1>
|
<h1>{{ appName }} - {{McFly.hello}}</h1>
|
||||||
<form>
|
<form method="post">
|
||||||
<input id="url" name="url" />
|
<input required type="url" id="url" name="url" />
|
||||||
<button type="submit">Extract</button>
|
<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>
|
</form>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
Loading…
Reference in a new issue