Can Elysia support Next.js, OpenAPI, and Vercel AI?

HEAD
3edbde4:blue_book: doc: update CONTRIBUTION.md
This post might have stale content, as HEAD is 98 commits ahead.

can this comply with these requirements?

  1. next.js, deployed to vercel
  2. openapi spec generation
  3. vercel/ai sdk compatibility (specifically, routes that return createUIMessageStreamResponse
Avatar of anthropic/claude-opus-4.5
Claude Opus 4.5
commented

Now I have a comprehensive picture. Let me provide you with a complete answer:


##Assessment: Can Elysia Comply with Your Requirements?

Based on my analysis of the Elysia codebase and documentation, here's the verdict for each requirement:

###✅ 1. Next.js, deployed to Vercel — YES

Elysia has official support for both:

Next.js Integration:

  • Works with Next.js App Router via route handlers
  • Create a catch-all route at app/api/[[...slugs]]/route.ts
  • Export Elysia's fetch handler as GET, POST, etc.
// app/api/[[...slugs]]/route.ts
import { Elysia } from 'elysia'

const app = new Elysia({ prefix: '/api' })
    .get('/hello', () => 'Hello from Elysia')

export const GET = app.fetch
export const POST = app.fetch

Vercel Deployment:

  • Elysia deploys to Vercel with zero configuration
  • Supports both Node.js and Bun runtimes
  • Vercel has official documentation for Elysia

The WebStandardAdapter in src/adapter/web-standard/index.ts makes this possible by being WinterTC compliant (Web Standard APIs).


###✅ 2. OpenAPI Spec Generation — YES (First-Class Support)

Elysia has exceptional OpenAPI support via @elysiajs/openapi:

import { Elysia } from 'elysia'
import { openapi, fromTypes } from '@elysiajs/openapi'

const app = new Elysia()
    .use(openapi({
        references: fromTypes()  // Auto-generates from TypeScript types!
    }))
    .post('/user', ({ body }) => body, {
        body: t.Object({
            name: t.String()
        }),
        detail: {
            summary: 'Create user',
            tags: ['User']
        }
    })

Key features:

  • Auto-generates OpenAPI 3.0 spec from route schemas
  • OpenAPI Type Gen — unique to Elysia, generates docs from TypeScript types without manual annotations (works with Drizzle, Prisma, etc.)
  • Scalar UI at /openapi endpoint
  • Raw JSON spec at /openapi/json
  • Supports Zod, Valibot via Standard Schema integration
  • Full detail field for route documentation per OpenAPI v3 spec

###⚠️ 3. Vercel AI SDK (createUIMessageStreamResponse) — Partially Supported

Elysia has AI SDK integration but with caveats for createUIMessageStreamResponse specifically:

What works: From src/adapter/utils.ts and the documentation, Elysia natively supports:

  • Returning Response objects directly — which createUIMessageStreamResponse() returns
  • ReadableStream streaming
  • Generator functions for manual streaming
  • Server-Sent Events (SSE)
import { streamText } from 'ai'
import { openai } from '@ai-sdk/openai'

new Elysia()
    .post('/chat', async () => {
        // Option 1: Return the Response directly
        const result = streamText({
            model: openai('gpt-4'),
            prompt: 'Hello'
        })
        return result.toDataStreamResponse()
        
        // Option 2: Return ReadableStream
        // return result.toDataStream()
    })

The createUIMessageStreamResponse concern: Looking at how Elysia handles responses in src/adapter/web-standard/handler.ts:

case 'Response':
    return handleResponse(response as Response, set, request)

Elysia does pass through Response objects, so createUIMessageStreamResponse() should work since it returns a standard Response. However:

  1. Potential issue: Elysia may modify headers or interfere with the stream in handleResponse(). The code shows it deletes content-encoding headers to prevent stream interference.

  2. Recommended approach: Return the Response directly without schema validation:

import { createUIMessageStreamResponse } from 'ai'

new Elysia()
    .post('/api/chat', async ({ body }) => {
        return createUIMessageStreamResponse({
            status: 200,
            stream: yourUIMessageStream
        })
    }, {
        // Skip response validation for streaming
        response: {}
    })

##Summary

RequirementSupportNotes
Next.js + Vercel✅ FullZero-config, official support
OpenAPI Generation✅ ExcellentBest-in-class with Type Gen
createUIMessageStreamResponse⚠️ Should workReturn as raw Response, may need testing

Recommendation: Elysia is a strong choice for requirements 1 & 2. For requirement 3, I'd suggest:

  1. Test createUIMessageStreamResponse in your specific setup
  2. Use result.toDataStreamResponse() from AI SDK if issues arise (definitely works)
  3. Check Elysia's AI SDK docs for latest patterns

Would you like me to dig deeper into any specific aspect?


END OF POST

Can Elysia support Next.js, OpenAPI, and Vercel AI? — elysiajs/elysia