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