Skip to content

Commit ed1c3d2

Browse files
committed
docs: add SSE example for path params, query params, and error responses
Cover missing SSE scenarios: path parameter (/stock/{symbol}/watch), query parameter (interval), and non-200 error response (404). https://claude.ai/code/session_015wYib3JNLu2XuN6LrJtFED
1 parent 461b1e8 commit ed1c3d2

File tree

5 files changed

+146
-2
lines changed

5 files changed

+146
-2
lines changed

examples/openapi-ts-sse/src/App.tsx

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { useRef, useState } from 'react';
22

3-
import { getStockHistory, watchSelectedStocks, watchStockPrices } from './client/sdk.gen';
3+
import {
4+
getStockHistory,
5+
watchSelectedStocks,
6+
watchSingleStock,
7+
watchStockPrices,
8+
} from './client/sdk.gen';
49
import type { StockUpdate } from './client/types.gen';
510

611
function App() {
@@ -72,6 +77,32 @@ function App() {
7277
setStatus('disconnected');
7378
};
7479

80+
// SSE with path parameter, query parameter, and error response
81+
const onWatchSingle = async () => {
82+
const controller = new AbortController();
83+
controllerRef.current = controller;
84+
setStatus('connected');
85+
setUpdates([]);
86+
87+
try {
88+
const { stream } = await watchSingleStock({
89+
path: { symbol: 'AAPL' },
90+
query: { interval: 2 },
91+
signal: controller.signal,
92+
});
93+
94+
for await (const update of stream) {
95+
setUpdates((prev) => [...prev, update]);
96+
}
97+
} catch {
98+
if (!controller.signal.aborted) {
99+
setStatus('error');
100+
return;
101+
}
102+
}
103+
setStatus('disconnected');
104+
};
105+
75106
// Cancel the stream using AbortController
76107
const onDisconnect = () => {
77108
controllerRef.current?.abort();
@@ -112,6 +143,9 @@ function App() {
112143
<button disabled={status === 'connected'} onClick={onWatchSelected}>
113144
Watch Selected (POST)
114145
</button>
146+
<button disabled={status === 'connected'} onClick={onWatchSingle}>
147+
Watch AAPL (Path Param)
148+
</button>
115149
<button disabled={status !== 'connected'} onClick={onDisconnect}>
116150
Disconnect
117151
</button>

examples/openapi-ts-sse/src/client/index.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
// This file is auto-generated by @hey-api/openapi-ts
22

3-
export { getStockHistory, type Options, watchSelectedStocks, watchStockPrices } from './sdk.gen';
3+
export {
4+
getStockHistory,
5+
type Options,
6+
watchSelectedStocks,
7+
watchSingleStock,
8+
watchStockPrices,
9+
} from './sdk.gen';
410
export type {
511
ClientOptions,
12+
ErrorResponse,
613
GetStockHistoryData,
714
GetStockHistoryResponse,
815
GetStockHistoryResponses,
@@ -14,6 +21,11 @@ export type {
1421
WatchSelectedStocksData,
1522
WatchSelectedStocksResponse,
1623
WatchSelectedStocksResponses,
24+
WatchSingleStockData,
25+
WatchSingleStockError,
26+
WatchSingleStockErrors,
27+
WatchSingleStockResponse,
28+
WatchSingleStockResponses,
1729
WatchStockPricesData,
1830
WatchStockPricesResponse,
1931
WatchStockPricesResponses,

examples/openapi-ts-sse/src/client/sdk.gen.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import type {
77
GetStockHistoryResponses,
88
WatchSelectedStocksData,
99
WatchSelectedStocksResponses,
10+
WatchSingleStockData,
11+
WatchSingleStockErrors,
12+
WatchSingleStockResponses,
1013
WatchStockPricesData,
1114
WatchStockPricesResponses,
1215
} from './types.gen';
@@ -58,6 +61,20 @@ export const watchSelectedStocks = <ThrowOnError extends boolean = false>(
5861
},
5962
});
6063

64+
/**
65+
* Watch a single stock
66+
*
67+
* Stream real-time price updates for a specific stock symbol.
68+
*/
69+
export const watchSingleStock = <ThrowOnError extends boolean = false>(
70+
options: Options<WatchSingleStockData, ThrowOnError>,
71+
) =>
72+
(options.client ?? client).sse.get<
73+
WatchSingleStockResponses,
74+
WatchSingleStockErrors,
75+
ThrowOnError
76+
>({ url: '/stock/{symbol}/watch', ...options });
77+
6178
/**
6279
* Get stock history
6380
*

examples/openapi-ts-sse/src/client/types.gen.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ export type MarketAlert = {
4444
type: 'market_alert';
4545
};
4646

47+
export type ErrorResponse = {
48+
code: string;
49+
message: string;
50+
};
51+
4752
export type WatchStockPricesData = {
4853
body?: never;
4954
path?: never;
@@ -77,6 +82,41 @@ export type WatchSelectedStocksResponses = {
7782
export type WatchSelectedStocksResponse =
7883
WatchSelectedStocksResponses[keyof WatchSelectedStocksResponses];
7984

85+
export type WatchSingleStockData = {
86+
body?: never;
87+
path: {
88+
/**
89+
* Stock ticker symbol (e.g. AAPL)
90+
*/
91+
symbol: string;
92+
};
93+
query?: {
94+
/**
95+
* Update interval in seconds
96+
*/
97+
interval?: number;
98+
};
99+
url: '/stock/{symbol}/watch';
100+
};
101+
102+
export type WatchSingleStockErrors = {
103+
/**
104+
* Stock symbol not found
105+
*/
106+
404: ErrorResponse;
107+
};
108+
109+
export type WatchSingleStockError = WatchSingleStockErrors[keyof WatchSingleStockErrors];
110+
111+
export type WatchSingleStockResponses = {
112+
/**
113+
* Stock price stream for the requested symbol
114+
*/
115+
200: StockUpdate;
116+
};
117+
118+
export type WatchSingleStockResponse = WatchSingleStockResponses[keyof WatchSingleStockResponses];
119+
80120
export type GetStockHistoryData = {
81121
body?: never;
82122
path?: never;

specs/3.1.x/sse-example.yaml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,37 @@ paths:
3434
text/event-stream:
3535
schema:
3636
$ref: '#/components/schemas/StockUpdate'
37+
/stock/{symbol}/watch:
38+
get:
39+
operationId: watchSingleStock
40+
summary: Watch a single stock
41+
description: Stream real-time price updates for a specific stock symbol.
42+
parameters:
43+
- name: symbol
44+
in: path
45+
required: true
46+
description: Stock ticker symbol (e.g. AAPL)
47+
schema:
48+
type: string
49+
- name: interval
50+
in: query
51+
description: Update interval in seconds
52+
schema:
53+
type: integer
54+
default: 5
55+
responses:
56+
'200':
57+
description: Stock price stream for the requested symbol
58+
content:
59+
text/event-stream:
60+
schema:
61+
$ref: '#/components/schemas/StockUpdate'
62+
'404':
63+
description: Stock symbol not found
64+
content:
65+
application/json:
66+
schema:
67+
$ref: '#/components/schemas/ErrorResponse'
3768
/stock/history:
3869
get:
3970
operationId: getStockHistory
@@ -137,3 +168,13 @@ components:
137168
- critical
138169
message:
139170
type: string
171+
ErrorResponse:
172+
type: object
173+
required:
174+
- code
175+
- message
176+
properties:
177+
code:
178+
type: string
179+
message:
180+
type: string

0 commit comments

Comments
 (0)