A comprehensive library for mime-type mapping
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Core functionality for resolving MIME types from file paths and extensions, with robust path handling and comprehensive type database coverage.
Determines the MIME type for a given file path or extension with intelligent path parsing.
/**
* Get mime type associated with an extension or file path
* @param path - File path or extension to look up
* @returns MIME type string or null if not found
*/
function getType(path: string): string | null;Usage Examples:
import mime from "mime";
// File extensions
mime.getType("js"); // "text/javascript"
mime.getType("json"); // "application/json"
mime.getType("png"); // "image/png"
// File paths (various formats)
mime.getType("script.js"); // "text/javascript"
mime.getType("dir/data.json"); // "application/json"
mime.getType("dir\\image.png"); // "image/png" (Windows paths)
mime.getType(".hidden.txt"); // "text/plain"
mime.getType("file.tar.gz"); // "application/gzip"
// Returns null for unrecognized or invalid inputs
mime.getType("unknown_extension"); // null
mime.getType("file_without_extension"); // null
mime.getType("path/file"); // null (no extension)Path Processing Rules:
Retrieves the default (primary) file extension for a given MIME type.
/**
* Get default file extension associated with a mime type
* @param type - MIME type to look up
* @returns Primary extension string or null if not found
*/
function getExtension(type: string): string | null;Usage Examples:
import mime from "mime";
// Standard MIME types
mime.getExtension("text/plain"); // "txt"
mime.getExtension("application/json"); // "json"
mime.getExtension("image/jpeg"); // "jpeg"
mime.getExtension("application/javascript"); // "js"
// MIME types with parameters (automatically stripped)
mime.getExtension("text/html; charset=utf-8"); // "html"
mime.getExtension("application/json; charset=utf-8"); // "json"
// Returns null for unrecognized types
mime.getExtension("unknown/type"); // null
mime.getExtension("invalid-mime-type"); // nullParameter Handling:
Retrieves all file extensions associated with a given MIME type (new in v4.0).
/**
* Get all file extensions associated with a mime type
* @param type - MIME type to look up
* @returns Set of all extensions or null if not found
*/
function getAllExtensions(type: string): Set<string> | null;Usage Examples:
import mime from "mime";
// Multiple extensions for common types
mime.getAllExtensions("image/jpeg"); // Set(3) { "jpeg", "jpg", "jpe" }
mime.getAllExtensions("text/javascript"); // Set(2) { "js", "mjs" }
mime.getAllExtensions("application/zip"); // Set(1) { "zip" }
// Single extension types
mime.getAllExtensions("text/plain"); // Set(1) { "txt" }
// Returns null for unrecognized types
mime.getAllExtensions("unknown/type"); // nullSet Iteration:
const extensions = mime.getAllExtensions("image/jpeg");
if (extensions) {
for (const ext of extensions) {
console.log(ext); // "jpeg", "jpg", "jpe"
}
// Convert to array if needed
const extArray = Array.from(extensions); // ["jpeg", "jpg", "jpe"]
}// Full version - includes all MIME types (800+ types, 1000+ extensions)
import mime from "mime";
// Lite version - excludes unofficial types (prs.*, x-*, vnd.*)
import mime from "mime/lite";Type Coverage:
All methods handle invalid input gracefully:
// Invalid inputs return null, never throw
mime.getType(null); // null
mime.getType(undefined); // null
mime.getType(123); // null
mime.getExtension(null); // null
mime.getAllExtensions(""); // null// Return types are explicitly nullable
const type: string | null = mime.getType("file.js");
const ext: string | null = mime.getExtension("text/plain");
const exts: Set<string> | null = mime.getAllExtensions("image/png");
// Always check for null before use
if (type !== null) {
console.log(`MIME type: ${type}`);
}