-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
5,609 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
})) ; | ||
}) |
Oops, something went wrong.