Skip to content

WIP: Fetch Adapter v2 #5

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

Draft
wants to merge 69 commits into
base: v1.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
69 commits
Select commit Hold shift + click to select a range
ab6a697
feat: add `fetch` adapter with cross-platform support
sgammon Nov 14, 2022
728dc27
fix: add polyfill for `AbortController` on Node 14
sgammon Oct 17, 2022
5cea891
chore: add web streams polyfill
sgammon Nov 14, 2022
c9427fc
chore: add `whatwg-fetch` polyfill
sgammon Dec 16, 2022
0f36532
feat(fetch): continue with implementation of Fetch adapter + API
sgammon Dec 17, 2022
3624b1f
chore: add karma-coverage for instrumentation
sgammon Dec 17, 2022
ab9b529
feat(fetch): implement mock `fetch` for testing
sgammon Dec 17, 2022
2173ebd
feat(fetch): add browser-side test suite
sgammon Dec 17, 2022
6a5c6c7
chore: ignore nyc-output / .husky
sgammon Dec 17, 2022
652ec27
chore: add `FetchAdapter` to ESM export
sgammon Dec 17, 2022
475b418
feat(fetch): update typescript typings with fetch types
sgammon Dec 17, 2022
157c653
feat(fetch): add new `generic` library target (ESM/pure JS)
sgammon Dec 17, 2022
4dfc7a9
feat(fetch): implement structure for abstract fetch testsuite
sgammon Dec 17, 2022
a968631
feat(fetch): add Deno benchmark and example
sgammon Dec 17, 2022
db1de9b
feat(fetch): add browser example
sgammon Dec 17, 2022
4e04f76
feat(fetch): add minimal browser samples
sgammon Dec 17, 2022
e8d6f3e
feat(fetch): add simple Node fetch sample
sgammon Dec 17, 2022
328220a
feat(fetch): add CloudFlare Workers sample
sgammon Dec 17, 2022
e1a042d
fix: build matrix, initial ci config for deno testing
sgammon Dec 17, 2022
6637938
fix: drop default fetch cache setting
sgammon Dec 17, 2022
2d6d78d
fix: browser example fixes
sgammon Dec 17, 2022
21f41e8
feat(fetch): platform-driven default adapters for each build
sgammon Dec 17, 2022
be92d9d
fix: copy new correct typings for generic bundle
sgammon Dec 17, 2022
41cd5e1
feat(fetch): tools to analyze browser/generic bundle sizes
sgammon Dec 17, 2022
d50c5a5
fix: tweaks to Node sample for fetch
sgammon Dec 17, 2022
3347dad
feat(fetch): construction on workers sample for axios/fetch
sgammon Dec 17, 2022
b348686
chore: test against Node 19
sgammon Dec 17, 2022
71749db
fix: labs CI matrix
sgammon Dec 17, 2022
dce9e02
feat(fetch): platform-specific defaults for `fetchOptions`
sgammon Dec 18, 2022
256195b
fix(fetch): fully mockable responses for fetch testing
sgammon Dec 18, 2022
b2d419c
fix: tweaks to fetch examples
sgammon Dec 18, 2022
1a6e074
feat(fetch): fetch example improvements + READMEs (browser, deno)
sgammon Dec 18, 2022
690be1c
feat(fetch): only ship .d.cts with non-modern targets
sgammon Dec 18, 2022
20b5633
feat(fetch): add explicit `axios/generic` bundle export
sgammon Dec 18, 2022
272f066
feat(fetch): variants and README for node fetch examples
sgammon Dec 18, 2022
58a2fc4
feat(fetch): better debug logging, assertions, support for `Blob`
sgammon Dec 18, 2022
503c724
feat(fetch): allow importing specific files for generic bundle
sgammon Dec 18, 2022
683469e
feat(fetch): document new fetch config in main README
sgammon Dec 18, 2022
100d31b
fix(fetch): refactor and cleanup response handler selection
sgammon Dec 18, 2022
3927308
fix(fetch): fixes for Fetch adapter in CloudFlare Workers env
sgammon Dec 18, 2022
0bc7dde
feat(fetch): sample for CloudFlare Workers
sgammon Dec 18, 2022
ae2ebe7
chore: add doc for workers sample
sgammon Dec 18, 2022
6e40e17
chore: updates to main README docs
sgammon Dec 18, 2022
9b8692c
feat(fetch): pure-ESM web testing
sgammon Dec 18, 2022
ce16244
chore: add bundle sizes for generic entrypoint
sgammon Dec 18, 2022
faede9b
chore: additional bundle size testing
sgammon Dec 18, 2022
da2308b
feat: export utils as individual symbols
sgammon Dec 18, 2022
2b9bed3
fix(fetch): avoid re-defining any variables named 'fetch'
sgammon Dec 18, 2022
f375b92
feat(fetch): treeshaking and terser tweaks, optimize license
sgammon Dec 18, 2022
4b1c994
fix: don't require Content-Type for body
sgammon Dec 18, 2022
181ebe9
feat(fetch): improvements to fetch typings
sgammon Dec 19, 2022
9bbd0b0
chore: cleanup abstract fetch testsuite
sgammon Dec 19, 2022
1ffc76e
fix: keep console debug statements during beta
sgammon Dec 19, 2022
e7d33a3
fix: 'modern' export profile types
sgammon Dec 19, 2022
eed4e5e
chore: smaller bundle size limits
sgammon Dec 19, 2022
b40c7c6
fix: handler selection with no `Content-Type` present
sgammon Dec 19, 2022
e0fddb0
fix: errors with previous typing updates
sgammon Dec 19, 2022
cdc1b3c
chore: drop unused webpack config
sgammon Dec 19, 2022
b1e4a3e
chore: package spec cleanup
sgammon Dec 19, 2022
fcb5351
chore: setup CI testing for Bun
sgammon Dec 19, 2022
de21e6d
fix: polynomial-capable regex in combineURLs helper
sgammon Dec 19, 2022
3fcd8cc
fix: pass relative URLs directly to the underlying fetcher
sgammon Dec 19, 2022
80d24e9
test: ESM API spec
sgammon Dec 19, 2022
a2d11ed
chore: cleanup abstract fetch testsuite
sgammon Dec 19, 2022
c6fa931
fix: disable protocol-relative URL testing for combineURLs (temp)
sgammon Dec 19, 2022
135495a
fix: drop missing test
sgammon Dec 19, 2022
ccfb365
fix: allow version tag in user-agent test
sgammon Sep 14, 2023
f7e700f
chore: version bump for `fetch` testing → `1.5.0-fetch`
sgammon Sep 14, 2023
9b315e1
chore: lock packages, rebuild dist
sgammon Sep 14, 2023
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: 61 additions & 0 deletions .github/workflows/ci.labs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
name: 'CI: Labs'

