Skip to content

Commit 1ae80c8

Browse files
KhafraDevmetcoder95
authored andcommitted
feat: expose content-type parser (nodejs#1895)
* feat: expose content-type parser * fix: add docs
1 parent a0270f1 commit 1ae80c8

File tree

5 files changed

+85
-0
lines changed

5 files changed

+85
-0
lines changed

docs/api/ContentType.md

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# MIME Type Parsing
2+
3+
## `MIMEType` interface
4+
5+
* **type** `string`
6+
* **subtype** `string`
7+
* **parameters** `Map<string, string>`
8+
* **essence** `string`
9+
10+
## `parseMIMEType(input)`
11+
12+
Implements [parse a MIME type](https://mimesniff.spec.whatwg.org/#parse-a-mime-type).
13+
14+
Parses a MIME type, returning its type, subtype, and any associated parameters. If the parser can't parse an input it returns the string literal `'failure'`.
15+
16+
```js
17+
import { parseMIMEType } from 'undici'
18+
19+
parseMIMEType('text/html; charset=gbk')
20+
// {
21+
// type: 'text',
22+
// subtype: 'html',
23+
// parameters: Map(1) { 'charset' => 'gbk' },
24+
// essence: 'text/html'
25+
// }
26+
```
27+
28+
Arguments:
29+
30+
* **input** `string`
31+
32+
Returns: `MIMEType|'failure'`
33+
34+
## `serializeAMimeType(input)`
35+
36+
Implements [serialize a MIME type](https://mimesniff.spec.whatwg.org/#serialize-a-mime-type).
37+
38+
Serializes a MIMEType object.
39+
40+
```js
41+
import { serializeAMimeType } from 'undici'
42+
43+
serializeAMimeType({
44+
type: 'text',
45+
subtype: 'html',
46+
parameters: new Map([['charset', 'gbk']]),
47+
essence: 'text/html'
48+
})
49+
// text/html;charset=gbk
50+
51+
```
52+
53+
Arguments:
54+
55+
* **mimeType** `MIMEType`
56+
57+
Returns: `string`

docsify/sidebar.md

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* [API Lifecycle](/docs/api/api-lifecycle.md "Undici API - Lifecycle")
2020
* [Diagnostics Channel Support](/docs/api/DiagnosticsChannel.md "Diagnostics Channel Support")
2121
* [WebSocket](/docs/api/WebSocket.md "Undici API - WebSocket")
22+
* [MIME Type Parsing](/docs/api/ContentType.md "Undici API - MIME Type Parsing")
2223
* Best Practices
2324
* [Proxy](/docs/best-practices/proxy.md "Connecting through a proxy")
2425
* [Client Certificate](/docs/best-practices/client-certificate.md "Connect using a client certificate")

index.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export * from './types/filereader'
2323
export * from './types/formdata'
2424
export * from './types/diagnostics-channel'
2525
export * from './types/websocket'
26+
export * from './types/content-type'
2627
export { Interceptable } from './types/mock-interceptor'
2728

2829
export { Dispatcher, BalancedPool, Pool, Client, buildConnector, errors, Agent, request, stream, pipeline, connect, upgrade, setGlobalDispatcher, getGlobalDispatcher, setGlobalOrigin, getGlobalOrigin, MockClient, MockPool, MockAgent, mockErrors, ProxyAgent, RedirectHandler, DecoratorHandler }

index.js

+5
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@ if (nodeMajor >= 16) {
134134
module.exports.getCookies = getCookies
135135
module.exports.getSetCookies = getSetCookies
136136
module.exports.setCookie = setCookie
137+
138+
const { parseMIMEType, serializeAMimeType } = require('./lib/fetch/dataURL')
139+
140+
module.exports.parseMIMEType = parseMIMEType
141+
module.exports.serializeAMimeType = serializeAMimeType
137142
}
138143

139144
if (nodeMajor >= 18 && hasCrypto) {

types/content-type.d.ts

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/// <reference types="node" />
2+
3+
interface MIMEType {
4+
type: string
5+
subtype: string
6+
parameters: Map<string, string>
7+
essence: string
8+
}
9+
10+
/**
11+
* Parse a string to a {@link MIMEType} object. Returns `failure` if the string
12+
* couldn't be parsed.
13+
* @see https://mimesniff.spec.whatwg.org/#parse-a-mime-type
14+
*/
15+
export function parseMIMEType (input: string): 'failure' | MIMEType
16+
17+
/**
18+
* Convert a MIMEType object to a string.
19+
* @see https://mimesniff.spec.whatwg.org/#serialize-a-mime-type
20+
*/
21+
export function serializeAMimeType (mimeType: MIMEType): string

0 commit comments

Comments
 (0)