Properly handle charset in content-type

This commit is contained in:
Gregory Schier
2024-10-23 05:49:14 -07:00
parent ff52ad5345
commit ec850f2cf0
3 changed files with 23 additions and 1 deletions

View File

@@ -5,6 +5,7 @@ import type {
HttpResponse,
HttpResponseHeader,
} from '@yaakapp-internal/models';
import MimeType from 'whatwg-mimetype';
export const BODY_TYPE_NONE = null;
export const BODY_TYPE_GRAPHQL = 'graphql';
@@ -60,5 +61,6 @@ export function getCharsetFromContentType(headers: HttpResponseHeader[]): string
const contentType = getContentTypeHeader(headers);
if (contentType == null) return null;
return contentType.toLowerCase().match(/charset="?([^ ;"]+)"?/)?.[1] || null;
const mimeType = new MimeType(contentType);
return mimeType.parameters.get('charset') ?? null;
}