Skip to content

Commit

Permalink
feat!: Upgrade to node-fetch v3 (#617)
Browse files Browse the repository at this point in the history
* feat!: Upgrade to `node-fetch` v3

* refactor: Use native `FormData` in testing

* feat!: Improve spec compliance

* test(temp): Run 18+

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

* feat: Improve types, streamline, and standardize

* test: Adopt `Headers` type from merge

* feat: Introduce `GaxiosOptionsPrepared` for improved type guarantees

* feat: Ensure `GaxiosOptionsPrepared.url` is always a `URL`

* refactor: Clean-up Types & Resolve lint warnings

* refactor: streamline `.data` for `Response`

* docs: Simplify example

* refactor: streamline, no error case

* feat: Basic `GET` Support for `Request` objects

`node-fetch` does not yet support webstreams, which is required for `.body` node-fetch/node-fetch#387

* test: remove `.only`

* refactor: simplify `httpMethodsToRetry`

* chore: update `nock`

* test: cleanup

* fix: `File` in Node 18

* docs: clarification for node-fetch `.data`

* chore: fix webpack for node-fetch v3

* docs: clarifications

* fix: Types for Node.js-only environments

---------

Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
  • Loading branch information
danielbankhead and gcf-owl-bot[bot] authored Oct 25, 2024
1 parent 4409086 commit 33702e6
Show file tree
Hide file tree
Showing 14 changed files with 582 additions and 452 deletions.
57 changes: 28 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ $ npm install gaxios

```js
const {request} = require('gaxios');
const res = await request({
url: 'https://www.googleapis.com/discovery/v1/apis/',
});
const res = await request('https://www.googleapis.com/discovery/v1/apis/');
```

## Setting Defaults
Expand Down Expand Up @@ -53,16 +51,27 @@ interface GaxiosOptions = {
baseURL: 'https://example.com';

// The HTTP methods to be sent with the request.
headers: { 'some': 'header' },

// The data to send in the body of the request. Data objects will be
// serialized as JSON.
headers: { 'some': 'header' } || new Headers(),

// The data to send in the body of the request. Objects will be serialized as JSON
// except for:
// - `ArrayBuffer`
// - `Blob`
// - `Buffer` (Node.js)
// - `DataView`
// - `File`
// - `FormData`
// - `ReadableStream`
// - `stream.Readable` (Node.js)
// - strings
// - `TypedArray` (e.g. `Uint8Array`, `BigInt64Array`)
// - `URLSearchParams`
// - all other objects where:
// - headers.get('Content-Type') === 'application/x-www-form-urlencoded' (as they will be serialized as `URLSearchParams`)
//
// Note: if you would like to provide a Content-Type header other than
// application/json you you must provide a string or readable stream, rather
// than an object:
// data: JSON.stringify({some: 'data'})
// data: fs.readFile('./some-data.jpeg')
// Here are a few examples that would prevent setting `Content-Type: application/json` by default:
// - data: JSON.stringify({some: 'data'}) // a `string`
// - data: fs.readFile('./some-data.jpeg') // a `stream.Readable`
data: {
some: 'data'
},
Expand All @@ -71,23 +80,12 @@ interface GaxiosOptions = {
// Defaults to `0`, which is the same as unset.
maxContentLength: 2000,

// The max number of HTTP redirects to follow.
// Defaults to 100.
maxRedirects: 100,

// The querystring parameters that will be encoded using `qs` and
// The query parameters that will be encoded using `URLSearchParams` and
// appended to the url
params: {
querystring: 'parameters'
},

// By default, we use the `querystring` package in node core to serialize
// querystring parameters. You can override that and provide your
// own implementation.
paramsSerializer: (params) => {
return qs.stringify(params);
},

// The timeout for the HTTP request in milliseconds. Defaults to 0.
timeout: 1000,

Expand All @@ -105,6 +103,8 @@ interface GaxiosOptions = {
// The expected return type of the request. Options are:
// json | stream | blob | arraybuffer | text | unknown
// Defaults to `unknown`.
// If the `fetchImplementation` is native `fetch`, the
// stream is a `ReadableStream`, otherwise `readable.Stream`
responseType: 'unknown',

// The node.js http agent to use for the request.
Expand All @@ -114,9 +114,9 @@ interface GaxiosOptions = {
// status code. Defaults to (>= 200 && < 300)
validateStatus: (status: number) => true,

// Implementation of `fetch` to use when making the API call. By default,
// will use the browser context if available, and fall back to `node-fetch`
// in node.js otherwise.
/**
* Implementation of `fetch` to use when making the API call. Will use `fetch` by default.
*/
fetchImplementation?: typeof fetch;

// Configuration for retrying of requests.
Expand Down Expand Up @@ -151,8 +151,7 @@ interface GaxiosOptions = {
// Enables default configuration for retries.
retry: boolean,

// Cancelling a request requires the `abort-controller` library.
// See https://github.com/bitinn/node-fetch#request-cancellation-with-abortsignal
// Enables aborting via AbortController
signal?: AbortSignal

/**
Expand Down
4 changes: 2 additions & 2 deletions browser-test/test.browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import assert from 'assert';
import {describe, it} from 'mocha';
import {request} from '../src/index';
import * as uuid from 'uuid';
const port = 7172; // should match the port defined in `webserver.ts`

describe('💻 browser tests', () => {
Expand Down Expand Up @@ -53,7 +52,8 @@ describe('💻 browser tests', () => {
body: 'hello world!',
},
];
const boundary = uuid.v4();
const boundary =
globalThis?.crypto.randomUUID() || (await import('crypto')).randomUUID();
const finale = `--${boundary}--`;
headers['Content-Type'] = `multipart/related; boundary=${boundary}`;

Expand Down
10 changes: 2 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,15 @@
"@types/mv": "^2.1.0",
"@types/ncp": "^2.0.1",
"@types/node": "^20.0.0",
"@types/node-fetch": "^2.5.7",
"@types/sinon": "^17.0.0",
"@types/tmp": "0.2.6",
"@types/uuid": "^10.0.0",
"abort-controller": "^3.0.0",
"assert": "^2.0.0",
"browserify": "^17.0.0",
"c8": "^8.0.0",
"cheerio": "1.0.0-rc.12",
"cors": "^2.8.5",
"execa": "^5.0.0",
"express": "^4.16.4",
"form-data": "^4.0.0",
"gts": "^5.0.0",
"is-docker": "^2.0.0",
"jsdoc": "^4.0.0",
Expand All @@ -77,7 +73,7 @@
"multiparty": "^4.2.1",
"mv": "^2.1.1",
"ncp": "^2.0.0",
"nock": "^13.0.0",
"nock": "^14.0.0-beta.13",
"null-loader": "^4.0.0",
"puppeteer": "^19.0.0",
"sinon": "^17.0.0",
Expand All @@ -91,8 +87,6 @@
"dependencies": {
"extend": "^3.0.2",
"https-proxy-agent": "^7.0.1",
"is-stream": "^2.0.0",
"node-fetch": "^2.6.9",
"uuid": "^9.0.1"
"node-fetch": "^3.3.2"
}
}
Loading

0 comments on commit 33702e6

Please sign in to comment.