node-fetch
Advanced tools
Comparing version 3.2.0 to 4.0.0-beta.1
@@ -5,2 +5,10 @@ /// <reference types="node" /> | ||
import {Agent} from 'http'; | ||
import { | ||
Blob, | ||
blobFrom, | ||
blobFromSync, | ||
File, | ||
fileFrom, | ||
fileFromSync | ||
} from 'fetch-blob/from.js'; | ||
@@ -16,2 +24,11 @@ type AbortSignal = { | ||
export { | ||
Blob, | ||
blobFrom, | ||
blobFromSync, | ||
File, | ||
fileFrom, | ||
fileFromSync | ||
}; | ||
/** | ||
@@ -118,5 +135,3 @@ * This Fetch API interface allows you to perform various actions on HTTP request and response headers. | ||
/** | ||
* @deprecated Please use 'response.arrayBuffer()' instead of 'response.buffer() | ||
*/ | ||
/** @deprecated Use `body.arrayBuffer()` instead. */ | ||
buffer(): Promise<Buffer>; | ||
@@ -123,0 +138,0 @@ arrayBuffer(): Promise<ArrayBuffer>; |
{ | ||
"name": "node-fetch", | ||
"version": "3.2.0", | ||
"version": "4.0.0-beta.1", | ||
"description": "A light-weight module that brings Fetch API to node.js", | ||
@@ -14,3 +14,3 @@ "main": "./src/index.js", | ||
"engines": { | ||
"node": "^12.20.0 || ^14.13.1 || >=16.0.0" | ||
"node": "^14.17.1 || >=16.0.0" | ||
}, | ||
@@ -17,0 +17,0 @@ "scripts": { |
138
README.md
@@ -67,2 +67,3 @@ <div align="center"> | ||
- [body.blob()](#bodyblob) | ||
- [body.formData()](#formdata) | ||
- [body.json()](#bodyjson) | ||
@@ -91,5 +92,4 @@ - [body.text()](#bodytext) | ||
- Use native promise and async functions. | ||
- Use native Node streams for body, on both request and response. | ||
- Use web streams for body, on both request and response. | ||
- Decode content encoding (gzip/deflate/brotli) properly, and convert string output (such as `res.text()` and `res.json()`) to UTF-8 automatically. | ||
- Useful extensions such as redirect limit, response size limit, [explicit errors][error-handling.md] for troubleshooting. | ||
@@ -99,2 +99,3 @@ ## Difference from client-side fetch | ||
- See known differences: | ||
- [As of v4.x](docs/v4-LIMITS.md) | ||
- [As of v3.x](docs/v3-LIMITS.md) | ||
@@ -107,3 +108,3 @@ - [As of v2.x](docs/v2-LIMITS.md) | ||
Current stable release (`3.x`) requires at least Node.js 12.20.0. | ||
Current beta release (`4.x`) requires at least Node.js 14.17.0. | ||
@@ -145,9 +146,20 @@ ```sh | ||
// fetch-polyfill.js | ||
import fetch from 'node-fetch'; | ||
import fetch, { | ||
Blob, | ||
blobFrom, | ||
blobFromSync, | ||
File, | ||
fileFrom, | ||
fileFromSync, | ||
FormData, | ||
Headers, | ||
Request, | ||
Response, | ||
} from 'node-fetch' | ||
if (!globalThis.fetch) { | ||
globalThis.fetch = fetch; | ||
globalThis.Headers = Headers; | ||
globalThis.Request = Request; | ||
globalThis.Response = Response; | ||
globalThis.fetch = fetch | ||
globalThis.Headers = Headers | ||
globalThis.Request = Request | ||
globalThis.Response = Response | ||
} | ||
@@ -165,2 +177,3 @@ | ||
- [3.x to 4.x upgrade guide](docs/v4-UPGRADE-GUIDE.md) | ||
- [2.x to 3.x upgrade guide](docs/v3-UPGRADE-GUIDE.md) | ||
@@ -397,9 +410,17 @@ - [1.x to 2.x upgrade guide](docs/v2-UPGRADE-GUIDE.md) | ||
```js | ||
import {fileFromSync} from 'fetch-blob/from.js'; | ||
import fetch from 'node-fetch'; | ||
import fetch { | ||
Blob, | ||
blobFrom, | ||
blobFromSync, | ||
File, | ||
fileFrom, | ||
fileFromSync, | ||
} from 'node-fetch' | ||
const blob = fileFromSync('./input.txt', 'text/plain'); | ||
const mimetype = 'text/plain' | ||
const blob = fileFromSync('./input.txt', mimetype) | ||
const url = 'https://httpbin.org/post' | ||
const response = await fetch('https://httpbin.org/post', {method: 'POST', body: blob}); | ||
const data = await response.json(); | ||
const response = await fetch(url, { method: 'POST', body: blob }) | ||
const data = await response.json() | ||
@@ -409,22 +430,46 @@ console.log(data) | ||
node-fetch also supports any spec-compliant FormData implementations such as [formdata-polyfill](https://www.npmjs.com/package/formdata-polyfill). But any other spec-compliant such as [formdata-node](https://github.com/octet-stream/form-data) works too, but we recommend formdata-polyfill because we use this one internally for decoding entries back to FormData. | ||
node-fetch comes with a spec-compliant [FormData] implementations for posting | ||
multipart/form-data payloads | ||
```js | ||
import fetch from 'node-fetch'; | ||
import {FormData} from 'formdata-polyfill/esm.min.js'; | ||
import fetch {FormData, File, fileFrom} from 'node-fetch' | ||
// Alternative hack to get the same FormData instance as node-fetch | ||
// const FormData = (await new Response(new URLSearchParams()).formData()).constructor | ||
const httpbin = 'https://httpbin.org/post' | ||
const formData = new FormData() | ||
const binary = new Uint8Array([ 97, 98, 99 ]) | ||
const abc = new File([binary], 'abc.txt'), { type: 'text/plain' }) | ||
const form = new FormData(); | ||
form.set('greeting', 'Hello, world!'); | ||
formData.set('greeting', 'Hello, world!') | ||
formData.set('file-upload', abc, 'new name.txt') | ||
const response = await fetch('https://httpbin.org/post', {method: 'POST', body: form}); | ||
const data = await response.json(); | ||
const response = await fetch(httpbin, { method: 'POST', body: formData }) | ||
const data = await response.json() | ||
console.log(data); | ||
console.log(data) | ||
``` | ||
node-fetch also support form-data but it's now discouraged due to not being spec-compliant and needs workarounds to function - which we hope to remove one day | ||
If you for some reason need to post a stream coming from any arbitrary place, | ||
then you can append a [Blob] or a [File] look-a-like item. | ||
The minium requirement is that it has: | ||
1. A `Symbol.toStringTag` getter or property that is either `Blob` or `File` | ||
2. A known size. | ||
3. And either a `stream()` method or a `arrayBuffer()` method that returns a ArrayBuffer. | ||
The `stream()` must return any async iterable object as long as it yields Uint8Array (or Buffer) | ||
so Node.Readable streams and whatwg streams works just fine. | ||
```js | ||
formData.append('upload', { | ||
[Symbol.toStringTag]: 'Blob', | ||
size: 3, | ||
*stream() { | ||
yield new Uint8Array([97, 98, 99]) | ||
}, | ||
arrayBuffer() { | ||
return new Uint8Array([97, 98, 99]).buffer | ||
} | ||
}, 'abc.txt') | ||
``` | ||
### Request cancellation with AbortSignal | ||
@@ -700,4 +745,3 @@ | ||
const meta = { | ||
'Content-Type': 'text/xml', | ||
'Breaking-Bad': '<3' | ||
'Content-Type': 'text/xml' | ||
}; | ||
@@ -707,3 +751,3 @@ const headers = new Headers(meta); | ||
// The above is equivalent to | ||
const meta = [['Content-Type', 'text/xml'], ['Breaking-Bad', '<3']]; | ||
const meta = [['Content-Type', 'text/xml']]; | ||
const headers = new Headers(meta); | ||
@@ -714,3 +758,2 @@ | ||
meta.set('Content-Type', 'text/xml'); | ||
meta.set('Breaking-Bad', '<3'); | ||
const headers = new Headers(meta); | ||
@@ -752,8 +795,38 @@ const copyOfHeaders = new Headers(headers); | ||
<small>_(spec-compliant)_</small> | ||
`fetch` comes with methods to parse `multipart/form-data` payloads as well as | ||
`x-www-form-urlencoded` bodies using `.formData()` this comes from the idea that | ||
Service Worker can intercept such messages before it's sent to the server to | ||
alter them. This is useful for anybody building a server so you can use it to | ||
parse & consume payloads. | ||
- Returns: `Promise` | ||
<details> | ||
<summary>Code example</summary> | ||
Consume the body and return a promise that will resolve to one of these formats. | ||
```js | ||
import http from 'node:http' | ||
import { Response } from 'node-fetch' | ||
http.createServer(async function (req, res) { | ||
const formData = await new Response(req, { | ||
headers: req.headers // Pass along the boundary value | ||
}).formData() | ||
const allFields = [...formData] | ||
const file = formData.get('uploaded-files') | ||
const arrayBuffer = await file.arrayBuffer() | ||
const text = await file.text() | ||
const whatwgReadableStream = file.stream() | ||
// other was to consume the request could be to do: | ||
const json = await new Response(req).json() | ||
const text = await new Response(req).text() | ||
const arrayBuffer = await new Response(req).arrayBuffer() | ||
const blob = await new Response(req, { | ||
headers: req.headers // So that `type` inherits `Content-Type` | ||
}.blob() | ||
}) | ||
``` | ||
</details> | ||
<a id="class-fetcherror"></a> | ||
@@ -809,1 +882,4 @@ | ||
[error-handling.md]: https://github.com/node-fetch/node-fetch/blob/master/docs/ERROR-HANDLING.md | ||
[FormData]: https://developer.mozilla.org/en-US/docs/Web/API/FormData | ||
[Blob]: https://developer.mozilla.org/en-US/docs/Web/API/Blob | ||
[File]: https://developer.mozilla.org/en-US/docs/Web/API/File |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
105618
2283
873
1
1