API for Assets Upload

For asset uploads, we build on top of an open-source standard: https://tus.io/.
To upload, you can use any of the Tus client libraries.

Javascript Example

This example requires that the npm module tus-js-client is installed.

import * as tus from 'tus-js-client';
import { Upload as TusUpload } from 'tus-js-client';
export interface IUpload extends TusUpload {
  url: string;
}
export interface IHandleFile {
  file: File | Blob | Pick<ReadableStreamDefaultReader, 'read'>;
  token: string;
  projectId: string;
  meta?: any;
  endpoint?: string;
  mode: string;
}

export const uploadFile = async ({
  file,
  token,
  projectId,
  meta = {},
  endpoint = 'https://cloud.caisy.io/',
  mode = 'document',
}: IHandleFile) => {
  const headers = {
    'x-caisy-token': token,
    'x-caisy-project-id': projectId,
  };
  return new Promise((resolve, reject) => {
    const upload = new tus.Upload(file, {
      headers,
      endpoint: endpoint + '/upload/',
      onError: function(error: Error) {
        reject(error);
      },
      metadata: {
        mode,
        filename: (file as File).name,
        filetype: (file as File).type,
        ...meta,
      },
      parallelUploads: 1,
      chunkSize: 3 * Math.pow(10, 6),
      onSuccess: function() {
        const assetUrl = upload.url.replace('/upload/', '/assets/');
        resolve(assetUrl);
      },
    }) as IUpload;

    upload.start();
  });
};

It's important to note that the project-id and token must be passed as HTTP headers for authorization.