Server sent event response viewer (#126)

This commit is contained in:
Gregory Schier
2024-10-11 06:52:32 -07:00
committed by GitHub
parent f974a66086
commit d754e7233d
24 changed files with 513 additions and 104 deletions

View File

@@ -35,3 +35,15 @@ function detectFromContent(
return fallback;
}
export function isJSON(content: string | null | undefined): boolean {
if (typeof content !== 'string') return false;
try {
JSON.parse(content)
return true;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
} catch (err) {
return false;
}
}

View File

@@ -1,26 +1,34 @@
import { readFile } from '@tauri-apps/plugin-fs';
import type { HttpResponse } from '@yaakapp-internal/models';
import type { ServerSentEvent } from '@yaakapp-internal/sse';
import { getCharsetFromContentType } from './model_util';
import { invokeCmd } from './tauri';
export async function getResponseBodyText(response: HttpResponse): Promise<string | null> {
if (response.bodyPath) {
const bytes = await readFile(response.bodyPath);
const charset = getCharsetFromContentType(response.headers);
if (!response.bodyPath) return null;
try {
return new TextDecoder(charset ?? 'utf-8', { fatal: true }).decode(bytes);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
} catch (err) {
// Failed to decode as text, so return null
return null;
}
const bytes = await readFile(response.bodyPath);
const charset = getCharsetFromContentType(response.headers);
try {
return new TextDecoder(charset ?? 'utf-8', { fatal: true }).decode(bytes);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
} catch (err) {
// Failed to decode as text, so return null
return null;
}
return null;
}
export async function getResponseBodyBlob(response: HttpResponse): Promise<Uint8Array | null> {
if (response.bodyPath) {
return readFile(response.bodyPath);
}
return null;
if (!response.bodyPath) return null;
return readFile(response.bodyPath);
}
export async function getResponseBodyEventSource(
response: HttpResponse,
): Promise<ServerSentEvent[]> {
if (!response.bodyPath) return [];
return invokeCmd<ServerSentEvent[]>('cmd_get_sse_events', {
filePath: response.bodyPath,
});
}

View File

@@ -32,6 +32,7 @@ type TauriCmd =
| 'cmd_get_folder'
| 'cmd_get_grpc_request'
| 'cmd_get_http_request'
| 'cmd_get_sse_events'
| 'cmd_get_key_value'
| 'cmd_get_settings'
| 'cmd_get_workspace'