use-tus 0.8.1 → 0.8.3

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
@@ -13,29 +13,25 @@
13
13
  </p>
14
14
 
15
15
  ## Features
16
- - Resumable file uploads on react.
16
+ - Resumable file uploads in React.
17
+ - Improved file upload management within the React component lifecycle.
17
18
  - Lightweight and simple interface hooks.
18
- - Managing the [Upload](https://github.com/tus/tus-js-client/blob/master/docs/api.md#tusuploadfile-options) by using context.
19
+ - Manage [Upload](https://github.com/tus/tus-js-client/blob/master/docs/api.md#tusuploadfile-options) instances via context.
19
20
  - TypeScript support.
20
21
 
21
22
  ## Demo
22
- You can try the [use-tus demo](https://kqito.github.io/use-tus/?path=/story/usetus--basic).
23
+ Try out the [use-tus demo](https://kqito.github.io/use-tus/?path=/story/usetus--basic).
23
24
 
24
25
 
25
26
  ## Installation
26
- You can install the package from npm.
27
- ```sh
28
- npm install use-tus tus-js-client
29
- ```
30
-
31
- or
27
+ Install the package using your package manager of choice.
32
28
 
33
29
  ```sh
34
- yarn add use-tus tus-js-client
30
+ npm install use-tus tus-js-client
35
31
  ```
36
32
 
37
33
  ## Usage
38
- We can use `useTus` as following.
34
+ Below is an example of how to use `useTus`.
39
35
 
40
36
  ```tsx
41
37
  import { useTus } from 'use-tus'
@@ -44,38 +40,26 @@ const Uploader = () => {
44
40
  const { upload, setUpload, isSuccess, error, remove } = useTus();
45
41
 
46
42
  const handleSetUpload = useCallback((event: ChangeEvent<HTMLInputElement>) => {
47
- const file = event.target.files.item(0);
48
-
49
- if (!file) {
50
- return;
51
- }
52
-
53
- setUpload(file, {
54
- endpoint: 'https://tusd.tusdemo.net/files/',
55
- metadata: {
56
- filename: file.name,
57
- filetype: file.type,
58
- },
59
- });
60
- },
61
- [setUpload]
62
- );
43
+ const file = event.target.files.item(0);
44
+ if (!file) return;
45
+
46
+ setUpload(file, {
47
+ endpoint: 'https://tusd.tusdemo.net/files/',
48
+ metadata: {
49
+ filename: file.name,
50
+ filetype: file.type,
51
+ },
52
+ });
53
+ }, [setUpload]);
63
54
 
64
55
  const handleStart = useCallback(() => {
65
- if (!upload) {
66
- return;
67
- }
68
-
69
- // Start to upload the file.
70
- upload.start();
56
+ if (upload) upload.start();
71
57
  }, [upload]);
72
58
 
73
59
  return (
74
60
  <div>
75
61
  <input type="file" onChange={handleSetUpload} />
76
- <button type="button" onClick={handleStart}>
77
- Upload
78
- </button>
62
+ <button type="button" onClick={handleStart}>Upload</button>
79
63
  </div>
80
64
  );
81
65
  };
@@ -104,49 +88,45 @@ const { upload, setUpload, isSuccess, isAborted, isUploading, error, remove } =
104
88
  - `Upload` (type: `Upload | undefined`) (default: undefined)
105
89
  - Option to specify customized own Upload class with this hooks.
106
90
 
107
- #### About `uploadOptions`
108
- This option extends the UploadOptions provided by `tus-js-client`, but it has been extended so that every callback can receive the upload instance as the final argument.
109
-
110
- For detail type information of `UploadOptions`, please see [here](https://github.com/tus/tus-js-client/blob/master/lib/index.d.ts#L22).
91
+ #### `uploadOptions`
92
+ This option extends the `UploadOptions` provided by `tus-js-client`, allowing every callback to receive the upload instance as the final argument. For detailed type information on `UploadOptions`, see [here](https://github.com/tus/tus-js-client/blob/master/lib/index.d.ts#L22).
111
93
 
112
- e.g.
94
+ Example:
113
95
 
114
96
  ```ts
115
- setUplaod(file, {
97
+ setUpload(file, {
116
98
  onSuccess: (upload) => {
117
99
  console.log(upload.url)
118
100
  },
119
101
  onError: (error, upload) => {
120
102
  console.log(error)
121
-
122
103
  setTimeout(() => {
123
104
  upload.start()
124
105
  }, 1000)
125
106
  }
126
- })
107
+ });
127
108
  ```
128
109
 
129
- ### Returns
110
+
111
+ #### Returns
130
112
  - `upload` (type: `tus.Upload | undefined`)
131
- - Object to be used when performing Resumable file upload.
132
- - This value is undefined unless the `setUpload` function called.
133
- - For detail usage, please see [here](https://github.com/tus/tus-js-client#example)
113
+ - Used for resumable file uploads. Undefined unless `setUpload` is called.
114
+ - For detailed usage, see [here](https://github.com/tus/tus-js-client#example).
134
115
 
135
116
  - `setUpload` (type: `(file: tus.Upload['file'], options?: TusHooksUploadFnOptions) => void`)
136
- - Function to create an `Upload`.
137
- - The property specified in `uploadOptions` will be overwritten if property of `options` are speicified.
117
+ - Function to create an `Upload`. `uploadOptions` properties are overwritten if `options` is specified.
138
118
 
139
119
  - `isSuccess` (type: `boolean`)
140
- - Whether the upload was successful or not.
120
+ - Indicates if the upload was successful.
141
121
 
142
122
  - `isAborted` (type: `boolean`)
143
- - Whether the upload was aborted or not.
123
+ - Indicates if the upload was aborted.
144
124
 
145
125
  - `isUploading` (type: `boolean`)
146
- - Indicates if an uploading is in progress or not.
126
+ - Indicates if an upload is in progress.
147
127
 
148
128
  - `error` (type: `Error | undefined`)
149
- - Error when upload fails.
129
+ - Error when the upload fails.
150
130
 
151
131
  - `remove` (type: `() => void`)
152
132
  - Function to reset states.
@@ -154,61 +134,52 @@ setUplaod(file, {
154
134
  ### `useTusStore` hooks
155
135
 
156
136
  ```tsx
157
- const { upload, setUpload, isSuccess, isAborted, isUploading, error, remove } = useTusStore(cacheKey, { autoAbort, autoStart, uploadOptions, Upload });
137
+ const { upload, setUpload, isSuccess, isAborted, isUploading, error, remove } = useTusStore(cacheKey, { autoAbort, autoStart, uploadOptions, Upload });
158
138
  ```
159
139
 
160
- `useTusStore` is a hooks that creates an object for resumable file upload and stores it in a context.
140
+ `useTusStore` creates an object for resumable file uploads and stores it in a context. This is useful for handling uploads across components or pages.
161
141
 
162
- This hooks is useful when you want to handle uploads across pages or components.
142
+ > [!NOTE]
143
+ > `useTusStore` requires `TusClientProvider` as a parent or higher element.
163
144
 
164
- **Note that using `useTusStore` hooks, the `TusClientProvider` must be specified as the parent or higher element.**
165
-
166
- ### Arguments
145
+ #### Arguments
167
146
  - `cacheKey` (type: `string`)
168
- - Specify the key associated with the `Upload` object is created by `setUpload` function.
147
+ - Key associated with the `Upload` object created by `setUpload`.
169
148
 
170
- - `autoAbort` (type: `boolean | undefined`) (default: true)
171
- - Whether or not to automatically abort uploads when useTusStore hooks is unmounted.
149
+ - `autoAbort` (type: `boolean | undefined`, default: `true`)
150
+ - Automatically abort uploads when `useTusStore` is unmounted.
172
151
 
173
- - `autoStart` (type: `boolean | undefined`) (default: false)
174
- - Whether or not to start upload the file after `setUpload` function.
152
+ - `autoStart` (type: `boolean | undefined`, default: `false`)
153
+ - Automatically start the upload after calling the `setUpload` function.
175
154
 
176
- - `uploadOptions` (type: `TusHooksUploadFnOptions | undefined`) (default: undefined)
177
- - Option to used by upload object that generated by that hooks.
178
- - For detail type information of `TusHooksUploadFnOptions`, please see [here](https://github.com/tus/tus-js-client/blob/master/lib/index.d.ts#L22).
155
+ - `uploadOptions` (type: `TusHooksUploadFnOptions | undefined`, default: `undefined`)
156
+ - Set options to be used by the upload object generated by this hook.
179
157
 
180
- - `Upload` (type: `Upload | undefined`) (default: undefined)
181
- - Option to specify customized own Upload class with this hooks.
158
+ - `Upload` (type: `Upload | undefined`, default: `undefined`)
159
+ - Specify a customized `Upload` class with this hook.
182
160
 
183
- ### Returns
161
+ #### Returns
184
162
  - `upload` (type: `tus.Upload | undefined`)
185
- - Object to be used when performing Resumable file upload.
186
- - This value is undefined unless the `setUpload` function called.
163
+ - Used for resumable file uploads. Undefined unless `setUpload` is called.
164
+ - Corresponds to the `Upload` associated with `cacheKey` in `TusClientProvider`.
187
165
 
188
- ### Returns
189
- - `upload` (type: `tus.Upload | undefined`)
190
- - Object to be used when performing Resumable file upload.
191
- - This value of the `Upload` associated with the cacheKey in the TusClientProvider. If not present, undefined.
192
- - For detail usage, please see [here](https://github.com/tus/tus-js-client#example)
193
-
194
- - `setUpload` (type: `(file: tus.Upload['file'], options?: tus.Upload['options']) => void`)
195
- - Function to create an `Upload`.
196
- - The property specified in `uploadOptions` will be overwritten if property of `options` are speicified.
166
+ - `setUpload` (type: `(file: tus.Upload['file'], options?: TusHooksUploadFnOptions) => void`)
167
+ - Function to create an `Upload`. `uploadOptions` properties are overwritten if `options` is specified.
197
168
 
198
169
  - `isSuccess` (type: `boolean`)
199
- - Whether the upload was successful or not.
170
+ - Indicates if the upload was successful.
200
171
 
201
172
  - `isAborted` (type: `boolean`)
202
- - Whether the upload was aborted or not.
173
+ - Indicates if the upload was aborted.
203
174
 
204
175
  - `isUploading` (type: `boolean`)
205
- - Indicates if an uploading is in progress or not.
176
+ - Indicates if an upload is in progress.
206
177
 
207
178
  - `error` (type: `Error | undefined`)
208
- - Error when upload fails.
179
+ - Error when the upload fails.
209
180
 
210
181
  - `remove` (type: `() => void`)
211
- - Function to delete the `Upload` associated with cacheKey.
182
+ - Function to delete the `Upload` associated with `cacheKey`.
212
183
 
213
184
  ### `TusClientProvider`
214
185
 
@@ -220,34 +191,35 @@ This hooks is useful when you want to handle uploads across pages or components.
220
191
  )
221
192
  ```
222
193
 
223
- `TusClientProvider` is the provider that stores the `Upload` with `useTusStore` hooks.
194
+ `TusClientProvider` stores `Upload` objects created with `useTusStore`.
224
195
 
225
- ### Props
196
+ #### Props
226
197
  - `defaultOptions` (type: `(file: tus.Upload['file']) => TusHooksUploadFnOptions | undefined`)
227
- - An object containing the default options used when creating a new upload. [detail](https://github.com/tus/tus-js-client/blob/master/docs/api.md#tusdefaultoptions)
198
+ - Default options object used when creating new uploads. For more details, see [here](https://github.com/tus/tus-js-client/blob/master/docs/api.md#tusdefaultoptions).
228
199
 
229
200
  ### `useTusClient`
230
201
 
231
202
  ```tsx
232
203
  const { state, removeUpload, reset } = useTusClient();
233
204
  ```
234
- `useTusClient` is a hooks that can be used to retrieve and reset the state of a `TusClientProvider`.
235
205
 
236
- ### Returns
206
+ `useTusClient` retrieves and resets the state of `TusClientProvider`.
207
+
208
+ #### Returns
237
209
  - `state` (type: `{ [cacheKey: string]: UploadState | undefined }`)
238
- - Upload information associated with cacheKey
210
+ - Upload information associated with `cacheKey`.
239
211
 
240
212
  - `removeUpload` (type: `(cacheKey: string) => void`)
241
- - Remove the upload instance associated with the specified cacheKey.
213
+ - Remove the upload instance associated with `cacheKey`.
242
214
 
243
215
  - `reset` (type: `() => void`)
244
- - Initialize the value of TusClientProvider
216
+ - Initialize the value of `TusClientProvider`.
245
217
 
246
218
  ## Examples
247
- The following are some example of how to use `use-tus`.
219
+ Here are some examples of how to use `use-tus`.
248
220
 
249
221
  ### Uploading a file
250
- The setUpload and `upload.start` functions can be used to perform resumable file uploads.
222
+ Use `setUpload` and `upload.start` functions to perform resumable file uploads.
251
223
 
252
224
  ```tsx
253
225
  import { useTus } from 'use-tus'
@@ -256,44 +228,33 @@ const Uploader = () => {
256
228
  const { upload, setUpload } = useTus();
257
229
 
258
230
  const handleSetUpload = useCallback((event: ChangeEvent<HTMLInputElement>) => {
259
- const file = event.target.files.item(0);
260
-
261
- if (!file) {
262
- return;
263
- }
264
-
265
- setUpload(file, {
266
- endpoint: 'https://tusd.tusdemo.net/files/',
267
- metadata: {
268
- filename: file.name,
269
- filetype: file.type,
270
- },
271
- });
272
- },
273
- [setUpload]
274
- );
231
+ const file = event.target.files.item(0);
232
+ if (!file) return;
233
+
234
+ setUpload(file, {
235
+ endpoint: 'https://tusd.tusdemo.net/files/',
236
+ metadata: {
237
+ filename: file.name,
238
+ filetype: file.type,
239
+ },
240
+ });
241
+ }, [setUpload]);
275
242
 
276
243
  const handleStart = useCallback(() => {
277
- if (!upload) {
278
- return;
279
- }
280
-
281
- // Start upload the file.
282
- upload.start();
244
+ if (upload) upload.start();
283
245
  }, [upload]);
284
246
 
285
247
  return (
286
248
  <div>
287
249
  <input type="file" onChange={handleSetUpload} />
288
- <button type="button" onClick={handleStart}>
289
- Upload
290
- </button>
250
+ <button type="button" onClick={handleStart}>Upload</button>
291
251
  </div>
292
252
  );
293
253
  };
294
254
  ```
295
255
 
296
- It is also possible to automatically upload files after setUpload by specifying the `autoStart` option.
256
+ > [!TIP]
257
+ > You can also set `autoStart` to automatically start uploading files after `setUpload` is called.
297
258
 
298
259
  ```tsx
299
260
  import { useTus } from 'use-tus'
@@ -302,30 +263,26 @@ const Uploader = () => {
302
263
  const { upload, setUpload } = useTus({ autoStart: true });
303
264
 
304
265
  const handleSetUpload = useCallback((event: ChangeEvent<HTMLInputElement>) => {
305
- const file = event.target.files.item(0);
266
+ const file = event.target.files.item(0);
267
+ if (!file) return;
268
+
269
+ setUpload(file, {
270
+ endpoint: 'https://tusd.tusdemo.net/files/',
271
+ metadata: {
272
+ filename: file.name,
273
+ filetype: file.type,
274
+ },
275
+ });
276
+ }, [setUpload]);
306
277
 
307
- if (!file) {
308
- return;
309
- }
310
-
311
- setUpload(file, {
312
- endpoint: 'https://tusd.tusdemo.net/files/',
313
- metadata: {
314
- filename: file.name,
315
- filetype: file.type,
316
- },
317
- });
318
- },
319
- [setUpload]
320
- );
321
278
  return (
322
279
  <input type="file" onChange={handleSetUpload} />
323
280
  );
324
281
  };
325
282
  ```
326
283
 
327
- ### Aborting a file upload
328
- You can abort the upload by using the `upload.abort` function.
284
+ ### Aborting a File Upload
285
+ Use the `upload.abort` function to abort an upload.
329
286
 
330
287
  ```tsx
331
288
  import { useTus } from 'use-tus'
@@ -334,54 +291,33 @@ const Aborter = () => {
334
291
  const { upload } = useTus();
335
292
 
336
293
  const handleAbort = useCallback(() => {
337
- if (!upload) {
338
- return;
339
- }
340
-
341
- upload.abort();
294
+ if (upload) upload.abort();
342
295
  }, [upload]);
343
296
 
344
297
  return (
345
298
  <div>
346
- <button type="button" onClick={handleAbort}>
347
- Abort
348
- </button>
299
+ <button type="button" onClick={handleAbort}>Abort</button>
349
300
  </div>
350
301
  );
351
302
  };
352
303
  ```
353
304
 
354
- You can also specify the `autoAbort` option to automatically stop uploads when unmounting hooks.
355
-
356
- ```tsx
357
- import { useTus } from 'use-tus'
358
-
359
- const Uploader = () => {
360
- const { upload, setUpload } = useTus({ autoAbort: true });
361
-
362
- // omitted...
363
- };
364
- ```
365
-
366
- ## Default options of upload
367
- You can specify default options in the `defaultOptions` props of the `TusClientProvider`.
305
+ ### Default Options for Upload
306
+ Specify default options in `defaultOptions` props of the `TusClientProvider`.
368
307
 
369
308
  ```tsx
370
309
  import { useTusStore, DefaultOptions, TusClientProvider } from 'use-tus'
371
310
 
372
- const defaultOptions: DefaultOptions = (contents) => {
373
- const file = contents instanceof File ? contents : undefined;
374
-
375
- return {
376
- endpoint: 'https://tusd.tusdemo.net/files/',
377
- metadata: file
311
+ const defaultOptions: DefaultOptions = (file) => ({
312
+ endpoint: 'https://tusd.tusdemo.net/files/',
313
+ metadata:
314
+ file instanceof File
378
315
  ? {
379
316
  filename: file.name,
380
317
  filetype: file.type,
381
318
  }
382
319
  : undefined,
383
- };
384
- };
320
+ });
385
321
 
386
322
  const App = () => (
387
323
  <TusClientProvider defaultOptions={defaultOptions}>
@@ -393,45 +329,34 @@ const Uploader = () => {
393
329
  const { setUpload } = useTusStore('cacheKey', { autoStart: true });
394
330
 
395
331
  const handleSetUpload = useCallback((event: ChangeEvent<HTMLInputElement>) => {
396
- const file = event.target.files.item(0);
332
+ const file = event.target.files.item(0);
333
+ if (!file) return;
397
334
 
398
- if (!file) {
399
- return;
400
- }
401
-
402
- // You no longer need to specify the options associated with upload.
403
- // If specified, it will override defaultOptions.
404
- setUpload(file);
405
- },
406
- [setUpload]
407
- );
335
+ // Uploads the selected file using default options.
336
+ // Overrides if options are provided to setUpload.
337
+ setUpload(file);
338
+ }, [setUpload]);
408
339
 
409
- return (
410
- <div>
411
- <input type="file" onChange={handleSetUpload} />
412
- </div>
413
- );
340
+ return <input type="file" onChange={handleSetUpload} />;
414
341
  };
415
342
  ```
416
343
 
417
- ### Specify upload key
418
- If you specify `cacheKey` as an argument to useTusStore, you can get the `upload` associated with it. This is useful for cross-page file uploads.
344
+ ### Specify Upload Key
345
+ Specify `cacheKey` to associate uploads across components/pages.
419
346
 
420
347
  ```tsx
421
- const SelectFileComponent = (file: File) => {
422
- // Create upload accosiated with 'upload-thumbnail' key
423
- const { setUpload } = useTusStore('upload-thumbnail')
348
+ import { useTusStore } from 'use-tus'
424
349
 
425
- setUpload(file)
426
- }
350
+ const SelectFileComponent = (file: File) => {
351
+ const { setUpload } = useTusStore('upload-thumbnail');
352
+ setUpload(file);
353
+ };
427
354
 
428
355
  const UploadFileComponent = () => {
429
- const { upload } = useTusStore('upload-thumbnail')
430
-
431
- upload.start()
432
- }
356
+ const { upload } = useTusStore('upload-thumbnail');
357
+ if (upload) upload.start();
358
+ };
433
359
  ```
434
360
 
435
-
436
361
  ## License
437
362
  [MIT © kqito](./LICENSE)
@@ -1,6 +1,5 @@
1
- /// <reference types="react" />
2
1
  declare const _default: {
3
2
  title: string;
4
3
  };
5
4
  export default _default;
6
- export declare const Basic: () => JSX.Element;
5
+ export declare const Basic: () => import("react/jsx-runtime").JSX.Element;
@@ -1,6 +1,5 @@
1
- /// <reference types="react" />
2
1
  declare const _default: {
3
2
  title: string;
4
3
  };
5
4
  export default _default;
6
- export declare const CacheKey: () => JSX.Element;
5
+ export declare const CacheKey: () => import("react/jsx-runtime").JSX.Element;
@@ -1,6 +1,5 @@
1
- /// <reference types="react" />
2
1
  declare const _default: {
3
2
  title: string;
4
3
  };
5
4
  export default _default;
6
- export declare const WithDefaultOptions: () => JSX.Element;
5
+ export declare const WithDefaultOptions: () => import("react/jsx-runtime").JSX.Element;
@@ -1,4 +1,3 @@
1
- /// <reference types="jest" />
2
1
  import type { Upload } from "tus-js-client";
3
2
  export declare const createUploadMock: (start: jest.Mock, abort: jest.Mock) => typeof Upload;
4
3
  export declare const createConsoleErrorMock: () => jest.SpyInstance<void, [message?: any, ...optionalParams: any[]], any>;
package/dist/index.cjs.js CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var r=require("react"),h=require("tus-js-client");const V=Object.freeze({autoAbort:!0,autoStart:!1,uploadOptions:void 0,Upload:h.Upload}),D=o=>({...V,...o}),m=({upload:o,abort:t,autoAbort:e})=>{r.useEffect(()=>{const s=async()=>{!o||!t||await t()};return()=>{e&&s()}},[e,o])},q=(o,t,e)=>{let s=o[e];const a=Object.getOwnPropertyDescriptor(o,e);Object.defineProperty(o,e,{get(){return a?.get?.()??s},set(n){a?.set?a.set.call(o,n):s=n,t(this)}})},I=({Upload:o,file:t,uploadOptions:e,uploadFnOptions:s,onChange:a,onStart:n,onAbort:d})=>{const u=new o(t,e),i=u.start.bind(u),l=u.abort.bind(u),U=()=>{i(),n()},E=async()=>{l(),d()};return u.start=U,u.abort=E,q(u,a,"url"),Object.entries(s).forEach(([A,p])=>{if(typeof p!="function")return;const c=(...O)=>p(...O,u);u.options[A]=c}),{upload:u,originalStart:i,originalAbort:l}};function R(o){const t=Object.entries(o).reduce((s,[a,n])=>({...s,[a]:typeof n!="function"?n:void 0}),{}),e=Object.entries(o).reduce((s,[a,n])=>({...s,[a]:typeof n=="function"?n:void 0}),{});return{uploadOptions:t,uploadFnOptions:e}}const F=o=>{o.findPreviousUploads().then(t=>{t.length&&o.resumeFromPreviousUpload(t[0]),o.start()})},w=Object.freeze({upload:void 0,isSuccess:!1,isAborted:!1,isUploading:!1,error:void 0}),z=(o={})=>{const{autoAbort:t,autoStart:e,uploadOptions:s,Upload:a}=D(o),[n,d]=r.useState(w),[u,i]=r.useState({originalAbort:void 0}),l=p=>{d(c=>c.upload===void 0?c:{...c,...p})},U=r.useCallback((p,c={})=>{const O={...s,...c};function T(){l({isSuccess:!0,isUploading:!1}),O?.onSuccess?.(b)}const v={...O,onSuccess:T,onError:f=>{l({error:f,isUploading:!1}),O?.onError?.(f,b)}},g=f=>{d(K=>({...K,upload:f}))},y=()=>{l({isUploading:!0,isAborted:!1})},N=()=>{l({isUploading:!1,isAborted:!0})},{uploadOptions:_,uploadFnOptions:S}=R(v),{upload:b,originalAbort:H}=I({Upload:a,file:p,uploadOptions:_,uploadFnOptions:S,onChange:g,onStart:y,onAbort:N});e&&F(b),d({upload:b,error:void 0,isSuccess:!1,isAborted:!1,isUploading:!1}),i({originalAbort:H})},[a,e,s]),E=r.useCallback(()=>{u?.originalAbort?.(),d(w),i({originalAbort:void 0})},[u]),A={...n,setUpload:U,remove:E};return m({upload:A.upload,abort:u.originalAbort,autoAbort:t??!1}),A},P={tusClientHasNotFounded:"No TusClient set, use TusClientProvider to set one",tusIsNotSupported:"This browser does not support uploads. Please use a modern browser instead."},x=r.createContext(void 0),L=r.createContext(void 0),k=()=>{const o=r.useContext(x);if(!o&&process.env.NODE_ENV!=="production")throw new Error(P.tusClientHasNotFounded);return r.useMemo(()=>o,[o])},j=()=>{const o=r.useContext(L);if(!o&&process.env.NODE_ENV!=="production")throw new Error(P.tusClientHasNotFounded);return r.useMemo(()=>o,[o])},X=(o,t)=>({type:"INSERT_UPLOAD_INSTANCE",payload:{cacheKey:o,uploadState:t}}),C=(o,t)=>({type:"UPDATE_UPLOAD_CONTEXT",payload:{cacheKey:o,context:t}}),M=o=>({type:"REMOVE_UPLOAD_INSTANCE",payload:{cacheKey:o}}),G=()=>({type:"RESET_CLIENT"}),J=o=>({type:"UPDATE_DEFAULT_OPTIONS",payload:{defaultOptions:o}}),B=(o,t={})=>{const{autoAbort:e,autoStart:s,uploadOptions:a,Upload:n}=D(t),{defaultOptions:d,uploads:u}=k(),i=j(),l=r.useCallback((p,c={})=>{const O={...d?.(p),...a,...c},T={...O,onSuccess:()=>{i(C(o,{isSuccess:!0,isUploading:!1})),O?.onSuccess?.(S)},onError:b=>{i(C(o,{error:b,isUploading:!1})),O?.onError?.(b,S)}},v=b=>{i(C(o,{upload:b}))},g=()=>{i(C(o,{isAborted:!1,isUploading:!0}))},y=()=>{i(C(o,{isAborted:!0,isUploading:!1}))},{uploadOptions:N,uploadFnOptions:_}=R(T),{upload:S}=I({Upload:n,file:p,uploadOptions:N,uploadFnOptions:_,onChange:v,onStart:g,onAbort:y});s&&F(S),i(X(o,{upload:S,error:void 0,isSuccess:!1,isAborted:!1,isUploading:!1}))},[n,s,o,d,a,i]),U=r.useMemo(()=>u[o],[o,u]),E=r.useCallback(()=>{U?.upload?.abort(),i(M(o))},[U,i,o]),A=U?{...U,setUpload:l,remove:E}:{upload:void 0,error:void 0,isSuccess:!1,isAborted:!1,isUploading:!1,setUpload:l,remove:E};return m({upload:A.upload,abort:A.upload?.abort,autoAbort:e??!1}),A},Q=()=>{const o=k(),t=j(),e=o.uploads,s=r.useCallback(n=>{t(M(n))},[t]),a=r.useCallback(()=>t(G()),[t]);return{state:e,removeUpload:s,reset:a}},W=(o,t)=>{switch(t.type){case"INSERT_UPLOAD_INSTANCE":{const{cacheKey:e,uploadState:s}=t.payload;return{...o,uploads:{...o.uploads,[e]:s}}}case"UPDATE_UPLOAD_CONTEXT":{const{cacheKey:e,context:s}=t.payload,a=o.uploads[e];return a?{...o,uploads:{...o.uploads,[e]:{...a,...s}}}:o}case"REMOVE_UPLOAD_INSTANCE":{const{cacheKey:e}=t.payload,s=o.uploads;return delete s[e],{...o,uploads:s}}case"UPDATE_DEFAULT_OPTIONS":{const{defaultOptions:e}=t.payload;return{...o,defaultOptions:e}}case"RESET_CLIENT":return{...o,uploads:{}};default:return o}},Y={uploads:{},defaultOptions:void 0},Z=({defaultOptions:o,children:t})=>{const[e,s]=r.useReducer(W,{...Y,defaultOptions:o});r.useEffect(()=>{h.isSupported||process.env.NODE_ENV==="production"||console.error(P.tusIsNotSupported)},[]),r.useEffect(()=>{e.defaultOptions!==o&&s(J(o))},[o,e.defaultOptions]);const a=r.createElement(L.Provider,{value:s},t);return r.createElement(x.Provider,{value:e},a)};exports.TusClientProvider=Z,exports.useTus=z,exports.useTusClient=Q,exports.useTusStore=B;
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var n=require("react"),h=require("tus-js-client");const V=Object.freeze({autoAbort:!0,autoStart:!1,uploadOptions:void 0,Upload:h.Upload}),D=o=>({...V,...o}),m=({upload:o,abort:t,autoAbort:e})=>{n.useEffect(()=>{const s=async()=>{!o||!t||await t()};return()=>{e&&s()}},[e,o])},q=(o,t,e)=>{let s=o[e];const a=Object.getOwnPropertyDescriptor(o,e);Object.defineProperty(o,e,{get(){return a?.get?.()??s},set(r){a?.set?a.set.call(o,r):s=r,t(this)}})},I=({Upload:o,file:t,uploadOptions:e,uploadFnOptions:s,onChange:a,onStart:r,onAbort:p})=>{const u=new o(t,e),i=u.start.bind(u),l=u.abort.bind(u),A=(...d)=>{i(...d),r()},E=async(...d)=>{l(...d),p()};return u.start=A,u.abort=E,q(u,a,"url"),Object.entries(s).forEach(([d,c])=>{if(typeof c!="function")return;const O=(...b)=>c(...b,u);u.options[d]=O}),{upload:u,originalStart:i,originalAbort:l}};function R(o){const t=Object.entries(o).reduce((s,[a,r])=>({...s,[a]:typeof r!="function"?r:void 0}),{}),e=Object.entries(o).reduce((s,[a,r])=>({...s,[a]:typeof r=="function"?r:void 0}),{});return{uploadOptions:t,uploadFnOptions:e}}const F=o=>{o.findPreviousUploads().then(t=>{t.length&&o.resumeFromPreviousUpload(t[0]),o.start()})},w=Object.freeze({upload:void 0,isSuccess:!1,isAborted:!1,isUploading:!1,error:void 0}),z=(o={})=>{const{autoAbort:t,autoStart:e,uploadOptions:s,Upload:a}=D(o),[r,p]=n.useState(w),[u,i]=n.useState({originalAbort:void 0}),l=c=>{p(O=>O.upload===void 0?O:{...O,...c})},A=n.useCallback((c,O={})=>{const b={...s,...O};function T(){l({isSuccess:!0,isUploading:!1}),b?.onSuccess?.(U)}const v={...b,onSuccess:T,onError:f=>{l({error:f,isUploading:!1}),b?.onError?.(f,U)}},y=f=>{p(K=>({...K,upload:f}))},g=()=>{l({isUploading:!0,isAborted:!1})},N=()=>{l({isUploading:!1,isAborted:!0})},{uploadOptions:_,uploadFnOptions:S}=R(v),{upload:U,originalAbort:H}=I({Upload:a,file:c,uploadOptions:_,uploadFnOptions:S,onChange:y,onStart:g,onAbort:N});e&&F(U),p({upload:U,error:void 0,isSuccess:!1,isAborted:!1,isUploading:!1}),i({originalAbort:H})},[a,e,s]),E=n.useCallback(()=>{u?.originalAbort?.(),p(w),i({originalAbort:void 0})},[u]),d={...r,setUpload:A,remove:E};return m({upload:d.upload,abort:u.originalAbort,autoAbort:t??!1}),d},P={tusClientHasNotFounded:"No TusClient set, use TusClientProvider to set one",tusIsNotSupported:"This browser does not support uploads. Please use a modern browser instead."},x=n.createContext(void 0),L=n.createContext(void 0),k=()=>{const o=n.useContext(x);if(!o&&process.env.NODE_ENV!=="production")throw new Error(P.tusClientHasNotFounded);return n.useMemo(()=>o,[o])},j=()=>{const o=n.useContext(L);if(!o&&process.env.NODE_ENV!=="production")throw new Error(P.tusClientHasNotFounded);return n.useMemo(()=>o,[o])},X=(o,t)=>({type:"INSERT_UPLOAD_INSTANCE",payload:{cacheKey:o,uploadState:t}}),C=(o,t)=>({type:"UPDATE_UPLOAD_CONTEXT",payload:{cacheKey:o,context:t}}),M=o=>({type:"REMOVE_UPLOAD_INSTANCE",payload:{cacheKey:o}}),G=()=>({type:"RESET_CLIENT"}),J=o=>({type:"UPDATE_DEFAULT_OPTIONS",payload:{defaultOptions:o}}),B=(o,t={})=>{const{autoAbort:e,autoStart:s,uploadOptions:a,Upload:r}=D(t),{defaultOptions:p,uploads:u}=k(),i=j(),l=n.useCallback((c,O={})=>{const b={...p?.(c),...a,...O},T={...b,onSuccess:()=>{i(C(o,{isSuccess:!0,isUploading:!1})),b?.onSuccess?.(S)},onError:U=>{i(C(o,{error:U,isUploading:!1})),b?.onError?.(U,S)}},v=U=>{i(C(o,{upload:U}))},y=()=>{i(C(o,{isAborted:!1,isUploading:!0}))},g=()=>{i(C(o,{isAborted:!0,isUploading:!1}))},{uploadOptions:N,uploadFnOptions:_}=R(T),{upload:S}=I({Upload:r,file:c,uploadOptions:N,uploadFnOptions:_,onChange:v,onStart:y,onAbort:g});s&&F(S),i(X(o,{upload:S,error:void 0,isSuccess:!1,isAborted:!1,isUploading:!1}))},[r,s,o,p,a,i]),A=n.useMemo(()=>u[o],[o,u]),E=n.useCallback(()=>{A?.upload?.abort(),i(M(o))},[A,i,o]),d=A?{...A,setUpload:l,remove:E}:{upload:void 0,error:void 0,isSuccess:!1,isAborted:!1,isUploading:!1,setUpload:l,remove:E};return m({upload:d.upload,abort:d.upload?.abort,autoAbort:e??!1}),d},Q=()=>{const o=k(),t=j(),e=o.uploads,s=n.useCallback(r=>{t(M(r))},[t]),a=n.useCallback(()=>t(G()),[t]);return{state:e,removeUpload:s,reset:a}},W=(o,t)=>{switch(t.type){case"INSERT_UPLOAD_INSTANCE":{const{cacheKey:e,uploadState:s}=t.payload;return{...o,uploads:{...o.uploads,[e]:s}}}case"UPDATE_UPLOAD_CONTEXT":{const{cacheKey:e,context:s}=t.payload,a=o.uploads[e];return a?{...o,uploads:{...o.uploads,[e]:{...a,...s}}}:o}case"REMOVE_UPLOAD_INSTANCE":{const{cacheKey:e}=t.payload,s=o.uploads;return delete s[e],{...o,uploads:s}}case"UPDATE_DEFAULT_OPTIONS":{const{defaultOptions:e}=t.payload;return{...o,defaultOptions:e}}case"RESET_CLIENT":return{...o,uploads:{}};default:return o}},Y={uploads:{},defaultOptions:void 0},Z=({defaultOptions:o,children:t})=>{const[e,s]=n.useReducer(W,{...Y,defaultOptions:o});n.useEffect(()=>{h.isSupported||process.env.NODE_ENV==="production"||console.error(P.tusIsNotSupported)},[]),n.useEffect(()=>{e.defaultOptions!==o&&s(J(o))},[o,e.defaultOptions]);const a=n.createElement(L.Provider,{value:s},t);return n.createElement(x.Provider,{value:e},a)};exports.TusClientProvider=Z,exports.useTus=z,exports.useTusClient=Q,exports.useTusStore=B;
package/dist/index.esm.js CHANGED
@@ -1 +1 @@
1
- import{useEffect as D,useState as R,useCallback as T,createContext as F,useContext as w,useMemo as m,useReducer as B,createElement as L}from"react";import{Upload as J,isSupported as Q}from"tus-js-client";const W=Object.freeze({autoAbort:!0,autoStart:!1,uploadOptions:void 0,Upload:J}),x=o=>({...W,...o}),j=({upload:o,abort:t,autoAbort:e})=>{D(()=>{const s=async()=>{!o||!t||await t()};return()=>{e&&s()}},[e,o])},Y=(o,t,e)=>{let s=o[e];const n=Object.getOwnPropertyDescriptor(o,e);Object.defineProperty(o,e,{get(){var r,d;return(d=(r=n==null?void 0:n.get)==null?void 0:r.call(n))!=null?d:s},set(r){n!=null&&n.set?n.set.call(o,r):s=r,t(this)}})},K=({Upload:o,file:t,uploadOptions:e,uploadFnOptions:s,onChange:n,onStart:r,onAbort:d})=>{const a=new o(t,e),E=a.start.bind(a),l=a.abort.bind(a),S=()=>{E(),r()},O=async()=>{l(),d()};return a.start=S,a.abort=O,Y(a,n,"url"),Object.entries(s).forEach(([A,u])=>{if(typeof u!="function")return;const i=(...p)=>u(...p,a);a.options[A]=i}),{upload:a,originalStart:E,originalAbort:l}};function H(o){const t=Object.entries(o).reduce((s,[n,r])=>({...s,[n]:typeof r!="function"?r:void 0}),{}),e=Object.entries(o).reduce((s,[n,r])=>({...s,[n]:typeof r=="function"?r:void 0}),{});return{uploadOptions:t,uploadFnOptions:e}}const V=o=>{o.findPreviousUploads().then(t=>{t.length&&o.resumeFromPreviousUpload(t[0]),o.start()})},k=Object.freeze({upload:void 0,isSuccess:!1,isAborted:!1,isUploading:!1,error:void 0}),Z=(o={})=>{const{autoAbort:t,autoStart:e,uploadOptions:s,Upload:n}=x(o),[r,d]=R(k),[a,E]=R({originalAbort:void 0}),l=u=>{d(i=>i.upload===void 0?i:{...i,...u})},S=T((u,i={})=>{const p={...s,...i};function v(){var c;l({isSuccess:!0,isUploading:!1}),(c=p==null?void 0:p.onSuccess)==null||c.call(p,b)}const g={...p,onSuccess:v,onError:c=>{var C;l({error:c,isUploading:!1}),(C=p==null?void 0:p.onError)==null||C.call(p,c,b)}},y=c=>{d(C=>({...C,upload:c}))},N=()=>{l({isUploading:!0,isAborted:!1})},_=()=>{l({isUploading:!1,isAborted:!0})},{uploadOptions:h,uploadFnOptions:P}=H(g),{upload:b,originalAbort:U}=K({Upload:n,file:u,uploadOptions:h,uploadFnOptions:P,onChange:y,onStart:N,onAbort:_});e&&V(b),d({upload:b,error:void 0,isSuccess:!1,isAborted:!1,isUploading:!1}),E({originalAbort:U})},[n,e,s]),O=T(()=>{var u;(u=a==null?void 0:a.originalAbort)==null||u.call(a),d(k),E({originalAbort:void 0})},[a]),A={...r,setUpload:S,remove:O};return j({upload:A.upload,abort:a.originalAbort,autoAbort:t!=null?t:!1}),A},I={tusClientHasNotFounded:"No TusClient set, use TusClientProvider to set one",tusIsNotSupported:"This browser does not support uploads. Please use a modern browser instead."},M=F(void 0),z=F(void 0),X=()=>{const o=w(M);if(!o&&process.env.NODE_ENV!=="production")throw new Error(I.tusClientHasNotFounded);return m(()=>o,[o])},G=()=>{const o=w(z);if(!o&&process.env.NODE_ENV!=="production")throw new Error(I.tusClientHasNotFounded);return m(()=>o,[o])},$=(o,t)=>({type:"INSERT_UPLOAD_INSTANCE",payload:{cacheKey:o,uploadState:t}}),f=(o,t)=>({type:"UPDATE_UPLOAD_CONTEXT",payload:{cacheKey:o,context:t}}),q=o=>({type:"REMOVE_UPLOAD_INSTANCE",payload:{cacheKey:o}}),oo=()=>({type:"RESET_CLIENT"}),to=o=>({type:"UPDATE_DEFAULT_OPTIONS",payload:{defaultOptions:o}}),eo=(o,t={})=>{var e;const{autoAbort:s,autoStart:n,uploadOptions:r,Upload:d}=x(t),{defaultOptions:a,uploads:E}=X(),l=G(),S=T((i,p={})=>{const v={...a==null?void 0:a(i),...r,...p},g={...v,onSuccess:()=>{var U;l(f(o,{isSuccess:!0,isUploading:!1})),(U=v==null?void 0:v.onSuccess)==null||U.call(v,b)},onError:U=>{var c;l(f(o,{error:U,isUploading:!1})),(c=v==null?void 0:v.onError)==null||c.call(v,U,b)}},y=U=>{l(f(o,{upload:U}))},N=()=>{l(f(o,{isAborted:!1,isUploading:!0}))},_=()=>{l(f(o,{isAborted:!0,isUploading:!1}))},{uploadOptions:h,uploadFnOptions:P}=H(g),{upload:b}=K({Upload:d,file:i,uploadOptions:h,uploadFnOptions:P,onChange:y,onStart:N,onAbort:_});n&&V(b),l($(o,{upload:b,error:void 0,isSuccess:!1,isAborted:!1,isUploading:!1}))},[d,n,o,a,r,l]),O=m(()=>E[o],[o,E]),A=T(()=>{var i;(i=O==null?void 0:O.upload)==null||i.abort(),l(q(o))},[O,l,o]),u=O?{...O,setUpload:S,remove:A}:{upload:void 0,error:void 0,isSuccess:!1,isAborted:!1,isUploading:!1,setUpload:S,remove:A};return j({upload:u.upload,abort:(e=u.upload)==null?void 0:e.abort,autoAbort:s!=null?s:!1}),u},so=()=>{const o=X(),t=G(),e=o.uploads,s=T(r=>{t(q(r))},[t]),n=T(()=>t(oo()),[t]);return{state:e,removeUpload:s,reset:n}},no=(o,t)=>{switch(t.type){case"INSERT_UPLOAD_INSTANCE":{const{cacheKey:e,uploadState:s}=t.payload;return{...o,uploads:{...o.uploads,[e]:s}}}case"UPDATE_UPLOAD_CONTEXT":{const{cacheKey:e,context:s}=t.payload,n=o.uploads[e];return n?{...o,uploads:{...o.uploads,[e]:{...n,...s}}}:o}case"REMOVE_UPLOAD_INSTANCE":{const{cacheKey:e}=t.payload,s=o.uploads;return delete s[e],{...o,uploads:s}}case"UPDATE_DEFAULT_OPTIONS":{const{defaultOptions:e}=t.payload;return{...o,defaultOptions:e}}case"RESET_CLIENT":return{...o,uploads:{}};default:return o}},ro={uploads:{},defaultOptions:void 0},ao=({defaultOptions:o,children:t})=>{const[e,s]=B(no,{...ro,defaultOptions:o});D(()=>{Q||process.env.NODE_ENV==="production"||console.error(I.tusIsNotSupported)},[]),D(()=>{e.defaultOptions!==o&&s(to(o))},[o,e.defaultOptions]);const n=L(z.Provider,{value:s},t);return L(M.Provider,{value:e},n)};export{ao as TusClientProvider,Z as useTus,so as useTusClient,eo as useTusStore};
1
+ import{useEffect as D,useState as R,useCallback as T,createContext as F,useContext as w,useMemo as m,useReducer as B,createElement as L}from"react";import{Upload as J,isSupported as Q}from"tus-js-client";const W=Object.freeze({autoAbort:!0,autoStart:!1,uploadOptions:void 0,Upload:J}),x=o=>({...W,...o}),j=({upload:o,abort:t,autoAbort:e})=>{D(()=>{const s=async()=>{!o||!t||await t()};return()=>{e&&s()}},[e,o])},Y=(o,t,e)=>{let s=o[e];const n=Object.getOwnPropertyDescriptor(o,e);Object.defineProperty(o,e,{get(){var r,d;return(d=(r=n==null?void 0:n.get)==null?void 0:r.call(n))!=null?d:s},set(r){n!=null&&n.set?n.set.call(o,r):s=r,t(this)}})},K=({Upload:o,file:t,uploadOptions:e,uploadFnOptions:s,onChange:n,onStart:r,onAbort:d})=>{const a=new o(t,e),A=a.start.bind(a),l=a.abort.bind(a),S=(...p)=>{A(...p),r()},v=async(...p)=>{l(...p),d()};return a.start=S,a.abort=v,Y(a,n,"url"),Object.entries(s).forEach(([p,u])=>{if(typeof u!="function")return;const i=(...c)=>u(...c,a);a.options[p]=i}),{upload:a,originalStart:A,originalAbort:l}};function H(o){const t=Object.entries(o).reduce((s,[n,r])=>({...s,[n]:typeof r!="function"?r:void 0}),{}),e=Object.entries(o).reduce((s,[n,r])=>({...s,[n]:typeof r=="function"?r:void 0}),{});return{uploadOptions:t,uploadFnOptions:e}}const V=o=>{o.findPreviousUploads().then(t=>{t.length&&o.resumeFromPreviousUpload(t[0]),o.start()})},k=Object.freeze({upload:void 0,isSuccess:!1,isAborted:!1,isUploading:!1,error:void 0}),Z=(o={})=>{const{autoAbort:t,autoStart:e,uploadOptions:s,Upload:n}=x(o),[r,d]=R(k),[a,A]=R({originalAbort:void 0}),l=u=>{d(i=>i.upload===void 0?i:{...i,...u})},S=T((u,i={})=>{const c={...s,...i};function b(){var O;l({isSuccess:!0,isUploading:!1}),(O=c==null?void 0:c.onSuccess)==null||O.call(c,U)}const g={...c,onSuccess:b,onError:O=>{var C;l({error:O,isUploading:!1}),(C=c==null?void 0:c.onError)==null||C.call(c,O,U)}},y=O=>{d(C=>({...C,upload:O}))},N=()=>{l({isUploading:!0,isAborted:!1})},_=()=>{l({isUploading:!1,isAborted:!0})},{uploadOptions:h,uploadFnOptions:P}=H(g),{upload:U,originalAbort:E}=K({Upload:n,file:u,uploadOptions:h,uploadFnOptions:P,onChange:y,onStart:N,onAbort:_});e&&V(U),d({upload:U,error:void 0,isSuccess:!1,isAborted:!1,isUploading:!1}),A({originalAbort:E})},[n,e,s]),v=T(()=>{var u;(u=a==null?void 0:a.originalAbort)==null||u.call(a),d(k),A({originalAbort:void 0})},[a]),p={...r,setUpload:S,remove:v};return j({upload:p.upload,abort:a.originalAbort,autoAbort:t!=null?t:!1}),p},I={tusClientHasNotFounded:"No TusClient set, use TusClientProvider to set one",tusIsNotSupported:"This browser does not support uploads. Please use a modern browser instead."},M=F(void 0),z=F(void 0),X=()=>{const o=w(M);if(!o&&process.env.NODE_ENV!=="production")throw new Error(I.tusClientHasNotFounded);return m(()=>o,[o])},G=()=>{const o=w(z);if(!o&&process.env.NODE_ENV!=="production")throw new Error(I.tusClientHasNotFounded);return m(()=>o,[o])},$=(o,t)=>({type:"INSERT_UPLOAD_INSTANCE",payload:{cacheKey:o,uploadState:t}}),f=(o,t)=>({type:"UPDATE_UPLOAD_CONTEXT",payload:{cacheKey:o,context:t}}),q=o=>({type:"REMOVE_UPLOAD_INSTANCE",payload:{cacheKey:o}}),oo=()=>({type:"RESET_CLIENT"}),to=o=>({type:"UPDATE_DEFAULT_OPTIONS",payload:{defaultOptions:o}}),eo=(o,t={})=>{var e;const{autoAbort:s,autoStart:n,uploadOptions:r,Upload:d}=x(t),{defaultOptions:a,uploads:A}=X(),l=G(),S=T((i,c={})=>{const b={...a==null?void 0:a(i),...r,...c},g={...b,onSuccess:()=>{var E;l(f(o,{isSuccess:!0,isUploading:!1})),(E=b==null?void 0:b.onSuccess)==null||E.call(b,U)},onError:E=>{var O;l(f(o,{error:E,isUploading:!1})),(O=b==null?void 0:b.onError)==null||O.call(b,E,U)}},y=E=>{l(f(o,{upload:E}))},N=()=>{l(f(o,{isAborted:!1,isUploading:!0}))},_=()=>{l(f(o,{isAborted:!0,isUploading:!1}))},{uploadOptions:h,uploadFnOptions:P}=H(g),{upload:U}=K({Upload:d,file:i,uploadOptions:h,uploadFnOptions:P,onChange:y,onStart:N,onAbort:_});n&&V(U),l($(o,{upload:U,error:void 0,isSuccess:!1,isAborted:!1,isUploading:!1}))},[d,n,o,a,r,l]),v=m(()=>A[o],[o,A]),p=T(()=>{var i;(i=v==null?void 0:v.upload)==null||i.abort(),l(q(o))},[v,l,o]),u=v?{...v,setUpload:S,remove:p}:{upload:void 0,error:void 0,isSuccess:!1,isAborted:!1,isUploading:!1,setUpload:S,remove:p};return j({upload:u.upload,abort:(e=u.upload)==null?void 0:e.abort,autoAbort:s!=null?s:!1}),u},so=()=>{const o=X(),t=G(),e=o.uploads,s=T(r=>{t(q(r))},[t]),n=T(()=>t(oo()),[t]);return{state:e,removeUpload:s,reset:n}},no=(o,t)=>{switch(t.type){case"INSERT_UPLOAD_INSTANCE":{const{cacheKey:e,uploadState:s}=t.payload;return{...o,uploads:{...o.uploads,[e]:s}}}case"UPDATE_UPLOAD_CONTEXT":{const{cacheKey:e,context:s}=t.payload,n=o.uploads[e];return n?{...o,uploads:{...o.uploads,[e]:{...n,...s}}}:o}case"REMOVE_UPLOAD_INSTANCE":{const{cacheKey:e}=t.payload,s=o.uploads;return delete s[e],{...o,uploads:s}}case"UPDATE_DEFAULT_OPTIONS":{const{defaultOptions:e}=t.payload;return{...o,defaultOptions:e}}case"RESET_CLIENT":return{...o,uploads:{}};default:return o}},ro={uploads:{},defaultOptions:void 0},ao=({defaultOptions:o,children:t})=>{const[e,s]=B(no,{...ro,defaultOptions:o});D(()=>{Q||process.env.NODE_ENV==="production"||console.error(I.tusIsNotSupported)},[]),D(()=>{e.defaultOptions!==o&&s(to(o))},[o,e.defaultOptions]);const n=L(z.Provider,{value:s},t);return L(M.Provider,{value:e},n)};export{ao as TusClientProvider,Z as useTus,so as useTusClient,eo as useTusStore};
package/dist/index.js CHANGED
@@ -52,12 +52,12 @@ const createUpload = ({ Upload, file, uploadOptions, uploadFnOptions, onChange,
52
52
  const upload = new Upload(file, uploadOptions);
53
53
  const originalStart = upload.start.bind(upload);
54
54
  const originalAbort = upload.abort.bind(upload);
55
- const start = () => {
56
- originalStart();
55
+ const start = (...args) => {
56
+ originalStart(...args);
57
57
  onStart();
58
58
  };
59
- const abort = async () => {
60
- originalAbort();
59
+ const abort = async (...args) => {
60
+ originalAbort(...args);
61
61
  onAbort();
62
62
  };
63
63
  upload.start = start;
@@ -12,5 +12,5 @@ export type CreateUploadParams = {
12
12
  export declare const createUpload: ({ Upload, file, uploadOptions, uploadFnOptions, onChange, onStart, onAbort, }: CreateUploadParams) => {
13
13
  upload: UploadType;
14
14
  originalStart: () => void;
15
- originalAbort: (shouldTerminate?: boolean | undefined) => Promise<void>;
15
+ originalAbort: (shouldTerminate?: boolean) => Promise<void>;
16
16
  };
@@ -3,6 +3,6 @@ import { TusHooksOptions } from "../../types";
3
3
  export declare const mergeUseTusOptions: (options: TusHooksOptions) => {
4
4
  autoAbort: boolean;
5
5
  autoStart: boolean;
6
- uploadOptions: import("../../types").TusHooksUploadOptions | undefined;
6
+ uploadOptions: import("../../types").TusHooksUploadOptions;
7
7
  Upload: typeof Upload;
8
8
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "use-tus",
3
- "version": "0.8.1",
3
+ "version": "0.8.3",
4
4
  "description": "React hooks for resumable file uploads using tus-js-client",
5
5
  "main": "dist/index.cjs.js",
6
6
  "module": "dist/index.esm.js",
@@ -73,7 +73,7 @@
73
73
  "storybook": "^7.5.1",
74
74
  "tailwindcss": "npm:@tailwindcss/postcss7-compat",
75
75
  "ts-jest": "^29.1.1",
76
- "tus-js-client": "^4.0.0",
76
+ "tus-js-client": "4.0.0",
77
77
  "typescript": "^5.2.2",
78
78
  "vite": "^4.5.0"
79
79
  },
@@ -81,7 +81,6 @@
81
81
  "react": ">=16.8",
82
82
  "tus-js-client": ">=2.2.0"
83
83
  },
84
- "packageManager": "pnpm@8.8.0",
85
84
  "scripts": {
86
85
  "build": "npm-run-all -s type clean build:rollup",
87
86
  "build:rollup": "cross-env NODE_ENV=production rollup -c",