Skip to content

Commit 784ce87

Browse files
authored
Add support for Server-Sent Events (SSE) in API Explorer and configuration (#1041)
* Add support for Server-Sent Events (SSE) in API Explorer and configuration * remove local test files
1 parent e4d3ec4 commit 784ce87

File tree

1 file changed

+26
-3
lines changed
  • packages/docusaurus-theme-openapi-docs/src/theme/ApiExplorer/Request

1 file changed

+26
-3
lines changed

packages/docusaurus-theme-openapi-docs/src/theme/ApiExplorer/Request/index.tsx

+26-3
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,37 @@ function Request({ item }: { item: ApiItem }) {
9595

9696
const methods = useForm({ shouldFocusError: false });
9797

98+
const handleEventStream = async (res) => {
99+
res.headers && dispatch(setHeaders(Object.fromEntries(res.headers)));
100+
dispatch(setCode(res.status));
101+
102+
const reader = res.body.getReader();
103+
const decoder = new TextDecoder();
104+
let result = "";
105+
while (true) {
106+
const { done, value } = await reader.read();
107+
if (done) break;
108+
result += decoder.decode(value, { stream: true });
109+
dispatch(setResponse(result));
110+
}
111+
};
112+
113+
const handleResponse = async (res) => {
114+
dispatch(setResponse(await res.text()));
115+
dispatch(setCode(res.status));
116+
res.headers && dispatch(setHeaders(Object.fromEntries(res.headers)));
117+
};
118+
98119
const onSubmit = async (data) => {
99120
dispatch(setResponse("Fetching..."));
100121
try {
101122
await delay(1200);
102123
const res = await makeRequest(postmanRequest, proxy, body);
103-
dispatch(setResponse(await res.text()));
104-
dispatch(setCode(res.status));
105-
res.headers && dispatch(setHeaders(Object.fromEntries(res.headers)));
124+
if (res.headers.get("content-type")?.includes("text/event-stream")) {
125+
await handleEventStream(res);
126+
} else {
127+
await handleResponse(res);
128+
}
106129
} catch (e: any) {
107130
console.log(e);
108131
dispatch(setResponse("Connection failed"));

0 commit comments

Comments
 (0)