mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-01-18 06:57:11 +01:00
27 lines
869 B
TypeScript
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;
|
|
}
|