optiq-image / upload
upload
optiq-image/upload — upload a compressed (or original) image to S3 through a presigned URL issued by your backend. Start with compressAndUpload.
Classes
UploadError
Extends
Error
Constructors
Constructor
ts
new UploadError(message, status?): UploadError;Parameters
| Parameter | Type |
|---|---|
message | string |
status? | number |
Returns
Overrides
Error.constructor
Properties
status?
ts
readonly status?: number;Interfaces
CompressAndUploadOptions
Properties
| Property | Type | Description |
|---|---|---|
compress? | (input, options) => Promise<CompressionResult> | Custom compressor (defaults to the shared compressImage). |
compression? | | false | Omit<CompressCallOptions, "signal" | "onProgress"> | Optiq options, or false to upload the original as-is. |
onCompressProgress? | (event) => void | - |
onStage? | (stage) => void | - |
onUploadProgress? | (p) => void | - |
requestPresignedUpload | RequestPresignedUpload | - |
signal? | AbortSignal | - |
transport? | HttpTransport | - |
CompressAndUploadResult
Properties
| Property | Type | Description |
|---|---|---|
bytes | number | - |
compression? | CompressionResult | - |
compressionError? | OptiqError | Set when compression failed and the original was uploaded instead. |
contentType | string | - |
fileName | string | - |
key? | string | - |
uploaded | "original" | "optimized" | Which bytes were uploaded. |
HttpRequest
Properties
| Property | Type |
|---|---|
body | Blob | FormData |
headers | Record<string, string> |
method | "PUT" | "POST" |
onProgress? | (p) => void |
signal? | AbortSignal |
url | string |
HttpResponse
Properties
| Property | Type |
|---|---|
headers | Record<string, string> |
status | number |
PresignedUpload
What your backend returns for a presigned S3 upload. Credentials never reach the browser.
Properties
PresignRequest
Properties
| Property | Type |
|---|---|
contentType | string |
fileName | string |
signal? | AbortSignal |
size | number |
UploadProgress
Properties
| Property | Type |
|---|---|
loaded | number |
total | number |
Type Aliases
HttpTransport
ts
type HttpTransport = (req) => Promise<HttpResponse>;Parameters
| Parameter | Type |
|---|---|
req | HttpRequest |
Returns
Promise<HttpResponse>
RequestPresignedUpload
ts
type RequestPresignedUpload = (req) => Promise<PresignedUpload>;Parameters
| Parameter | Type |
|---|---|
req | PresignRequest |
Returns
Promise<PresignedUpload>
Variables
xhrTransport
ts
const xhrTransport: HttpTransport;XMLHttpRequest transport: unlike fetch, it reports upload progress in all browsers.
Functions
compressAndUpload()
ts
function compressAndUpload(file, opts): Promise<CompressAndUploadResult>;Compress (optional, never required) then upload through a presigned URL from your backend. If compression fails or does not help, the original file is uploaded unchanged. Upload errors are thrown; the caller still holds the original file and can retry.
Parameters
| Parameter | Type |
|---|---|
file | File |
opts | CompressAndUploadOptions |
Returns
Promise<CompressAndUploadResult>
uploadToPresignedUrl()
ts
function uploadToPresignedUrl(
body,
target,
opts
): Promise<HttpResponse>;Parameters
| Parameter | Type |
|---|---|
body | Blob |
target | PresignedUpload |
opts | { contentType: string; fileName?: string; onProgress?: (p) => void; signal?: AbortSignal; transport?: HttpTransport; } |
opts.contentType | string |
opts.fileName? | string |
opts.onProgress? | (p) => void |
opts.signal? | AbortSignal |
opts.transport? | HttpTransport |
Returns
Promise<HttpResponse>