@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.
184 lines (116 loc) • 5.27 kB
Markdown
[@nexim/upload-sdk](../README.md) / UploadImageMachine
# Class: UploadImageMachine
Specialized state machine for handling image uploads.
Extends UploadFileMachine with image-specific functionality including:
- Image resizing based on preset configurations
- Client-side image optimization
- Automatic format conversion
## Extends
- [`UploadFileMachine`](UploadFileMachine.md)
## Constructors
### Constructor
> **new UploadImageMachine**(`options`: [`UploadImageMachineOptions`](../type-aliases/UploadImageMachineOptions.md)): `UploadImageMachine`
#### Parameters
| Parameter | Type |
| --------- | --------------------------------------------------------------------------- |
| `options` | [`UploadImageMachineOptions`](../type-aliases/UploadImageMachineOptions.md) |
#### Returns
`UploadImageMachine`
#### Overrides
[`UploadFileMachine`](UploadFileMachine.md).[`constructor`](UploadFileMachine.md#constructor)
## Properties
| Property | Modifier | Type | Default value | Description | Inherited from |
| -------------------------------------- | ----------- | ---------------- | ------------- | ---------------------------------------------- | ----------------------------------------------------------------------------------------- |
| <a id="defaultpath"></a> `defaultPath` | `public` | `string` | `undefined` | The default path where the image will be saved | - |
| <a id="fileblob_"></a> `fileBlob_` | `protected` | `null` \| `Blob` | `null` | - | [`UploadFileMachine`](UploadFileMachine.md).[`fileBlob_`](UploadFileMachine.md#fileblob_) |
## Methods
### clear()
> **clear**(): `void`
Clear the state.
#### Returns
`void`
#### Inherited from
[`UploadFileMachine`](UploadFileMachine.md).[`clear`](UploadFileMachine.md#clear)
---
### generateFetchOption\_()
> `protected` **generateFetchOption\_**(`options`: [`UploadImageMachineOptions`](../type-aliases/UploadImageMachineOptions.md)): `FetchOptions`
Generates fetch options for the image upload request.
Extends the base fetch options with image-specific parameters.
#### Parameters
| Parameter | Type |
| --------- | --------------------------------------------------------------------------- |
| `options` | [`UploadImageMachineOptions`](../type-aliases/UploadImageMachineOptions.md) |
#### Returns
`FetchOptions`
#### Overrides
[`UploadFileMachine`](UploadFileMachine.md).[`generateFetchOption_`](UploadFileMachine.md#generatefetchoption_)
---
### 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');
});
```
#### Inherited from
[`UploadFileMachine`](UploadFileMachine.md).[`syncUpload`](UploadFileMachine.md#syncupload)
---
### upload()
> **upload**(`file`: `Blob`): `void`
Initiates an image upload process.
The image will be optimized according to the preset configuration before uploading.
#### Parameters
| Parameter | Type | Description |
| --------- | ------ | ------------------------ |
| `file` | `Blob` | The image file to upload |
#### Returns
`void`
#### Example
```ts
const uploadImageMachine = new UploadImageMachine({
pathWithoutExtension: 'path/to/image',
authHeader: 'Bearer token',
description: 'Image description',
apiEndpoint: 'https://api.example.com/upload',
presetName: 'thumbnail',
});
uploadImageMachine.subscribe(({ state }) => {
if (state === 'complete') {
console.log('Image uploaded successfully');
} else if (state === 'failed') {
console.error('Image upload failed');
}
});
uploadImageMachine.upload(file);
```
#### Overrides
[`UploadFileMachine`](UploadFileMachine.md).[`upload`](UploadFileMachine.md#upload)
---
### uploadFile\_()
> `protected` **uploadFile\_**(): `void`
#### Returns
`void`
#### Inherited from
[`UploadFileMachine`](UploadFileMachine.md).[`uploadFile_`](UploadFileMachine.md#uploadfile_)