-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into alberto/ts-reference-imports-ast
- Loading branch information
Showing
6 changed files
with
114 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
fern/pages/api-definition/fern-definition/endpoints/sse.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
--- | ||
title: Server Sent Events and Streaming APIs | ||
subtitle: Use the `response-stream` key to model streaming endpoints | ||
--- | ||
|
||
<Note> | ||
Specifying `response-stream` on an endpoints allows you to represent endpoint responses that are streaming. | ||
</Note> | ||
|
||
|
||
## JSON streaming | ||
|
||
If your API returns a series of `JSON` chunks as seen below | ||
|
||
```json | ||
{ "text": "Hi, I am a" } | ||
{ "text": "chatbot. Do you have any"} | ||
{ "text": "questions for me"} | ||
``` | ||
|
||
then simply specify the response under `response-stream` for your endpoint. | ||
|
||
```yaml title="chat.yml" {4} | ||
service: | ||
base-path: /chat | ||
endpoints: | ||
stream: | ||
method: POST | ||
path: "" | ||
response-stream: Chat | ||
|
||
types: | ||
Chat: | ||
properties: | ||
text: string | ||
``` | ||
## Server sent events | ||
If your API returns server-sent-events, with the `data` and `event` keys as seen below | ||
|
||
```json | ||
data: { "text": "Hi, I am a" } | ||
data: { "text": "chatbot. Do you have any"} | ||
data: { "text": "questions for me"} | ||
``` | ||
|
||
then make sure to include `format: sse`. | ||
|
||
```yaml title="chat.yml" {9} | ||
service: | ||
base-path: /chat | ||
endpoints: | ||
stream: | ||
method: POST | ||
path: "" | ||
response-stream: | ||
type: Chat | ||
format: sse | ||
types: | ||
Chat: | ||
properties: | ||
text: string | ||
``` | ||
|
||
## `Stream` parameter | ||
|
||
It has become common practice for endpoints to have a `stream` parameter that | ||
controls whether the response is streamed or not. Fern supports this pattern in a first | ||
class way. | ||
|
||
Simply specify the `stream-condition` as well as the ordinary response and the streaming response: | ||
|
||
```yaml title="chat.yml" {7} | ||
service: | ||
base-path: /chat | ||
endpoints: | ||
stream: | ||
method: POST | ||
path: "" | ||
stream-condition: $request.stream | ||
request: | ||
name: StreamChatRequest | ||
body: | ||
properties: | ||
stream: boolean | ||
response: Chat | ||
response-stream: | ||
type: ChatChunk | ||
format: sse | ||
types: | ||
Chat: | ||
properties: | ||
text: string | ||
tokens: integer | ||
ChatChunk: | ||
properties: | ||
text: string | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
53.16.0 | ||
53.17.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters