From 032c1e4787d5c79234de0293fa5fe553b0c9c471 Mon Sep 17 00:00:00 2001 From: Robin Tail Date: Mon, 9 Dec 2024 18:45:28 +0100 Subject: [PATCH] Consumption example. --- CHANGELOG.md | 14 +++++++++++--- README.md | 6 ++++++ .../unit/__snapshots__/documentation.spec.ts.snap | 8 ++++++++ tests/unit/__snapshots__/integration.spec.ts.snap | 2 +- 4 files changed, 26 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f1710e1b3..39f8efab3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,10 +5,12 @@ ### v21.5.0 - Feat: Introducing [Server-Sent Events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events): - - Event streams feature is now available using `EventStreamFactory` class; - - Client application can subscribe to the event stream using `EventSource` class instance. + - Basic implementation of the event streams feature is now available using `EventStreamFactory` class; + - The new factory is similar to `EndpointsFactory` including the middlewares support; + - Client application can subscribe to the event stream using `EventSource` class instance; + - `Documentation` and `Integration` do not have yet a special depiction of such endpoints. -```typescript +```ts import { z } from "zod"; import { EventStreamFactory } from "express-zod-api"; import { setTimeout } from "node:timers/promises"; @@ -26,6 +28,12 @@ export const subscriptionEndpoint = EventStreamFactory({ }); ``` +```js +new EventSource("https://example.com/api/v1/time", (event) => { + const data = JSON.parse(event.data); // number +}); +``` + ### v21.4.0 - Return type of public methods `getTags()` and `getScopes()` of `Endpoint` corrected to `ReadyonlyArray`; diff --git a/README.md b/README.md index af8be642f..ba143f7d3 100644 --- a/README.md +++ b/README.md @@ -1218,6 +1218,12 @@ export const subscriptionEndpoint = EventStreamFactory({ }); ``` +```js +new EventSource("https://example.com/api/v1/time", (event) => { + const data = JSON.parse(event.data); // number +}); +``` + If you need more capabilities, such as bidirectional event sending, I have developed an additional websocket operating framework, [Zod Sockets](https://github.com/RobinTail/zod-sockets), which has similar principles and capabilities. diff --git a/tests/unit/__snapshots__/documentation.spec.ts.snap b/tests/unit/__snapshots__/documentation.spec.ts.snap index e4faeb4bc..81330b529 100644 --- a/tests/unit/__snapshots__/documentation.spec.ts.snap +++ b/tests/unit/__snapshots__/documentation.spec.ts.snap @@ -1721,6 +1721,8 @@ paths: /v1/events/time: get: operationId: GetV1EventsTime + tags: + - subscriptions parameters: - name: trigger in: query @@ -1797,6 +1799,8 @@ tags: description: Everything about the users - name: files description: Everything about the files processing + - name: subscriptions + description: Everything about the subscriptions servers: - url: https://example.com " @@ -2104,6 +2108,8 @@ paths: /v1/events/time: get: operationId: GetV1EventsTime + tags: + - subscriptions parameters: - name: trigger in: query @@ -2512,6 +2518,8 @@ tags: description: Everything about the users - name: files description: Everything about the files processing + - name: subscriptions + description: Everything about the subscriptions servers: - url: https://example.com " diff --git a/tests/unit/__snapshots__/integration.spec.ts.snap b/tests/unit/__snapshots__/integration.spec.ts.snap index 3fd39126a..8df3b9f5a 100644 --- a/tests/unit/__snapshots__/integration.spec.ts.snap +++ b/tests/unit/__snapshots__/integration.spec.ts.snap @@ -707,7 +707,7 @@ export const endpointTags = { "get /v1/avatar/stream": ["users", "files"], "post /v1/avatar/upload": ["files"], "post /v1/avatar/raw": ["files"], - "get /v1/events/time": [], + "get /v1/events/time": ["subscriptions"], }; export type Implementation = (