diff --git a/examples/typescript-hono/hono.tsx b/examples/typescript-hono/hono.tsx index b82e36c..a06f6bf 100644 --- a/examples/typescript-hono/hono.tsx +++ b/examples/typescript-hono/hono.tsx @@ -1,16 +1,14 @@ -/** @jsx jsx */ -/** @jsxImportSource hono/jsx */ import { cloud } from "@wingcloud/framework"; import { Hono } from "hono"; import type { FC } from 'hono/jsx' -export const myServer = async ({bucket}: { bucket: cloud.IBucketClient }) => { +export const myServer = async ({ bucket }: { bucket: cloud.IBucketClient }) => { const file = await bucket.get("hello"); console.log(file); return file; } -const app = new Hono() +export const app = new Hono() const Layout: FC = (props) => { return ( @@ -38,4 +36,6 @@ app.get('/', (c) => { return c.html() }) -export default app \ No newline at end of file +app.get('/api', (c) => { + return c.json({ message: 'Hello World!' }) +}) diff --git a/examples/typescript-hono/main.ts b/examples/typescript-hono/main.ts index 6570dfa..2a3c73e 100644 --- a/examples/typescript-hono/main.ts +++ b/examples/typescript-hono/main.ts @@ -1,20 +1,5 @@ -import { cloud, inflight, main } from "@wingcloud/framework"; -import myServer from "./hono"; - -const streamToString = async (stream: ReadableStream) => { - let reader = stream.getReader(); - let decoder = new TextDecoder('utf-8'); - let data = await reader.read(); - let body = ''; - - while (!data.done) { - body += decoder.decode(data.value, {stream: true}); - data = await reader.read(); - } - - body += decoder.decode(); // finish the stream - return body; -} +import { cloud, main, lift } from "@wingcloud/framework"; +import { app } from "./hono"; main((root, test) => { let bucket = new cloud.Bucket(root, "Bucket"); @@ -23,22 +8,33 @@ main((root, test) => { const api = new cloud.Api(root, "api"); - api.get("/api", inflight(async (ctx, req) => { - const requestInit: RequestInit = { - headers: req.headers, - method: req.method, - } - - const request = new Request(`${api.url}/${req.path}`, requestInit); - const result = await myServer.fetch(request); - let headersRecord: Record = {}; - result.headers.forEach((value: any, name: any) => { - headersRecord[name] = value; - }); - return { - status: result.status, - headers: headersRecord, - body: await streamToString(result.body) - } - })) ; -}) \ No newline at end of file + api.get( + "/api", + lift({ apiUrl: api.url }).inflight(async ({ apiUrl }, req) => { + const request = new Request(`${apiUrl}${req.path}`, { + headers: req.headers, + method: req.method, + }); + const response = await app.fetch(request); + + let headers: Record = {}; + response.headers.forEach((value, name) => { + headers[name] = value; + }); + + return { + status: response.status, + headers, + body: await response.text(), + }; + }) + ); + + test( + "GET /api", + lift({ apiUrl: api.url }).inflight(async ({ apiUrl }) => { + const response = await fetch(`${apiUrl}/api`); + console.log(await response.text()); + }) + ); +});