https://github.com/sleeplessbyte/fetch-media
Utility function that uses fetch to fetch a media-enabled resource
https://github.com/sleeplessbyte/fetch-media
Last synced: 15 days ago
JSON representation
Utility function that uses fetch to fetch a media-enabled resource
- Host: GitHub
- URL: https://github.com/sleeplessbyte/fetch-media
- Owner: SleeplessByte
- License: mit
- Created: 2020-06-02T15:28:57.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2025-01-06T13:43:38.000Z (over 1 year ago)
- Last Synced: 2025-11-19T22:05:06.630Z (7 months ago)
- Language: TypeScript
- Homepage:
- Size: 1.53 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# fetch-media
Utility function that uses fetch to fetch a media-enabled resource
**Does _not_ work flawlessly with Node 18+ `fetch` because their `Response` class is implemented differently; may or may not work well with libraries that change the global `fetch`**.
## Installation
```bash
yarn add fetch-media
```
If you're using `react-native`, this will use the unbundled source _TypeScript_. As such, the normally rolled-up dependency `@ungap/url-search-params` isn't available.
You **must** install this dependency.
```bash
yarn add @ungap/url-search-params
```
## Usage
```typescript
import { fetchMedia } from 'fetch-media';
/**
* Fetches media from a URL
*
* - Automatically encodes the request body if the contentType is a JSON type
* - Automatically decodes the response body
* - as parsed JSON if it's JSON
* - as string if it's text
* - as ArrayBuffer or Blob if it's binary
* - as FormData if it's multipart/form-data
* - as UrlSearchParams if it has a body of url encoded form data
* - Automatically parses errors, problems, structured errors, etc.
*
* @see MediaOptions
*
* @param url the fully qualified url to fetch from
* @param param1 the {MediaOptions}
* @returns A fetch promise
*/
fetchMedia('https://example.org', {
headers: { accept: 'text/html' },
method: 'GET',
}).then((result) => {
/* html body as string */
});
```
---
You can use `fetchMediaWrapped` to get the full response (so you can read out headers)