@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.
82 lines • 3.09 kB
TypeScript
import { uploadImagePresetRecord } from '@nexim/upload-types';
import { UploadFileMachine, type UploadFileMachineOptions, type UploadFileMachineState } from './upload-file-machine.js';
import type { FetchOptions } from '@alwatr/flux';
/**
* Configuration options for initializing an UploadImageMachine instance.
* Extends UploadFileMachineOptions with image-specific options.
*/
export type UploadImageMachineOptions = UploadFileMachineOptions & {
/**
* The preset name that defines image processing parameters.
* Must be a key from the uploadImagePresetRecord.
*/
presetName: keyof typeof uploadImagePresetRecord;
};
/**
* States for the image upload machine.
* Inherits all states from UploadFileMachineState.
*/
export type UploadImageMachineState = UploadFileMachineState;
/**
* 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
*/
export declare class UploadImageMachine extends UploadFileMachine {
/** The default path where the image will be saved */
defaultPath: string;
/** Stores preset configuration for the current upload */
private uploadImagePresetRecord__;
/** Temporarily stores the original file before optimization */
private nonOptimizedFileBlob__;
constructor(options: UploadImageMachineOptions);
/**
* Initiates an image upload process.
* The image will be optimized according to the preset configuration before uploading.
*
* @param file - The image file to upload
*
* @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);
* ```
*/
upload(file: Blob): void;
/**
* Handles the image resizing process before uploading.
* Called automatically when entering the loading state.
*/
private uploadAfterResize__;
/**
* Generates fetch options for the image upload request.
* Extends the base fetch options with image-specific parameters.
*/
protected generateFetchOption_(options: UploadImageMachineOptions): FetchOptions;
/**
* Resizes an image according to preset configuration.
* Maintains aspect ratio when only width or height is specified.
*
* @param file - The original image file
* @returns A promise resolving to the resized image blob
*/
private resizeImage__;
}
//# sourceMappingURL=upload-image-machine.d.ts.map