type-is

中文文档
3.0.0发布于 2026/09/22MITTypeScriptES Module构建来源已验证近 30 天下载 6.4亿

Infer the content-type of a request.

原始使用文档
文档目录11 节
  1. Install
  2. API
  3. new TypeIs(types, options?)
  4. typeIs.is(value)
  5. typeIs.request(request)
  6. hasBody(request)
  7. match(expected)
  8. normalize(type, options?)
  9. Examples
  10. Example body parser
  11. License

type-is

NPM Version NPM Downloads Node.js Version Build Status Test Coverage

Infer the content-type of a request.

Install

$ npm install type-is

API

import { createServer } from "http";
import { TypeIs } from "type-is";

const isText = new TypeIs(["text/*"]);

createServer(function (req, res) {
  res.end(
    "you " + (isText.request(req) ? "sent" : "did not send") + " me text",
  );
});

new TypeIs(types, options?)

Creates a reusable content type matcher. The optional options object accepts:

  • lookup: A function for resolving shorthand types (see normalize).
  • parameterValue: A (key: string, value: string) => string function that normalizes both configured and incoming parameter values before comparison. By default, charset values are lowercased and other values are unchanged.

Each type in the types array can be one of the following:

  • A mime type such as application/json.
  • A mime type with a wildcard such as */* or */json or application/*.
  • A suffix such as +json. This can be combined with a wildcard such as */vnd+json or application/*+json.
  • A configured shorthand such as multipart or urlencoded.
  • Any of the above with parameters that must also match, such as text/html; charset=utf-8.

typeIs.is(value)

Checks a content-type value and returns the first configured type that matches, or undefined when none match.

const isJson = new TypeIs(["application/json", "application/*+json"]);

isJson.is("application/json"); // => 'application/json'
isJson.is("application/vnd.api+json"); // => 'application/*+json'
isJson.is("text/html"); // => undefined

typeIs.request(request)

Checks a request against the configured types. If the request has no body, even if there is a Content-Type header, then undefined is returned. Otherwise it uses is to check the content-type header.

// req.headers.content-type = 'application/json'

new TypeIs(["json"]).request(req); // => 'application/json'
new TypeIs(["application/*"]).request(req); // => 'application/*'
new TypeIs(["application/json"]).request(req); // => 'application/json'

new TypeIs(["text/html"]).request(req); // => undefined

hasBody(request)

Returns true if the given request has a body based on the HTTP headers provided.

Having a body has no relation to how large the body is (it may be 0 bytes). This is similar to how file existence works. If a body does exist, then this indicates that there is data to read from the Node.js request stream.

if (typeis.hasBody(req)) {
  // read the body, since there is one

  req.on("data", function (chunk) {
    // ...
  });
}

match(expected)

Compile the type string expected into a function that matches a MIME type.

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

normalize(type, options?)

Normalize a type string. This works by performing the following:

  • If the string contains a /, then it is returned as the type.
  • If the string starts with + (so it is a +suffix shorthand like +json), then it is expanded to contain the complete wildcard notation of */*+suffix.
  • Else the string is assumed to be a file extension and the mapped media type is returned, or the original input if there is no mapping.

The default extensions is kept minimal:

  • 'json' -> 'application/json'
  • 'multipart' -> 'multipart/*'
  • 'urlencoded' -> 'application/x-www-form-urlencoded'

You can pass an options object of { lookup: (value: string) => string | string[] | undefined } to provide additional mappings, e.g. mime.lookup.

Examples

Example body parser

const express = require("express");
const { TypeIs, hasBody } = require("type-is");

const app = express();
const typeIs = new TypeIs(["urlencoded", "json", "multipart"]);

app.use(function bodyParser(req, res, next) {
  if (!hasBody(req)) {
    return next();
  }

  switch (typeIs.request(req)) {
    case "application/x-www-form-urlencoded":
      // parse urlencoded body
      throw new Error("implement urlencoded body parsing");
    case "application/json":
      // parse json body
      throw new Error("implement json body parsing");
    case "multipart/*":
      // parse multipart body
      throw new Error("implement multipart body parsing");
    default:
      // 415 error code
      res.statusCode = 415;
      res.end();
      break;
  }
});

License

MIT

版本与发布记录

type-is 共发布 41 个版本,首次发布于 2013/12/28;过去 12 个月发布了 2 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
3.0.0latest2026/09/221 个月前
2.1.02026/05/135 个月前
2.0.12025/03/271.6 年前
2.0.0next2024/09/012 年前
1.6.182019/04/267 年前
1.6.172019/04/267 年前

关于 type-is 的常见问题

type-is 是什么?

Infer the content-type of a request.

如何安装 type-is?

在项目目录运行 npm install type-is,或使用 pnpm add / yarn add / bun add。

type-is 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

type-is 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

type-is 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >= 22。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

type-is 可以商用吗?

type-is 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

type-is 的下载量有多少?

根据 npm 官方下载统计,type-is 在 2026/10/05 – 2026/11/03 这 30 天内被下载 643,246,843 次,日均约 21,441,561 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

type-is 还在维护吗?

最新版本 3.0.0 发布于 2026/09/22(1 个月前);至今共发布 41 个版本,其中过去 12 个月发布了 2 个;首次发布于 2013/12/28。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。