Hono v3.7.0 is out now! Let's take a look at the new features.
c.stream()
and c.streamText()
We added the awaited functionality related to streaming. c.stream()
and c.streamText()
.
You can easily create HTTP Streaming endpoints with them.
app.get('/', (c) => {
return c.streamText(async (stream) => {
stream.writeln('Hello!')
await stream.sleep(1000)
stream.writeln('Hono!')
})
})
You know Streaming works well with AI. With streamText()
you can write your ChatGPT Gateway in elegant code.
app.post('/api', async (c) => {
const body = await c.req.json<{ message: string }>()
const openai = new OpenAI({ apiKey: c.env.OPENAI_API_KEY })
const chatStream = await openai.chat.completions.create({
messages: PROMPT(body.message),
model: 'gpt-3.5-turbo',
stream: true
})
return c.streamText(async (stream) => {
for await (const message of chatStream) {
await stream.write(message.choices[0]?.delta.content ?? '')
}
})
})
This application can display streamed data from OpenAI's API in a flowing manner.
Screen.Recording.2023-09-21.at.11.11.36.mov
Thanks, @sor4chi and @geelen !
Testing Helper
With testClient
in Testing Helper you can easily write your tests. The object returned by this function is the hc
client, so you can define your request with the editor completion.
import { testClient } from 'hono/testing'
it('test', async() => {
const app = new Hono().get('/search', (c) => c.jsonT({ hello: 'world' }))
const res = await testClient(app).search.$get()
expect(await res.json()).toEqual({ hello: 'world' })
})
sc.mov
Thanks, @hagishi !
JWT helper
We uses JWT functions internally, but now they are exported as JWT Helper. You can import and use them.
import { decode, sign, verify } from 'hono/jwt'
Thanks, @julianpoma !
All Updates
- feat: Improvement of Parse Body by @bakunya in #1461
- feat: Refactor ClientRequest Type for header validation by @hagishi in #1462
- feat: expose the Jwt fucntions under the 'hono/jwt' path by @julianpoma in #1472
- feat: add Hono test client (RPC) by @hagishi in #1451
- chore(benchmark): add memoirist router by @yusukebe in #1474
- feat: add devcontainer to improve developer experience by @C-Dao in #1100
- fix: error to retrieve cookie by @uaichat in #1386
- feat(client) add cookie type validate by @hagishi in #1476
- fix(jwt): avoid using
enum
by @yusukebe in #1485 - fix(context): fix
c.stream()
andc.streamText()
matters by @yusukebe in #1482 - fix(jwt): incorrect error message by @NicoPlyley in #1487
- Next by @yusukebe in #1489
New Contributors
- @bakunya made their first contribution in #1461
- @julianpoma made their first contribution in #1472
- @C-Dao made their first contribution in #1100
- @uaichat made their first contribution in #1386
- @NicoPlyley made their first contribution in #1487
Full Changelog: v3.6.3...v3.7.0