@wcstack/upload 1.9.1 → 1.10.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/README.md CHANGED
@@ -1,283 +1,283 @@
1
- # @wcstack/upload
2
-
3
- `@wcstack/upload` is a declarative file upload component for the wcstack ecosystem.
4
-
5
- It is not a visible UI widget.
6
- It is a hidden **upload I/O node** that turns file upload into bindable state.
7
-
8
- With `@wcstack/state`, `<wcs-upload>` exposes a small async state surface:
9
-
10
- - input / command surface: `files`, `trigger`
11
- - configuration surface: `url`, `method`, `field-name`, `accept`, `max-size`, `manual`, `multiple`
12
- - output state surface: `value`, `loading`, `progress`, `error`, `status`
13
-
14
- This means file upload can be expressed as state transitions and DOM bindings instead of ad-hoc `XMLHttpRequest` glue code.
15
-
16
- `@wcstack/upload` follows the same HAWC-style split as other wcstack I/O packages:
17
-
18
- - **Core** (`UploadCore`) handles XHR upload, progress tracking, abort, and async state
19
- - **Shell** (`<wcs-upload>`) exposes that state as a custom element and `wc-bindable` surface
20
- - frameworks and binding systems consume it through `wc-bindable-protocol`
21
-
22
- ## Why this exists
23
-
24
- File upload usually spreads across too many places:
25
-
26
- - file input handling
27
- - `FormData` creation
28
- - progress events
29
- - loading flags
30
- - error handling
31
- - abort on disconnect
32
-
33
- `@wcstack/upload` moves that logic into a reusable component and exposes the result as bindable state.
34
-
35
- ## Install
36
-
37
- ```bash
38
- npm install @wcstack/upload
39
- ```
40
-
41
- ## Quick Start
42
-
43
- ### 1. Auto upload when files are assigned
44
-
45
- ```html
46
- <script type="module" src="https://esm.run/@wcstack/upload/auto"></script>
47
-
48
- <wcs-upload id="avatar-upload" url="/api/upload"></wcs-upload>
49
- <input id="avatar-input" type="file" accept="image/*">
50
-
51
- <script type="module">
52
- const upload = document.getElementById("avatar-upload");
53
- const input = document.getElementById("avatar-input");
54
-
55
- input.addEventListener("change", () => {
56
- upload.files = input.files;
57
- });
58
-
59
- upload.addEventListener("wcs-upload:progress", (event) => {
60
- console.log("progress", event.detail);
61
- });
62
-
63
- upload.addEventListener("wcs-upload:response", (event) => {
64
- console.log("uploaded", event.detail.value);
65
- });
66
- </script>
67
- ```
68
-
69
- Default behavior:
70
-
71
- - assigning `files` starts upload immediately
72
- - files are sent as `multipart/form-data`
73
- - request method defaults to `POST`
74
- - field name defaults to `file`
75
-
76
- ### 2. Manual upload with `trigger`
77
-
78
- Use `manual` when you want to choose files first and upload later.
79
-
80
- ```html
81
- <script type="module" src="https://esm.run/@wcstack/upload/auto"></script>
82
-
83
- <wcs-upload id="resume-upload" url="/api/upload" manual></wcs-upload>
84
-
85
- <input id="resume-input" type="file">
86
- <button id="resume-button">Upload</button>
87
-
88
- <script type="module">
89
- const upload = document.getElementById("resume-upload");
90
- const input = document.getElementById("resume-input");
91
- const button = document.getElementById("resume-button");
92
-
93
- input.addEventListener("change", () => {
94
- upload.files = input.files;
95
- });
96
-
97
- button.addEventListener("click", () => {
98
- upload.trigger = true;
99
- });
100
- </script>
101
- ```
102
-
103
- `trigger` is a one-way command surface:
104
-
105
- - writing `true` starts `upload()`
106
- - after completion it resets itself to `false`
107
- - that reset dispatches `wcs-upload:trigger-changed`
108
-
109
- ### 3. Declarative trigger target
110
-
111
- When auto trigger is enabled, a clickable element can point at a `<wcs-upload>` by id.
112
-
113
- ```html
114
- <script type="module" src="https://esm.run/@wcstack/upload/auto"></script>
115
-
116
- <wcs-upload id="photo-upload" url="/api/upload" manual></wcs-upload>
117
- <input id="photo-input" type="file">
118
- <button data-uploadtarget="photo-upload">Upload</button>
119
-
120
- <script type="module">
121
- const upload = document.getElementById("photo-upload");
122
- const input = document.getElementById("photo-input");
123
-
124
- input.addEventListener("change", () => {
125
- upload.files = input.files;
126
- });
127
- </script>
128
- ```
129
-
130
- By default, the trigger attribute is `data-uploadtarget`.
131
-
132
- ### 4. With `@wcstack/state`
133
-
134
- ```html
135
- <script type="module" src="https://esm.run/@wcstack/state/auto"></script>
136
- <script type="module" src="https://esm.run/@wcstack/upload/auto"></script>
137
-
138
- <wcs-state>
139
- <script type="module">
140
- export default {
141
- uploadResult: null,
142
- uploadLoading: false,
143
- uploadProgress: 0,
144
- uploadError: null,
145
- };
146
- </script>
147
-
148
- <wcs-upload
149
- id="state-upload"
150
- url="/api/upload"
151
- manual
152
- data-wcs="
153
- value: uploadResult;
154
- loading: uploadLoading;
155
- progress: uploadProgress;
156
- error: uploadError
157
- ">
158
- </wcs-upload>
159
-
160
- <input id="state-upload-input" type="file">
161
- <button data-uploadtarget="state-upload">Upload</button>
162
-
163
- <progress max="100" data-wcs="value: uploadProgress"></progress>
164
- <p data-wcs="textContent: uploadLoading"></p>
165
-
166
- <script type="module">
167
- const upload = document.getElementById("state-upload");
168
- const input = document.getElementById("state-upload-input");
169
-
170
- input.addEventListener("change", () => {
171
- upload.files = input.files;
172
- });
173
- </script>
174
- </wcs-state>
175
- ```
176
-
177
- In this setup, upload becomes a bindable async node:
178
-
179
- - the element performs the request
180
- - async state flows back as `value`, `loading`, `progress`, `error`, `status`
181
- - the UI binds to those paths declaratively
182
-
183
- ## Public API
184
-
185
- ### Element attributes and properties
186
-
187
- | Name | Type | Default | Description |
188
- |---|---|---|---|
189
- | `url` | `string` | `""` | Upload endpoint |
190
- | `method` | `string` | `"POST"` | HTTP method |
191
- | `field-name` | `string` | `"file"` | FormData field name |
192
- | `multiple` | `boolean` | `false` | Marks the element as multi-file capable |
193
- | `max-size` | `number` | `Infinity` | Maximum allowed file size in bytes |
194
- | `accept` | `string` | `""` | Accepted MIME types or file extensions |
195
- | `manual` | `boolean` | `false` | Disables auto upload on `files` assignment |
196
- | `files` | `FileList \| File[] \| null` | `null` | Files to upload |
197
- | `trigger` | `boolean` | `false` | Write-only command surface for manual upload |
198
- | `value` | `any` | `null` | Parsed response body or response text |
199
- | `loading` | `boolean` | `false` | Upload state flag |
200
- | `progress` | `number` | `0` | Upload progress from `0` to `100` |
201
- | `error` | `any` | `null` | Validation, network, or response error |
202
- | `status` | `number` | `0` | HTTP response status |
203
- | `promise` | `Promise<any>` | resolved `null` | Current upload promise |
204
-
205
- ### Methods
206
-
207
- #### `upload()`
208
-
209
- Starts upload with the current `files` and returns a promise.
210
- Returns `null` when there are no files or validation fails.
211
-
212
- #### `abort()`
213
-
214
- Aborts the current request.
215
-
216
- ## Events
217
-
218
- | Event | `detail` | Description |
219
- |---|---|---|
220
- | `wcs-upload:files-changed` | `FileList \| File[] \| null` | Fired when `files` changes |
221
- | `wcs-upload:trigger-changed` | `boolean` | Fired when `trigger` resets to `false` |
222
- | `wcs-upload:loading-changed` | `boolean` | Fired when loading state changes |
223
- | `wcs-upload:progress` | `number` | Fired on upload progress updates |
224
- | `wcs-upload:error` | error object | Fired on validation, network, or HTTP error |
225
- | `wcs-upload:response` | `{ value, status }` | Fired on successful HTTP response |
226
-
227
- ## Validation
228
-
229
- `<wcs-upload>` validates files before sending:
230
-
231
- - `max-size` rejects files larger than the configured byte size
232
- - `accept` supports MIME types like `image/*`, exact MIME types like `application/pdf`, and extensions like `.pdf`
233
-
234
- Validation failure dispatches `wcs-upload:error` and the request is not started.
235
-
236
- ## wc-bindable surface
237
-
238
- `<wcs-upload>` exposes a `wcBindable` definition with these bindable properties:
239
-
240
- - `value`
241
- - `loading`
242
- - `progress`
243
- - `error`
244
- - `status`
245
- - `trigger`
246
- - `files`
247
-
248
- This makes the element consumable from wc-bindable-aware systems, including `@wcstack/state`.
249
-
250
- ## Headless API
251
-
252
- If you do not need the custom element shell, you can use `UploadCore` directly:
253
-
254
- ```ts
255
- import { UploadCore } from "@wcstack/upload";
256
-
257
- const core = new UploadCore();
258
- const result = await core.upload("/api/upload", files, {
259
- method: "PUT",
260
- fieldName: "attachment",
261
- headers: {
262
- Authorization: "Bearer token",
263
- },
264
- });
265
- ```
266
-
267
- `UploadCore` exposes the same async state as properties and dispatches the same events.
268
-
269
- ## Manual bootstrap
270
-
271
- ```ts
272
- import { bootstrapUpload } from "@wcstack/upload";
273
-
274
- bootstrapUpload({
275
- autoTrigger: true,
276
- triggerAttribute: "data-uploadtarget",
277
- tagNames: {
278
- upload: "wcs-upload",
279
- },
280
- });
281
- ```
282
-
1
+ # @wcstack/upload
2
+
3
+ `@wcstack/upload` is a declarative file upload component for the wcstack ecosystem.
4
+
5
+ It is not a visible UI widget.
6
+ It is a hidden **upload I/O node** that turns file upload into bindable state.
7
+
8
+ With `@wcstack/state`, `<wcs-upload>` exposes a small async state surface:
9
+
10
+ - input / command surface: `files`, `trigger`
11
+ - configuration surface: `url`, `method`, `field-name`, `accept`, `max-size`, `manual`, `multiple`
12
+ - output state surface: `value`, `loading`, `progress`, `error`, `status`
13
+
14
+ This means file upload can be expressed as state transitions and DOM bindings instead of ad-hoc `XMLHttpRequest` glue code.
15
+
16
+ `@wcstack/upload` follows the same HAWC-style split as other wcstack I/O packages:
17
+
18
+ - **Core** (`UploadCore`) handles XHR upload, progress tracking, abort, and async state
19
+ - **Shell** (`<wcs-upload>`) exposes that state as a custom element and `wc-bindable` surface
20
+ - frameworks and binding systems consume it through `wc-bindable-protocol`
21
+
22
+ ## Why this exists
23
+
24
+ File upload usually spreads across too many places:
25
+
26
+ - file input handling
27
+ - `FormData` creation
28
+ - progress events
29
+ - loading flags
30
+ - error handling
31
+ - abort on disconnect
32
+
33
+ `@wcstack/upload` moves that logic into a reusable component and exposes the result as bindable state.
34
+
35
+ ## Install
36
+
37
+ ```bash
38
+ npm install @wcstack/upload
39
+ ```
40
+
41
+ ## Quick Start
42
+
43
+ ### 1. Auto upload when files are assigned
44
+
45
+ ```html
46
+ <script type="module" src="https://esm.run/@wcstack/upload/auto"></script>
47
+
48
+ <wcs-upload id="avatar-upload" url="/api/upload"></wcs-upload>
49
+ <input id="avatar-input" type="file" accept="image/*">
50
+
51
+ <script type="module">
52
+ const upload = document.getElementById("avatar-upload");
53
+ const input = document.getElementById("avatar-input");
54
+
55
+ input.addEventListener("change", () => {
56
+ upload.files = input.files;
57
+ });
58
+
59
+ upload.addEventListener("wcs-upload:progress", (event) => {
60
+ console.log("progress", event.detail);
61
+ });
62
+
63
+ upload.addEventListener("wcs-upload:response", (event) => {
64
+ console.log("uploaded", event.detail.value);
65
+ });
66
+ </script>
67
+ ```
68
+
69
+ Default behavior:
70
+
71
+ - assigning `files` starts upload immediately
72
+ - files are sent as `multipart/form-data`
73
+ - request method defaults to `POST`
74
+ - field name defaults to `file`
75
+
76
+ ### 2. Manual upload with `trigger`
77
+
78
+ Use `manual` when you want to choose files first and upload later.
79
+
80
+ ```html
81
+ <script type="module" src="https://esm.run/@wcstack/upload/auto"></script>
82
+
83
+ <wcs-upload id="resume-upload" url="/api/upload" manual></wcs-upload>
84
+
85
+ <input id="resume-input" type="file">
86
+ <button id="resume-button">Upload</button>
87
+
88
+ <script type="module">
89
+ const upload = document.getElementById("resume-upload");
90
+ const input = document.getElementById("resume-input");
91
+ const button = document.getElementById("resume-button");
92
+
93
+ input.addEventListener("change", () => {
94
+ upload.files = input.files;
95
+ });
96
+
97
+ button.addEventListener("click", () => {
98
+ upload.trigger = true;
99
+ });
100
+ </script>
101
+ ```
102
+
103
+ `trigger` is a one-way command surface:
104
+
105
+ - writing `true` starts `upload()`
106
+ - after completion it resets itself to `false`
107
+ - that reset dispatches `wcs-upload:trigger-changed`
108
+
109
+ ### 3. Declarative trigger target
110
+
111
+ When auto trigger is enabled, a clickable element can point at a `<wcs-upload>` by id.
112
+
113
+ ```html
114
+ <script type="module" src="https://esm.run/@wcstack/upload/auto"></script>
115
+
116
+ <wcs-upload id="photo-upload" url="/api/upload" manual></wcs-upload>
117
+ <input id="photo-input" type="file">
118
+ <button data-uploadtarget="photo-upload">Upload</button>
119
+
120
+ <script type="module">
121
+ const upload = document.getElementById("photo-upload");
122
+ const input = document.getElementById("photo-input");
123
+
124
+ input.addEventListener("change", () => {
125
+ upload.files = input.files;
126
+ });
127
+ </script>
128
+ ```
129
+
130
+ By default, the trigger attribute is `data-uploadtarget`.
131
+
132
+ ### 4. With `@wcstack/state`
133
+
134
+ ```html
135
+ <script type="module" src="https://esm.run/@wcstack/state/auto"></script>
136
+ <script type="module" src="https://esm.run/@wcstack/upload/auto"></script>
137
+
138
+ <wcs-state>
139
+ <script type="module">
140
+ export default {
141
+ uploadResult: null,
142
+ uploadLoading: false,
143
+ uploadProgress: 0,
144
+ uploadError: null,
145
+ };
146
+ </script>
147
+
148
+ <wcs-upload
149
+ id="state-upload"
150
+ url="/api/upload"
151
+ manual
152
+ data-wcs="
153
+ value: uploadResult;
154
+ loading: uploadLoading;
155
+ progress: uploadProgress;
156
+ error: uploadError
157
+ ">
158
+ </wcs-upload>
159
+
160
+ <input id="state-upload-input" type="file">
161
+ <button data-uploadtarget="state-upload">Upload</button>
162
+
163
+ <progress max="100" data-wcs="value: uploadProgress"></progress>
164
+ <p data-wcs="textContent: uploadLoading"></p>
165
+
166
+ <script type="module">
167
+ const upload = document.getElementById("state-upload");
168
+ const input = document.getElementById("state-upload-input");
169
+
170
+ input.addEventListener("change", () => {
171
+ upload.files = input.files;
172
+ });
173
+ </script>
174
+ </wcs-state>
175
+ ```
176
+
177
+ In this setup, upload becomes a bindable async node:
178
+
179
+ - the element performs the request
180
+ - async state flows back as `value`, `loading`, `progress`, `error`, `status`
181
+ - the UI binds to those paths declaratively
182
+
183
+ ## Public API
184
+
185
+ ### Element attributes and properties
186
+
187
+ | Name | Type | Default | Description |
188
+ |---|---|---|---|
189
+ | `url` | `string` | `""` | Upload endpoint |
190
+ | `method` | `string` | `"POST"` | HTTP method |
191
+ | `field-name` | `string` | `"file"` | FormData field name |
192
+ | `multiple` | `boolean` | `false` | Marks the element as multi-file capable |
193
+ | `max-size` | `number` | `Infinity` | Maximum allowed file size in bytes |
194
+ | `accept` | `string` | `""` | Accepted MIME types or file extensions |
195
+ | `manual` | `boolean` | `false` | Disables auto upload on `files` assignment |
196
+ | `files` | `FileList \| File[] \| null` | `null` | Files to upload |
197
+ | `trigger` | `boolean` | `false` | Write-only command surface for manual upload |
198
+ | `value` | `any` | `null` | Parsed response body or response text |
199
+ | `loading` | `boolean` | `false` | Upload state flag |
200
+ | `progress` | `number` | `0` | Upload progress from `0` to `100` |
201
+ | `error` | `any` | `null` | Validation, network, or response error |
202
+ | `status` | `number` | `0` | HTTP response status |
203
+ | `promise` | `Promise<any>` | resolved `null` | Current upload promise |
204
+
205
+ ### Methods
206
+
207
+ #### `upload()`
208
+
209
+ Starts upload with the current `files` and returns a promise.
210
+ Returns `null` when there are no files or validation fails.
211
+
212
+ #### `abort()`
213
+
214
+ Aborts the current request.
215
+
216
+ ## Events
217
+
218
+ | Event | `detail` | Description |
219
+ |---|---|---|
220
+ | `wcs-upload:files-changed` | `FileList \| File[] \| null` | Fired when `files` changes |
221
+ | `wcs-upload:trigger-changed` | `boolean` | Fired when `trigger` resets to `false` |
222
+ | `wcs-upload:loading-changed` | `boolean` | Fired when loading state changes |
223
+ | `wcs-upload:progress` | `number` | Fired on upload progress updates |
224
+ | `wcs-upload:error` | error object | Fired on validation, network, or HTTP error |
225
+ | `wcs-upload:response` | `{ value, status }` | Fired on successful HTTP response |
226
+
227
+ ## Validation
228
+
229
+ `<wcs-upload>` validates files before sending:
230
+
231
+ - `max-size` rejects files larger than the configured byte size
232
+ - `accept` supports MIME types like `image/*`, exact MIME types like `application/pdf`, and extensions like `.pdf`
233
+
234
+ Validation failure dispatches `wcs-upload:error` and the request is not started.
235
+
236
+ ## wc-bindable surface
237
+
238
+ `<wcs-upload>` exposes a `wcBindable` definition with these bindable properties:
239
+
240
+ - `value`
241
+ - `loading`
242
+ - `progress`
243
+ - `error`
244
+ - `status`
245
+ - `trigger`
246
+ - `files`
247
+
248
+ This makes the element consumable from wc-bindable-aware systems, including `@wcstack/state`.
249
+
250
+ ## Headless API
251
+
252
+ If you do not need the custom element shell, you can use `UploadCore` directly:
253
+
254
+ ```ts
255
+ import { UploadCore } from "@wcstack/upload";
256
+
257
+ const core = new UploadCore();
258
+ const result = await core.upload("/api/upload", files, {
259
+ method: "PUT",
260
+ fieldName: "attachment",
261
+ headers: {
262
+ Authorization: "Bearer token",
263
+ },
264
+ });
265
+ ```
266
+
267
+ `UploadCore` exposes the same async state as properties and dispatches the same events.
268
+
269
+ ## Manual bootstrap
270
+
271
+ ```ts
272
+ import { bootstrapUpload } from "@wcstack/upload";
273
+
274
+ bootstrapUpload({
275
+ autoTrigger: true,
276
+ triggerAttribute: "data-uploadtarget",
277
+ tagNames: {
278
+ upload: "wcs-upload",
279
+ },
280
+ });
281
+ ```
282
+
283
283
  Use this when you want to customize the tag name or trigger attribute instead of relying on `@wcstack/upload/auto`.
package/dist/auto.js CHANGED
@@ -1,3 +1,3 @@
1
- import { bootstrapUpload } from "./index.esm.js";
2
-
3
- bootstrapUpload();
1
+ import { bootstrapUpload } from "./index.esm.js";
2
+
3
+ bootstrapUpload();
package/dist/auto.min.js CHANGED
@@ -1,3 +1,3 @@
1
- import { bootstrapUpload } from "./index.esm.min.js";
2
-
3
- bootstrapUpload();
1
+ import { bootstrapUpload } from "./index.esm.min.js";
2
+
3
+ bootstrapUpload();