Skip to content
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

Add support for defining streams #4513

Merged
merged 33 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
9c539e8
Add @typespec/streams library
Sep 6, 2024
b9681fa
add events/sse
Sep 22, 2024
7ba0d24
improve HttpStream
Sep 22, 2024
5731ffd
update semantic walker to emit exitTuple
Sep 24, 2024
c5f6270
add events validation and event definition generation
Sep 24, 2024
32c76e0
rename StreamTestRunner to StreamsTestRunner
Sep 24, 2024
eb4181e
update libraries to include Streams in their test hosts
Sep 24, 2024
8793877
update events decorator implementation names
Sep 24, 2024
ae12147
update streams to use useStateMap
Sep 24, 2024
bc90463
update events to use diagnosticCollector and make EventDefinition pro…
Sep 24, 2024
03d4d09
remove unused events namespace const
Sep 24, 2024
eb29ede
update events decorators to use useStateSet and useStateMap
Sep 24, 2024
9c55dfc
add http streams tests
Sep 24, 2024
28b40ae
add tests for sse package
Sep 24, 2024
10caf81
Merge remote-tracking branch 'upstream/main' into add-streaming
Sep 24, 2024
76d8a30
update docs for new packages
Sep 24, 2024
2ee63e3
update package-lock.json
Sep 24, 2024
e170c64
remove unnecessary eslintrc files
Sep 24, 2024
e2e05c1
fix formatting
Sep 24, 2024
c03aff2
update vitest in new packages
Sep 25, 2024
47130b8
update getEventDefinitions to be experimental
Sep 25, 2024
b757bd9
Merge remote-tracking branch 'upstream/main' into add-streaming
Sep 25, 2024
3bd0d7b
add changelog entries
Sep 25, 2024
8bfe046
add new packages to sidebar
Sep 25, 2024
1584184
fix streams doc reference
Sep 25, 2024
8785cb1
Merge remote-tracking branch 'upstream/main' into add-streaming
Sep 25, 2024
3872e4c
more changelog!
Sep 25, 2024
3e540ff
Merge remote-tracking branch 'upstream/main' into add-streaming
Sep 25, 2024
1a60ed5
Merge branch 'main' into add-streaming
chrisradek Sep 26, 2024
8501fad
Merge remote-tracking branch 'upstream/main' into add-streaming
Oct 5, 2024
895ebc8
update http to export tsp streams submodule
Oct 5, 2024
d03d43a
update lock file
Oct 5, 2024
44996e5
remove changelogs that are no longer needed
Oct 5, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .chronus/changes/add-streaming-2024-8-25-11-52-49.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: feature
packages:
- "@typespec/events"
---

Adds a new core package for describing events.
7 changes: 7 additions & 0 deletions .chronus/changes/add-streaming-2024-8-25-11-53-37.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: feature
packages:
- "@typespec/streams"
---

Adds a new core package for describing streams and the type of data they contain.
7 changes: 7 additions & 0 deletions .chronus/changes/add-streaming-2024-8-25-11-54-27.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: feature
packages:
- "@typespec/sse"
---

Adds a new core package to describe server-sent events.
7 changes: 7 additions & 0 deletions .chronus/changes/add-streaming-2024-8-25-11-56-3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: feature
packages:
- "@typespec/http"
---

Adds HttpStream and JsonlStream models to to support streaming use-cases.
7 changes: 7 additions & 0 deletions .chronus/changes/add-streaming-2024-8-25-11-56-36.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@typespec/compiler"
---

Fixes issue with the semantic walker where `exitTuple` was not being emitted.
112 changes: 112 additions & 0 deletions docs/libraries/events/reference/decorators.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
---
title: "Decorators"
toc_min_heading_level: 2
toc_max_heading_level: 3
---

# Decorators

## TypeSpec.Events

### `@contentType` {#@TypeSpec.Events.contentType}

Specifies the content type of the event envelope, event body, or event payload.
When applied to an event payload, that field must also have a corresponding `@data`
decorator.

```typespec
@TypeSpec.Events.contentType(contentType: valueof string)
```

#### Target

`UnionVariant | ModelProperty`

#### Parameters

| Name | Type | Description |
| ----------- | ---------------- | ----------- |
| contentType | `valueof string` | |

#### Examples

```typespec
@events
union MixedEvents {
@contentType("application/json")
message: {
id: string,
text: string,
},
}
```

##### Specify the content type of the event payload.

```typespec
@events
union MixedEvents {
{
done: true,
},
{
done: false,
@data @contentType("text/plain") value: string,
},
}
```

### `@data` {#@TypeSpec.Events.data}

Identifies the payload of an event.
Only one field in an event can be marked as the payload.