on:
push:
branches: '**'
pull_request:
branches: '**'

permissions:
contents: read

jobs:
build:
runs-on: ubuntu-latest

strategy:
matrix:
engine: [deno]
version: [v1.x]
mode: [labs]
include:
- engine: deno
version: v1.x
- engine: node
version: 19.x
mode: labs
Comment on lines +24 to +26
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

timeout bug on node 19

- engine: bun
version: 0.3.0
mode: labs
Comment on lines +27 to +29
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test task missing for bun


steps:
- uses: actions/checkout@v3
with:
persist-credentials: false

## --- Runtime Setup

- name: Setup node
uses: actions/setup-node@v3
if: ${{ matrix.engine == 'node' }}
with:
node-version: ${{ matrix.version }}
cache: npm
- name: Setup Deno
uses: denoland/setup-deno@v1
if: ${{ matrix.engine == 'deno' }}
with:
deno-version: ${{ matrix.version }}
- name: Setup Bun
uses: antongolub/action-setup-bun@v1
if: ${{ matrix.engine == 'bun' }}
with:
bun-version: ${{ matrix.version }}

## --- Build + Test

- run: npm install
- run: npm run build
if: ${{ matrix.engine == 'deno' }}
- run: npm run test:${{ matrix.engine }}
continue-on-error: ${{ matrix.mode == 'labs' }}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ backup/
/.husky/
.npmrc
.env
.husky/
.nyc_output/
133 changes: 132 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@
## Features

