Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,13 @@ fetch('/bear', {
open(r.headers.get('location'));
return r.json();
})

// fetch image
fetch('https://i.imgur.com/4AiXzf8.jpg', {
responseType: 'blob'
}).then( r => {
return r.blob();
})
```

* * *
Expand All @@ -140,6 +147,7 @@ Unfetch will account for the following properties in `options`:
* `headers`: An `Object` containing additional information to be sent with the request, e.g. `{ 'Content-Type': 'application/json' }` to indicate a JSON-typed request body.
* `credentials`: ⚠ Accepts a `"include"` string, which will allow both CORS and same origin requests to work with cookies. As pointed in the ['Caveats' section](#caveats), Unfetch won't send or receive cookies otherwise. The `"same-origin"` value is not supported. ⚠
* `body`: The content to be transmitted in request's body. Common content types include `FormData`, `JSON`, `Blob`, `ArrayBuffer` or plain text.
* `responseType`: An enumerated string value specifying the [type](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/responseType) of data contained in the response, the default value of text is used.

### `response` Methods and Attributes
These methods are used to handle the response accordingly in your Promise chain. Instead of implementing full spec-compliant [Response Class](https://fetch.spec.whatwg.org/#response-class) functionality, Unfetch provides the following methods and attributes:
Expand Down
5 changes: 4 additions & 1 deletion src/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ export default function (url, options) {
options = options || {};
return new Promise((resolve, reject) => {
const request = new XMLHttpRequest();
if (options.responseType) {
request.responseType = options.responseType;
}
const keys = [];
const headers = {};

Expand All @@ -12,7 +15,7 @@ export default function (url, options) {
url: request.responseURL,
text: () => Promise.resolve(request.responseText),
json: () => Promise.resolve(request.responseText).then(JSON.parse),
blob: () => Promise.resolve(new Blob([request.response])),
blob: () => Promise.resolve(new Blob([request.response], {type: request.response.type})),
clone: response,
headers: {
keys: () => keys,
Expand Down