|
| 1 | +# API Reference |
| 2 | + |
| 3 | +## ChunkUploader |
| 4 | + |
| 5 | +```ts |
| 6 | +class ChunkUploader<TMetadata extends Metadata> { |
| 7 | + constructor(options: ChunkUploaderOptions<TMetadata>); |
| 8 | + get status(): ChunkUploaderStatus; |
| 9 | + get bytesUploaded(): number; |
| 10 | + get error(): unknown; |
| 11 | + get file(): File; |
| 12 | + start(): boolean; |
| 13 | + get canStart(): boolean; |
| 14 | + pause(): boolean; |
| 15 | + get canPause(): boolean; |
| 16 | + resume(): boolean; |
| 17 | + get canResume(): boolean; |
| 18 | + abort(): boolean; |
| 19 | + get canAbort(): boolean; |
| 20 | +} |
| 21 | +``` |
| 22 | + |
| 23 | +### Constructor |
| 24 | + |
| 25 | +```ts |
| 26 | +new ChunkUploader(options: ChunkUploaderOptions) |
| 27 | +``` |
| 28 | + |
| 29 | +- __Parameters__ |
| 30 | + - options: [ChunkUploaderOptions](#chunkuploaderoptions) |
| 31 | + |
| 32 | +### Properties or Methods |
| 33 | + |
| 34 | +#### __status__ - `Accessor` |
| 35 | + |
| 36 | +```ts |
| 37 | +status: 'pending' | 'uploading' | 'pausing' | 'paused' | 'aborted' | 'complete' | 'error' |
| 38 | +``` |
| 39 | + |
| 40 | +The status of the uploader |
| 41 | + |
| 42 | +#### __bytesUploaded__ - `Accessor` |
| 43 | + |
| 44 | +```ts |
| 45 | +bytesUploaded: number |
| 46 | +``` |
| 47 | + |
| 48 | +Bytes uploaded so far. |
| 49 | + |
| 50 | +#### __error__ - `Accessor` |
| 51 | + |
| 52 | +```ts |
| 53 | +error: unknown |
| 54 | +``` |
| 55 | + |
| 56 | +The error that occurred during the upload process. This is `undefined` if no error occurred. |
| 57 | + |
| 58 | +#### __file__ - `Accessor` |
| 59 | + |
| 60 | +```ts |
| 61 | +file: File |
| 62 | +``` |
| 63 | + |
| 64 | +The file for the upload process. |
| 65 | + |
| 66 | +#### __start__ - `Method` |
| 67 | + |
| 68 | +```ts |
| 69 | +start(): boolean |
| 70 | +``` |
| 71 | + |
| 72 | +Start the upload process. returns `false` if the status is not `pending`. |
| 73 | + |
| 74 | +- status: `pending` -> `uploading` -> `complete` or `error` |
| 75 | + |
| 76 | +#### __canStart__ - `Accessor` |
| 77 | + |
| 78 | +```ts |
| 79 | +canStart: boolean |
| 80 | +``` |
| 81 | + |
| 82 | +`true` if the status is `pending`. |
| 83 | + |
| 84 | + |
| 85 | +#### __pause__ - `Method` |
| 86 | + |
| 87 | +```ts |
| 88 | +pause(): boolean |
| 89 | +``` |
| 90 | + |
| 91 | +Pause the upload process. returns `false` if the status is not `uploading`. |
| 92 | + |
| 93 | +Note that the status at the end could be `complete` if the last chunk is being uploaded when the function is called. |
| 94 | + |
| 95 | +- status: `uploading` -> `pausing` -> `paused` or `complete` |
| 96 | + |
| 97 | +#### __canPause__ - `Accessor` |
| 98 | + |
| 99 | +```ts |
| 100 | +canPause: boolean |
| 101 | +``` |
| 102 | + |
| 103 | +`true` if the status is `uploading`. |
| 104 | + |
| 105 | +#### __resume__ - `Method` |
| 106 | + |
| 107 | +```ts |
| 108 | +resume(): boolean |
| 109 | +``` |
| 110 | + |
| 111 | +Resume the upload process. returns `false` if the status is not `paused` or `error`. |
| 112 | + |
| 113 | +- status: `paused` or `error` -> `uploading` -> `complete` or `error` |
| 114 | + |
| 115 | +#### __canResume__ - `Accessor` |
| 116 | + |
| 117 | +```ts |
| 118 | +canResume: boolean |
| 119 | +``` |
| 120 | + |
| 121 | +`true` if the status is `paused` or `error`. |
| 122 | + |
| 123 | +#### __abort__ - `Method` |
| 124 | + |
| 125 | +```ts |
| 126 | +abort(): boolean |
| 127 | +``` |
| 128 | + |
| 129 | +Abort the upload process. returns `false` if the status is not `paused` or `error`. |
| 130 | + |
| 131 | +- status: `paused` or `error` -> `aborted` |
| 132 | + |
| 133 | +#### __canAbort__ - `Accessor` |
| 134 | + |
| 135 | +```ts |
| 136 | +canAbort: boolean |
| 137 | +``` |
| 138 | + |
| 139 | +`true` if the status is `paused` or `error`. |
| 140 | + |
| 141 | +## ChunkUploaderOptions |
| 142 | + |
| 143 | +```ts |
| 144 | +interface ChunkUploaderOptions<TMetadata extends Metadata> { |
| 145 | + file: File; |
| 146 | + onChunkUpload: ChunkUploadHandler<TMetadata>; |
| 147 | + metadata: TMetadata; |
| 148 | + chunkBytes?: number; |
| 149 | + retryDelays?: number[]; |
| 150 | + onChunkComplete?: (bytesAccepted: number, bytesTotal: number) => void; |
| 151 | + onSuccess?: () => void; |
| 152 | + onError?: (error: unknown) => void; |
| 153 | + onPaused?: () => void; |
| 154 | + onAborted?: (metadata: TMetadata) => void; |
| 155 | + onStatusChange?: ( |
| 156 | + oldStatus: ChunkUploaderStatus | undefined, |
| 157 | + newStatus: ChunkUploaderStatus |
| 158 | + ) => void; |
| 159 | +} |
| 160 | +``` |
| 161 | + |
| 162 | +### Properties |
| 163 | + |
| 164 | +#### __file__ - `Required` |
| 165 | + |
| 166 | +```ts |
| 167 | +file: File |
| 168 | +``` |
| 169 | + |
| 170 | +The file to be uploaded |
| 171 | + |
| 172 | +#### __onChunkUpload__ - `Required` |
| 173 | + |
| 174 | +```ts |
| 175 | +onChunkUpload: (chunkFormData, metadata) => Promise<void> |
| 176 | +``` |
| 177 | + |
| 178 | +The function that defines how the chunk is uploaded to the server. |
| 179 | + |
| 180 | +- __Parameters__ |
| 181 | + - chunkFormData: [ChunkFormData](#chunkformdata) |
| 182 | + - metadata: [Metadata](#metadata---required) |
| 183 | + |
| 184 | +#### __metadata__ - `Required` |
| 185 | + |
| 186 | +```ts |
| 187 | +metadata: (TMetadata extends Record<string, string | boolean | number | undefined | null>) |
| 188 | +``` |
| 189 | + |
| 190 | +The metadata to send with each chunk. This can be used to send additional information like the file name, file type, etc. |
| 191 | + |
| 192 | +#### __chunkBytes__ - `Optional` |
| 193 | + |
| 194 | +Default: `5MB` (5 * 1024 * 1024) |
| 195 | + |
| 196 | +```ts |
| 197 | +chunkBytes?: number |
| 198 | +``` |
| 199 | + |
| 200 | +The number of bytes to send in each chunk. |
| 201 | + |
| 202 | +#### __retryDelays__ - `Optional` |
| 203 | + |
| 204 | +Default: `[1000, 2000, 4000, 8000]` |
| 205 | + |
| 206 | +```ts |
| 207 | +retryDelays?: number[] |
| 208 | +``` |
| 209 | + |
| 210 | +Milliseconds to wait before retrying a failed chunk upload. Set to an empty array to disable retries. |
| 211 | + |
| 212 | +#### __onChunkComplete__ - `Optional` |
| 213 | + |
| 214 | +Default: `undefined` |
| 215 | + |
| 216 | +```ts |
| 217 | +onChunkComplete?: (bytesAccepted: number, bytesTotal: number) => void |
| 218 | +``` |
| 219 | + |
| 220 | +A callback that is called when a chunk is uploaded. |
| 221 | + |
| 222 | +#### __onSuccess__ - `Optional` |
| 223 | + |
| 224 | +Default: `undefined` |
| 225 | + |
| 226 | +```ts |
| 227 | +onSuccess?: () => void |
| 228 | +``` |
| 229 | + |
| 230 | +A callback that is called when the file is sucessfully uploaded. |
| 231 | + |
| 232 | +#### __onError__ - `Optional` |
| 233 | + |
| 234 | +Default: `undefined` |
| 235 | + |
| 236 | +```ts |
| 237 | +onError?: (error: unknown) => void |
| 238 | +``` |
| 239 | + |
| 240 | +A callback that is called when an error occurs during the upload process. |
| 241 | + |
| 242 | +#### __onPaused__ - `Optional` |
| 243 | + |
| 244 | +Default: `undefined` |
| 245 | + |
| 246 | +```ts |
| 247 | +onPaused?: () => void |
| 248 | +``` |
| 249 | + |
| 250 | +A callback that is called when the upload process is paused. |
| 251 | + |
| 252 | +#### __onAborted__ - `Optional` |
| 253 | + |
| 254 | +Default: `undefined` |
| 255 | + |
| 256 | +```ts |
| 257 | +onAborted?: (metadata) => void |
| 258 | +``` |
| 259 | + |
| 260 | +A callback that is called when the upload process is aborted. |
| 261 | + |
| 262 | +- __Parameters__ |
| 263 | + - metadata: [Metadata](#metadata---required) |
| 264 | + |
| 265 | +#### __onStatusChange__ - `Optional` |
| 266 | + |
| 267 | +Default: `undefined` |
| 268 | + |
| 269 | +```ts |
| 270 | +onStatusChange?: (oldStatus, newStatus) => void |
| 271 | +``` |
| 272 | + |
| 273 | +A callback that is called when the status of the uploader changes. |
| 274 | + |
| 275 | +- __Parameters__ |
| 276 | + - oldStatus: [ChunkUploaderStatus](#status) | `undefined` |
| 277 | + - newStatus: [ChunkUploaderStatus](#status) |
| 278 | + |
| 279 | +## ChunkFormData |
| 280 | + |
| 281 | +```ts |
| 282 | +interface ChunkFormData { |
| 283 | + get(name: 'blob'): Blob; |
| 284 | + get(name: 'offset'): `${number}`; |
| 285 | + get(name: 'length'): `${number}`; |
| 286 | + get(name: 'retry'): `${number}`; |
| 287 | + get(name: 'total'): `${number}`; |
| 288 | + get(name: 'isLastChunk'): 'true' | 'false'; |
| 289 | +} |
| 290 | +``` |
| 291 | + |
| 292 | +The form data that is sent with each chunk. This includes the chunk blob, the offset, the length, and other information. |
0 commit comments