Skip to content

Commit c7c9262

Browse files
committed
remove string comparison
1 parent 3120e33 commit c7c9262

File tree

5 files changed

+9
-50
lines changed

5 files changed

+9
-50
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,5 @@ typings/
4343

4444
dist
4545
coverage
46-
coverage.lcov
46+
coverage.lcov
47+
.DS_Store

migration.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
| `urlencoded()` `interpretNumericEntities` option |||
1818
| `urlencoded()` `depth` option |||
1919

20-
\* The `type` option can be a string or a function. `milliparsec` does not
21-
depend on the `type-is` package and does not support wildcard MIME strings (for
22-
example `*/json`). It only does a simple string comparisong of the first part of
23-
the `Content-Type` header. Unlike body-parser, milliparsec does not set default
24-
`type` on parsers.
20+
\* The `type` option can only be a function. `milliparsec` does not depend on
21+
the `type-is` package and does not support wildcard MIME strings (for example
22+
`*/json`). Unlike body-parser, milliparsec does not set default `type` on
23+
parsers.

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export type ParserOptions<T extends Record<string, any> = Record<string, any>> =
2222
/**
2323
* Middleware content type
2424
*/
25-
type: string | ((req: IncomingMessage) => boolean)
25+
type: (req: IncomingMessage) => boolean
2626
}> &
2727
T
2828

src/utils.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,5 @@ import type { ParserOptions } from './types.js'
33

44
export const hasBody = (method: string): boolean => ['POST', 'PUT', 'PATCH', 'DELETE'].includes(method)
55

6-
export const checkType = (req: IncomingMessage, type: ParserOptions['type']): boolean => {
7-
if (typeof type === 'string') return type === req.headers['content-type']?.split(';')[0]
8-
if (typeof type === 'function') return type(req)
9-
return true
10-
}
6+
export const checkType = (req: IncomingMessage, type: ParserOptions['type']): boolean =>
7+
typeof type === 'function' ? type(req) : true

test.ts

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -565,44 +565,6 @@ describe('Content-Type filtering', () => {
565565
}).expect(200, { hello: 'world' })
566566
})
567567

568-
it('matches with a specified MIME type', async () => {
569-
const server = createServer(async (req: ReqWithBody, res) => {
570-
await json({ type: 'application/json' })(req, res, (err) => err && console.log(err))
571-
572-
res.setHeader('Content-Type', 'application/json')
573-
574-
res.end(JSON.stringify(req.body))
575-
})
576-
577-
await makeFetch(server)('/', {
578-
body: JSON.stringify({ hello: 'world' }),
579-
method: 'POST',
580-
headers: {
581-
Accept: 'application/json',
582-
'Content-Type': 'application/json'
583-
}
584-
}).expect(200, { hello: 'world' })
585-
})
586-
587-
it('matches with a MIME + encoding type', async () => {
588-
const server = createServer(async (req: ReqWithBody, res) => {
589-
await json({ type: 'application/json' })(req, res, (err) => err && console.log(err))
590-
591-
res.setHeader('Content-Type', 'application/json; charset=utf-8')
592-
593-
res.end(JSON.stringify(req.body))
594-
})
595-
596-
await makeFetch(server)('/', {
597-
body: JSON.stringify({ hello: 'world' }),
598-
method: 'POST',
599-
headers: {
600-
Accept: 'application/json',
601-
'Content-Type': 'application/json; charset=utf-8'
602-
}
603-
}).expect(200, { hello: 'world' })
604-
})
605-
606568
it('executes the type(req) fn if type is a function', async () => {
607569
const server = createServer(async (req: ReqWithBody, res) => {
608570
await json({ type: (req) => req.headers['content-type'] === 'application/json' })(

0 commit comments

Comments
 (0)