Skip to content

Commit

Permalink
[refactor] use Event Target class to implement fetch() progress (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
TechQuery authored Dec 1, 2024
1 parent 963fbab commit ce37db3
Show file tree
Hide file tree
Showing 13 changed files with 1,265 additions and 1,402 deletions.
51 changes: 44 additions & 7 deletions ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Automatic Parsed type:

#### Installation

```shell
```powershell
npm install koajax
```

Expand All @@ -55,15 +55,16 @@ npm install koajax

#### Installation

```shell
npm install koajax jsdom
```powershell
npm install koajax core-js jsdom
```

#### `index.ts`

```javascript
import { polyfill } from 'koajax/source/polyfill';

import { HTTPClient } from 'koajax';
import { polyfill } from 'koajax/source/polyfill'

const origin = 'https://your-target-origin.com';

Expand All @@ -80,7 +81,7 @@ polyfill(origin).then(() => {

#### Execution

```shell
```powershell
npx tsx index.ts
```

Expand Down Expand Up @@ -141,9 +142,44 @@ document.querySelector('input[type="file"]').onchange = async ({
};
```

#### Single HTTP request based on Fetch `duplex` streams

> This experimental feature has [some limitations][7].
```diff
-import { request } from 'koajax';
+import { requestFetch } from 'koajax';

document.querySelector('input[type="file"]').onchange = async ({
target: { files }
}) => {
for (const file of files) {
- const { upload, download, response } = request({
+ const { upload, download, response } = requestFetch({
method: 'POST',
path: '/files',
+ headers: {
+ 'Content-Type': file.type,
+ 'Content-Length': file.size + ''
+ },
- body: file,
+ body: file.stream(),
responseType: 'json'
});

for await (const { loaded } of upload)
console.log(`Upload ${file.name} : ${(loaded / file.size) * 100}%`);

const { body } = await response;

console.log(`Upload ${file.name} : ${body.url}`);
}
};
```

#### Multiple HTTP requests based on `Range` header

```shell
```powershell
npm i native-file-system-adapter # Web standard API polyfill
```

Expand Down Expand Up @@ -176,7 +212,7 @@ document.querySelector('#download').onclick = async () => {

### Global Error fallback

```shell
```powershell
npm install browser-unhandled-rejection # Web standard API polyfill
```

Expand Down Expand Up @@ -226,3 +262,4 @@ document.querySelector('input[type="file"]').onchange = async ({
[4]: https://www.jsdelivr.com/package/npm/koajax
[5]: https://nodei.co/npm/koajax/
[6]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of#Iterating_over_async_generators
[7]: https://developer.chrome.com/docs/capabilities/web-apis/fetch-streaming-requests#restrictions
29 changes: 15 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "koajax",
"version": "3.0.3",
"version": "3.1.0",
"license": "LGPL-3.0",
"author": "[email protected]",
"description": "HTTP Client based on Koa-like middlewares",
Expand All @@ -25,36 +25,37 @@
"main": "dist/index.js",
"module": "dist/index.esm.js",
"dependencies": {
"@swc/helpers": "^0.5.13",
"core-js": "^3.38.1",
"@swc/helpers": "^0.5.15",
"regenerator-runtime": "^0.14.1",
"web-streams-polyfill": "^4.0.0",
"web-utility": "^4.4.1"
"web-utility": "^4.4.2"
},
"peerDependencies": {
"core-js": ">=3",
"jsdom": ">=21"
},
"devDependencies": {
"@parcel/packager-ts": "~2.12.0",
"@parcel/transformer-typescript-types": "~2.12.0",
"@types/core-js": "^2.5.8",
"@parcel/packager-ts": "~2.13.2",
"@parcel/transformer-typescript-types": "~2.13.2",
"@types/jest": "^29.5.14",
"@types/jsdom": "^21.1.7",
"@types/node": "^20.17.1",
"@types/node": "^20.17.9",
"abortcontroller-polyfill": "^1.7.6",
"core-js": "^3.39.0",
"cross-env": "^7.0.3",
"husky": "^9.1.6",
"husky": "^9.1.7",
"jest": "^29.7.0",
"jest-environment-jsdom": "^29.7.0",
"jsdom": "^25.0.1",
"lint-staged": "^15.2.10",
"open-cli": "^8.0.0",
"parcel": "~2.12.0",
"prettier": "^3.3.3",
"parcel": "~2.13.2",
"prettier": "^3.4.1",
"ts-jest": "^29.2.5",
"ts-node": "^10.9.2",
"typedoc": "^0.26.10",
"typedoc-plugin-mdn-links": "^3.3.5",
"typescript": "~5.6.3"
"typedoc": "^0.27.2",
"typedoc-plugin-mdn-links": "^4.0.2",
"typescript": "~5.7.2"
},
"prettier": {
"singleQuote": true,
Expand Down
Loading

0 comments on commit ce37db3

Please sign in to comment.