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 Server-Sent Events (SSE) in API Explorer and configuration #1041

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,37 @@ function Request({ item }: { item: ApiItem }) {

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

const handleEventStream = async (res) => {
res.headers && dispatch(setHeaders(Object.fromEntries(res.headers)));
dispatch(setCode(res.status));

const reader = res.body.getReader();
const decoder = new TextDecoder();
let result = "";
while (true) {
const { done, value } = await reader.read();
if (done) break;
result += decoder.decode(value, { stream: true });
dispatch(setResponse(result));
}
};

const handleResponse = async (res) => {
dispatch(setResponse(await res.text()));
dispatch(setCode(res.status));
res.headers && dispatch(setHeaders(Object.fromEntries(res.headers)));
};

const onSubmit = async (data) => {
dispatch(setResponse("Fetching..."));
try {
await delay(1200);
const res = await makeRequest(postmanRequest, proxy, body);
dispatch(setResponse(await res.text()));
dispatch(setCode(res.status));
res.headers && dispatch(setHeaders(Object.fromEntries(res.headers)));
if (res.headers.get("content-type")?.includes("text/event-stream")) {
await handleEventStream(res);
} else {
await handleResponse(res);
}
} catch (e: any) {
console.log(e);
dispatch(setResponse("Connection failed"));
Expand Down
Loading