Skip to content

Commit 033dc4e

Browse files
Lars-Erik Roaldlroal
authored andcommitted
documented hono adapter
1 parent 8d39de5 commit 033dc4e

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

README.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1150,7 +1150,7 @@ async function deleteRows() {
11501150
</details>
11511151
11521152
<details id="in-the-browser"><summary><strong>In the browser</strong></summary>
1153-
<p>You can use <strong><i>Orange</i></strong> in the browser by using the adapter for Express. Instead of sending raw SQL queries from the client to the server, this approach records the method calls in the client. These method calls are then replayed at the server, ensuring a higher level of security by not exposing raw SQL on the client side.
1153+
<p>You can use <strong><i>Orange</i></strong> in the browser by using the adapter for Express or Hono. Instead of sending raw SQL queries from the client to the server, this approach records the method calls in the client. These method calls are then replayed at the server, ensuring a higher level of security by not exposing raw SQL on the client side.
11541154
Raw sql queries, raw sql filters and transactions are disabled at the http client due to security reasons. If you would like Orange to support other web frameworks, like nestJs, fastify, etc, please let me know.</p>
11551155
11561156
<sub>📄 server.ts</sub>
@@ -1197,6 +1197,32 @@ async function updateRows() {
11971197

11981198
```
11991199
1200+
__Hono adapter__
1201+
1202+
You can host the same HTTP endpoint with Hono by replacing `db.express()` with `db.hono()`. The browser client setup stays the same (`map.http(...)`), so you can reuse the `browser.ts` example above.
1203+
1204+
<sub>📄 server.ts</sub>
1205+
1206+
```ts
1207+
import map from './map';
1208+
import { Hono } from 'hono';
1209+
import { cors } from 'hono/cors';
1210+
import { serve } from '@hono/node-server';
1211+
1212+
const db = map.sqlite('demo.db');
1213+
const app = new Hono();
1214+
1215+
app.use('/orange', cors());
1216+
app.use('/orange/*', cors());
1217+
// for demonstrational purposes, authentication middleware is not shown here.
1218+
app.all('/orange', db.hono());
1219+
app.all('/orange/*', db.hono());
1220+
1221+
serve({ fetch: app.fetch, port: 3000 });
1222+
```
1223+
1224+
`baseFilter` and transaction hooks are also supported in `db.hono({...})`, using Hono-style request/response objects.
1225+
12001226
__Interceptors and base filter__
12011227
12021228
In the next setup, axios interceptors are employed on the client side to add an Authorization header of requests. Meanwhile, on the server side, an Express middleware (validateToken) is utilized to ensure the presence of the Authorization header, while a base filter is applied on the order table to filter incoming requests based on the customerId extracted from this header. This combined approach enhances security by ensuring that users can only access data relevant to their authorization level and that every request is accompanied by a token. In real-world applications, it's advisable to use a more comprehensive token system and expand error handling to manage a wider range of potential issues.

0 commit comments

Comments
 (0)