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.
- package/README.md +71 -30
- package/dist/TusClientProvider/TusClientProvider.d.ts +6 -3
- package/dist/TusClientProvider/constants.d.ts +4 -0
- package/dist/TusClientProvider/index.d.ts +3 -2
- package/dist/{core → TusClientProvider/store}/contexts.d.ts +3 -3
- package/dist/{core → TusClientProvider/store}/tucClientActions.d.ts +9 -10
- package/dist/{core → TusClientProvider/store}/tusClientReducer.d.ts +5 -5
- package/dist/TusClientProvider/types.d.ts +2 -0
- package/dist/__stories__/Basic.stories.d.ts +1 -1
- package/dist/__stories__/CacheKey.stories.d.ts +1 -1
- package/dist/__stories__/DefaultOptions.stories.d.ts +1 -1
- package/dist/__stories__/components/BasicButton/BasicButton.d.ts +4 -4
- package/dist/__stories__/components/BasicButton/index.d.ts +1 -1
- package/dist/__stories__/components/LoadingCircle/LoadingCircle.d.ts +2 -2
- package/dist/__stories__/components/LoadingCircle/index.d.ts +1 -1
- package/dist/__stories__/components/ProgressBar/ProgressBar.d.ts +2 -2
- package/dist/__stories__/components/ProgressBar/index.d.ts +1 -1
- package/dist/__stories__/components/UploadIcon/UploadIcon.d.ts +2 -2
- package/dist/__stories__/components/UploadIcon/index.d.ts +1 -1
- package/dist/__tests__/useTusStore.test.d.ts +1 -0
- package/dist/__tests__/utils/getDefaultOptions.d.ts +2 -0
- package/dist/__tests__/utils/mock.d.ts +6 -1
- package/dist/index.cjs.js +266 -343
- package/dist/index.d.ts +3 -4
- package/dist/index.esm.js +267 -324
- package/dist/index.js +175 -211
- package/dist/useTus/index.d.ts +3 -2
- package/dist/useTus/types.d.ts +8 -9
- package/dist/useTus/useTus.d.ts +2 -2
- package/dist/useTus/useTusStore.d.ts +2 -0
- package/dist/useTus/utils/createUpload.d.ts +7 -0
- package/dist/useTus/utils/startOrResumeUpload.d.ts +2 -0
- package/dist/useTus/utils/useAutoAbort.d.ts +2 -0
- package/dist/useTus/utils/useMergeTusOptions.d.ts +6 -0
- package/dist/useTusClient/index.d.ts +1 -1
- package/dist/useTusClient/useTusClient.d.ts +1 -1
- package/package.json +9 -10
- package/dist/TusClientProvider/TusController.d.ts +0 -4
- package/dist/core/constants.d.ts +0 -4
- package/dist/core/tusHandler.d.ts +0 -17
- package/dist/core/types.d.ts +0 -8
- package/dist/useTus/utils.d.ts +0 -16
- package/dist/utils/uid.d.ts +0 -1
package/dist/index.js
CHANGED
|
@@ -1,24 +1,137 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
|
|
1
|
+
import { useEffect, useMemo, useState, useCallback, createContext, useContext, useReducer, createElement } from 'react';
|
|
2
|
+
import { Upload, isSupported } from 'tus-js-client';
|
|
3
|
+
|
|
4
|
+
const createUpload = (file, options, dispatchIsAborted) => {
|
|
5
|
+
const upload = new Upload(file, options);
|
|
6
|
+
const originalStart = upload.start.bind(upload);
|
|
7
|
+
const originalAbort = upload.abort.bind(upload);
|
|
8
|
+
const start = () => {
|
|
9
|
+
originalStart();
|
|
10
|
+
dispatchIsAborted(false);
|
|
11
|
+
};
|
|
12
|
+
const abort = async () => {
|
|
13
|
+
originalAbort();
|
|
14
|
+
dispatchIsAborted(true);
|
|
15
|
+
};
|
|
16
|
+
upload.start = start;
|
|
17
|
+
upload.abort = abort;
|
|
18
|
+
return { upload, originalStart, originalAbort };
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const startOrResumeUpload = (upload) => {
|
|
22
|
+
upload.findPreviousUploads().then((previousUploads) => {
|
|
23
|
+
if (previousUploads.length) {
|
|
24
|
+
upload.resumeFromPreviousUpload(previousUploads[0]);
|
|
25
|
+
}
|
|
26
|
+
upload.start();
|
|
27
|
+
});
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const useAutoAbort = (upload, abort, autoAbort) => {
|
|
31
|
+
useEffect(() => {
|
|
32
|
+
const abortUploading = async () => {
|
|
33
|
+
if (!upload || !abort) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
await abort();
|
|
37
|
+
};
|
|
38
|
+
return () => {
|
|
39
|
+
if (!autoAbort) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
abortUploading();
|
|
43
|
+
};
|
|
44
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
45
|
+
}, [autoAbort, upload]);
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const defaultUseTusOptionsValue = Object.freeze({
|
|
49
|
+
autoAbort: true,
|
|
50
|
+
autoStart: false,
|
|
51
|
+
});
|
|
52
|
+
const useMergeTusOptions = (options) => useMemo(() => ({
|
|
53
|
+
...defaultUseTusOptionsValue,
|
|
54
|
+
...(options || {}),
|
|
55
|
+
}), [options]);
|
|
56
|
+
|
|
57
|
+
const initialUseTusState = Object.freeze({
|
|
58
|
+
upload: undefined,
|
|
59
|
+
isSuccess: false,
|
|
60
|
+
isAborted: false,
|
|
61
|
+
error: undefined,
|
|
62
|
+
originalAbort: undefined,
|
|
63
|
+
});
|
|
64
|
+
const useTus = (baseOption) => {
|
|
65
|
+
const { autoAbort, autoStart, uploadOptions } = useMergeTusOptions(baseOption);
|
|
66
|
+
const [tusState, setTusState] = useState(initialUseTusState);
|
|
67
|
+
const updateTusState = useCallback((newOptions) => {
|
|
68
|
+
setTusState((tus) => ({
|
|
69
|
+
...tus,
|
|
70
|
+
...newOptions,
|
|
71
|
+
}));
|
|
72
|
+
}, []);
|
|
73
|
+
const setUpload = useCallback((file, options = {}) => {
|
|
74
|
+
const targetOptions = {
|
|
75
|
+
...uploadOptions,
|
|
76
|
+
...options,
|
|
77
|
+
};
|
|
78
|
+
const onSuccess = () => {
|
|
79
|
+
updateTusState({ isSuccess: true });
|
|
80
|
+
targetOptions?.onSuccess?.();
|
|
81
|
+
};
|
|
82
|
+
const onError = (error) => {
|
|
83
|
+
updateTusState({ error });
|
|
84
|
+
targetOptions?.onError?.(error);
|
|
85
|
+
};
|
|
86
|
+
const mergedUploadOptions = {
|
|
87
|
+
...targetOptions,
|
|
88
|
+
onSuccess,
|
|
89
|
+
onError,
|
|
90
|
+
};
|
|
91
|
+
const dispatchIsAborted = (isAborted) => {
|
|
92
|
+
updateTusState({ isAborted });
|
|
93
|
+
};
|
|
94
|
+
const { upload, originalAbort } = createUpload(file, mergedUploadOptions, dispatchIsAborted);
|
|
95
|
+
if (autoStart) {
|
|
96
|
+
startOrResumeUpload(upload);
|
|
97
|
+
}
|
|
98
|
+
updateTusState({ ...initialUseTusState, upload, originalAbort });
|
|
99
|
+
}, [autoStart, updateTusState, uploadOptions]);
|
|
100
|
+
const remove = useCallback(() => {
|
|
101
|
+
// `upload.abort` function will set `isAborted` state.
|
|
102
|
+
// So call the original function for restore state.
|
|
103
|
+
tusState?.originalAbort?.();
|
|
104
|
+
setTusState(initialUseTusState);
|
|
105
|
+
}, [tusState]);
|
|
106
|
+
const tusResult = useMemo(() => ({
|
|
107
|
+
upload: tusState?.upload,
|
|
108
|
+
isSuccess: tusState?.isSuccess ?? false,
|
|
109
|
+
error: tusState?.error,
|
|
110
|
+
isAborted: tusState?.isAborted ?? false,
|
|
111
|
+
setUpload,
|
|
112
|
+
remove,
|
|
113
|
+
}), [tusState, setUpload, remove]);
|
|
114
|
+
useAutoAbort(tusResult.upload, tusState.originalAbort, autoAbort ?? false);
|
|
115
|
+
return tusResult;
|
|
116
|
+
};
|
|
4
117
|
|
|
5
118
|
const ERROR_MESSAGES = {
|
|
6
|
-
tusClientHasNotFounded:
|
|
7
|
-
tusIsNotSupported:
|
|
119
|
+
tusClientHasNotFounded: "No TusClient set, use TusClientProvider to set one",
|
|
120
|
+
tusIsNotSupported: "This browser does not support uploads. Please use a modern browser instead.",
|
|
8
121
|
};
|
|
9
122
|
|
|
10
123
|
const TusClientStateContext = createContext(undefined);
|
|
11
124
|
const TusClientDispatchContext = createContext(undefined);
|
|
12
125
|
const useTusClientState = () => {
|
|
13
126
|
const tusClientState = useContext(TusClientStateContext);
|
|
14
|
-
if (!tusClientState && process.env.NODE_ENV !==
|
|
127
|
+
if (!tusClientState && process.env.NODE_ENV !== "production") {
|
|
15
128
|
throw new Error(ERROR_MESSAGES.tusClientHasNotFounded);
|
|
16
129
|
}
|
|
17
130
|
return useMemo(() => tusClientState, [tusClientState]);
|
|
18
131
|
};
|
|
19
132
|
const useTusClientDispatch = () => {
|
|
20
133
|
const tusClientDispatch = useContext(TusClientDispatchContext);
|
|
21
|
-
if (!tusClientDispatch && process.env.NODE_ENV !==
|
|
134
|
+
if (!tusClientDispatch && process.env.NODE_ENV !== "production") {
|
|
22
135
|
throw new Error(ERROR_MESSAGES.tusClientHasNotFounded);
|
|
23
136
|
}
|
|
24
137
|
return useMemo(() => tusClientDispatch, [
|
|
@@ -27,7 +140,7 @@ const useTusClientDispatch = () => {
|
|
|
27
140
|
};
|
|
28
141
|
|
|
29
142
|
const insertUploadInstance = (cacheKey, upload) => ({
|
|
30
|
-
type:
|
|
143
|
+
type: "INSERT_UPLOAD_INSTANCE",
|
|
31
144
|
payload: {
|
|
32
145
|
cacheKey,
|
|
33
146
|
uploadState: {
|
|
@@ -38,164 +151,73 @@ const insertUploadInstance = (cacheKey, upload) => ({
|
|
|
38
151
|
},
|
|
39
152
|
});
|
|
40
153
|
const updateSuccessUpload = (cacheKey) => ({
|
|
41
|
-
type:
|
|
154
|
+
type: "UPDATE_SUCCESS_UPLOAD",
|
|
42
155
|
payload: {
|
|
43
156
|
cacheKey,
|
|
44
157
|
},
|
|
45
158
|
});
|
|
46
159
|
const updateErrorUpload = (cacheKey, error) => ({
|
|
47
|
-
type:
|
|
160
|
+
type: "UPDATE_ERROR_UPLOAD",
|
|
48
161
|
payload: {
|
|
49
162
|
cacheKey,
|
|
50
163
|
error,
|
|
51
164
|
},
|
|
52
165
|
});
|
|
53
166
|
const updateIsAbortedUpload = (cacheKey, isAborted) => ({
|
|
54
|
-
type:
|
|
167
|
+
type: "UPDATE_IS_ABORTED_UPLOAD",
|
|
55
168
|
payload: {
|
|
56
169
|
cacheKey,
|
|
57
170
|
isAborted,
|
|
58
171
|
},
|
|
59
172
|
});
|
|
60
173
|
const removeUploadInstance = (cacheKey) => ({
|
|
61
|
-
type:
|
|
174
|
+
type: "REMOVE_UPLOAD_INSTANCE",
|
|
62
175
|
payload: {
|
|
63
176
|
cacheKey,
|
|
64
177
|
},
|
|
65
178
|
});
|
|
66
|
-
const
|
|
67
|
-
type:
|
|
68
|
-
payload
|
|
179
|
+
const updateDefaultOptions = (defaultOptions) => ({
|
|
180
|
+
type: "UPDATE_DEFAULT_OPTIONS",
|
|
181
|
+
payload: {
|
|
182
|
+
defaultOptions,
|
|
183
|
+
},
|
|
69
184
|
});
|
|
70
185
|
|
|
71
|
-
const
|
|
72
|
-
const
|
|
73
|
-
|
|
74
|
-
dispatch(updateSuccessUpload(cacheKey));
|
|
75
|
-
}
|
|
76
|
-
if (!cacheKey) {
|
|
77
|
-
internalDispatch((internalTusState) => ({
|
|
78
|
-
...internalTusState,
|
|
79
|
-
isSuccess: true,
|
|
80
|
-
}));
|
|
81
|
-
}
|
|
82
|
-
if (handlers.onSuccess) {
|
|
83
|
-
handlers.onSuccess();
|
|
84
|
-
}
|
|
85
|
-
};
|
|
86
|
-
const onError = (err) => {
|
|
87
|
-
if (cacheKey) {
|
|
88
|
-
dispatch(updateErrorUpload(cacheKey, err));
|
|
89
|
-
}
|
|
90
|
-
if (!cacheKey) {
|
|
91
|
-
internalDispatch((internalTusState) => ({
|
|
92
|
-
...internalTusState,
|
|
93
|
-
error: err,
|
|
94
|
-
}));
|
|
95
|
-
}
|
|
96
|
-
if (handlers.onError) {
|
|
97
|
-
handlers.onError(err);
|
|
98
|
-
}
|
|
99
|
-
};
|
|
100
|
-
return {
|
|
101
|
-
onSuccess,
|
|
102
|
-
onError,
|
|
103
|
-
};
|
|
104
|
-
};
|
|
105
|
-
const startOrResumeUpload = (upload) => {
|
|
106
|
-
upload.findPreviousUploads().then((previousUploads) => {
|
|
107
|
-
if (previousUploads.length) {
|
|
108
|
-
upload.resumeFromPreviousUpload(previousUploads[0]);
|
|
109
|
-
}
|
|
110
|
-
upload.start();
|
|
111
|
-
});
|
|
112
|
-
};
|
|
113
|
-
const createUpload = (file, options, { dispatch, internalDispatch, cacheKey }) => {
|
|
114
|
-
const upload = new Upload(file, options);
|
|
115
|
-
const originalStart = upload.start.bind(upload);
|
|
116
|
-
const originalAbort = upload.abort.bind(upload);
|
|
117
|
-
const dispatchIsAborted = (isAborted) => {
|
|
118
|
-
if (cacheKey) {
|
|
119
|
-
dispatch(updateIsAbortedUpload(cacheKey, isAborted));
|
|
120
|
-
return;
|
|
121
|
-
}
|
|
122
|
-
internalDispatch((internalTusState) => ({
|
|
123
|
-
...internalTusState,
|
|
124
|
-
isAborted,
|
|
125
|
-
}));
|
|
126
|
-
};
|
|
127
|
-
const start = () => {
|
|
128
|
-
originalStart();
|
|
129
|
-
dispatchIsAborted(false);
|
|
130
|
-
};
|
|
131
|
-
const abort = async () => {
|
|
132
|
-
originalAbort();
|
|
133
|
-
dispatchIsAborted(true);
|
|
134
|
-
};
|
|
135
|
-
upload.start = start;
|
|
136
|
-
upload.abort = abort;
|
|
137
|
-
return upload;
|
|
138
|
-
};
|
|
139
|
-
|
|
140
|
-
const defaultUseTusOptionsValue = Object.freeze({
|
|
141
|
-
cacheKey: undefined,
|
|
142
|
-
autoAbort: true,
|
|
143
|
-
autoStart: false,
|
|
144
|
-
});
|
|
145
|
-
const initialUseTusState = Object.freeze({
|
|
146
|
-
upload: undefined,
|
|
147
|
-
isSuccess: false,
|
|
148
|
-
isAborted: false,
|
|
149
|
-
error: undefined,
|
|
150
|
-
});
|
|
151
|
-
const useTus = (useTusOptions) => {
|
|
152
|
-
const { cacheKey, autoAbort, autoStart } = {
|
|
153
|
-
...defaultUseTusOptionsValue,
|
|
154
|
-
...(useTusOptions || {}),
|
|
155
|
-
};
|
|
156
|
-
const [internalTusState, setInternalTusState] = useState(initialUseTusState);
|
|
157
|
-
const tusClientState = useTusClientState();
|
|
186
|
+
const useTusStore = (cacheKey, baseOption) => {
|
|
187
|
+
const { autoAbort, autoStart, uploadOptions } = useMergeTusOptions(baseOption);
|
|
188
|
+
const { defaultOptions, uploads } = useTusClientState();
|
|
158
189
|
const tusClientDispatch = useTusClientDispatch();
|
|
159
|
-
const tus = tusClientState.tusHandler.getTus;
|
|
160
190
|
const setUpload = useCallback((file, options = {}) => {
|
|
161
|
-
const dispatcher = {
|
|
162
|
-
cacheKey,
|
|
163
|
-
dispatch: tusClientDispatch,
|
|
164
|
-
internalDispatch: setInternalTusState,
|
|
165
|
-
};
|
|
166
191
|
const targetOptions = {
|
|
167
|
-
...
|
|
192
|
+
...defaultOptions?.(file),
|
|
193
|
+
...uploadOptions,
|
|
168
194
|
...options,
|
|
169
195
|
};
|
|
170
|
-
const
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
}
|
|
174
|
-
const
|
|
196
|
+
const onSuccess = () => {
|
|
197
|
+
tusClientDispatch(updateSuccessUpload(cacheKey));
|
|
198
|
+
targetOptions?.onSuccess?.();
|
|
199
|
+
};
|
|
200
|
+
const onError = (error) => {
|
|
201
|
+
tusClientDispatch(updateErrorUpload(cacheKey, error));
|
|
202
|
+
targetOptions?.onError?.(error);
|
|
203
|
+
};
|
|
204
|
+
const mergedUploadOptions = {
|
|
175
205
|
...targetOptions,
|
|
176
206
|
onSuccess,
|
|
177
207
|
onError,
|
|
178
208
|
};
|
|
179
|
-
const
|
|
209
|
+
const dispatchIsAborted = (isAborted) => {
|
|
210
|
+
tusClientDispatch(updateIsAbortedUpload(cacheKey, isAborted));
|
|
211
|
+
};
|
|
212
|
+
const { upload } = createUpload(file, mergedUploadOptions, dispatchIsAborted);
|
|
180
213
|
if (autoStart) {
|
|
181
214
|
startOrResumeUpload(upload);
|
|
182
215
|
}
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
}
|
|
187
|
-
setInternalTusState({
|
|
188
|
-
...initialUseTusState,
|
|
189
|
-
upload,
|
|
190
|
-
});
|
|
191
|
-
}, [tusClientDispatch, cacheKey, tus, autoStart]);
|
|
192
|
-
const targetTusState = useMemo(() => (cacheKey ? tusClientState.uploads[cacheKey] : internalTusState), [cacheKey, tusClientState, internalTusState]);
|
|
216
|
+
tusClientDispatch(insertUploadInstance(cacheKey, upload));
|
|
217
|
+
}, [autoStart, cacheKey, defaultOptions, tusClientDispatch, uploadOptions]);
|
|
218
|
+
const targetTusState = useMemo(() => uploads[cacheKey], [cacheKey, uploads]);
|
|
193
219
|
const remove = useCallback(() => {
|
|
194
220
|
targetTusState?.upload?.abort();
|
|
195
|
-
if (!cacheKey) {
|
|
196
|
-
setInternalTusState(initialUseTusState);
|
|
197
|
-
return;
|
|
198
|
-
}
|
|
199
221
|
tusClientDispatch(removeUploadInstance(cacheKey));
|
|
200
222
|
}, [targetTusState, tusClientDispatch, cacheKey]);
|
|
201
223
|
const tusResult = useMemo(() => ({
|
|
@@ -206,42 +228,13 @@ const useTus = (useTusOptions) => {
|
|
|
206
228
|
setUpload,
|
|
207
229
|
remove,
|
|
208
230
|
}), [targetTusState, setUpload, remove]);
|
|
209
|
-
|
|
210
|
-
useEffect(() => {
|
|
211
|
-
const abortUploading = async () => {
|
|
212
|
-
if (!tusResult.upload) {
|
|
213
|
-
return;
|
|
214
|
-
}
|
|
215
|
-
await tusResult.upload.abort();
|
|
216
|
-
};
|
|
217
|
-
return () => {
|
|
218
|
-
if (!autoAbort) {
|
|
219
|
-
return;
|
|
220
|
-
}
|
|
221
|
-
abortUploading();
|
|
222
|
-
};
|
|
223
|
-
}, [autoAbort, tusResult.upload]);
|
|
231
|
+
useAutoAbort(tusResult.upload, tusResult.upload?.abort, autoAbort ?? false);
|
|
224
232
|
return tusResult;
|
|
225
233
|
};
|
|
226
234
|
|
|
227
|
-
const initialDefaultOptions = () => tus.defaultOptions;
|
|
228
|
-
class TusHandler {
|
|
229
|
-
constructor(tusConfigs) {
|
|
230
|
-
const { canStoreURLs = tus.canStoreURLs, defaultOptions = initialDefaultOptions, } = tusConfigs || {};
|
|
231
|
-
this.tus = {
|
|
232
|
-
...tus,
|
|
233
|
-
canStoreURLs,
|
|
234
|
-
defaultOptions,
|
|
235
|
-
};
|
|
236
|
-
}
|
|
237
|
-
get getTus() {
|
|
238
|
-
return this.tus;
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
|
|
242
235
|
const tusClientReducer = (state, actions) => {
|
|
243
236
|
switch (actions.type) {
|
|
244
|
-
case
|
|
237
|
+
case "INSERT_UPLOAD_INSTANCE": {
|
|
245
238
|
const { cacheKey, uploadState } = actions.payload;
|
|
246
239
|
return {
|
|
247
240
|
...state,
|
|
@@ -251,7 +244,7 @@ const tusClientReducer = (state, actions) => {
|
|
|
251
244
|
},
|
|
252
245
|
};
|
|
253
246
|
}
|
|
254
|
-
case
|
|
247
|
+
case "UPDATE_SUCCESS_UPLOAD": {
|
|
255
248
|
const { cacheKey } = actions.payload;
|
|
256
249
|
const target = state.uploads[cacheKey];
|
|
257
250
|
if (!target) {
|
|
@@ -268,7 +261,7 @@ const tusClientReducer = (state, actions) => {
|
|
|
268
261
|
},
|
|
269
262
|
};
|
|
270
263
|
}
|
|
271
|
-
case
|
|
264
|
+
case "UPDATE_ERROR_UPLOAD": {
|
|
272
265
|
const { cacheKey, error } = actions.payload;
|
|
273
266
|
const target = state.uploads[cacheKey];
|
|
274
267
|
if (!target) {
|
|
@@ -285,7 +278,7 @@ const tusClientReducer = (state, actions) => {
|
|
|
285
278
|
},
|
|
286
279
|
};
|
|
287
280
|
}
|
|
288
|
-
case
|
|
281
|
+
case "UPDATE_IS_ABORTED_UPLOAD": {
|
|
289
282
|
const { cacheKey, isAborted } = actions.payload;
|
|
290
283
|
const target = state.uploads[cacheKey];
|
|
291
284
|
if (!target) {
|
|
@@ -302,7 +295,7 @@ const tusClientReducer = (state, actions) => {
|
|
|
302
295
|
},
|
|
303
296
|
};
|
|
304
297
|
}
|
|
305
|
-
case
|
|
298
|
+
case "REMOVE_UPLOAD_INSTANCE": {
|
|
306
299
|
const { cacheKey } = actions.payload;
|
|
307
300
|
const newUploads = state.uploads;
|
|
308
301
|
delete newUploads[cacheKey];
|
|
@@ -311,20 +304,11 @@ const tusClientReducer = (state, actions) => {
|
|
|
311
304
|
uploads: newUploads,
|
|
312
305
|
};
|
|
313
306
|
}
|
|
314
|
-
case
|
|
315
|
-
|
|
316
|
-
tusHandler: new TusHandler(),
|
|
317
|
-
uploads: {},
|
|
318
|
-
};
|
|
319
|
-
}
|
|
320
|
-
case 'UPDATE_TUS_HANDLER_OPTIONS': {
|
|
321
|
-
const { canStoreURLs, defaultOptions } = actions.payload;
|
|
307
|
+
case "UPDATE_DEFAULT_OPTIONS": {
|
|
308
|
+
const { defaultOptions } = actions.payload;
|
|
322
309
|
return {
|
|
323
310
|
...state,
|
|
324
|
-
|
|
325
|
-
canStoreURLs,
|
|
326
|
-
defaultOptions,
|
|
327
|
-
}),
|
|
311
|
+
defaultOptions,
|
|
328
312
|
};
|
|
329
313
|
}
|
|
330
314
|
default:
|
|
@@ -333,53 +317,33 @@ const tusClientReducer = (state, actions) => {
|
|
|
333
317
|
};
|
|
334
318
|
const tusClientInitialState = {
|
|
335
319
|
uploads: {},
|
|
336
|
-
|
|
320
|
+
defaultOptions: undefined,
|
|
337
321
|
};
|
|
338
322
|
|
|
339
|
-
const
|
|
340
|
-
const
|
|
341
|
-
|
|
342
|
-
|
|
323
|
+
const TusClientProvider = ({ defaultOptions, children, }) => {
|
|
324
|
+
const [tusClientState, tusClientDispatch] = useReducer(tusClientReducer, {
|
|
325
|
+
...tusClientInitialState,
|
|
326
|
+
defaultOptions,
|
|
327
|
+
});
|
|
328
|
+
// Output error if tus has not supported
|
|
343
329
|
useEffect(() => {
|
|
344
|
-
if (
|
|
345
|
-
process.env.NODE_ENV === 'production') {
|
|
330
|
+
if (isSupported || process.env.NODE_ENV === "production") {
|
|
346
331
|
return;
|
|
347
332
|
}
|
|
348
333
|
// eslint-disable-next-line no-console
|
|
349
334
|
console.error(ERROR_MESSAGES.tusIsNotSupported);
|
|
350
|
-
}, [
|
|
335
|
+
}, []);
|
|
336
|
+
// Set defaultOptions to the context
|
|
351
337
|
useEffect(() => {
|
|
352
|
-
if (
|
|
353
|
-
tus.canStoreURLs === canStoreURLs) {
|
|
338
|
+
if (tusClientState.defaultOptions === defaultOptions) {
|
|
354
339
|
return;
|
|
355
340
|
}
|
|
356
|
-
tusClientDispatch(
|
|
357
|
-
}, [
|
|
358
|
-
|
|
359
|
-
canStoreURLs,
|
|
360
|
-
defaultOptions,
|
|
361
|
-
tus.canStoreURLs,
|
|
362
|
-
tus.defaultOptions,
|
|
363
|
-
]);
|
|
364
|
-
return createElement(Fragment, {}, children);
|
|
365
|
-
};
|
|
366
|
-
|
|
367
|
-
const TusClientProvider = ({ canStoreURLs, defaultOptions, children, }) => {
|
|
368
|
-
const [tusClientState, tusClientDispatch] = useReducer(tusClientReducer, {
|
|
369
|
-
...tusClientInitialState,
|
|
370
|
-
tusHandler: new TusHandler({
|
|
371
|
-
canStoreURLs,
|
|
372
|
-
defaultOptions,
|
|
373
|
-
}),
|
|
374
|
-
});
|
|
375
|
-
const tusControllerElement = createElement(TusController, {
|
|
376
|
-
canStoreURLs,
|
|
377
|
-
defaultOptions,
|
|
378
|
-
}, children);
|
|
379
|
-
const tusClientDispatchContextProviderElement = createElement(TusClientDispatchContext.Provider, { value: tusClientDispatch }, tusControllerElement);
|
|
341
|
+
tusClientDispatch(updateDefaultOptions(defaultOptions));
|
|
342
|
+
}, [defaultOptions, tusClientState.defaultOptions]);
|
|
343
|
+
const tusClientDispatchContextProviderElement = createElement(TusClientDispatchContext.Provider, { value: tusClientDispatch }, children);
|
|
380
344
|
return createElement(TusClientStateContext.Provider, {
|
|
381
345
|
value: tusClientState,
|
|
382
346
|
}, tusClientDispatchContextProviderElement);
|
|
383
347
|
};
|
|
384
348
|
|
|
385
|
-
export { TusClientProvider, useTus };
|
|
349
|
+
export { TusClientProvider, useTus, useTusStore };
|
package/dist/useTus/index.d.ts
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
export { useTus } from
|
|
2
|
-
export
|
|
1
|
+
export { useTus } from "./useTus";
|
|
2
|
+
export { useTusStore } from "./useTusStore";
|
|
3
|
+
export type { UseTusResult, UseTusOptions } from "./types";
|
package/dist/useTus/types.d.ts
CHANGED
|
@@ -1,15 +1,14 @@
|
|
|
1
|
-
import type { Upload } from
|
|
2
|
-
export
|
|
3
|
-
cacheKey?: string;
|
|
1
|
+
import type { Upload, UploadOptions } from "tus-js-client";
|
|
2
|
+
export interface UseTusOptions {
|
|
4
3
|
autoAbort?: boolean;
|
|
5
4
|
autoStart?: boolean;
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
uploadOptions?: UploadOptions;
|
|
6
|
+
}
|
|
7
|
+
export interface UseTusResult {
|
|
8
8
|
upload?: Upload;
|
|
9
|
-
setUpload: (file: Upload[
|
|
10
|
-
remove: () => void;
|
|
9
|
+
setUpload: (file: Upload["file"], options?: Upload["options"]) => void;
|
|
11
10
|
isSuccess: boolean;
|
|
12
11
|
isAborted: boolean;
|
|
13
12
|
error?: Error;
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
remove: () => void;
|
|
14
|
+
}
|
package/dist/useTus/useTus.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { UseTusOptions, UseTusResult } from
|
|
2
|
-
export declare const useTus: (
|
|
1
|
+
import { UseTusOptions, UseTusResult } from "./types";
|
|
2
|
+
export declare const useTus: (baseOption?: UseTusOptions) => UseTusResult;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Upload } from "tus-js-client";
|
|
2
|
+
export declare type DispatchIsAborted = (isAborted: boolean) => void;
|
|
3
|
+
export declare const createUpload: (file: Upload["file"], options: Upload["options"], dispatchIsAborted: DispatchIsAborted) => {
|
|
4
|
+
upload: Upload;
|
|
5
|
+
originalStart: () => void;
|
|
6
|
+
originalAbort: (shouldTerminate?: boolean | undefined) => Promise<void>;
|
|
7
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { UseTusOptions } from "../types";
|
|
2
|
+
export declare const useMergeTusOptions: (options: UseTusOptions | undefined) => {
|
|
3
|
+
autoAbort?: boolean | undefined;
|
|
4
|
+
autoStart?: boolean | undefined;
|
|
5
|
+
uploadOptions?: import("tus-js-client").UploadOptions | undefined;
|
|
6
|
+
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export { useTusClient } from
|
|
1
|
+
export { useTusClient } from "./useTusClient";
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export declare const useTusClient: () => {
|
|
2
2
|
state: {
|
|
3
|
-
[cacheKey: string]: import("../
|
|
3
|
+
[cacheKey: string]: import("../TusClientProvider/store/tusClientReducer").UploadState | undefined;
|
|
4
4
|
};
|
|
5
5
|
removeUpload: (cacheKey: string) => void;
|
|
6
6
|
reset: () => void;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "use-tus",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
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",
|
|
@@ -11,8 +11,8 @@
|
|
|
11
11
|
"test": "cross-env NODE_ENV=TEST jest",
|
|
12
12
|
"type": "tsc --noEmit",
|
|
13
13
|
"format": "npm-run-all -s format:*",
|
|
14
|
-
"format:fix": "prettier --write '
|
|
15
|
-
"format:lint": "eslint ./
|
|
14
|
+
"format:fix": "prettier --write './**/*.{ts,tsx,js,jsx,json}'",
|
|
15
|
+
"format:lint": "eslint ./ --ext .ts,.tsx",
|
|
16
16
|
"clean": "rimraf ./dist",
|
|
17
17
|
"prepublishOnly": "npm run build",
|
|
18
18
|
"prepare": "husky install",
|
|
@@ -59,8 +59,7 @@
|
|
|
59
59
|
"@storybook/addon-links": "^6.2.9",
|
|
60
60
|
"@storybook/react": "^6.2.9",
|
|
61
61
|
"@testing-library/jest-dom": "^5.11.10",
|
|
62
|
-
"@testing-library/react": "^
|
|
63
|
-
"@testing-library/react-hooks": "^5.1.1",
|
|
62
|
+
"@testing-library/react": "^13.3.0",
|
|
64
63
|
"@types/jest": "^26.0.22",
|
|
65
64
|
"@types/react": "^17.0.3",
|
|
66
65
|
"@types/react-dom": "^17.0.3",
|
|
@@ -87,17 +86,17 @@
|
|
|
87
86
|
"npm-run-all": "^4.1.5",
|
|
88
87
|
"postcss": "^7",
|
|
89
88
|
"prettier": "^2.2.1",
|
|
90
|
-
"react": "^
|
|
91
|
-
"react-dom": "^
|
|
89
|
+
"react": "^18.1.0",
|
|
90
|
+
"react-dom": "^18.1.0",
|
|
92
91
|
"rimraf": "^3.0.2",
|
|
93
92
|
"rollup": "^2.44.0",
|
|
94
93
|
"tailwindcss": "npm:@tailwindcss/postcss7-compat",
|
|
95
94
|
"ts-jest": "^26.5.4",
|
|
96
|
-
"
|
|
97
|
-
"
|
|
95
|
+
"tus-js-client": "^2.2.0",
|
|
96
|
+
"typescript": "^4.7.3"
|
|
98
97
|
},
|
|
99
98
|
"peerDependencies": {
|
|
100
|
-
"react": "
|
|
99
|
+
"react": ">=16.8",
|
|
101
100
|
"tus-js-client": "^2.2.0"
|
|
102
101
|
},
|
|
103
102
|
"dependencies": {}
|
package/dist/core/constants.d.ts
DELETED