-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
141 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { vike, vike as default } from './runtime/frameworks/elysia.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`)) | ||
} |