use-tus 0.5.0 → 0.7.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.
Files changed (43) hide show
  1. package/README.md +71 -30
  2. package/dist/TusClientProvider/TusClientProvider.d.ts +6 -3
  3. package/dist/TusClientProvider/constants.d.ts +4 -0
  4. package/dist/TusClientProvider/index.d.ts +3 -2
  5. package/dist/{core → TusClientProvider/store}/contexts.d.ts +3 -3
  6. package/dist/{core → TusClientProvider/store}/tucClientActions.d.ts +9 -10
  7. package/dist/{core → TusClientProvider/store}/tusClientReducer.d.ts +5 -5
  8. package/dist/TusClientProvider/types.d.ts +2 -0
  9. package/dist/__stories__/Basic.stories.d.ts +1 -1
  10. package/dist/__stories__/CacheKey.stories.d.ts +1 -1
  11. package/dist/__stories__/DefaultOptions.stories.d.ts +1 -1
  12. package/dist/__stories__/components/BasicButton/BasicButton.d.ts +4 -4
  13. package/dist/__stories__/components/BasicButton/index.d.ts +1 -1
  14. package/dist/__stories__/components/LoadingCircle/LoadingCircle.d.ts +2 -2
  15. package/dist/__stories__/components/LoadingCircle/index.d.ts +1 -1
  16. package/dist/__stories__/components/ProgressBar/ProgressBar.d.ts +2 -2
  17. package/dist/__stories__/components/ProgressBar/index.d.ts +1 -1
  18. package/dist/__stories__/components/UploadIcon/UploadIcon.d.ts +2 -2
  19. package/dist/__stories__/components/UploadIcon/index.d.ts +1 -1
  20. package/dist/__tests__/useTusStore.test.d.ts +1 -0
  21. package/dist/__tests__/utils/getDefaultOptions.d.ts +2 -0
  22. package/dist/__tests__/utils/mock.d.ts +6 -1
  23. package/dist/index.cjs.js +266 -343
  24. package/dist/index.d.ts +3 -4
  25. package/dist/index.esm.js +267 -324
  26. package/dist/index.js +175 -211
  27. package/dist/useTus/index.d.ts +3 -2
  28. package/dist/useTus/types.d.ts +8 -9
  29. package/dist/useTus/useTus.d.ts +2 -2
  30. package/dist/useTus/useTusStore.d.ts +2 -0
  31. package/dist/useTus/utils/createUpload.d.ts +7 -0
  32. package/dist/useTus/utils/startOrResumeUpload.d.ts +2 -0
  33. package/dist/useTus/utils/useAutoAbort.d.ts +2 -0
  34. package/dist/useTus/utils/useMergeTusOptions.d.ts +6 -0
  35. package/dist/useTusClient/index.d.ts +1 -1
  36. package/dist/useTusClient/useTusClient.d.ts +1 -1
  37. package/package.json +9 -10
  38. package/dist/TusClientProvider/TusController.d.ts +0 -4
  39. package/dist/core/constants.d.ts +0 -4
  40. package/dist/core/tusHandler.d.ts +0 -17
  41. package/dist/core/types.d.ts +0 -8
  42. package/dist/useTus/utils.d.ts +0 -16
  43. package/dist/utils/uid.d.ts +0 -1
package/README.md CHANGED
@@ -14,6 +14,7 @@
14
14
 
15
15
  ## Features
16
16
  - Resumable file uploads on react.
17
+ - Lightweight and simple interface hooks.
17
18
  - Managing the [Upload](https://github.com/tus/tus-js-client/blob/master/docs/api.md#tusuploadfile-options) by using context.
18
19
  - TypeScript support.
19
20
 
@@ -28,6 +29,7 @@ npm install use-tus tus-js-client
28
29
  ```
29
30
 
30
31
  or
32
+
31
33
  ```sh
32
34
  yarn add use-tus tus-js-client
33
35
  ```
@@ -36,13 +38,7 @@ yarn add use-tus tus-js-client
36
38
  We can use `useTus` as following.
37
39
 
38
40
  ```tsx
39
- import { useTus, TusClientProvider } from 'use-tus'
40
-
41
- const App = () => (
42
- <TusClientProvider>
43
- <Uploader />
44
- </TusClientProvider>
45
- );
41
+ import { useTus } from 'use-tus'
46
42
 
47
43
  const Uploader = () => {
48
44
  const { upload, setUpload, isSuccess, error, remove } = useTus();
@@ -70,7 +66,7 @@ const Uploader = () => {
70
66
  return;
71
67
  }
72
68
 
73
- // Start upload the file.
69
+ // Start to upload the file.
74
70
  upload.start();
75
71
  }, [upload]);
