@browsery/type-is

中文文档
2.0.2-rev.1发布于 2026/10/07MITTypeScriptES Module构建来源已验证近 30 天下载 3644

Browser compatible type-is module

原始使用文档
文档目录10 节
  1. Install
  2. API
  3. typeis(request, types)
  4. typeis.hasBody(request)
  5. typeis.is(mediaType, types)
  6. typeis.match(expected, actual)
  7. typeis.normalize(type)
  8. Examples
  9. Example body parser
  10. License

@browsery/type-is

Browser compatible "type-is"!

This module bundles type-is module for browsers!

type-is

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

Infer the content-type of a request.

Install

This is a Node.js module available through the npm registry. Installation is done using the npm install command:

$ npm install type-is

API

var http = require('http')
var typeis = require('type-is')

http.createServer(function (req, res) {
  var istext = typeis(req, ['text/*'])
  res.end('you ' + (istext ? 'sent' : 'did not send') + ' me text')
})

typeis(request, types)

Checks if the request is one of the types. If the request has no body, even if there is a Content-Type header, then null is returned. If the Content-Type header is invalid or does not matches any of the types, then false is returned. Otherwise, a string of the type that matched is returned.

The request argument is expected to be a Node.js HTTP request. The types argument is an array of type strings.

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

  • A file extension name such as json. This name will be returned if matched.
  • A mime type such as application/json.
  • A mime type with a wildcard such as */* or */json or application/*. The full mime type will be returned if matched.
  • A suffix such as +json. This can be combined with a wildcard such as */vnd+json or application/*+json. The full mime type will be returned if matched.

Some examples to illustrate the inputs and returned value:

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

typeis(req, ['json']) // => 'json'
typeis(req, ['html', 'json']) // => 'json'
typeis(req, ['application/*']) // => 'application/json'
typeis(req, ['application/json']) // => 'application/json'

typeis(req, ['html']) // => false

typeis.hasBody(request)

Returns a Boolean if the given request has a body, regardless of the Content-Type header.

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) {
    // ...
  })
}

typeis.is(mediaType, types)

Checks if the mediaType is one of the types. If the mediaType is invalid or does not matches any of the types, then false is returned. Otherwise, a string of the type that matched is returned.

The mediaType argument is expected to be a media type string. The types argument is an array of type strings.

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

  • A file extension name such as json. This name will be returned if matched.
  • A mime type such as application/json.
  • A mime type with a wildcard such as */* or */json or application/*. The full mime type will be returned if matched.
  • A suffix such as +json. This can be combined with a wildcard such as */vnd+json or application/*+json. The full mime type will be returned if matched.

Some examples to illustrate the inputs and returned value:

var mediaType = 'application/json'

typeis.is(mediaType, ['json']) // => 'json'
typeis.is(mediaType, ['html', 'json']) // => 'json'
typeis.is(mediaType, ['application/*']) // => 'application/json'
typeis.is(mediaType, ['application/json']) // => 'application/json'

typeis.is(mediaType, ['html']) // => false

typeis.match(expected, actual)

Match the type string expected with actual, taking in to account wildcards. A wildcard can only be in the type of the subtype part of a media type and only in the expected value (as actual should be the real media type to match). A suffix can still be included even with a wildcard subtype. If an input is malformed, false will be returned.

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.normalize(type)

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

  • If the type is not a string, false is returned.
  • 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.
  • If the string contains a /, then it is returned as the type.
  • Else the string is assumed to be a file extension and the mapped media type is returned, or false is there is no mapping.

This includes two special mappings:

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

Examples

Example body parser

var express = require('express')
var typeis = require('type-is')

var app = express()

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

  switch (typeis(req, ['urlencoded', 'json', 'multipart'])) {
    case 'urlencoded':
      // parse urlencoded body
      throw new Error('implement urlencoded body parsing')
    case '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

版本与发布记录

@browsery/type-is 共发布 16 个版本,首次发布于 2023/10/17;过去 12 个月发布了 3 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
2.0.2-rev.1latest2026/10/071 个月前
2.0.2-r22026/09/072 个月前
2.0.2-r12026/04/047 个月前
2.0.12025/10/231 年前
1.6.18-r82024/09/162 年前
1.6.18-r72024/08/202 年前

关于 @browsery/type-is 的常见问题

@browsery/type-is 是什么?

Browser compatible type-is module

如何安装 @browsery/type-is?

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

@browsery/type-is 支持 TypeScript 吗?

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

@browsery/type-is 支持 ESM 还是 CommonJS?

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

@browsery/type-is 可以商用吗?

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

@browsery/type-is 的下载量有多少?

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

@browsery/type-is 还在维护吗?

最新版本 2.0.2-rev.1 发布于 2026/10/07(1 个月前);至今共发布 16 个版本,其中过去 12 个月发布了 3 个;首次发布于 2023/10/17。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。