https://github.com/tom-sherman/fetch-multipart
https://github.com/tom-sherman/fetch-multipart
Last synced: 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/tom-sherman/fetch-multipart
- Owner: tom-sherman
- Created: 2022-06-21T15:11:13.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2023-04-25T12:22:04.000Z (about 2 years ago)
- Last Synced: 2025-04-13T20:47:26.761Z (2 months ago)
- Language: TypeScript
- Size: 31.3 KB
- Stars: 13
- Watchers: 2
- Forks: 0
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# fetch-multipart
> **This library is very WIP. It doesn't quite work yet...**
Standards-inspired `multipart/*` parsing. It's like `response.text()` but for multipart bodies!
## Goals
- 100% standards compliant and isomorphic. Use it in the browser, Cloudflare Workers, Deno, and whatever new JS environment was created last week
- Support all multipart bodies: `multipart/form-data`, `multipart/mixed`, `multipart/digest`, and `multipart/parallel`
- Support nested multipart bodies## API
### `multipart(input: MultipartInput): AsyncGenerator>`
Pass a `Request` or `Response` and receive an async iterator of `BodyPart`s.
### `MultipartInput`
A `Request`/`Response`-like object.
Needs to have a `body` that is a `ReadableStream | null` and a `headers` of type `Headers`.
### `BodyPart`
Conceptually multipart bodies are comprised of one or more "body parts". Each body part comprises a single part of the multipart body.
`BodyPart` shares many properties and methods with `Response` eg. `.json()`, `.text()`, `.blob()`. You can also access the underlying stream via `.body` just as you would do with a `Response`.
You can also handle nested multipart bodies by calling the `.multipart()` method.
## Usage
```js
import { multipart } from "fetch-multipart";const parts = await fetch('/api').then(multipart);
for await (const bodyPart of parts) {
bodyPart.headers.get("content-disposition"); // read the headers for this part
await bodyPart.arrayBuffer(); // Read the body part's body in an ArrayBuffer
await bodyPart.json(); // Read the body part's body as JSON
await streamUpload(bodyPart.body); // Access the body stream directly. eg. to stream to storage
}
```## Prior art
- https://github.com/mscdex/busboy
- https://github.com/deligenius/multiparser
- https://github.com/maraisr/meros