Infer the content-type of a request.
npx @tessl/cli install tessl/npm-type-is@2.0.0Type-Is is a Node.js utility library for inferring and checking the content-type of HTTP requests. It provides essential MIME type detection capabilities for web servers and Express.js applications, with support for wildcards, file extensions, and complex MIME type patterns.
npm install type-isconst typeis = require("type-is");For ES modules:
import typeis from "type-is";
// Properties are available on the default import
// typeis.is, typeis.hasBody, typeis.normalize, typeis.matchconst http = require("http");
const typeis = require("type-is");
http.createServer(function (req, res) {
const isText = typeis(req, ["text/*"]);
res.end("you " + (isText ? "sent" : "did not send") + " me text");
});
// Check if request has JSON content
if (typeis(req, ["json"])) {
// Handle JSON data
}
// Check for multiple types
const bodyType = typeis(req, ["urlencoded", "json", "multipart"]);
switch (bodyType) {
case "urlencoded":
// Handle form data
break;
case "json":
// Handle JSON data
break;
case "multipart":
// Handle multipart data
break;
}Check if an HTTP request matches one or more content types. Returns the first matching type, false if no match, or null if the request has no body.
/**
* Check if the incoming request contains the "Content-Type" header field,
* and it contains any of the given mime types.
* @param {Object} req - HTTP request object
* @param {Array|String} types - Array of type strings or individual type arguments
* @returns {String|false|null} First matching type, false if no match, null if no body
*/
function typeis(req, types);Usage Examples:
// With Content-Type: application/json
typeis(req, ["json"]); // => 'json'
typeis(req, ["html", "json"]); // => 'json'
typeis(req, ["application/*"]); // => 'application/json'
typeis(req, ["html"]); // => false
// No body (no transfer-encoding or content-length)
typeis(req, ["json"]); // => nullCheck if a media type string matches one or more types without requiring a request object.
/**
* Check if a media type matches one or more types
* @param {String|Object} mediaType - Media type string or request object
* @param {Array|String} types - Array of type strings or individual type arguments
* @returns {String|false} First matching type or false if no match
*/
typeis.is(mediaType, types);Usage Examples:
const mediaType = "application/json";
typeis.is(mediaType, ["json"]); // => 'json'
typeis.is(mediaType, ["html", "json"]); // => 'json'
typeis.is(mediaType, ["application/*"]); // => 'application/json'
typeis.is(mediaType, ["html"]); // => false
// Can also accept request objects
typeis.is(req, ["json"]); // Uses req.headers['content-type']Check if an HTTP request has a body, regardless of the Content-Type header.
/**
* Check if a request has a request body
* @param {Object} req - HTTP request object
* @returns {Boolean} true if request has body (transfer-encoding or content-length headers)
*/
typeis.hasBody(req);Usage Examples:
if (typeis.hasBody(req)) {
// Read the body, since there is one
req.on("data", function (chunk) {
// Handle data chunks
});
}Normalize a type string by expanding shortcuts and mapping file extensions to MIME types.
/**
* Normalize a type string
* @param {String} type - Type string to normalize
* @returns {String|false} Normalized MIME type or false if invalid
*/
typeis.normalize(type);Usage Examples:
typeis.normalize("json"); // => 'application/json'
typeis.normalize("urlencoded"); // => 'application/x-www-form-urlencoded'
typeis.normalize("multipart"); // => 'multipart/*'
typeis.normalize("+json"); // => '*/*+json'
typeis.normalize("text/html"); // => 'text/html' (pass-through)
typeis.normalize("unknown"); // => falseMatch an expected MIME type pattern against an actual MIME type with wildcard support.
/**
* Match expected type against actual type with wildcard support
* @param {String} expected - Expected MIME type pattern (can contain wildcards)
* @param {String} actual - Actual MIME type to match against
* @returns {Boolean} true if types match
*/
typeis.match(expected, actual);Usage Examples:
typeis.match("text/html", "text/html"); // => true
typeis.match("*/html", "text/html"); // => true
typeis.match("text/*", "text/html"); // => true
typeis.match("*/*", "text/html"); // => true
typeis.match("*/*+json", "application/x-custom+json"); // => true
typeis.match("text/html", "text/plain"); // => falseType-Is supports various type format patterns:
typeis(req, ["json", "html", "png"]); // Mapped to MIME types
typeis(req, [".json", ".html"]); // Extensions with dotstypeis(req, ["application/json", "text/html", "image/png"]);typeis(req, ["text/*"]); // Any text type
typeis(req, ["*/json"]); // Any type with json subtype
typeis(req, ["*/*"]); // Any typetypeis(req, ["+json"]); // Any type ending with +json
typeis(req, ["application/*+xml"]); // Application types ending with +xmltypeis(req, ["urlencoded"]); // => 'application/x-www-form-urlencoded'
typeis(req, ["multipart"]); // => 'multipart/*'falsefalsefalsefalse (or null for requests without body)null for main typeis() function// Request object structure (Node.js HTTP request)
interface Request {
headers: {
'content-type'?: string;
'transfer-encoding'?: string;
'content-length'?: string;
[key: string]: string | undefined;
};
}
// Type strings can be:
type TypeString =
| string // File extension: 'json', 'html'
| string // MIME type: 'application/json', 'text/html'
| string // Wildcard: 'text/*', '*/json', '*/*'
| string // Suffix: '+json', 'application/*+xml'
| string; // Special: 'urlencoded', 'multipart'
// Function argument types
type TypesArgument = TypeString | TypeString[];