-
-
Notifications
You must be signed in to change notification settings - Fork 106
feat: add elysiajs #2114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
feat: add elysiajs #2114
Conversation
📝 WalkthroughWalkthroughA new Elysia middleware handler is introduced for integrating ZenStack's RPC API with Prisma, including error handling and schema loading. An index file re-exports the handler, and a comprehensive test suite is added to validate both RPC and REST handler behaviors using Elysia, Prisma, and Zod schemas. Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant ElysiaApp
participant Middleware
participant Prisma
participant RPCApiHandler
Client->>ElysiaApp: HTTP Request (any method/path)
ElysiaApp->>Middleware: Passes request to catch-all handler
Middleware->>Prisma: getPrisma(context)
alt Prisma client unavailable
Middleware-->>Client: 500 Error (Prisma client not found)
else Path missing
Middleware-->>Client: 400 Error (Path missing)
else
Middleware->>RPCApiHandler: method, path, query, body, Prisma, metadata, schemas
RPCApiHandler-->>Middleware: { status, body }
Middleware-->>Client: Response with status and body
end
Possibly related PRs
Note ⚡️ AI Code Reviews for VS Code, Cursor, WindsurfCodeRabbit now has a plugin for VS Code, Cursor and Windsurf. This brings AI code reviews directly in the code editor. Each commit is reviewed immediately, finding bugs before the PR is raised. Seamless context handoff to your AI code agent ensures that you can easily incorporate review feedback. Note ⚡️ Faster reviews with cachingCodeRabbit now supports caching for code and dependencies, helping speed up reviews. This means quicker feedback, reduced wait times, and a smoother review experience overall. Cached data is encrypted and stored securely. This feature will be automatically enabled for all accounts on May 16th. To opt out, configure 📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Nitpick comments (2)
packages/server/src/elysia/handler.ts (1)
27-33
: Add type safety when retrieving Prisma clientThe current implementation casts the Prisma client directly to
DbClientContract
without any type checking. Consider adding runtime type verification to ensure the object has the expected shape.-const prisma = (await options.getPrisma({ request, body, set } as ElysiaContext)) as DbClientContract; +const prismaClient = await options.getPrisma({ request, body, set } as ElysiaContext); + +// Validate that we have a proper Prisma client +const prisma = prismaClient as DbClientContract; +if (!prisma || typeof prisma.$transaction !== 'function') { set.status = 500; return { message: 'unable to get prisma from request context' }; }packages/server/tests/adapter/elysia.test.ts (1)
180-183
: Add test coverage for SuperJSON parsingThe
unmarshal
function supports SuperJSON parsing, but this functionality is never tested. Consider adding tests that utilizeuseSuperJson = true
to ensure this path is covered.You could add a test case similar to the existing ones but that uses SuperJSON for serialization:
it('supports SuperJSON serialization', async () => { // Test setup with SuperJSON // ... expect(await unmarshal(r, true)).toMatchObject({ // expected structure }); });
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (1)
packages/server/package.json
is excluded by!**/*.json
📒 Files selected for processing (3)
packages/server/src/elysia/handler.ts
(1 hunks)packages/server/src/elysia/index.ts
(1 hunks)packages/server/tests/adapter/elysia.test.ts
(1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
packages/server/src/elysia/handler.ts (3)
packages/server/src/types.ts (1)
AdapterBaseOptions
(32-56)packages/server/src/shared.ts (1)
loadAssets
(5-21)packages/runtime/src/types.ts (1)
DbClientContract
(91-93)
🔇 Additional comments (9)
packages/server/src/elysia/index.ts (1)
1-1
: Clean barrel file for Elysia adapter exportsThis re-export approach is clean and follows the module pattern used elsewhere in the codebase, providing a convenient entry point for consumers to import the Elysia handler.
packages/server/src/elysia/handler.ts (3)
1-6
: Appropriate imports for Elysia adapter implementationAll necessary imports are correctly included, utilizing types and functionality from both Elysia and the ZenStack ecosystem.
10-15
: Well-typed interface for Elysia optionsThe
ElysiaOptions
interface properly extends the base adapter options and adds the requiredgetPrisma
callback to retrieve a Prisma client from the request context.
21-24
: Clean initialization in the handler factoryThe implementation correctly loads model metadata and Zod schemas using the shared
loadAssets
function, and sets up the default request handler when none is provided.packages/server/tests/adapter/elysia.test.ts (5)
1-12
: Test setup has appropriate imports and configurationsThe imports and setup code correctly include all necessary dependencies for testing the Elysia adapter.
13-83
: Comprehensive RPC handler tests cover critical pathsThe test suite for RPC handler covers all essential operations including querying, creating, updating, and deleting records, as well as more advanced functionality like filtering, aggregation, and grouping.
85-112
: Custom load path test verifies schema loading flexibilityThis test correctly verifies that the adapter can load model metadata and Zod schemas from a custom output path, confirming the adapter's ability to work with various project configurations.
115-172
: REST handler tests provide adequate coverageThe REST handler tests properly validate CRUD operations and filtering capabilities using the REST endpoint format.
185-189
: Clean helper function for Elysia app creationThe
createElysiaApp
helper function provides a clean way to set up an Elysia app with the middleware, promoting code reuse across tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @rodrigoburigool, many thanks for making this PR!
I've added a few comments. Please help check when you have time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
🧹 Nitpick comments (3)
packages/server/src/elysia/handler.ts (3)
29-30
: Remove unnecessaryasync
wrapperNo
await
is used in this factory, yet marking the arrow function asasync
makes the return typePromise<Elysia>
. Consumers must thenawait
or unwrap it even though the operation is synchronous.- return async (app: Elysia) => { + return (app: Elysia) => {
39-48
: NormalizebasePath
once to avoid subtle prefix-matching bugs
startsWith
will fail if a user provides"api"
instead of"/api"
, silently forwarding an untrimmed path. Consider normalizingoptions.basePath
during handler construction (e.g., ensure a leading/
and strip trailing/
) and then compare against the sanitized form.
71-76
: Log internal errors for observabilityReturning a generic 500 is good for security, but swallowing the actual exception hampers debugging. Emit the error through the provided logger (if any) or fall back to
console.error
:} catch (err) { + options.logger?.error?.('Elysia handler error:', err); + if (!options.logger) { + // eslint-disable-next-line no-console + console.error('Elysia handler error:', err); + } set.status = 500; return { message: 'An internal server error occurred', }; }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
Cache: Disabled due to data retention organization setting
Knowledge Base: Disabled due to data retention organization setting
⛔ Files ignored due to path filters (2)
packages/server/package.json
is excluded by!**/*.json
pnpm-lock.yaml
is excluded by!**/pnpm-lock.yaml
,!**/*.yaml
📒 Files selected for processing (1)
packages/server/src/elysia/handler.ts
(1 hunks)
Add Elysia adapter