Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: fetch (POC) #618

Draft
wants to merge 44 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
58c7dc2
feat!: Upgrade to `node-fetch` v3
d-goog Apr 16, 2024
f8570d9
Merge branch 'main' into upgrade-node-fetch
danielbankhead Apr 16, 2024
7fd2a95
refactor: Use native `FormData` in testing
d-goog Apr 17, 2024
0cc7856
Merge branch 'upgrade-node-fetch' of github.com:googleapis/gaxios int…
d-goog Apr 17, 2024
23e7626
feat: `fetch` (POC)
d-goog Apr 19, 2024
a61cfa5
feat!: Improve spec compliance
d-goog Apr 19, 2024
35a73b1
test(temp): Run 18+
d-goog Apr 19, 2024
e7addeb
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Apr 19, 2024
9816229
feat: Improve types, streamline, and standardize
d-goog May 14, 2024
7315442
feat!: Do Not Treat Buffers as JSON By Default
d-goog May 15, 2024
93bf4a8
Merge branch 'main' of github.com:googleapis/gaxios into upgrade-node…
d-goog May 15, 2024
273e516
Merge branch 'upgrade-node-fetch' of github.com:googleapis/gaxios int…
d-goog May 15, 2024
cc2a68b
Merge branch 'upgrade-node-fetch' of github.com:googleapis/gaxios int…
d-goog May 15, 2024
9164b87
test: Adopt `Headers` type from merge
d-goog May 15, 2024
2e389c8
Merge branch 'upgrade-node-fetch' of github.com:googleapis/gaxios int…
d-goog May 15, 2024
d01ecde
Merge branch 'upgrade-node-fetch' of github.com:googleapis/gaxios int…
d-goog May 15, 2024
84faea8
feat: Introduce `GaxiosOptionsPrepared` for improved type guarantees
d-goog May 16, 2024
4e6de5f
feat: Ensure `GaxiosOptionsPrepared.url` is always a `URL`
d-goog May 16, 2024
e6c2371
refactor: Clean-up Types & Resolve lint warnings
d-goog May 16, 2024
2089d83
Merge branch 'upgrade-node-fetch' of github.com:googleapis/gaxios int…
d-goog May 16, 2024
a8b2a3d
Merge branch 'upgrade-node-fetch' of github.com:googleapis/gaxios int…
d-goog May 16, 2024
01ac8c7
chore: merge conflicts
d-goog May 16, 2024
10d557a
feat: Support `request(URL)`
d-goog May 16, 2024
7138ccb
chore: more merge resolutions
d-goog May 17, 2024
98f7009
refactor: streamline `.data` for `Response`
d-goog May 20, 2024
e66c4ba
Merge branch 'upgrade-node-fetch' of github.com:googleapis/gaxios int…
d-goog May 20, 2024
fe2133e
Merge branch 'upgrade-node-fetch' of github.com:googleapis/gaxios int…
d-goog May 20, 2024
4894e39
docs: Simplify example
d-goog May 22, 2024
3b5ad04
Merge branch 'upgrade-node-fetch' of github.com:googleapis/gaxios int…
d-goog May 22, 2024
9a070ef
chore: Remove `node-fetch` patch
d-goog May 22, 2024
b030c81
chore: Update summary
d-goog May 22, 2024
a7e2e83
refactor: streamline, no error case
d-goog May 22, 2024
0baab44
Merge branch 'main' of github.com:googleapis/gaxios into do-not-treat…
d-goog Jul 1, 2024
b2ec395
docs: clarification
d-goog Jul 1, 2024
8413f68
feat: Basic `GET` Support for `Request` objects
d-goog Sep 11, 2024
da7ba40
Merge branch 'upgrade-node-fetch' into native-fetch
d-goog Sep 11, 2024
e3220b9
chore: Improve `Request` Support
d-goog Sep 11, 2024
04e264d
test: remove `.only`
d-goog Sep 11, 2024
d163a90
refactor: simplify `httpMethodsToRetry`
d-goog Sep 11, 2024
705fad3
chore: update `nock`
d-goog Sep 11, 2024
9e52755
test: cleanup
d-goog Sep 11, 2024
e5baa94
Merge branch 'upgrade-node-fetch' into do-not-treat-buffer-as-json
d-goog Sep 11, 2024
ec5e4e0
Merge branch 'do-not-treat-buffer-as-json' into native-fetch
d-goog Sep 11, 2024
50321d6
docs: Document `Request` and `RequestInit`
d-goog Sep 11, 2024
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
61 changes: 31 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![codecov](https://codecov.io/gh/googleapis/gaxios/branch/master/graph/badge.svg)](https://codecov.io/gh/googleapis/gaxios)
[![Code Style: Google](https://img.shields.io/badge/code%20style-google-blueviolet.svg)](https://github.com/google/gts)

> An HTTP request client that provides an `axios` like interface over top of `node-fetch`.
> An HTTP request client that provides a lightweight interface on top of `fetch`.

## Install

Expand All @@ -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 All @@ -41,6 +39,9 @@ over other authentication methods, i.e., application default credentials.

## Request Options

`GaxiosOptions` extends from [`RequestInit`](https://developer.mozilla.org/en-US/docs/Web/API/RequestInit).
`Gaxios#request` also accepts [`Request`](https://developer.mozilla.org/en-US/docs/Web/API/Request) objects as well.

```ts
interface GaxiosOptions = {
// The url to which the request should be sent. Required.
Expand All @@ -53,16 +54,26 @@ 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['Content-Type'] === 'application/x-www-form-urlencoded' (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')
// In all other cases, if you would like to prevent `application/json` as the
// default you must set the `Content-Type` header.
data: {
some: 'data'
},
Expand All @@ -71,23 +82,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 +105,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 +116,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 +153,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
11 changes: 2 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,14 @@
"@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",
"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",
"karma": "^6.0.0",
Expand All @@ -74,7 +70,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": "^18.0.0",
Expand All @@ -87,9 +83,6 @@
},
"dependencies": {
"extend": "^3.0.2",
"https-proxy-agent": "^7.0.1",
"is-stream": "^2.0.0",
"node-fetch": "^2.6.9",
"uuid": "^10.0.0"
"https-proxy-agent": "^7.0.1"
}
}
Loading