or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-type-is

Infer the content-type of a request.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/type-is@2.0.x

To install, run

npx @tessl/cli install tessl/npm-type-is@2.0.0

index.mddocs/

Type-Is

Type-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.

Package Information

  • Package Name: type-is
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install type-is

Core Imports

const 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.match

Basic Usage

const 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;
}

Capabilities

Request Content-Type Checking

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"]); // => null

Media Type String Checking

Check 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']

Request Body Detection

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
  });
}

Type Normalization

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"); // => false

MIME Type Matching

Match 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"); // => false

Type Formats

Type-Is supports various type format patterns:

File Extensions

typeis(req, ["json", "html", "png"]); // Mapped to MIME types
typeis(req, [".json", ".html"]); // Extensions with dots

Full MIME Types

typeis(req, ["application/json", "text/html", "image/png"]);

Wildcard Patterns

typeis(req, ["text/*"]); // Any text type
typeis(req, ["*/json"]); // Any type with json subtype
typeis(req, ["*/*"]); // Any type

Suffix Patterns

typeis(req, ["+json"]); // Any type ending with +json
typeis(req, ["application/*+xml"]); // Application types ending with +xml

Special Shortcuts

typeis(req, ["urlencoded"]); // => 'application/x-www-form-urlencoded'
typeis(req, ["multipart"]); // => 'multipart/*'

Error Handling

  • Invalid MIME types: Return false
  • Non-string inputs to normalize: Return false
  • Malformed type patterns: Return false
  • Missing Content-Type header: Return false (or null for requests without body)
  • Request without body: Return null for main typeis() function

Types

// 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[];