```typespec
@TypeSpec.Events.data
```

#### Target

`ModelProperty`

#### Parameters

None

#### Examples

```typespec
@events
union MixedEvents {
{
metadata: Record<string>,
@data payload: string,
},
}
```

### `@events` {#@TypeSpec.Events.events}

Specify that this union describes a set of events.

```typespec
@TypeSpec.Events.events
```

#### Target

`Union`

#### Parameters

None

#### Examples

```typespec
@events
union MixedEvents {
pingEvent: string,
doneEvent: "done",
}
```
40 changes: 40 additions & 0 deletions docs/libraries/events/reference/index.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
title: Overview
sidebar_position: 0
toc_min_heading_level: 2
toc_max_heading_level: 3
---

import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';

# Overview

TypeSpec library providing events bindings

## Install

<Tabs>
<TabItem value="spec" label="In a spec" default>

```bash
npm install @typespec/events
```

</TabItem>
<TabItem value="library" label="In a library" default>

```bash
npm install --save-peer @typespec/events
```

</TabItem>
</Tabs>

## TypeSpec.Events

### Decorators

- [`@contentType`](./decorators.md#@TypeSpec.Events.contentType)
- [`@data`](./decorators.md#@TypeSpec.Events.data)
- [`@events`](./decorators.md#@TypeSpec.Events.events)
71 changes: 71 additions & 0 deletions docs/libraries/sse/reference/data-types.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
---
title: "Data types"
toc_min_heading_level: 2
toc_max_heading_level: 3
---

# Data types

## TypeSpec.SSE

### `SSEStream` {#TypeSpec.SSE.SSEStream}

Describes a stream of server-sent events.

The content-type is set to `text/event-stream`.

The server-sent events are described by `Type`.
The event type for any event can be defined by using named union variants.
When a union variant is not named, it is considered a 'message' event.

```typespec
model TypeSpec.SSE.SSEStream<Type>
```

#### Template Parameters

| Name | Description |
| ---- | ---------------------------------------------------- |
| Type | The set of models describing the server-sent events. |

#### Examples

##### Mix of named union variants and terminal event

```typespec
model UserConnect {
username: string;
time: string;
}
model UserMessage {
username: string;
time: string;
text: string;
}
model UserDisconnect {
username: string;
time: string;
}
@TypeSpec.Events.events
union ChannelEvents {
userconnect: UserConnect,
usermessage: UserMessage,
userdisconnect: UserDisconnect,
@Events.contentType("text/plain")
@terminalEvent
"[unsubscribe]",
}
op subscribeToChannel(): SSEStream<ChannelEvents>;
```

#### Properties

| Name | Type | Description |
| ----------- | --------------------- | ----------- |
| contentType | `"text/event-stream"` | |
| body | `string` | |
26 changes: 26 additions & 0 deletions docs/libraries/sse/reference/decorators.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
title: "Decorators"
toc_min_heading_level: 2
toc_max_heading_level: 3
---

# Decorators

## TypeSpec.SSE

### `@terminalEvent` {#@TypeSpec.SSE.terminalEvent}

Indicates that the presence of this event is a terminal event,
and the client should disconnect from the server.

```typespec
@TypeSpec.SSE.terminalEvent
```

#### Target

`UnionVariant`

#### Parameters

None
42 changes: 42 additions & 0 deletions docs/libraries/sse/reference/index.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
title: Overview
sidebar_position: 0
toc_min_heading_level: 2
toc_max_heading_level: 3
---

import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';

# Overview

TypeSpec library providing server sent events bindings

## Install

<Tabs>
<TabItem value="spec" label="In a spec" default>

```bash
npm install @typespec/sse
```

</TabItem>
<TabItem value="library" label="In a library" default>

```bash
npm install --save-peer @typespec/sse
```

</TabItem>
</Tabs>

## TypeSpec.SSE

### Decorators

- [`@terminalEvent`](./decorators.md#@TypeSpec.SSE.terminalEvent)

### Models

- [`SSEStream`](./data-types.md#TypeSpec.SSE.SSEStream)
31 changes: 31 additions & 0 deletions docs/libraries/stream/reference/data-types.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
title: "Data types"
toc_min_heading_level: 2
toc_max_heading_level: 3
---

# Data types

## TypeSpec.Streams

### `Stream` {#TypeSpec.Streams.Stream}

Defines a model that represents a stream protocol type whose data is described
by `Type`.

This can be useful when the underlying data type is not relevant, or to serve as
a base type for custom streams.

```typespec
model TypeSpec.Streams.Stream<Type>
```

#### Template Parameters

| Name | Description |
| ---- | ------------------------------ |
| Type | The type of the stream's data. |

#### Properties

None
Loading
Loading