@spoosh/plugin-progress 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +48 -0
- package/dist/index.d.mts +35 -0
- package/dist/index.d.ts +35 -0
- package/dist/index.js +64 -0
- package/dist/index.mjs +41 -0
- package/package.json +50 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Spoosh
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# @spoosh/plugin-progress
|
|
2
|
+
|
|
3
|
+
Upload and download progress tracking plugin for Spoosh via XHR transport.
|
|
4
|
+
|
|
5
|
+
**[Documentation](https://spoosh.dev/docs/react/plugins/progress)** · **Requirements:** TypeScript >= 5.0 · **Peer Dependencies:** `@spoosh/core`
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @spoosh/plugin-progress
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import { Spoosh } from "@spoosh/core";
|
|
17
|
+
import { progressPlugin } from "@spoosh/plugin-progress";
|
|
18
|
+
|
|
19
|
+
const client = new Spoosh<ApiSchema, Error>("/api").use([progressPlugin()]);
|
|
20
|
+
|
|
21
|
+
// Enable per-request
|
|
22
|
+
useRead((api) => api("files/:id").GET(), { progress: true });
|
|
23
|
+
|
|
24
|
+
// File upload with progress
|
|
25
|
+
const { trigger, progress } = useWrite((api) => api("files").POST, {
|
|
26
|
+
progress: true,
|
|
27
|
+
});
|
|
28
|
+
trigger({ body: form({ file: selectedFile }) });
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Per-Request Options
|
|
32
|
+
|
|
33
|
+
| Option | Type | Description |
|
|
34
|
+
| ---------- | ---------------------------- | ----------------------------------------------------------- |
|
|
35
|
+
| `progress` | `boolean \| ProgressOptions` | Enable progress tracking. Automatically uses XHR transport. |
|
|
36
|
+
|
|
37
|
+
### ProgressOptions
|
|
38
|
+
|
|
39
|
+
| Option | Type | Description |
|
|
40
|
+
| ------------- | -------- | ---------------------------------------------------------------------------- |
|
|
41
|
+
| `totalHeader` | `string` | Response header to read total size from when `Content-Length` is unavailable |
|
|
42
|
+
|
|
43
|
+
## Result
|
|
44
|
+
|
|
45
|
+
| Field | Type | Description |
|
|
46
|
+
| -------- | -------- | -------------------------- |
|
|
47
|
+
| `loaded` | `number` | Bytes transferred so far |
|
|
48
|
+
| `total` | `number` | Total bytes (0 if unknown) |
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { SpooshPlugin } from '@spoosh/core';
|
|
2
|
+
|
|
3
|
+
interface ProgressInfo {
|
|
4
|
+
loaded: number;
|
|
5
|
+
total: number;
|
|
6
|
+
}
|
|
7
|
+
interface ProgressOptions {
|
|
8
|
+
/** Response header name to read total size from when Content-Length is unavailable. */
|
|
9
|
+
totalHeader?: string;
|
|
10
|
+
}
|
|
11
|
+
interface ProgressReadOptions {
|
|
12
|
+
/** Enable progress tracking. Forces XHR transport. */
|
|
13
|
+
progress?: boolean | ProgressOptions;
|
|
14
|
+
}
|
|
15
|
+
interface ProgressWriteOptions {
|
|
16
|
+
/** Enable progress tracking. Forces XHR transport. */
|
|
17
|
+
progress?: boolean | ProgressOptions;
|
|
18
|
+
}
|
|
19
|
+
type ProgressInfiniteReadOptions = ProgressReadOptions;
|
|
20
|
+
interface ProgressReadResult {
|
|
21
|
+
progress?: ProgressInfo;
|
|
22
|
+
}
|
|
23
|
+
interface ProgressWriteResult {
|
|
24
|
+
progress?: ProgressInfo;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
declare function progressPlugin(): SpooshPlugin<{
|
|
28
|
+
readOptions: ProgressReadOptions;
|
|
29
|
+
writeOptions: ProgressWriteOptions;
|
|
30
|
+
infiniteReadOptions: ProgressInfiniteReadOptions;
|
|
31
|
+
readResult: ProgressReadResult;
|
|
32
|
+
writeResult: ProgressWriteResult;
|
|
33
|
+
}>;
|
|
34
|
+
|
|
35
|
+
export { type ProgressInfiniteReadOptions, type ProgressInfo, type ProgressOptions, type ProgressReadOptions, type ProgressReadResult, type ProgressWriteOptions, type ProgressWriteResult, progressPlugin };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { SpooshPlugin } from '@spoosh/core';
|
|
2
|
+
|
|
3
|
+
interface ProgressInfo {
|
|
4
|
+
loaded: number;
|
|
5
|
+
total: number;
|
|
6
|
+
}
|
|
7
|
+
interface ProgressOptions {
|
|
8
|
+
/** Response header name to read total size from when Content-Length is unavailable. */
|
|
9
|
+
totalHeader?: string;
|
|
10
|
+
}
|
|
11
|
+
interface ProgressReadOptions {
|
|
12
|
+
/** Enable progress tracking. Forces XHR transport. */
|
|
13
|
+
progress?: boolean | ProgressOptions;
|
|
14
|
+
}
|
|
15
|
+
interface ProgressWriteOptions {
|
|
16
|
+
/** Enable progress tracking. Forces XHR transport. */
|
|
17
|
+
progress?: boolean | ProgressOptions;
|
|
18
|
+
}
|
|
19
|
+
type ProgressInfiniteReadOptions = ProgressReadOptions;
|
|
20
|
+
interface ProgressReadResult {
|
|
21
|
+
progress?: ProgressInfo;
|
|
22
|
+
}
|
|
23
|
+
interface ProgressWriteResult {
|
|
24
|
+
progress?: ProgressInfo;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
declare function progressPlugin(): SpooshPlugin<{
|
|
28
|
+
readOptions: ProgressReadOptions;
|
|
29
|
+
writeOptions: ProgressWriteOptions;
|
|
30
|
+
infiniteReadOptions: ProgressInfiniteReadOptions;
|
|
31
|
+
readResult: ProgressReadResult;
|
|
32
|
+
writeResult: ProgressWriteResult;
|
|
33
|
+
}>;
|
|
34
|
+
|
|
35
|
+
export { type ProgressInfiniteReadOptions, type ProgressInfo, type ProgressOptions, type ProgressReadOptions, type ProgressReadResult, type ProgressWriteOptions, type ProgressWriteResult, progressPlugin };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
progressPlugin: () => progressPlugin
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(src_exports);
|
|
26
|
+
|
|
27
|
+
// src/plugin.ts
|
|
28
|
+
function progressPlugin() {
|
|
29
|
+
return {
|
|
30
|
+
name: "spoosh:progress",
|
|
31
|
+
operations: ["read", "write", "infiniteRead"],
|
|
32
|
+
middleware: async (context, next) => {
|
|
33
|
+
const pluginOptions = context.pluginOptions;
|
|
34
|
+
if (!pluginOptions?.progress) {
|
|
35
|
+
return next();
|
|
36
|
+
}
|
|
37
|
+
const progressOptions = typeof pluginOptions.progress === "object" ? pluginOptions.progress : {};
|
|
38
|
+
context.requestOptions = {
|
|
39
|
+
...context.requestOptions,
|
|
40
|
+
transport: "xhr",
|
|
41
|
+
transportOptions: {
|
|
42
|
+
onProgress: (event, xhr) => {
|
|
43
|
+
let total = event.total;
|
|
44
|
+
if (!event.lengthComputable && progressOptions.totalHeader) {
|
|
45
|
+
const headerVal = xhr.getResponseHeader(
|
|
46
|
+
progressOptions.totalHeader
|
|
47
|
+
);
|
|
48
|
+
if (headerVal) {
|
|
49
|
+
total = parseInt(headerVal, 10);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
context.stateManager.setMeta(context.queryKey, {
|
|
53
|
+
progress: {
|
|
54
|
+
loaded: event.loaded,
|
|
55
|
+
total
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
return next();
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// src/plugin.ts
|
|
2
|
+
function progressPlugin() {
|
|
3
|
+
return {
|
|
4
|
+
name: "spoosh:progress",
|
|
5
|
+
operations: ["read", "write", "infiniteRead"],
|
|
6
|
+
middleware: async (context, next) => {
|
|
7
|
+
const pluginOptions = context.pluginOptions;
|
|
8
|
+
if (!pluginOptions?.progress) {
|
|
9
|
+
return next();
|
|
10
|
+
}
|
|
11
|
+
const progressOptions = typeof pluginOptions.progress === "object" ? pluginOptions.progress : {};
|
|
12
|
+
context.requestOptions = {
|
|
13
|
+
...context.requestOptions,
|
|
14
|
+
transport: "xhr",
|
|
15
|
+
transportOptions: {
|
|
16
|
+
onProgress: (event, xhr) => {
|
|
17
|
+
let total = event.total;
|
|
18
|
+
if (!event.lengthComputable && progressOptions.totalHeader) {
|
|
19
|
+
const headerVal = xhr.getResponseHeader(
|
|
20
|
+
progressOptions.totalHeader
|
|
21
|
+
);
|
|
22
|
+
if (headerVal) {
|
|
23
|
+
total = parseInt(headerVal, 10);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
context.stateManager.setMeta(context.queryKey, {
|
|
27
|
+
progress: {
|
|
28
|
+
loaded: event.loaded,
|
|
29
|
+
total
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
return next();
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
export {
|
|
40
|
+
progressPlugin
|
|
41
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@spoosh/plugin-progress",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Upload and download progress tracking plugin for Spoosh via XHR transport",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/spooshdev/spoosh.git",
|
|
9
|
+
"directory": "packages/plugin-progress"
|
|
10
|
+
},
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/spooshdev/spoosh/issues"
|
|
13
|
+
},
|
|
14
|
+
"homepage": "https://spoosh.dev/docs/react/plugins/progress",
|
|
15
|
+
"publishConfig": {
|
|
16
|
+
"access": "public"
|
|
17
|
+
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"spoosh",
|
|
20
|
+
"plugin",
|
|
21
|
+
"progress",
|
|
22
|
+
"upload",
|
|
23
|
+
"download",
|
|
24
|
+
"xhr"
|
|
25
|
+
],
|
|
26
|
+
"files": [
|
|
27
|
+
"dist"
|
|
28
|
+
],
|
|
29
|
+
"exports": {
|
|
30
|
+
".": {
|
|
31
|
+
"types": "./dist/index.d.ts",
|
|
32
|
+
"import": "./dist/index.mjs",
|
|
33
|
+
"require": "./dist/index.js"
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
"peerDependencies": {
|
|
37
|
+
"@spoosh/core": ">=0.10.0"
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"@spoosh/core": "0.10.0",
|
|
41
|
+
"@spoosh/test-utils": "0.1.5"
|
|
42
|
+
},
|
|
43
|
+
"scripts": {
|
|
44
|
+
"dev": "tsup --watch",
|
|
45
|
+
"build": "tsup",
|
|
46
|
+
"typecheck": "tsc --noEmit",
|
|
47
|
+
"lint": "eslint src --max-warnings 0",
|
|
48
|
+
"format": "prettier --write 'src/**/*.ts'"
|
|
49
|
+
}
|
|
50
|
+
}
|