All files / src/storage/gaia hub.ts

41.66% Statements 5/12
0% Branches 0/2
50% Functions 1/2
41.66% Lines 5/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49  3x 3x                 3x                                                                   3x 1x    
import type { GaiaHubConfig } from './types';
import { getBlockstackErrorFromResponse } from './errors';
import { fetchPrivate } from 'micro-stacks/common';
 
interface UploadToGaiaHub {
  filename: string;
  contents: Blob | Uint8Array | ArrayBufferView | string;
  hubConfig: GaiaHubConfig;
  contentType?: string;
}
 
export async function uploadToGaiaHub(options: UploadToGaiaHub): Promise<any> {
  const { filename, contents, hubConfig, contentType = 'application/octet-stream' } = options;
  const headers: { [key: string]: string } = {
    'Content-Type': contentType,
    Authorization: `bearer ${hubConfig.token}`,
  };
 
  const response = await fetchPrivate(
    `${hubConfig.server}/store/${hubConfig.address}/${filename}`,
    {
      method: 'POST',
      headers,
      body: contents,
    }
  );
 
  Iif (!response.ok) {
    throw await getBlockstackErrorFromResponse(
      response,
      'Error when uploading to Gaia hub.',
      hubConfig
    );
  }
  const responseText = await response.text();
  return JSON.parse(responseText);
}
 
/**
 *
 * @param filename
 * @param hubConfig
 *
 * @ignore
 */
export function getFullReadUrl(filename: string, hubConfig: GaiaHubConfig): Promise<string> {
  return Promise.resolve(`${hubConfig.url_prefix}${hubConfig.address}/${filename}`);
}