Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .eslintignore

This file was deleted.

9 changes: 0 additions & 9 deletions .eslintrc.yml

This file was deleted.

136 changes: 17 additions & 119 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,128 +1,26 @@
name: ci

name: CI
on:
push:
branches:
- master
- '2.x'
paths-ignore:
- '*.md'
pull_request:
paths-ignore:
- '*.md'

- push
- pull_request
permissions:
contents: read

jobs:
test:
permissions:
checks: write # for coverallsapp/github-action to create new checks
contents: read # for actions/checkout to fetch code
name: Node.js ${{ matrix.node-version }}
runs-on: ubuntu-latest
strategy:
matrix:
name:
- Node.js 18.x
- Node.js 19.x
- Node.js 20.x
- Node.js 21.x
- Node.js 22.x

include:
- name: Node.js 18.x
node-version: "18"

- name: Node.js 19.x
node-version: "19"

- name: Node.js 20.x
node-version: "20"

- name: Node.js 21.x
node-version: "21"

- name: Node.js 22.x
node-version: "22"

node-version:
- 18
- '*'
steps:
- uses: actions/checkout@v5.0.0

- name: Install Node.js ${{ matrix.node-version }}
shell: bash -eo pipefail -l {0}
run: |
nvm install --default ${{ matrix.node-version }}
dirname "$(nvm which ${{ matrix.node-version }})" >> "$GITHUB_PATH"

- name: Configure npm
run: |
if [[ "$(npm config get package-lock)" == "true" ]]; then
npm config set package-lock false
else
npm config set shrinkwrap false
fi

- name: Remove npm module(s) ${{ matrix.npm-rm }}
run: npm rm --silent --save-dev ${{ matrix.npm-rm }}
if: matrix.npm-rm != ''

- name: Install npm module(s) ${{ matrix.npm-i }}
run: npm install --save-dev ${{ matrix.npm-i }}
if: matrix.npm-i != ''

- name: Setup Node.js version-specific dependencies
shell: bash
run: |
# eslint for linting
# - remove on Node.js < 10
if [[ "$(cut -d. -f1 <<< "${{ matrix.node-version }}")" -lt 10 ]]; then
node -pe 'Object.keys(require("./package").devDependencies).join("\n")' | \
grep -E '^eslint(-|$)' | \
sort -r | \
xargs -n1 npm rm --silent --save-dev
fi

- name: Install Node.js dependencies
run: npm install

- name: List environment
id: list_env
shell: bash
run: |
echo "node@$(node -v)"
echo "npm@$(npm -v)"
npm -s ls ||:
(npm -s ls --depth=0 ||:) | awk -F'[ @]' 'NR>1 && $2 { print "::set-output name=" $2 "::" $3 }'

- name: Run tests
shell: bash
run: |
if npm -ps ls nyc | grep -q nyc; then
npm run test-ci
else
npm test
fi

- name: Lint code
if: steps.list_env.outputs.eslint != ''
run: npm run lint

- name: Collect code coverage
uses: coverallsapp/github-action@master
if: steps.list_env.outputs.nyc != ''
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
flag-name: run-${{ matrix.test_number }}
parallel: true

coverage:
permissions:
checks: write # for coverallsapp/github-action to create new checks
needs: test
runs-on: ubuntu-latest
steps:
- name: Upload code coverage
uses: coverallsapp/github-action@master
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
parallel-finished: true
- uses: actions/checkout@v6
- uses: actions/setup-node@v6
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
- uses: codecov/codecov-action@v5
with:
name: Node.js ${{ matrix.node-version }}
token: ${{ secrets.CODECOV_TOKEN }}
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.nyc_output/
coverage/
dist/
node_modules/
npm-debug.log
package-lock.json
*.tsbuildinfo
84 changes: 40 additions & 44 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,23 @@ Infer the content-type of a request.

## Install

This is a [Node.js](https://nodejs.org/en/) module available through the
[npm registry](https://www.npmjs.com/). Installation is done using the
[`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally):

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

## API

```js
var http = require('http')
var typeis = require('type-is')
import { createServer } from "http";
import * as typeis from "type-is";

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

### typeis(request, types)
### request(req, 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
Expand All @@ -55,12 +51,12 @@ Some examples to illustrate the inputs and returned value:
```js
// 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'
request(req, ["json"]); // => 'json'
request(req, ["html", "json"]); // => 'json'
request(req, ["application/*"]); // => 'application/json'
request(req, ["application/json"]); // => 'application/json'

typeis(req, ['html']) // => false
request(req, ["html"]); // => false
```

### typeis.hasBody(request)
Expand All @@ -76,9 +72,9 @@ 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) {
req.on("data", function (chunk) {
// ...
})
});
}
```

Expand All @@ -105,14 +101,14 @@ Each type in the `types` array can be one of the following:
Some examples to illustrate the inputs and returned value:

```js
var mediaType = 'application/json'
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, ["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.is(mediaType, ["html"]); // => false
```

### typeis.match(expected, actual)
Expand All @@ -124,11 +120,11 @@ suffix can still be included even with a wildcard subtype. If an input is
malformed, `false` will be returned.

```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
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)
Expand All @@ -152,33 +148,33 @@ This includes two special mappings:
### Example body parser

```js
var express = require('express')
var typeis = require('type-is')
var express = require("express");
var typeis = require("type-is");

var app = express()
var app = express();

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

switch (typeis(req, ['urlencoded', 'json', 'multipart'])) {
case 'urlencoded':
switch (typeis.request(req, ["urlencoded", "json", "multipart"])) {
case "urlencoded":
// parse urlencoded body
throw new Error('implement urlencoded body parsing')
case 'json':
throw new Error("implement urlencoded body parsing");
case "json":
// parse json body
throw new Error('implement json body parsing')
case 'multipart':
throw new Error("implement json body parsing");
case "multipart":
// parse multipart body
throw new Error('implement multipart body parsing')
throw new Error("implement multipart body parsing");
default:
// 415 error code
res.statusCode = 415
res.end()
break
res.statusCode = 415;
res.end();
break;
}
})
});
```

## License
Expand Down
Loading
Loading