### Install Smash Uploader JS via npm Source: https://github.com/fromsmash/smash-uploader-js/blob/main/README.md Install the library using npm for Node.js projects. ```bash npm install @smash-sdk/uploader ``` -------------------------------- ### Create SmashUploader Instance Source: https://github.com/fromsmash/smash-uploader-js/blob/main/README.md Instantiate the SmashUploader with required region and API token. Ensure the region matches your API key's region. ```javascript const uploader = new SmashUploader({ region: "eu-west-3", token: "Put your Smash API Key here" }) ``` -------------------------------- ### Import SmashUploader Library Source: https://github.com/fromsmash/smash-uploader-js/blob/main/README.md Import the SmashUploader class using either ES6 modules or CommonJS syntax. ```javascript // Using ES6 module import { SmashUploader } from '@smash-sdk/uploader'; ``` ```javascript // Or using CommonJS module const { SmashUploader } = require('@smash-sdk/uploader'); ``` -------------------------------- ### SmashUploader Constructor Source: https://github.com/fromsmash/smash-uploader-js/blob/main/README.md Instantiates the SmashUploader with the specified region and API token. ```APIDOC ## new SmashUploader(options) ### Description Initializes a new instance of the SmashUploader. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters - **options** (object) - Required - Configuration options for the uploader. - **region** (string) - Required - The Smash region to use. Must be one of: 'eu-west-1', 'eu-west-2', 'eu-west-3', 'eu-central-1', 'us-east-1', 'us-east-2', 'us-west-1', 'us-west-2', 'ca-central-1'. - **token** (string) - Required - Your Smash API Key. ### Request Example ```javascript const uploader = new SmashUploader({ region: "eu-west-3", token: "Put your Smash API Key here" }) ``` ### Response None (constructor does not return a value directly, but initializes the object). #### Success Response (N/A) #### Response Example (N/A) ``` -------------------------------- ### Listen to Upload Progress Events in JavaScript Source: https://github.com/fromsmash/smash-uploader-js/blob/main/README.md Use the `on` method to subscribe to the 'progress' event. The event data includes total bytes, uploaded bytes, percentage, speed, and estimated remaining time. ```javascript uploader.on('progress', (event) => { console.log(`Upload progress: ${event.data.percent}%`); }); ``` -------------------------------- ### Include Smash Uploader JS via CDN Source: https://github.com/fromsmash/smash-uploader-js/blob/main/README.md Include the library directly from unpkg CDN for browser usage. ```html ``` -------------------------------- ### Upload Files in Node.js Source: https://github.com/fromsmash/smash-uploader-js/blob/main/README.md Upload a list of local files using the uploader instance in a Node.js environment. Handles success and error logging. ```javascript const files = [ "./dummyFiles/dummy1.png", "./dummyFiles/dummy2.png", "./dummyFiles/dummy3.txt", ]; uploader.upload({ files }) .then(({ transfer }) => { console.log("Transfer", transfer.transferUrl); }) .catch(error => { console.log("Error", error); }); ``` -------------------------------- ### uploader.upload Source: https://github.com/fromsmash/smash-uploader-js/blob/main/README.md Uploads a list of files using the configured uploader instance. ```APIDOC ## uploader.upload(params) ### Description Initiates the file upload process. ### Method `upload` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters - **params** (object) - Required - An object containing the files to upload. - **files** (Array) - Required - An array of files to upload. In Node.js, these can be file paths (strings). In the browser, these are File objects. ### Request Example ```javascript // Node.js example const files = ["./dummyFiles/dummy1.png"]; uploader.upload({ files }) // Browser example const fileInput = document.querySelector('input[type="file"]'); uploader.upload({ files: [...fileInput.files] }) ``` ### Response #### Success Response (Promise) - **transfer** (object) - Contains information about the transfer. - **transferUrl** (string) - The URL of the initiated transfer. #### Response Example ```json { "transfer": { "transferUrl": "https://smash.com/transfer/abcdef12345" } } ``` ### Error Handling - The promise will reject with an error object if the upload fails. ``` -------------------------------- ### Upload Files in Browser Source: https://github.com/fromsmash/smash-uploader-js/blob/main/README.md Upload files selected via an HTML file input element in a browser environment. Handles success and error logging. ```javascript const fileInput = document.querySelector('input[type="file"]'); const files = [...fileInput.files[0]]; uploader.upload({ files: [...fileInput.files] }) .then(({ transfer }) => { console.log("Transfer", transfer.transferUrl); }) .catch(error => { console.log("Error", error); }); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.