@nexim/upload-sdk
Version:
TypeScript SDK for seamless integration with Nexim Media Upload Service. It provides state machine-based upload handling, progress tracking, and type-safe API for image optimization and file uploads.
158 lines (100 loc) • 3.73 kB
Markdown
[@nexim/upload-sdk](../README.md) / UploadFileMachine
# Class: UploadFileMachine
A state machine for managing file uploads.
Handles file upload lifecycle including state management, API requests, and error handling.
## Extends
- `AlwatrFluxStateMachine`\<[`UploadFileMachineState`](../type-aliases/UploadFileMachineState.md), `Event`\>
## Extended by
- [`UploadImageMachine`](UploadImageMachine.md)
## Constructors
### Constructor
> **new UploadFileMachine**(`options`: [`UploadFileMachineOptions`](../type-aliases/UploadFileMachineOptions.md)): `UploadFileMachine`
#### Parameters
| Parameter | Type |
| --------- | ------------------------------------------------------------------------- |
| `options` | [`UploadFileMachineOptions`](../type-aliases/UploadFileMachineOptions.md) |
#### Returns
`UploadFileMachine`
#### Overrides
`AlwatrFluxStateMachine<UploadFileMachineState, Event>.constructor`
## Properties
| Property | Modifier | Type | Default value |
| ---------------------------------- | ----------- | ---------------- | ------------- |
| <a id="fileblob_"></a> `fileBlob_` | `protected` | `null` \| `Blob` | `null` |
## Methods
### clear()
> **clear**(): `void`
Clear the state.
#### Returns
`void`
### generateFetchOption\_()
> `protected` **generateFetchOption\_**(`options`: [`UploadFileMachineOptions`](../type-aliases/UploadFileMachineOptions.md)): `FetchOptions`
#### Parameters
| Parameter | Type |
| --------- | ------------------------------------------------------------------------- |
| `options` | [`UploadFileMachineOptions`](../type-aliases/UploadFileMachineOptions.md) |
#### Returns
`FetchOptions`
### syncUpload()
> **syncUpload**(`file`: `Blob`): `Promise`\<`boolean`\>
Uploads an file synchronously and returns the default path upon completion.
Creates a promise wrapper around the asynchronous upload process.
Subscribes to state changes and resolves/rejects based on upload completion state.
#### Parameters
| Parameter | Type | Description |
| --------- | ------ | -------------------------------------------------- |
| `file` | `Blob` | The blob object containing the file data to upload |
#### Returns
`Promise`\<`boolean`\>
Promise that resolves with the default path string if successful, null if failed
#### Example
```ts
const uploadFileMachine = new UploadFileMachine({
pathWithoutExtension: 'path/to/file',
authHeader: 'Bearer token',
description: 'File description',
apiEndpoint: 'https://api.example.com/upload',
});
uploadFileMachine
.syncUpload(file)
.then((path) => {
console.log('File uploaded successfully to:', path);
})
.catch(() => {
console.error('File upload failed');
});
```
### upload()
> **upload**(`file`: `Blob`): `void`
Upload file.
#### Parameters
| Parameter | Type | Description |
| --------- | ------ | -------------- |
| `file` | `Blob` | File to upload |
#### Returns
`void`
#### Example
```ts
const uploadFileMachine = new UploadFileMachine({
pathWithoutExtension: 'path/to/file',
authHeader: 'Bearer token',
description: 'File description',
apiEndpoint: 'https://api.example.com/upload',
});
uploadFileMachine.subscribe(({ state }) => {
if (state === 'complete') {
console.log('File uploaded successfully');
} else if (state === 'failed') {
console.error('File upload failed');
}
});
uploadFileMachine.upload(file);
```
### uploadFile\_()
> `protected` **uploadFile\_**(): `void`
#### Returns
`void`