Upload SDK

Types

Reference for the TypeScript types exported by Upload SDK.

Import shared types from the package root and provider-specific config types from the provider subpath:

import type {
  AssetUploadConfig,
  Duration,
  DurationUnit,
  FileSizeLimit,
  FileSizeUnit,
  MetadataValue,
  PrepareUploadOutput,
  ProviderPrepareUploadInput,
  StorageProvider,
  UploadMetadata,
  UploadSDKErrorCode,
} from "@marinedotsh/upload-sdk";
import type {
  AwsS3Config,
  ImageKitConfig,
} from "@marinedotsh/upload-sdk/providers";

AssetUploadConfig

Defines the settings available for an upload asset.

type AssetUploadConfig<
  TStorageProfileName extends string = string,
> = {
  storageProfile?: TStorageProfileName;
  keyPrefix: string;
  expiresIn?: Duration;

  limits?: {
    maxFileSize?: FileSizeLimit;
    maxFiles?: number;
    concurrency?: number;
  };

  accept?: {
    mimeTypes?: string[];
    extensions?: string[];
  };

  metadata?: UploadMetadata;
};
PropertyRequiredDescription
storageProfileNoStorage profile used by the asset
keyPrefixYesPath added before the generated filename
expiresInNoHow long the prepared upload remains valid
limits.maxFileSizeNoMaximum reported file size
limits.maxFilesNoBatch limit field; not currently enforced
limits.concurrencyNoConcurrency field; not currently enforced
accept.mimeTypesNoAccepted client-reported MIME types
accept.extensionsNoAccepted filename extensions
metadataNoStatic metadata included with the upload
const avatar: AssetUploadConfig<"imageUploads"> = {
  storageProfile: "imageUploads",
  keyPrefix: "avatars",
  accept: {
    mimeTypes: ["image/png", "image/jpeg"],
    extensions: [".png", ".jpg", ".jpeg"],
  },
  limits: {
    maxFileSize: {
      value: 2,
      unit: "MB",
    },
  },
};

See Upload Assets.

PrepareUploadOutput

The signed upload instructions returned by prepareUpload().

type PrepareUploadOutput = {
  strategy: "multipart";
  url: string;
  method: "POST";
  headers: Record<string, string>;
  fields: Record<string, string>;
  key: string;
  expiresAt: string;
};
PropertyDescription
strategyUpload request format
urlProvider upload endpoint
methodHTTP method used for the upload
headersHeaders required by the provider
fieldsSigned multipart form fields
keyGenerated storage key
expiresAtExpiration as an ISO 8601 string

The only supported strategy is currently "multipart" with method "POST".

const preparedUpload: PrepareUploadOutput =
  await uploader.prepareUpload("avatar", input);

See Prepared Uploads.

Prepare-upload input

uploader.prepareUpload() accepts:

{
  filename: string;
  contentType: string;
  size: number;
}
PropertyDescription
filenameClient-reported filename
contentTypeClient-reported MIME type
sizeClient-reported size in bytes
await uploader.prepareUpload("avatar", {
  filename: "profile.png",
  contentType: "image/png",
  size: 120_000,
});

FileSizeUnit

Units accepted by FileSizeLimit.

type FileSizeUnit =
  | "KB"
  | "MB"
  | "GB"
  | "TB";

FileSizeLimit

A human-readable file-size limit.

type FileSizeLimit = {
  value: number;
  unit: FileSizeUnit;
};
const limit: FileSizeLimit = {
  value: 20,
  unit: "MB",
};

The SDK converts the configured value to bytes before preparing the upload.

DurationUnit

Units accepted by Duration.

type DurationUnit =
  | "seconds"
  | "minutes"
  | "hours";

Duration

A duration used for prepared-upload expiration.

type Duration = {
  value: number;
  unit: DurationUnit;
};
const expiration: Duration = {
  value: 5,
  unit: "minutes",
};

The selected provider may apply its own minimum and maximum expiration.

MetadataValue

A value supported in upload metadata.

type MetadataValue =
  | string
  | number
  | boolean;

UploadMetadata

A record of metadata values.

type UploadMetadata =
  Record<string, MetadataValue>;
const metadata: UploadMetadata = {
  purpose: "avatar",
  version: 1,
  temporary: true,
};

Providers encode metadata differently.

AwsS3Config

Configuration accepted by awsS3().

type AwsS3Config = {
  bucket: string;
  region: string;

  credentials: {
    accessKeyId: string;
    secretAccessKey: string;
    sessionToken?: string;
  };
};
PropertyRequiredDescription
bucketYesS3 bucket name
regionYesAWS region containing the bucket
credentials.accessKeyIdYesAWS access key ID
credentials.secretAccessKeyYesAWS secret access key
credentials.sessionTokenNoToken used with temporary credentials
const config: AwsS3Config = {
  bucket: process.env.AWS_BUCKET!,
  region: process.env.AWS_REGION!,
  credentials: {
    accessKeyId: process.env.AWS_ACCESS_KEY_ID!,
    secretAccessKey:
      process.env.AWS_SECRET_ACCESS_KEY!,
    sessionToken: process.env.AWS_SESSION_TOKEN,
  },
};

See AWS S3.

ImageKitConfig

Configuration accepted by imageKit().

type ImageKitConfig = {
  publicKey: string;
  privateKey: string;
};
PropertyDescription
publicKeyImageKit public key
privateKeyPrivate key used to sign uploads
const config: ImageKitConfig = {
  publicKey: process.env.IMAGEKIT_PUBLIC_KEY!,
  privateKey: process.env.IMAGEKIT_PRIVATE_KEY!,
};

See ImageKit.

UploadSDKErrorCode

The machine-readable codes used by UploadSDKError.

type UploadSDKErrorCode =
  | "INVALID_UPLOAD_INPUT"
  | "INVALID_UPLOAD_CONFIG"
  | "UNSUPPORTED_UPLOAD_OPERATION"
  | "UPLOAD_PROVIDER_ERROR";
CodeMeaning
INVALID_UPLOAD_INPUTReported file information is invalid or not accepted
INVALID_UPLOAD_CONFIGAsset or provider configuration is invalid
UNSUPPORTED_UPLOAD_OPERATIONThe requested operation is not supported
UPLOAD_PROVIDER_ERRORThe provider could not prepare the upload

See Validation and Errors.

Provider-facing types

Most applications do not need to use these types directly. They describe the contract between the uploader and its provider implementations.

ProviderPrepareUploadInput

Normalized information passed to a provider after asset validation and key generation.

type ProviderPrepareUploadInput = {
  key: string;
  contentType: string;
  expiresInSeconds?: number;
  metadata?: UploadMetadata;

  accept?: {
    mimeTypes?: string[];
  };

  limits?: {
    maxFileSizeBytes?: number;
  };
};

Filename extensions are validated before this stage, so the provider receives the final generated key rather than the original filename.

StorageProvider

The provider shape used by the uploader.

type StorageProvider = {
  prepareUpload(
    input: ProviderPrepareUploadInput,
  ): Promise<PrepareUploadOutput>;
};

The built-in awsS3() and imageKit() factories return this shape.

Next steps

On this page