forked from nodejs/undici
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: expose content-type parser (nodejs#1895)
* feat: expose content-type parser * fix: add docs
- Loading branch information
Showing
5 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# MIME Type Parsing | ||
|
||
## `MIMEType` interface | ||
|
||
* **type** `string` | ||
* **subtype** `string` | ||
* **parameters** `Map<string, string>` | ||
* **essence** `string` | ||
|
||
## `parseMIMEType(input)` | ||
|
||
Implements [parse a MIME type](https://mimesniff.spec.whatwg.org/#parse-a-mime-type). | ||
|
||
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'`. | ||
|
||
```js | ||
import { parseMIMEType } from 'undici' | ||
|
||
parseMIMEType('text/html; charset=gbk') | ||
// { | ||
// type: 'text', | ||
// subtype: 'html', | ||
// parameters: Map(1) { 'charset' => 'gbk' }, | ||
// essence: 'text/html' | ||
// } | ||
``` | ||
|
||
Arguments: | ||
|
||
* **input** `string` | ||
|
||
Returns: `MIMEType|'failure'` | ||
|
||
## `serializeAMimeType(input)` | ||
|
||
Implements [serialize a MIME type](https://mimesniff.spec.whatwg.org/#serialize-a-mime-type). | ||
|
||
Serializes a MIMEType object. | ||
|
||
```js | ||
import { serializeAMimeType } from 'undici' | ||
|
||
serializeAMimeType({ | ||
type: 'text', | ||
subtype: 'html', | ||
parameters: new Map([['charset', 'gbk']]), | ||
essence: 'text/html' | ||
}) | ||
// text/html;charset=gbk | ||
|
||
``` | ||
|
||
Arguments: | ||
|
||
* **mimeType** `MIMEType` | ||
|
||
Returns: `string` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/// <reference types="node" /> | ||
|
||
interface MIMEType { | ||
type: string | ||
subtype: string | ||
parameters: Map<string, string> | ||
essence: string | ||
} | ||
|
||
/** | ||
* Parse a string to a {@link MIMEType} object. Returns `failure` if the string | ||
* couldn't be parsed. | ||
* @see https://mimesniff.spec.whatwg.org/#parse-a-mime-type | ||
*/ | ||
export function parseMIMEType (input: string): 'failure' | MIMEType | ||
|
||
/** | ||
* Convert a MIMEType object to a string. | ||
* @see https://mimesniff.spec.whatwg.org/#serialize-a-mime-type | ||
*/ | ||
export function serializeAMimeType (mimeType: MIMEType): string |