Skip to content

Commit

Permalink
draft: ts hono
Browse files Browse the repository at this point in the history
  • Loading branch information
skorfmann committed Feb 27, 2024
1 parent 2c1b7bd commit 209a9e5
Show file tree
Hide file tree
Showing 6 changed files with 5,609 additions and 0 deletions.
3 changes: 3 additions & 0 deletions examples/typescript-hono/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Wing Typescript Hono.js Example

This is based on the [Wing Typescript SDK](https://www.winglang.io/docs/typescript/) and demonstrates an integration with [hono](https://hono.dev/).
41 changes: 41 additions & 0 deletions examples/typescript-hono/hono.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/** @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 }) => {
const file = await bucket.get("hello");
console.log(file);
return file;
}

const app = new Hono()

const Layout: FC = (props) => {
return (
<html>
<body>{props.children}</body>
</html>
)
}

const Top: FC<{ messages: string[] }> = (props: { messages: string[] }) => {
return (
<Layout>
<h1>Hello Hono!</h1>
<ul>
{props.messages.map((message) => {
return <li>{message}!!</li>
})}
</ul>
</Layout>
)
}

app.get('/', (c) => {
const messages = ['Good Morning', 'Good Evening', 'Good Night']
return c.html(<Top messages={messages} />)
})

export default app
44 changes: 44 additions & 0 deletions examples/typescript-hono/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
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;
}

main((root, test) => {
let bucket = new cloud.Bucket(root, "Bucket");

bucket.addObject("hello", "Hello World from lifted Bucket!");

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)
}
})) ;
})
Loading

0 comments on commit 209a9e5

Please sign in to comment.