Skip to content

Commit

Permalink
fix(cli): generated sse examples must have a data and event key (#5421
Browse files Browse the repository at this point in the history
)
  • Loading branch information
dsinghvi authored Dec 16, 2024
1 parent 8f7ce9f commit eff31d2
Show file tree
Hide file tree
Showing 23 changed files with 159 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,13 @@ export class ExampleEndpointFactory {
endpoint.response?.type === "streamingJson" ||
endpoint.response?.type === "streamingSse"
) {
responseExamples.push([undefined, EndpointResponseExample.withStreaming([example])]);
responseExamples.push([
undefined,
EndpointResponseExample.withStreaming({
sse: endpoint.response?.type === "streamingSse",
events: [example]
})
]);
}
}
} else {
Expand All @@ -138,7 +144,13 @@ export class ExampleEndpointFactory {
endpoint.response?.type === "streamingJson" ||
endpoint.response?.type === "streamingSse"
) {
responseExamples.push([exampleId, EndpointResponseExample.withStreaming([example])]);
responseExamples.push([
undefined,
EndpointResponseExample.withStreaming({
sse: endpoint.response?.type === "streamingSse",
events: [example]
})
]);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2110,13 +2110,16 @@ service:
"stream": [
{
"data": {
"code": "code",
"event": "event",
"message": "message",
"data": {
"code": "code",
"event": "event",
"message": "message",
},
"event": "error",
"id": 1,
"retry": 1,
},
"event": "error",
"id": 1,
"retry": 1,
"event": "",
},
],
},
Expand Down Expand Up @@ -2797,13 +2800,15 @@ service:
id: 6aac6f73-bd89-4a76-ab32-5a6c422e8b0b
response:
stream:
- event: error
- event: ''
data:
code: code
event: event
message: message
id: 1
retry: 1
event: error
data:
code: code
event: event
message: message
id: 1
retry: 1
audiences:
- public
source:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@
},
"response": {
"stream": [
"string",
{
"data": "string",
"event": "",
},
],
},
},
Expand Down Expand Up @@ -120,7 +123,8 @@
prompt: prompt
response:
stream:
- string
- event: ''
data: string
source:
openapi: ../openapi.yml
",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@
"type": "object"
},
"response": {
"value": [
"sse": false,
"events": [
{
"properties": {
"id": {
Expand Down Expand Up @@ -378,7 +379,8 @@
"type": "object"
},
"response": {
"value": [
"sse": false,
"events": [
{
"properties": {
"id": {
Expand Down Expand Up @@ -734,7 +736,8 @@
"type": "object"
},
"response": {
"value": [
"sse": false,
"events": [
{
"properties": {
"id": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3333,7 +3333,8 @@
"queryParameters": [],
"headers": [],
"response": {
"value": [
"sse": true,
"events": [
{
"value": {
"value": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1309,7 +1309,8 @@
"type": "object"
},
"response": {
"value": [
"sse": false,
"events": [
{
"value": {
"value": {
Expand Down Expand Up @@ -2007,7 +2008,8 @@
"type": "object"
},
"response": {
"value": [
"sse": false,
"events": [
{
"properties": {
"delta": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,8 @@
"type": "object"
},
"response": {
"value": [
"sse": false,
"events": [
{
"properties": {
"id": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@
"type": "object"
},
"response": {
"value": [
"sse": false,
"events": [
{
"properties": {
"id": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@
"type": "object"
},
"response": {
"value": [
"sse": false,
"events": [
{
"properties": {
"id": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@
"type": "object"
},
"response": {
"value": [
"sse": false,
"events": [
{
"properties": {
"text": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@
"type": "object"
},
"response": {
"value": [
"sse": true,
"events": [
{
"value": {
"value": "string",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@
"type": "object"
},
"response": {
"value": [
"sse": false,
"events": [
{
"properties": {
"chunk": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2002,13 +2002,16 @@ service:
"stream": [
{
"data": {
"code": "code",
"event": "event",
"message": "message",
"data": {
"code": "code",
"event": "event",
"message": "message",
},
"event": "error",
"id": 1,
"retry": 1,
},
"event": "error",
"id": 1,
"retry": 1,
"event": "",
},
],
},
Expand Down Expand Up @@ -2689,13 +2692,15 @@ service:
id: 6aac6f73-bd89-4a76-ab32-5a6c422e8b0b
response:
stream:
- event: error
- event: ''
data:
code: code
event: event
message: message
id: 1
retry: 1
event: error
data:
code: code
event: event
message: message
id: 1
retry: 1
audiences:
- public
source:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@
},
"response": {
"stream": [
"string",
{
"data": "string",
"event": "",
},
],
},
},
Expand Down Expand Up @@ -120,7 +123,8 @@
prompt: prompt
response:
stream:
- string
- event: ''
data: string
source:
openapi: ../openapi.yml
",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,17 @@ export function convertEndpointResponseExample(
endpointResponseExample: EndpointResponseExample
): RawSchemas.ExampleStreamResponseSchema | RawSchemas.ExampleBodyResponseSchema {
switch (endpointResponseExample.type) {
case "withStreaming":
return {
stream: convertArrayExample(endpointResponseExample.value)
};
case "withStreaming": {
if (endpointResponseExample.sse) {
return {
stream: convertSseExample(endpointResponseExample.events)
};
} else {
return {
stream: convertArrayExample(endpointResponseExample.events)
};
}
}
case "withoutStreaming":
return {
body: convertFullExample(endpointResponseExample.value)
Expand Down Expand Up @@ -97,6 +104,12 @@ function convertObject(object: Record<PropertyKey, FullExample>): RawSchemas.Exa
);
}

function convertSseExample(fullExamples: FullExample[]): RawSchemas.ExampleSseEventSchema[] {
return fullExamples.map((fullExample) => {
return { event: "", data: convertFullExample(fullExample) };
});
}

function convertArrayExample(fullExamples: FullExample[]): RawSchemas.ExampleTypeReferenceSchema[] {
return fullExamples.map((fullExample) => {
return convertFullExample(fullExample);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,13 @@ types:
EndpointResponseExample:
docs: An example that has either a list of streaming examples or a single non-streaming example.
union:
withStreaming: list<example.FullExample>
withStreaming: StreamingResponseExample
withoutStreaming: example.FullExample

StreamingResponseExample:
properties:
sse: boolean
events: list<example.FullExample>

PathParameterExample:
properties:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ export type EndpointResponseExample =
| FernOpenapiIr.EndpointResponseExample.WithoutStreaming;

export declare namespace EndpointResponseExample {
interface WithStreaming extends _Utils {
interface WithStreaming extends FernOpenapiIr.StreamingResponseExample, _Utils {
type: "withStreaming";
value: FernOpenapiIr.FullExample[];
}

interface WithoutStreaming extends _Utils {
Expand All @@ -27,16 +26,18 @@ export declare namespace EndpointResponseExample {
}

interface _Visitor<_Result> {
withStreaming: (value: FernOpenapiIr.FullExample[]) => _Result;
withStreaming: (value: FernOpenapiIr.StreamingResponseExample) => _Result;
withoutStreaming: (value: FernOpenapiIr.FullExample) => _Result;
_other: (value: { type: string }) => _Result;
}
}

export const EndpointResponseExample = {
withStreaming: (value: FernOpenapiIr.FullExample[]): FernOpenapiIr.EndpointResponseExample.WithStreaming => {
withStreaming: (
value: FernOpenapiIr.StreamingResponseExample
): FernOpenapiIr.EndpointResponseExample.WithStreaming => {
return {
value: value,
...value,
type: "withStreaming",
_visit: function <_Result>(
this: FernOpenapiIr.EndpointResponseExample.WithStreaming,
Expand Down Expand Up @@ -66,7 +67,7 @@ export const EndpointResponseExample = {
): _Result => {
switch (value.type) {
case "withStreaming":
return visitor.withStreaming(value.value);
return visitor.withStreaming(value);
case "withoutStreaming":
return visitor.withoutStreaming(value.value);
default:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* This file was auto-generated by Fern from our API Definition.
*/

import * as FernOpenapiIr from "../../../index";

export interface StreamingResponseExample {
sse: boolean;
events: FernOpenapiIr.FullExample[];
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export * from "./FernExample";
export * from "./EndpointExample";
export * from "./FullEndpointExample";
export * from "./EndpointResponseExample";
export * from "./StreamingResponseExample";
export * from "./PathParameterExample";
export * from "./QueryParameterExample";
export * from "./HeaderExample";
Expand Down
Loading

0 comments on commit eff31d2

Please sign in to comment.