Add TypeScript/JavaScript client support for SSE events#75
Conversation
Co-authored-by: ddunkin <264744+ddunkin@users.noreply.github.com>
Co-authored-by: ddunkin <264744+ddunkin@users.noreply.github.com>
ddunkin
left a comment
There was a problem hiding this comment.
@copilot EventSource only supports GET. We need to support POST also, so may have to use fetch directly. See conversation FacilityApi/FacilityApi.github.io#32 (comment)
Is there any benefit to using EventSource for GET and a different implementation for POST?
…parsing Co-authored-by: ddunkin <264744+ddunkin@users.noreply.github.com>
Implemented support for both GET and POST methods! The generator now uses:
Both approaches return the same async iterable interface. The fetch implementation reads the response body as a stream and parses SSE format ( Benefits of dual approach:
Commit: a875fde |
@copilot having two different implementations is worse unless there is some benefit to using EventSource if available. |
…rce) Co-authored-by: ddunkin <264744+ddunkin@users.noreply.github.com>
Simplified to use a single fetch-based implementation for all HTTP methods (GET, POST, etc.). Removed the EventSource implementation to reduce complexity. Benefits:
All events now use Commit: 4984962 |
|
@copilot move |
Co-authored-by: ddunkin <264744+ddunkin@users.noreply.github.com>
Moved Changes:
The function is now part of the shared facility-core package, eliminating code duplication across generated clients. Commit: a02dcbd |
|
@copilot update versions in package.json and Directory.Build.props (minor version bump) and update ReleaseNotes.md and ReleaseNotesNpm.md. |
Co-authored-by: ddunkin <264744+ddunkin@users.noreply.github.com>
Updated versions and release notes: Version bumps (minor):
Release notes added:
Commit: f0e4589 |
|
@copilot fix build errors Will this implementation work on browsers and Node.js? |
Co-authored-by: ddunkin <264744+ddunkin@users.noreply.github.com>
... Fixed TypeScript build errors by declaring Browser compatibility: ✅ Works in all modern browsers (Chrome 43+, Firefox 65+, Safari 10.1+, Edge 79+) Node.js compatibility: ✅ Works natively in Node.js 16.5+ (has TextDecoder and ReadableStream built-in). For older Node.js versions (14-16.4), users can install polyfills like The implementation uses standard Web APIs that are available in both environments:
Commit: 8c5c3d6 |
This reverts commit 8c5c3d6.
… iteration for SSE data processing.
| var utilityImports = new List<string> { "fetchResponse", "createResponseError", "createRequiredRequestFieldError" }; | ||
| if (httpServiceInfo.Events.Count > 0) | ||
| utilityImports.Add("createFetchEventStream"); | ||
| code.WriteLine($"const {{ {string.Join(", ", utilityImports)} }} = HttpClientUtility;"); |
There was a problem hiding this comment.
Not actually suggesting a change, but it's funny that a raw string inverts what doubled brackets mean.
| code.WriteLine($"const {{ {string.Join(", ", utilityImports)} }} = HttpClientUtility;"); | |
| code.WriteLine($$"""const { {{string.Join(", ", utilityImports)}} } = HttpClientUtility;"""); |
| return false; | ||
| } | ||
|
|
||
| private void WriteFetchEventStreamHelper(CodeWriter code) |
There was a problem hiding this comment.
I'm so used to the build catching that.
Implement TypeScript/JavaScript client support for events (SSE streaming)
✅ Complete - All Tasks Done
Implementation Summary
This PR adds client-only support for Facility events using Server-Sent Events (SSE) with
createFetchEventStreamnow part of the coreHttpClientUtility.Core Changes:
Promise<IServiceResult<AsyncIterable<IServiceResult<TResponse>>>>createFetchEventStreamtoHttpClientUtilitynamespaceIFetchResponseinterface to includeokandbodyproperties for SSE supportTextDecoderandReadableStreamAPIs (available in both browsers and Node.js 16.5+)HttpClientUtility.createFetchEventStream(TypeScript) or imports from destructuredHttpClientUtility(JavaScript)TypeScript Compatibility Fix:
TextDecoderas globalanyto work with ES2019 lib targetbodytype toanyto avoid needing DOM lib forReadableStreamRuntime Compatibility:
The implementation works in:
web-streams-polyfill,text-encoding)Generated Code Example:
Usage Pattern:
Design Decisions:
Test Results
Release Notes
NuGet (3.6.0):
npm (2.3.0):
HttpClientUtility.createFetchEventStreamfor Server-Sent Events support.Files Changed
ts/src/facilityCore.ts- Fixed TypeScript compilation, added global declarationsDirectory.Build.props- Bumped version to 3.6.0ts/package.json- Bumped version to 2.3.0ReleaseNotes.md- Added 3.6.0 release notesReleaseNotesNpm.md- Added 2.3.0 release notessrc/Facility.CodeGen.JavaScript/JavaScriptGenerator.cs- Use HttpClientUtility.createFetchEventStreamtests/.../JavaScriptGeneratorTests.cs- Updated testsOriginal prompt
Implement TypeScript/JavaScript client support for events (SSE streaming)
Overview
Add client-only support for Facility events using Server-Sent Events (SSE) in the TypeScript/JavaScript generator. Events were added to the core Facility definition in #47 but intentionally skipped in #67. This implementation should be minimal and use the EventSource API with support for Node.js polyfills.
Reference Implementations
Requirements
1. TypeScript Method Signature
Events should generate client methods with the signature:
Example for an event like:
Should generate:
2. Client Implementation Requirements
Use EventSource API
EventSourceAPI for SSE connectionseventsourcepackage)HTTP Mapping
Content-Type: text/event-streamdata:event contains a JSON-serialized response DTOSSE Parsing
data: {...}\n\nmessageevents (default SSE event type)Error Handling
3. Consumer Usage Pattern
4. Code Generation Changes
Generator Updates (
src/Facility.CodeGen.JavaScript/JavaScriptGenerator.cs)ServiceEventInfoalongsideServiceMethodInfoHelper/Utility Code
5. Testing
conformance/ConformanceApi.fsd) - already has fibonacci event definedImplementation Notes
EventSource Limitations
[http(method: GET)]in JavaScript clientsRecommended approach: Start with GET-only support, add a validation warning if an event uses POST
Node.js Polyfill Support
Users can install and configure EventSource polyfill like:
The generated code should work with any EventSource-compatible implementation.
Minimal Implementation Philosophy
File Changes Expected
src/Facility.CodeGen.JavaScript/JavaScriptGenerator.cs- Main generator logictests/Facility.CodeGen.JavaScript.UnitTests/JavaScriptGeneratorTests.cs- Add test casesSuccess Criteria
This pull request was created as a result of the following prompt from Copilot chat.
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.