Skip to content

Commit

Permalink
Consumption example.
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinTail committed Dec 9, 2024
1 parent 3fe9861 commit 032c1e4
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
14 changes: 11 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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<string>`;
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
8 changes: 8 additions & 0 deletions tests/unit/__snapshots__/documentation.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1721,6 +1721,8 @@ paths:
/v1/events/time:
get:
operationId: GetV1EventsTime
tags:
- subscriptions
parameters:
- name: trigger
in: query
Expand Down Expand Up @@ -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
"
Expand Down Expand Up @@ -2104,6 +2108,8 @@ paths:
/v1/events/time:
get:
operationId: GetV1EventsTime
tags:
- subscriptions
parameters:
- name: trigger
in: query
Expand Down Expand Up @@ -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
"
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/__snapshots__/integration.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand Down

0 comments on commit 032c1e4

Please sign in to comment.