Skip to content

Commit

Permalink
elysia
Browse files Browse the repository at this point in the history
  • Loading branch information
nitedani committed Jul 30, 2024
1 parent 2a13f6d commit 08a0a6a
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 2 deletions.
18 changes: 18 additions & 0 deletions packages/vike-node/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,24 @@ function startServer() {
}
```

Elysia (Bun):

```js
// server/index.js

import { Elysia } from 'elysia'
import vike from 'vike-node/elysia'

startServer()

function startServer() {
const app = new Elysia()
app.use(vike())
const port = +(process.env.PORT || 3000)
app.listen(port, () => console.log(`Server running at http://localhost:${port}`))
}
```

## Migration guide:

```diff
Expand Down
5 changes: 5 additions & 0 deletions packages/vike-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"./connect": "./dist/connect.js",
"./fastify": "./dist/fastify.js",
"./hono": "./dist/hono.js",
"./elysia": "./dist/elysia.js",
"./plugin": "./dist/plugin/index.js"
},
"scripts": {
Expand All @@ -31,6 +32,7 @@
"@types/node": "^20.14.12",
"fastify": "^4.28.1",
"hono": "^4.5.1",
"elysia": "^1.1.4",
"typescript": "^5.5.4",
"vike": "^0.4.181",
"vite": "^5.3.5"
Expand All @@ -46,6 +48,9 @@
"hono": [
"./dist/hono.d.ts"
],
"elysia": [
"./dist/elysia.d.ts"
],
"plugin": [
"./dist/plugin/index.d.ts"
]
Expand Down
1 change: 1 addition & 0 deletions packages/vike-node/src/elysia.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { vike, vike as default } from './runtime/frameworks/elysia.js'
52 changes: 52 additions & 0 deletions packages/vike-node/src/runtime/frameworks/elysia.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
export { vike }

import { Elysia, NotFoundError } from 'elysia'
import { connectToWeb } from '../adapters/connectToWeb.js'
import { createHandler } from '../handler.js'
import type { VikeOptions } from '../types.js'

/**
* Creates an Elysia plugin to handle Vike requests.
*
* @param {VikeOptions<Request>} [options] - Configuration options for Vike.
*
* @returns {Elysia} An Elysia plugin that handles all GET requests and processes them with Vike.
*
* @description
* The plugin:
* 1. Sets up a catch-all GET route handler that processes requests using Vike's handler.
* 2. Throws a NotFoundError if Vike doesn't handle the request, allowing Elysia to manage 404 responses.
*
* @example
* ```js
* import { Elysia } from 'elysia'
* import { vike } from 'vike-node/elysia'
*
* const app = new Elysia()
* app.use(vike())
* app.listen(3000)
* ```
*
* @throws {NotFoundError} Thrown when Vike doesn't handle the request, allowing Elysia to manage 404 responses.
*/
function vike(options?: VikeOptions<Request>): Elysia {
const handler = createHandler(options)
return new Elysia({
name: 'vike-node:elysia'
}).get('*', async (ctx) => {
const response = await connectToWeb((req, res, next) =>
handler({
req,
res,
next,
platformRequest: ctx.request
})
)(ctx.request)

if (response) {
return response
}

throw new NotFoundError()
})
}
4 changes: 2 additions & 2 deletions packages/vike-node/src/runtime/frameworks/hono.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ import type { VikeOptions } from '../types.js'
* ```
*
*/
function vike(options?: VikeOptions): MiddlewareHandler {
const handler = createHandler<HonoRequest>(options)
function vike(options?: VikeOptions<HonoRequest>): MiddlewareHandler {
const handler = createHandler(options)
return async function middleware(ctx, next) {
const req = ctx.env.incoming as IncomingMessage
globalStore.setupHMRProxy(req)
Expand Down
31 changes: 31 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test/vike-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"express": "^4.19.2",
"fastify": "^4.28.1",
"hono": "^4.5.1",
"elysia": "^1.1.4",
"prisma": "^5.17.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
Expand Down
31 changes: 31 additions & 0 deletions test/vike-node/server/index-elysia.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Elysia } from 'elysia'
import { telefunc } from 'telefunc'
import vike from 'vike-node/elysia'
import { init } from '../database/todoItems'

startServer()

async function startServer() {
await init()
const app = new Elysia()

const port = process.env.PORT || 3000
app.post('/_telefunc', async (ctx) => {
const context = {}
const httpResponse = await telefunc({
url: ctx.request.url,
method: ctx.request.method,
body: await ctx.request.text(),
context
})
const { body, statusCode, contentType } = httpResponse
return new Response(body, { headers: { 'content-type': contentType }, status: statusCode })
})

app.onAfterHandle((ctx) => {
ctx.set.headers['x-test'] = 'test'
})

app.use(vike())
app.listen(+port, () => console.log(`Server running at http://localhost:${port}`))
}

0 comments on commit 08a0a6a

Please sign in to comment.