Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ts hono ssr example #45

Merged
merged 12 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/).
19 changes: 19 additions & 0 deletions examples/typescript-hono/api-route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { cloud } from "@wingcloud/framework";
import { lift } from "@wingcloud/framework";
import { handleRequest } from "./mapper";

export const apiRoute = (api: cloud.Api, path: string, ctx?: {bucket: cloud.Bucket}) => {
const liftedHandler = () => {
return lift({ apiUrl: api.url, bucket: ctx ? ctx.bucket : undefined}).inflight(async ({ apiUrl, bucket }, req) => {
return handleRequest(apiUrl, req, bucket);
})
}

api.get(path, liftedHandler());
api.post(path, liftedHandler());
api.put(path, liftedHandler());
api.patch(path, liftedHandler());
api.delete(path, liftedHandler());
api.connect(path, liftedHandler());
api.options(path, liftedHandler());
skorfmann marked this conversation as resolved.
Show resolved Hide resolved
}
44 changes: 44 additions & 0 deletions examples/typescript-hono/hono.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { cloud } from "@wingcloud/framework";
import { Hono } from "hono";
import type { FC } from 'hono/jsx'

type Bindings = {
BUCKET: cloud.IBucketClient
}
skorfmann marked this conversation as resolved.
Show resolved Hide resolved

export const app = new Hono<{ Bindings: Bindings }>();

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('/', async (c) => {
const bucket = c.env.BUCKET;
const messages = ['Good Morning', 'Good Evening', 'Good Night', await bucket.get('hello')]
skorfmann marked this conversation as resolved.
Show resolved Hide resolved
return c.html(<Top messages={messages} />)
})

app.get('/api', (c) => {
return c.json({ message: 'Hello World!' })
})

app.post('/api', (c) => {
return c.json({ message: 'Posted' })
})
47 changes: 47 additions & 0 deletions examples/typescript-hono/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { cloud, main, lift } from "@wingcloud/framework";
import { apiRoute } from "./api-route";
import { match } from "node:assert";

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");

apiRoute(api, "/", { bucket });
apiRoute(api, "/api");

test(
"GET /",
lift({ apiUrl: api.url }).inflight(async ({ apiUrl }) => {
const response = await fetch(apiUrl);
const text = await response.text();
match(text, /Hello World from lifted Bucket/);
})
);

test(
"GET /api",
lift({ apiUrl: api.url }).inflight(async ({ apiUrl }) => {
const response = await fetch(`${apiUrl}/api`);
const text = await response.text();
match(text, /Hello World/);
})
);

test(
"POST /api",
lift({ apiUrl: api.url }).inflight(async ({ apiUrl }) => {
const response = await fetch(`${apiUrl}/api`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({})
});
const text = await response.text();
match(text, /Posted/);
})
);
});
28 changes: 28 additions & 0 deletions examples/typescript-hono/mapper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import type { cloud } from "@wingcloud/framework";
import { app } from "./hono";

const mapRequest = (url: string, req: cloud.ApiRequest): Request => {
return new Request(`${url}${req.path}`, {
headers: req.headers,
method: req.method,
});
}

const mapResponse = async (res: Response): Promise<cloud.ApiResponse> => {
let headers: Record<string, string> = {};
res.headers.forEach((value, name) => {
headers[name] = value;
});

return {
status: res.status,
headers,
body: await res.text(),
};
}

export const handleRequest = async(url: string, request: cloud.ApiRequest, bucket?: cloud.Bucket): Promise<cloud.ApiResponse> => {
const req = mapRequest(url, request);
const response = await app.fetch(req, {BUCKET: bucket});
return mapResponse(response);
}
Loading
Loading