-
-
Notifications
You must be signed in to change notification settings - Fork 8.1k
feat(core): add Promise<Observable> support for SSE handlers #15721
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: master
Are you sure you want to change the base?
Conversation
Adds support for returning Promise<Observable<MessageEvent>> from SSE handlers, enabling async validation before sending HTTP headers. This allows developers to: - Perform async validation before establishing SSE connection - Return custom HTTP status codes (404, 401, etc.) - Set headers based on async data - Implement proper resource validation patterns Implementation details: - RouterResponseController.sse() now accepts Promise<Observable> - Custom status codes extracted from response.statusCode - SseStream.pipe() accepts optional statusCode parameter - Full backward compatibility maintained Fixes nestjs#12260
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.
Pull Request Overview
This PR adds support for returning Promise<Observable<MessageEvent>>
from SSE handlers, enabling async validation and custom HTTP status code handling before establishing SSE connections.
Key Changes:
- SSE handlers can now return
Promise<Observable>
in addition toObservable
, allowing async validation before streaming - Custom HTTP status codes can be set via
response.status()
and are automatically applied to SSE connections - Full backward compatibility maintained with existing synchronous handlers
Reviewed Changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
File | Description |
---|---|
packages/core/router/router-response-controller.ts | Made sse() method async, added support for Promise<Observable> input, and implemented custom status code extraction from response object |
packages/core/router/router-execution-context.ts | Updated SSE handler wrapper to await the async responseController.sse() call |
packages/core/router/sse-stream.ts | Added optional statusCode parameter to pipe() method options |
packages/core/test/router/router-response-controller.spec.ts | Added tests for Promise<Observable> support and custom status code handling |
packages/core/test/router/sse-stream.spec.ts | Added tests verifying custom and default status code behavior in pipe() |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Pull Request Test Coverage Report for Build a5f50a3a-aaea-4bd2-9ff9-c0e49284cc52Details
💛 - Coveralls |
Description
This PR adds support for returning
Promise<Observable<MessageEvent>>
from SSE handlers, enabling async validation and custom HTTP status codes before establishing the SSE connection.Motivation
Fixes #12260
Currently, SSE handlers can only return
Observable<MessageEvent>
, which means HTTP headers are sent immediately. This prevents implementing common patterns like:Changes
Core Implementation
Promise<Observable>
in addition toObservable
statusCode
parameterKey Features
response.status()
Example Usage
Resource validation with 404 response
Authentication with 401 response
Testing
Comprehensive tests added:
Technical Details
Promise.resolve()
for efficient handling of both sync and async casesChecklist