/** * Content-Type Detection Utilities * * Helper functions for adapter developers to detect and classify content types. * These utilities help determine how to parse and handle HTTP response bodies. * * @module utils/content-type */ /** * JSON content types including standard and specialized formats */ export type JsonContentType = | "application/json" | "application/ld+json" | "application/x-ndjson" | "application/jsonlines" | `application/${string}+json`; /** * Text-based content types */ export type TextContentType = | `text/${string}` | "application/xml" | "application/xhtml+xml"; /** * Image content types */ export type ImageContentType = `image/${string}`; /** * Binary/application content types */ export type BinaryContentType = | "application/octet-stream" | "application/pdf" | "application/zip" | `application/${string}`; /** * Union of all recognized content types */ export type ContentType = | JsonContentType | TextContentType | ImageContentType | BinaryContentType | string; /** * Check if Content-Type indicates JSON format * * Recognizes: * - application/json * - application/ld+json (JSON-LD) * - application/x-ndjson (NDJSON) * - application/jsonlines (JSON Lines) * - Any type with +json suffix (e.g., application/vnd.api+json) * * @param contentType - Content-Type header value (may include parameters like charset) * @returns True if content type indicates JSON format * * @example * ```typescript * isJsonContentType('application/json') // true * isJsonContentType('application/json; charset=utf-8') // true * isJsonContentType('application/vnd.api+json') // true * isJsonContentType('text/html') // false * ``` */ export function isJsonContentType(contentType: string): boolean { // Extract MIME type without parameters (e.g., "charset=utf-8") const parts = contentType.toLowerCase().split(";"); const mimeType = (parts[0] ?? "").trim(); return ( mimeType === "application/json" || mimeType === "application/ld+json" || mimeType === "application/x-ndjson" || mimeType === "application/jsonlines" || mimeType.endsWith("+json") ); } /** * Check if Content-Type indicates NDJSON (Newline-Delimited JSON) format * * Recognizes: * - application/x-ndjson * - application/jsonlines * * Note: Some APIs may use application/json for NDJSON responses. * Use this function in combination with response inspection if needed. * * @param contentType - Content-Type header value * @returns True if content type indicates NDJSON format * * @example * ```typescript * isNDJSONContentType('application/x-ndjson') // true * isNDJSONContentType('application/jsonlines') // true * isNDJSONContentType('application/json') // false * ``` */ export function isNDJSONContentType(contentType: string): boolean { // Extract MIME type without parameters (e.g., "charset=utf-8") const parts = contentType.toLowerCase().split(";"); const mimeType = (parts[0] ?? "").trim(); return mimeType === "application/x-ndjson" || mimeType === "application/jsonlines"; } /** * Check if Content-Type indicates text format * * Recognizes: * - Any text/* type (text/plain, text/html, text/csv, etc.) * - application/xml * - application/xhtml+xml * * @param contentType - Content-Type header value * @returns True if content type indicates text format * * @example * ```typescript * isTextContentType('text/plain') // true * isTextContentType('text/html; charset=utf-8') // true * isTextContentType('application/xml') // true * isTextContentType('application/json') // false * isTextContentType('image/png') // false * ``` */ export function isTextContentType(contentType: string): boolean { // Extract MIME type without parameters (e.g., "charset=utf-8") const parts = contentType.toLowerCase().split(";"); const mimeType = (parts[0] ?? "").trim(); return ( mimeType.startsWith("text/") || mimeType === "application/xml" || mimeType === "application/xhtml+xml" ); }