-
-
Notifications
You must be signed in to change notification settings - Fork 105
feat(server): add ApiHandlerService to handle request with NestJS #2099
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
Conversation
📝 WalkthroughWalkthroughThis update introduces a new Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant NestJS
participant ApiHandlerService
participant PrismaClient
participant RequestHandler
Client->>NestJS: Sends HTTP request
NestJS->>ApiHandlerService: Injects request, Prisma client, HTTP adapter
ApiHandlerService->>PrismaClient: Loads model metadata and schemas
ApiHandlerService->>RequestHandler: Invokes with request details, Prisma client, metadata, schemas
RequestHandler-->>ApiHandlerService: Returns response (body, status)
alt status >= 400
ApiHandlerService->>NestJS: Throws HttpException
else status < 400
ApiHandlerService->>NestJS: Returns response body
end
NestJS-->>Client: Sends response or error
Suggested reviewers
📜 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 (
|
3afc53d
to
09677d7
Compare
09677d7
to
a20e743
Compare
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: 0
🧹 Nitpick comments (4)
packages/server/src/nestjs/api-handler.service.ts (4)
16-16
: Consider breaking long constructor lineThe constructor parameter list is quite long. Consider formatting it across multiple lines for better readability.
- constructor(private readonly httpAdapterHost: HttpAdapterHost, @Inject(ENHANCED_PRISMA) private readonly prisma: DbClientContract, @Inject(REQUEST) private readonly request: unknown) {} + constructor( + private readonly httpAdapterHost: HttpAdapterHost, + @Inject(ENHANCED_PRISMA) private readonly prisma: DbClientContract, + @Inject(REQUEST) private readonly request: unknown + ) {}
23-25
: URL construction approach might be fragilePrefixing with "http://" to create a valid URL works but might not handle all hostname formats. Consider using a more robust URL parsing approach.
- // prefix with http:// to make a valid url accepted by URL constructor - const url = new URL(`http://${hostname}${requestUrl}`); + // Ensure valid URL regardless of hostname format + const fullUrl = hostname.includes('://') ? `${hostname}${requestUrl}` : `http://${hostname}${requestUrl}`; + const url = new URL(fullUrl);
29-29
: Type safety consideration for request bodyThe current approach for extracting the request body uses type assertion without validation. Consider adding error handling for cases where the body property might be missing.
- const requestBody = (this.request as {body: unknown}).body; + const requestBody = this.request && typeof this.request === 'object' && 'body' in this.request + ? (this.request as {body: unknown}).body + : undefined;
43-49
: Fix typo in comment and improve error handlingThere's a typo in the comment, and the error handling could be more specific about the error type.
- // if reponse code >= 400 throw nestjs HttpException + // if response code >= 400 throw nestjs HttpException // the error response will be generated by nestjs // caller can use try/catch to deal with this manually also if (response.status >= 400) { // eslint-disable-next-line @typescript-eslint/no-explicit-any - throw new HttpException(response.body as Record<string, any>, response.status) + throw new HttpException( + typeof response.body === 'object' ? response.body as Record<string, any> : { message: String(response.body) }, + response.status + ) }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (1)
packages/server/tsconfig.json
is excluded by!**/*.json
📒 Files selected for processing (8)
packages/server/src/nestjs/api-handler.service.ts
(1 hunks)packages/server/src/nestjs/index.ts
(1 hunks)packages/server/src/nestjs/interfaces/api-handler-options.interface.ts
(1 hunks)packages/server/src/nestjs/interfaces/index.ts
(1 hunks)packages/server/src/nestjs/interfaces/zenstack-module-options.interface.ts
(1 hunks)packages/server/src/nestjs/zenstack.constants.ts
(1 hunks)packages/server/src/nestjs/zenstack.module.ts
(1 hunks)packages/server/tests/adapter/nestjs.test.ts
(3 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (3)
packages/server/src/nestjs/interfaces/api-handler-options.interface.ts (1)
packages/server/src/types.ts (1)
AdapterBaseOptions
(32-56)
packages/server/src/nestjs/api-handler.service.ts (4)
packages/server/src/nestjs/zenstack.constants.ts (1)
ENHANCED_PRISMA
(4-4)packages/runtime/src/types.ts (1)
DbClientContract
(91-93)packages/server/src/nestjs/interfaces/api-handler-options.interface.ts (1)
ApiHandlerOptions
(3-5)packages/server/src/shared.ts (1)
loadAssets
(5-21)
packages/server/tests/adapter/nestjs.test.ts (2)
packages/server/tests/utils.ts (1)
schema
(3-29)packages/testtools/src/schema.ts (1)
loadSchema
(163-369)
🔇 Additional comments (12)
packages/server/src/nestjs/zenstack.constants.ts (1)
1-4
: Well-structured constant definition with clear documentation.The constant is properly documented with a JSDoc comment and follows the standard naming convention for constants (uppercase with underscores). Centralizing this token in a dedicated constants file is a good practice to avoid duplication and maintain consistency across the codebase.
packages/server/src/nestjs/interfaces/index.ts (1)
1-2
: Good pattern for centralizing interface exports.Creating a dedicated index file to re-export related interfaces follows best practices for module organization. This approach simplifies imports for consumers and provides a single entry point for related interfaces, making the codebase more maintainable.
packages/server/src/nestjs/index.ts (1)
2-3
: Properly expanded public API with new exports.The addition of exports for the new
ApiHandlerService
and constants aligns with the PR objectives. This makes the new functionality available to consumers while maintaining a well-defined public API surface.packages/server/src/nestjs/interfaces/api-handler-options.interface.ts (1)
1-5
: Well-defined interface with appropriate extension.The
ApiHandlerOptions
interface correctly extendsAdapterBaseOptions
and adds the optionalbaseUrl
property. This aligns with the PR objectives to provide greater flexibility for routing in NestJS environments. The interface follows TypeScript naming conventions and is cleanly implemented.packages/server/src/nestjs/zenstack.module.ts (1)
1-3
: Code organization improvementThis refactoring enhances maintainability by importing the constants and interfaces from dedicated files instead of declaring them locally in the module file. This follows NestJS best practices for modular code organization.
packages/server/src/nestjs/api-handler.service.ts (2)
14-16
: The service scope is appropriately set to REQUESTUsing request-scoped injection is the correct approach for this service since it needs to handle individual HTTP requests and access the request object.
26-26
: Good implementation of baseUrl handlingThe conditional path slicing for baseUrl provides flexibility for route configuration, which aligns with the PR objective of allowing more control over routing behavior.
packages/server/tests/adapter/nestjs.test.ts (3)
215-271
: Well-structured test for default behaviorThis test case thoroughly verifies the default behavior of the ApiHandlerService, including proper mocking of dependencies and validation of the response structure.
273-365
: Comprehensive test for REST API handlerThis test case provides excellent coverage of the service's capability to use a custom handler with complex response formats. The response validation is thorough and helps ensure compatibility with JSON:API specifications.
367-424
: Validates baseUrl functionalityThis test case confirms that the baseUrl option correctly modifies path handling, which is essential for the PR's objective of providing flexibility in routing.
packages/server/src/nestjs/interfaces/zenstack-module-options.interface.ts (2)
6-11
: Well-defined interface with clear documentationThe ZenStackModuleOptions interface is well-designed with clear JSDoc comments. The getEnhancedPrisma method signature allows for model-specific enhancement, which provides flexibility.
16-41
: Comprehensive async options interface following NestJS patternsThe ZenStackModuleAsyncOptions interface follows NestJS conventions for async module configuration. The detailed documentation and optional properties provide flexibility for different use cases.
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 @ppodds , many thanks for making this contribution! I think it's a good addition, with the service you can even implement a catch all route to automatically forward all requests to the underlying handler, right?
Will you be interested in changing the documentation as well? Basically after the zenstack module is registered, you can have two paths to using it: 1. directly inject the enhanced prisma client and use it in whatever service/controller you want; 2. use the api handler service to automate request handling.
Sure! I will add it after the PR has been merged. |
Awesome! The piece of documentation is here: https://github.com/zenstackhq/zenstack-docs/blob/main/docs/reference/server-adapters/nestjs.mdx I'm merging this PR. |
When using
@zenstackhq/server
with NestJS, the API handler only works with the Fastify or Express adapter and requires registering middleware during module or app initialization. This approach creates a tight dependency on the framework implementation and makes it difficult to customize routing behavior.This PR not only restructures the NestJS adapter folder to better align with NestJS conventions, but also introduces an
ApiHandlerService
that users can import and use. With this service, users have the flexibility to apply the handler to specific routes as needed and gain more control over the response generated by the API handler.Usage
post.module.ts
post.controller.ts
I’m happy to contribute additional tests or usage examples if needed. Suggestions or improvements are very welcome—I'm open to discussion!