Détail du package

type-is

jshttp152.1mMIT2.0.1

Infer the content-type of a request.

content, type, checking

readme

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

changelog

2.0.1 / 2025-03-27

2.0.0 / 2024-08-31

  • Drop node <18
  • Use content-type@^1.0.5 and media-typer@^1.0.0 for type validation
    • No behavior changes, upgrades media-typer
  • deps: mime-types@^3.0.0
    • Add application/toml with extension .toml
    • Add application/ubjson with extension .ubj
    • Add application/x-keepass2 with extension .kdbx
    • Add deprecated iWorks mime types and extensions
    • Add extension .amr to audio/amr
    • Add extension .cjs to application/node
    • Add extension .dbf to application/vnd.dbf
    • Add extension .m4s to video/iso.segment
    • Add extension .mvt to application/vnd.mapbox-vector-tile
    • Add extension .mxmf to audio/mobile-xmf
    • Add extension .opus to audio/ogg
    • Add extension .rar to application/vnd.rar
    • Add extension .td to application/urc-targetdesc+xml
    • Add extension .trig to application/trig
    • Add extensions from IANA for application/*+xml types
    • Add image/avif with extension .avif
    • Add image/ktx2 with extension .ktx2
    • Add image/vnd.ms-dds with extension .dds
    • Add new upstream MIME types
    • Fix extension of application/vnd.apple.keynote to be .key
    • Remove ambigious extensions from IANA for application/*+xml types
    • Update primary extension to .es for application/ecmascript

1.6.18 / 2019-04-26

  • Fix regression passing request object to typeis.is

1.6.17 / 2019-04-25

  • deps: mime-types@~2.1.24
    • Add Apple file extensions from IANA
    • Add extension .csl to application/vnd.citationstyles.style+xml
    • Add extension .es to application/ecmascript
    • Add extension .nq to application/n-quads
    • Add extension .nt to application/n-triples
    • Add extension .owl to application/rdf+xml
    • Add extensions .siv and .sieve to application/sieve
    • Add extensions from IANA for image/* types
    • Add extensions from IANA for model/* types
    • Add extensions to HEIC image types
    • Add new mime types
    • Add text/mdx with extension .mdx
  • perf: prevent internal throw on invalid type

1.6.16 / 2018-02-16

  • deps: mime-types@~2.1.18
    • Add application/raml+yaml with extension .raml
    • Add application/wasm with extension .wasm
    • Add text/shex with extension .shex
    • Add extensions for JPEG-2000 images
    • Add extensions from IANA for message/* types
    • Add extension .mjs to application/javascript
    • Add extension .wadl to application/vnd.sun.wadl+xml
    • Add extension .gz to application/gzip
    • Add glTF types and extensions
    • Add new mime types
    • Update extensions .md and .markdown to be text/markdown
    • Update font MIME types
    • Update text/hjson to registered application/hjson

1.6.15 / 2017-03-31

  • deps: mime-types@~2.1.15
    • Add new mime types

1.6.14 / 2016-11-18

  • deps: mime-types@~2.1.13
    • Add new mime types

1.6.13 / 2016-05-18

  • deps: mime-types@~2.1.11
    • Add new mime types

1.6.12 / 2016-02-28

  • deps: mime-types@~2.1.10
    • Add new mime types
    • Fix extension of application/dash+xml
    • Update primary extension for audio/mp4

1.6.11 / 2016-01-29

  • deps: mime-types@~2.1.9
    • Add new mime types

1.6.10 / 2015-12-01

  • deps: mime-types@~2.1.8
    • Add new mime types

1.6.9 / 2015-09-27

  • deps: mime-types@~2.1.7
    • Add new mime types

1.6.8 / 2015-09-04

  • deps: mime-types@~2.1.6
    • Add new mime types

1.6.7 / 2015-08-20

  • Fix type error when given invalid type to match against
  • deps: mime-types@~2.1.5
    • Add new mime types

1.6.6 / 2015-07-31

  • deps: mime-types@~2.1.4
    • Add new mime types

1.6.5 / 2015-07-16

  • deps: mime-types@~2.1.3
    • Add new mime types

1.6.4 / 2015-07-01

  • deps: mime-types@~2.1.2
    • Add new mime types
  • perf: enable strict mode
  • perf: remove argument reassignment

1.6.3 / 2015-06-08

  • deps: mime-types@~2.1.1
    • Add new mime types
  • perf: reduce try block size
  • perf: remove bitwise operations

1.6.2 / 2015-05-10

  • deps: mime-types@~2.0.11
    • Add new mime types

1.6.1 / 2015-03-13

  • deps: mime-types@~2.0.10
    • Add new mime types

1.6.0 / 2015-02-12

  • fix false-positives in hasBody Transfer-Encoding check
  • support wildcard for both type and subtype (*/*)

1.5.7 / 2015-02-09

  • fix argument reassignment
  • deps: mime-types@~2.0.9
    • Add new mime types

1.5.6 / 2015-01-29

  • deps: mime-types@~2.0.8
    • Add new mime types

1.5.5 / 2014-12-30

  • deps: mime-types@~2.0.7
    • Add new mime types
    • Fix missing extensions
    • Fix various invalid MIME type entries
    • Remove example template MIME types
    • deps: mime-db@~1.5.0

1.5.4 / 2014-12-10

  • deps: mime-types@~2.0.4
    • Add new mime types
    • deps: mime-db@~1.3.0

1.5.3 / 2014-11-09

  • deps: mime-types@~2.0.3
    • Add new mime types
    • deps: mime-db@~1.2.0

1.5.2 / 2014-09-28

  • deps: mime-types@~2.0.2
    • Add new mime types
    • deps: mime-db@~1.1.0

1.5.1 / 2014-09-07

  • Support Node.js 0.6
  • deps: media-typer@0.3.0
  • deps: mime-types@~2.0.1
    • Support Node.js 0.6

1.5.0 / 2014-09-05

  • fix hasbody to be true for content-length: 0

1.4.0 / 2014-09-02

  • update mime-types

1.3.2 / 2014-06-24

  • use ~ range on mime-types

1.3.1 / 2014-06-19

  • fix global variable leak

1.3.0 / 2014-06-19

  • improve type parsing

    • invalid media type never matches
    • media type not case-sensitive
    • extra LWS does not affect results

1.2.2 / 2014-06-19

  • fix behavior on unknown type argument

1.2.1 / 2014-06-03

  • switch dependency from mime to mime-types@1.0.0

1.2.0 / 2014-05-11

  • support suffix matching:

    • +json matches application/vnd+json
    • */vnd+json matches application/vnd+json
    • application/*+json matches application/vnd+json

1.1.0 / 2014-04-12

  • add non-array values support
  • expose internal utilities:

    • .is()
    • .hasBody()
    • .normalize()
    • .match()

1.0.1 / 2014-03-30

  • add multipart as a shorthand