- Make [XMLHttpRequests](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest) from the browser
- Make [fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) requests from the browser, node.js, or pure-JS
runtimes like Workers, Deno, Bun, etc.
- Make [http](https://nodejs.org/api/http.html) requests from node.js
- Supports the [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) API
- Intercept request and response
Expand Down Expand Up @@ -165,6 +167,23 @@ Using unpkg CDN:
<script src="https://unpkg.com/[email protected]/dist/axios.min.js"></script>
```

### Pure JS environments

Using Axios in **Deno**:
```js
import axios from "https://cdn.jsdelivr.net/npm/[email protected]/dist/generic/axios.min.mjs";
```

Using in CloudFlare Workers, Bun, and other environments which support the Fetch API:
```js
{npm|pnpm|yarn} install axios
```

Then:
```js
import axios from "axios/generic";
```

## Example

> **Note**: CommonJS usage
Expand Down Expand Up @@ -286,6 +305,9 @@ axios({
```js
// Send a GET request (default method)
axios('/user/12345');

// You can also use a `URL` object
axios(new URL('https://example.com/user/12345'));
```

### Request method aliases
Expand Down Expand Up @@ -346,7 +368,8 @@ These are the available config options for making requests. Only the `url` is re

```js
{
// `url` is the server URL that will be used for the request
// `url` is the server URL that will be used for the request. you can
// also pass `new URL(...)`, in environments that support it.
url: '/user',

// `method` is the request method to be used when making the request
Expand Down Expand Up @@ -538,6 +561,114 @@ These are the available config options for making requests. Only the `url` is re
// an alternative way to cancel Axios requests using AbortController
signal: new AbortController().signal,

// pre-parsed URL, to avoid re-parsing the URL when using the fetch adapter.
// optional.
parsedUrl: new URL('...'),

// fetch implementation to use. defaults to the native fetch implementation for
// the active platform, or falls back to a polyfill (`whatwg-fetch`), or a cross-
// platform polyfill (`cross-fetch`).
fetcher: fetch,

// options passed to the underlying `fetch` implementation; only active when
// `adapter` is set to `{adapter: 'fetch'}` or `{adapter: axios.FetchAdapter}`.
// default values below are for `fetch` implementation in browsers. certain fetch
// implementations may require different or limited options.
//
// see MDN for more information (`options` parameter):
// https://developer.mozilla.org/en-US/docs/Web/API/Request/Request
fetchOptions: {
// the general request mode to apply to an operation. options are:
// - `cors` (default): allow cross-origin requests.
// - `no-cors`: do not allow cross-origin requests.
// - `same-origin`: only allow requests to the same origin.
//
// MDN: https://developer.mozilla.org/en-US/docs/Web/API/Request/mode
mode: 'cors',

// should the user's credentials and cookies be sent with the request? options are:
// - `same-origin` (default): only send for same-origin requests.
// - `include`: include credentials for all requests.
// - `omit`: do not send credentials.
//
// DANGER: Be careful with this property. Setting the request to `include` can expose
// users to serious privacy and security risks. Make sure the origins you are exchanging
// data with use HTTPS and are covered by other strong security practices.
//
// MDN: https://developer.mozilla.org/en-US/docs/Web/API/Request/credentials
credentials: 'same-origin',

// allow caching of fetched responses? options are:
// - `default` (default): default environment behavior. in browsers, caching
// is typically enabled for GET requests that are marked cacheable.
// - `no-store`: do not store responses in cache.
// - `reload`: fetch from server and update cache.
// - `no-cache`: fetch from server and do not update cache.
// - `force-cache`: use cache if available, otherwise fetch from server and
// update the cache.
// - `only-if-cached`: use cache if available, otherwise fail. this mode can
// only be used when `mode` is set to `same-origin`.
//
// MDN: https://developer.mozilla.org/en-US/docs/Web/API/Request/cache
cache: 'default',

// follow redirects returned in fetch calls? options are:
// - `follow` (default): follow redirects
// - `error`: reject with an error
// - `manual`: handle redirects manually
//
// MDN: https://developer.mozilla.org/en-US/docs/Web/API/Request/redirect
redirect: 'follow',

// what referrer, if any, should be sent with requests? options are:
// - `about:client` (default): anonymized referrer is sent.
// - `no-referrer`: no referrer is sent.
// - `client`: full referrer is sent.
// - `<url>`: a custom URL value can be set and sent.
//
// See also: `referrerPolicy`.
//
// MDN: https://developer.mozilla.org/en-US/docs/Web/API/Request/referrer
referrer: 'about:client',

// what policy should be applied when deciding whether to send a referrer? options are:
// - `strict-origin-when-cross-origin` (default): send the origin, path, and querystring
// when performing same-origin requests. For cross-origin requests, send only the origin.
// The referrer is never sent to non-secure origins in this mode.
// - `no-referrer-when-downgrade`: send referrer only when navigating to a more or
// equivalently secure origin.
// - `no-referrer`: never send a referrer.
// - `origin`: send the origin only.
// - `origin-when-cross-origin`: send only the origin when interacting with a different origin.
// - `same-origin`: send the referrer only when interacting with same-origin endpoints.
// - `strict-origin`: send the origin only when the protocol level stays the same.
// - `unsafe-url`: send the full referrer URL unconditionally to all origins.
//
// See also: https://developer.mozilla.org/en-US/docs/Web/API/Request/referrerPolicy
referrerPolicy: 'strict-origin-when-cross-origin',

// should an integrity value be applied and checked for the request? options are:
// - no value (default): no integrity is checked.
// - `<hash-algorithm>-<base64-value>`: integrity is checked using the specified algorithm.
// for example, `sha256-<hash>`.
//
// MDN: https://developer.mozilla.org/en-US/docs/Web/API/Request/integrity
integrity: 'sha256-<base64-encoded-hash>',

// should the connection to the server be kept alive? options are:
// - true (default): keep the connection alive.
// - false: close the connection after the request is complete.
// MDN: https://developer.mozilla.org/en-US/docs/Web/API/Request/Request
keepalive: true,

// an `AbortSignal` object which can be used to communicate with or abort the request;
// if both this property and the main config `signal` property are set, this property
// takes precedence.
//
// MDN: https://developer.mozilla.org/en-US/docs/Web/API/Request/signal
signal: new AbortController().signal
},

// `decompress` indicates whether or not the response body should be decompressed
// automatically. If set to `true` will also remove the 'content-encoding' header
// from the responses objects of all decompressed responses
Expand Down
Loading