# type-is 中文文档

规范地址：https://npmcn.com/package/type-is/

版本：3\.0\.0
npm 元数据更新时间：2026-09-21T23:32:08.673Z
中文译文更新时间：2026-11-04T19:57:09.755Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/type-is
原文对照：https://npmcn.com/package/type-is/?lang=original
项目主页：https://github.com/jshttp/type-is#readme
源码仓库：https://github.com/jshttp/type-is

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：27.7 kB，6 个文件
- 模块格式：ES Module
- TypeScript 类型：包内自带
- Node.js 要求：\>= 22
- 运行依赖：1 个
- 首次发布：2013-12-28T00:06:19.362Z
- 最新版本发布：2026-09-21T23:32:08.285Z
- 近 30 天下载量：643246843（2026-10-05 至 2026-11-03，npm 官方下载统计）
- 已发布版本数：41（近 12 个月 2 个）

## 中文简介

推断请求的内容类型。

## 安装

```sh
npm install type-is
```

也可以使用：`pnpm add type-is`、`yarn add type-is`、`bun add type-is`

## README

# 类型

[![NPM Version](https://badgen.net/npm/v/type-is)](https://npmjs.org/package/type-is) [![NPM Downloads](https://badgen.net/npm/dm/type-is)](https://npmjs.org/package/type-is) [![Node.js Version](https://badgen.net/npm/node/type-is)](https://nodejs.org/en/download) [![Build Status](https://badgen.net/github/checks/jshttp/type-is/master?label=ci)](https://github.com/jshttp/type-is/actions/workflows/ci.yml) [![Test Coverage](https://badgen.net/coveralls/c/github/jshttp/type-is/master)](https://coveralls.io/r/jshttp/type-is?branch=master)

推断请求的内容类型。

## 安装

```sh
$ npm install type-is
```

## API

```js
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（类型，选项？）

创建可重用的内容类型匹配器。可选 `options` 对象接受:

-   `lookup`解析简写类型的函数(参见 [`normalize`](https://npmcn.com/package/type-is/#readme-normalizetype)）.
-   `parameterValue`:一个 `(key: string, value: string) => string` 函数，在比较之前对配置和传入参数值进行规范化。默认情况下, `charset` 值小写，其他值不变。

每一种 `types` Array可以是以下类型之一：

-   mime类型，如 `application/json`。
-   带有通配符的mime类型，例如 `*/*` 或 `*/json` 或 `application/*`。
-   后缀如 `+json`． 这可以与通配符结合使用，例如 `*/vnd+json` 或 `application/*+json`。
-   配置的简写，例如 `multipart` 或 `urlencoded`。
-   上述任何一个参数也必须匹配，例如 `text/html; charset=utf-8`。

### typeIs.is(值)

检查 `content-type` 值并返回匹配的第一个配置类型，或 `undefined` 当没有匹配时。

```js
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(请求)

根据配置的类型检查请求。如果请求没有正文，即使有 `Content-Type` 头,然后 `undefined` 返回。否则它会使用 `is` 要查看 `content-type` 头。

```js
// 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(请求)

如果给定，则返回true `request` 有一个基于所提供的HTTP头的正文。

主体的大小与主体的大小无关（主体可以是0字节）。 这类似于文件存在的工作原理。如果肉体确实存在，那么 表示有数据要从Node.js请求流中读取。

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

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

### 匹配(预计)

编译类型字符串 `expected` 转换为与MIME类型匹配的函数。

```js
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
```

### 正常化(类型、期权?)

规范化 `type` 字符串。它通过执行以下操作来工作：

-   如果字符串包含 `/`，然后作为类型返回。
-   如果字符串以 `+` (所以它是 `+suffix` 速记像 `+json`)，然后将其扩展为包含完整的通配符符号 `*/*+suffix`。
-   否则，该字符串将被假定为文件扩展名并返回映射的媒体类型，如果没有映射，则返回原始输入。

默认扩展保持最小：

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

的选项对象 `{ lookup: (value: string) => string | string[] | undefined }` 提供额外的映射，例如 [`mime.lookup`](https://github.com/jshttp/mime-types#mimelookuppath)。

## 例子

### 示例体解析器

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

## 许可证

[麻省理工学院](https://npmcn.com/package/type-is/LICENSE)

## 常见问题

### type-is 是什么？

推断请求的内容类型。

### 如何安装 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 和提交记录判断。

