Files
yaak/src-web/lib/responseBody.ts
2024-09-22 21:27:10 -07:00

27 lines
869 B
TypeScript

import { readFile } from '@tauri-apps/plugin-fs';
import type { HttpResponse } from '@yaakapp-internal/models';
import { getCharsetFromContentType } from './model_util';
export async function getResponseBodyText(response: HttpResponse): Promise<string | null> {
if (response.bodyPath) {
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;
}