Skip to content

Commit

Permalink
proposal for fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkMcCulloh committed Feb 27, 2024
1 parent 209a9e5 commit ac54bdb
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 41 deletions.
10 changes: 5 additions & 5 deletions examples/typescript-hono/hono.tsx
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down Expand Up @@ -38,4 +36,6 @@ app.get('/', (c) => {
return c.html(<Top messages={messages} />)
})

export default app
app.get('/api', (c) => {
return c.json({ message: 'Hello World!' })
})
68 changes: 32 additions & 36 deletions examples/typescript-hono/main.ts
Original file line number Diff line number Diff line change
@@ -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");
Expand All @@ -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<string, string> = {};
result.headers.forEach((value: any, name: any) => {
headersRecord[name] = value;
});
return {
status: result.status,
headers: headersRecord,
body: await streamToString(result.body)
}
})) ;
})
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<string, string> = {};
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());
})
);
});

0 comments on commit ac54bdb

Please sign in to comment.