76
72
 
@@ -90,27 +86,79 @@ const Uploader = () => {
90
86
  ### `useTus` hooks
91
87
 
92
88
  ```tsx
93
- const { upload, setUpload, isSuccess, isAborted, error, remove } = useTus({ cacheKey, autoAbort, autoStart });
89
+ const { upload, setUpload, isSuccess, isAborted, error, remove } = useTus({ autoAbort, autoStart, uploadOptions });
94
90
  ```
95
91
 
96
- `useTus` is a hooks to get or create an `Upload` of tus.
92
+ `useTus` is a hooks that creates an object to perform Resumable file upload.
97
93
 
98
94
  ### Arguments
99
- - `cacheKey` (type: `string | undefined`)
100
- - Specify the key associated with the `Upload` if it's undefined, a random string will be specified.
101
-
102
95
  - `autoAbort` (type: `boolean | undefined`) (default: true)
103
96
  - Whether or not to automatically abort uploads when useTus hooks is unmounted.
104
97
 
105
98
  - `autoStart` (type: `boolean | undefined`) (default: false)
106
99
  - Whether or not to start upload the file after `setUpload` function.
107
100
 
101
+ - `uploadOptions` (type: `UploadOptions | undefined`) (default: undefined)
102
+ - Option to used by upload object that generated by that hooks.
103
+ - For detail type information of `UploadOptions`, please see [here](https://github.com/tus/tus-js-client/blob/master/lib/index.d.ts#L22).
104
+
105
+ ### Returns
106
+ - `upload` (type: `tus.Upload | undefined`)
107
+ - Object to be used when performing Resumable file upload.
108
+ - This value is undefined unless the `setUpload` function called.
109
+ - For detail usage, please see [here](https://github.com/tus/tus-js-client#example)
110
+
111
+ - `setUpload` (type: `(file: tus.Upload['file'], options?: UploadOptions) => void`)
112
+ - Function to create an `Upload`.
113
+ - The property specified in `uploadOptions` will be overwritten if property of `options` are speicified.
114
+
115
+ - `isSuccess` (type: `boolean`)
116
+ - Whether the upload was successful or not.
117
+
118
+ - `isAborted` (type: `boolean`)
119
+ - Whether the upload was aborted or not.
120
+
121
+ - `error` (type: `Error | undefined`)
122
+ - Error when upload fails.
123
+
124
+ - `remove` (type: `() => void`)
125
+ - Function to reset states.
126
+
127
+ ### `useTusStore` hooks
128
+
129
+ ```tsx
130
+ const { upload, setUpload, isSuccess, isAborted, error, remove } = useTusStore(cacheKey, { autoAbort, autoStart, uploadOptions });
131
+ ```
132
+
133
+ `useTusStore` is a hooks that creates an object for resumable file upload and stores it in a context.
134
+
135
+ This hooks is useful when you want to handle uploads across pages or components.
136
+
137
+ **Note that using `useTusStore` hooks, the `TusClientProvider` must be specified as the parent or higher element.**
138
+
139
+ ### Arguments
140
+ - `cacheKey` (type: `string`)
141
+ - Specify the key associated with the `Upload` object is created by `setUpload` function.
142
+
143
+ - `autoAbort` (type: `boolean | undefined`) (default: true)
144
+ - Whether or not to automatically abort uploads when useTusStore hooks is unmounted.
145
+
146
+ - `autoStart` (type: `boolean | undefined`) (default: false)
147
+ - Whether or not to start upload the file after `setUpload` function.
148
+
149
+ - `uploadOptions` (type: `UploadOptions | undefined`) (default: undefined)
150
+ - Option to used by upload object that generated by that hooks.
151
+ - For detail type information of `UploadOptions`, please see [here](https://github.com/tus/tus-js-client/blob/master/lib/index.d.ts#L22).
152
+
108
153
  ### Returns
109
154
  - `upload` (type: `tus.Upload | undefined`)
110
- - The value of the `Upload` associated with the cacheKey in the TusClientProvider. If not present, undefined.
155
+ - Object to be used when performing Resumable file upload.
156
+ - This value of the `Upload` associated with the cacheKey in the TusClientProvider. If not present, undefined.
157
+ - For detail usage, please see [here](https://github.com/tus/tus-js-client#example)
111
158
 
112
159
  - `setUpload` (type: `(file: tus.Upload['file'], options?: tus.Upload['options']) => void`)
113
- - Function to create an `Upload` and store it in TusClientProvider.
160
+ - Function to create an `Upload`.
161
+ - The property specified in `uploadOptions` will be overwritten if property of `options` are speicified.
114
162
 
115
163
  - `isSuccess` (type: `boolean`)
116
164
  - Whether the upload was successful or not.
@@ -128,21 +176,16 @@ const { upload, setUpload, isSuccess, isAborted, error, remove } = useTus({ cach
128
176
 
129
177
  ```tsx
130
178
  () => (
131
- <TusClientProvider>
179
+ <TusClientProvider defaultOptions={defaultOptions}>
132
180
  {children}
133
181
  </TusClientProvider>
134
182
  )
135
183
  ```
136
184
 
137
- `TusClientProvider` is the provider that stores the `Upload` with `useTus` hooks.
138
-
139
- **In order to use `useTus`, you need to set `TusClientProvider`.**
185
+ `TusClientProvider` is the provider that stores the `Upload` with `useTusStore` hooks.
140
186
 
141
187
  ### Props
142
- - `canStoreURLs` (type: `boolean | undefined`)
143
- - A boolean indicating whether the current environment allows storing URLs enabling the corresponding upload to be resumed. [detail](https://github.com/tus/tus-js-client/blob/master/docs/api.md#tuscanstoreurls)
144
-
145
- - `defaultOptions` (type: `(file: tus.Upload['file']) => tus.DefaltOptions | undefined`)
188
+ - `defaultOptions` (type: `(file: tus.Upload['file']) => UploadOptions | undefined`)
146
189
  - 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)
147
190
 
148
191
  ### `useTusClient`
@@ -165,8 +208,6 @@ const { state, removeUpload, reset } = useTusClient();
165
208
  ## Examples
166
209
  The following are some example of how to use `use-tus`.
167
210
 
168
- Note that the `TusClientProvider` must be specified as the parent or higher element.
169
-
170
211
  ### Uploading a file
171
212
  The setUpload and `upload.start` functions can be used to perform resumable file uploads.
172
213
 
@@ -288,7 +329,7 @@ const Uploader = () => {
288
329
  You can specify default options in the `defaultOptions` props of the `TusClientProvider`.
289
330
 
290
331
  ```tsx
291
- import { useTus, DefaultOptions, TusClientProvider } from 'use-tus'
332
+ import { useTusStore, DefaultOptions, TusClientProvider } from 'use-tus'
292
333
 
293
334
  const defaultOptions: DefaultOptions = (contents) => {
294
335
  const file = contents instanceof File ? contents : undefined;
@@ -311,7 +352,7 @@ const App = () => (
311
352
  );
312
353
 
313
354
  const Uploader = () => {
314
- const { setUpload } = useTus({ autoAtart: true });
355
+ const { setUpload } = useTusStore('cacheKey', { autoAtart: true });
315
356
 
316
357
  const handleSetUpload = useCallback((event: ChangeEvent<HTMLInputElement>) => {
317
358
  const file = event.target.files.item(0);
@@ -336,18 +377,18 @@ const Uploader = () => {
336
377
  ```
337
378
 
338
379
  ### Specify upload key
339
- If you specify `cacheKey` as an argument to useTus, you can get the `upload` associated with it. This is useful for cross-page file uploads.
380
+ 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.
340
381
 
341
382
  ```tsx
342
383
  const SelectFileComponent = (file: File) => {
343
384
  // Create upload accosiated with 'upload-thumbnail' key
344
- const { setUpload } = useTus({ cacheKey: 'upload-thumbnail' })
385
+ const { setUpload } = useTusStore('upload-thumbnail')
345
386
 
346
387
  setUpload(file)
347
388
  }
348
389
 
349
390
  const UploadFileComponent = () => {
350
- const { upload } = useTus({ cacheKey: 'upload-thumbnail' })
391
+ const { upload } = useTusStore('upload-thumbnail')
351
392
 
352
393
  upload.start()
353
394
  }
@@ -1,4 +1,7 @@
1
- import { FC } from 'react';
2
- import { TusConfigs } from '../core/tusHandler';
3
- export declare type TusClientProviderProps = Readonly<TusConfigs>;
1
+ import { FC } from "react";
2
+ import { DefaultOptions } from "./types";
3
+ export declare type TusClientProviderProps = Readonly<{
4
+ children?: React.ReactNode;
5
+ defaultOptions?: DefaultOptions;
6
+ }>;
4
7
  export declare const TusClientProvider: FC<TusClientProviderProps>;
@@ -0,0 +1,4 @@
1
+ export declare const ERROR_MESSAGES: {
2
+ readonly tusClientHasNotFounded: "No TusClient set, use TusClientProvider to set one";
3
+ readonly tusIsNotSupported: "This browser does not support uploads. Please use a modern browser instead.";
4
+ };
@@ -1,2 +1,3 @@
1
- export { TusClientProvider } from './TusClientProvider';
2
- export type { TusClientProviderProps } from './TusClientProvider';
1
+ export { TusClientProvider } from "./TusClientProvider";
2
+ export type { TusClientProviderProps } from "./TusClientProvider";
3
+ export type { DefaultOptions } from "./types";
@@ -1,6 +1,6 @@
1
- import type { Dispatch } from 'react';
2
- import { TusClientActions } from './tucClientActions';
3
- import { TusClientState } from './tusClientReducer';
1
+ import type { Dispatch } from "react";
2
+ import { TusClientActions } from "./tucClientActions";
3
+ import { TusClientState } from "./tusClientReducer";
4
4
  export declare const TusClientStateContext: import("react").Context<TusClientState | undefined>;
5
5
  export declare const TusClientDispatchContext: import("react").Context<Dispatch<TusClientActions> | undefined>;
6
6
  export declare const useTusClientState: () => TusClientState;
@@ -1,6 +1,6 @@
1
- import type { Upload } from 'tus-js-client';
2
- import { TusConfigs } from './tusHandler';
3
- export declare type TusClientActions = ReturnType<typeof insertUploadInstance | typeof removeUploadInstance | typeof resetClient | typeof updateSuccessUpload | typeof updateErrorUpload | typeof updateIsAbortedUpload | typeof updateTusHandlerOptions>;
1
+ import type { Upload } from "tus-js-client";
2
+ import { DefaultOptions } from "../types";
3
+ export declare type TusClientActions = ReturnType<typeof insertUploadInstance | typeof removeUploadInstance | typeof updateSuccessUpload | typeof updateErrorUpload | typeof updateIsAbortedUpload | typeof resetClient | typeof updateDefaultOptions>;
4
4
  export declare const insertUploadInstance: (cacheKey: string, upload: Upload) => {
5
5
  readonly type: "INSERT_UPLOAD_INSTANCE";
6
6
  readonly payload: {
@@ -18,7 +18,7 @@ export declare const updateSuccessUpload: (cacheKey: string) => {
18
18
  readonly cacheKey: string;
19
19
  };
20
20
  };
21
- export declare const updateErrorUpload: (cacheKey: string, error?: Error | undefined) => {
21
+ export declare const updateErrorUpload: (cacheKey: string, error?: Error) => {
22
22
  readonly type: "UPDATE_ERROR_UPLOAD";
23
23
  readonly payload: {
24
24
  readonly cacheKey: string;
@@ -41,10 +41,9 @@ export declare const removeUploadInstance: (cacheKey: string) => {
41
41
  export declare const resetClient: () => {
42
42
  readonly type: "RESET_CLIENT";
43
43
  };
44
- export declare const updateTusHandlerOptions: (payload: TusConfigs) => {
45
- readonly type: "UPDATE_TUS_HANDLER_OPTIONS";
46
- readonly payload: Partial<{
47
- canStoreURLs: boolean;
48
- defaultOptions: import("./tusHandler").DefaultOptions;
49
- }>;
44
+ export declare const updateDefaultOptions: (defaultOptions: DefaultOptions | undefined) => {
45
+ readonly type: "UPDATE_DEFAULT_OPTIONS";
46
+ readonly payload: {
47
+ readonly defaultOptions: DefaultOptions | undefined;
48
+ };
50
49
  };
@@ -1,7 +1,7 @@
1
- import type { Reducer } from 'react';
2
- import type { Upload } from 'tus-js-client';
3
- import { TusClientActions } from './tucClientActions';
4
- import { TusHandler } from './tusHandler';
1
+ import type { Reducer } from "react";
2
+ import type { Upload } from "tus-js-client";
3
+ import { DefaultOptions } from "../types";
4
+ import { TusClientActions } from "./tucClientActions";
5
5
  export declare type UploadState = {
6
6
  upload: Upload | undefined;
7
7
  isSuccess: boolean;
@@ -12,7 +12,7 @@ export declare type TusClientState = {
12
12
  uploads: {
13
13
  [cacheKey: string]: UploadState | undefined;
14
14
  };
15
- tusHandler: TusHandler;
15
+ defaultOptions: DefaultOptions | undefined;
16
16
  };
17
17
  export declare const tusClientReducer: Reducer<TusClientState, TusClientActions>;
18
18
  export declare const tusClientInitialState: TusClientState;
@@ -0,0 +1,2 @@
1
+ import { Upload, UploadOptions } from "tus-js-client";
2
+ export declare type DefaultOptions = (file: Upload["file"]) => UploadOptions;
@@ -1,5 +1,5 @@
1
1
  /// <reference types="react" />
2
- import { Meta } from '@storybook/react';
2
+ import { Meta } from "@storybook/react";
3
3
  declare const _default: Meta<import("@storybook/react").Args>;
4
4
  export default _default;
5
5
  export declare const Basic: () => JSX.Element;
@@ -1,5 +1,5 @@
1
1
  /// <reference types="react" />
2
- import { Meta } from '@storybook/react';
2
+ import { Meta } from "@storybook/react";
3
3
  declare const _default: Meta<import("@storybook/react").Args>;
4
4
  export default _default;
5
5
  export declare const CacheKey: () => JSX.Element;
@@ -1,5 +1,5 @@
1
1
  /// <reference types="react" />
2
- import { Meta } from '@storybook/react';
2
+ import { Meta } from "@storybook/react";
3
3
  declare const _default: Meta<import("@storybook/react").Args>;
4
4
  export default _default;
5
5
  export declare const WithDefaultOptions: () => JSX.Element;
@@ -1,9 +1,9 @@
1
- import { ComponentProps } from 'react';
1
+ import { ComponentProps } from "react";
2
2
  declare type BasicButtonProps = {
3
- onClick: ComponentProps<'button'>['onClick'];
3
+ onClick: ComponentProps<"button">["onClick"];
4
4
  title: string;
5
- disabled?: ComponentProps<'button'>['disabled'];
6
- styleColor?: 'primary' | 'basic' | 'error';
5
+ disabled?: ComponentProps<"button">["disabled"];
6
+ styleColor?: "primary" | "basic" | "error";
7
7
  };
8
8
  export declare const BasicButton: import("react").ForwardRefExoticComponent<BasicButtonProps & import("react").RefAttributes<HTMLButtonElement>>;
9
9
  export {};
@@ -1 +1 @@
1
- export { BasicButton } from './BasicButton';
1
+ export { BasicButton } from "./BasicButton";
@@ -1,2 +1,2 @@
1
- import type { VFC } from 'react';
2
- export declare const LoadingCircle: VFC;
1
+ import type { FC } from "react";
2
+ export declare const LoadingCircle: FC;
@@ -1 +1 @@
1
- export { LoadingCircle } from './LoadingCircle';
1
+ export { LoadingCircle } from "./LoadingCircle";
@@ -1,7 +1,7 @@
1
- import { VFC } from 'react';
1
+ import { FC } from "react";
2
2
  declare type ProgressBarProps = {
3
3
  title?: string;
4
4
  value?: number;
5
5
  };
6
- export declare const ProgressBar: VFC<ProgressBarProps>;
6
+ export declare const ProgressBar: FC<ProgressBarProps>;
7
7
  export {};
@@ -1 +1 @@
1
- export { ProgressBar } from './ProgressBar';
1
+ export { ProgressBar } from "./ProgressBar";
@@ -1,2 +1,2 @@
1
- import { VFC } from 'react';
2
- export declare const UploadIcon: VFC;
1
+ import { FC } from "react";
2
+ export declare const UploadIcon: FC;
@@ -1 +1 @@
1
- export { UploadIcon } from './UploadIcon';
1
+ export { UploadIcon } from "./UploadIcon";
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,2 @@
1
+ import { Upload } from "tus-js-client";
2
+ export declare const getDefaultOptions: () => Upload["options"];
@@ -1,5 +1,10 @@
1
1
  /// <reference types="jest" />
2
2
  /// <reference types="webpack-env" />
3
3
  /// <reference types="node" />
4
+ /// <reference types="node" />
5
+ /// <reference types="node" />
6
+ /// <reference types="node" />
7
+ /// <reference types="node" />
4
8
  export declare const createConsoleErrorMock: () => jest.SpyInstance<void, [message?: any, ...optionalParams: any[]]>;
5
- export declare const insertEnvValue: (value: NodeJS.Process['env']) => void;
9
+ export declare const insertEnvValue: (value: NodeJS.Process["env"]) => void;
10
+ export declare const startOrResumeUploadMock: jest.SpyInstance<void, [upload: import("tus-js-client").Upload]>;