@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.
95 lines • 3.1 kB
TypeScript
import { AlwatrFluxStateMachine, type FetchOptions } from '@alwatr/flux';
/**
* Configuration options for initializing an UploadFileMachine instance.
*/
export type UploadFileMachineOptions = {
/**
* Path where the file will be saved, without the file extension
*/
pathWithoutExtension: string;
/**
* Authorization header value for API requests
*/
authHeader: string;
/**
* Description of the file for maintenance purposes
*/
description: string;
/**
* API endpoint URL for upload requests
*/
apiEndpoint: string;
};
export type UploadFileMachineState = 'initial' | 'loading' | 'failed' | 'complete';
type Event = 'request' | 'loading_failed' | 'loading_success';
/**
* A state machine for managing file uploads.
* Handles file upload lifecycle including state management, API requests, and error handling.
*
* @noInheritDoc
*/
export declare class UploadFileMachine extends AlwatrFluxStateMachine<UploadFileMachineState, Event> {
protected fileBlob_: Blob | null;
private apiRequestFetchMachine__;
constructor(options: UploadFileMachineOptions);
/**
* 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.
*
* @param file - The blob object containing the file data to upload
* @returns 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');
* });
* ```
*/
syncUpload(file: Blob): Promise<boolean>;
/**
* Upload file.
*
* @param file - File to upload
*
* @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);
* ```
*/
upload(file: Blob): void;
/**
* Clear the state.
*/
clear(): void;
protected generateFetchOption_(options: UploadFileMachineOptions): FetchOptions;
protected uploadFile_(): void;
}
export {};
//# sourceMappingURL=upload-file-machine.d.ts.map