All files / src apps.ts

0% Statements 0/23
0% Branches 0/8
0% Functions 0/3
0% Lines 0/23

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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106                                                                                                                                                                                                                   
import * as did from './did'
import * as ucan from './ucan'
import * as ucanInternal from './ucan/internal'
import { api, Maybe, isDefined } from './common'
import { setup } from './setup/internal'
 
 
export type App = {
  domain: string
}
 
 
 
/**
 * Get A list of all of your apps and their associated domain names
 */
export async function index(): Promise<Array<App>> {
  const apiEndpoint = setup.endpoints.api
 
  const localUcan = await ucanInternal.lookupFilesystemUcan("*")
  if (localUcan === null) {
    throw "Could not find your local UCAN"
  }
 
  const jwt = ucan.encode(await ucan.build({
    audience: await api.did(),
    issuer: await did.ucan(),
    proof: localUcan, 
    potency: null
  }))
 
  const response = await fetch(`${apiEndpoint}/app`, {
    method: 'GET',
    headers: {
      'authorization': `Bearer ${jwt}`
    }
  })
 
  const data = await response.json();
  return data
}
 
/**
 * Creates a new app, assigns an initial subdomain, and sets an asset placeholder
 *
 * @param subdomain Subdomain to create the fission app with
 */
export async function create(
  subdomain: Maybe<string>
): Promise<App> {
  const apiEndpoint = setup.endpoints.api
 
  const localUcan = await ucanInternal.lookupFilesystemUcan("*")
  if (localUcan === null) {
    throw "Could not find your local UCAN"
  }
 
  const jwt = ucan.encode(await ucan.build({
    audience: await api.did(),
    issuer: await did.ucan(),
    proof: localUcan, 
    potency: null
  }))
 
  const url = isDefined(subdomain) ? `${apiEndpoint}/app?${subdomain}` : `${apiEndpoint}/app`
 
  const response = await fetch(url, {
    method: 'POST',
    headers: {
      'authorization': `Bearer ${jwt}`
    }
  })
  const data = await response.json();
  return data
}
 
/**
 * Destroy app by any associated URL
 *
 * @param url The url we want to delete
 */
export async function deleteByURL(
  url: string
): Promise<void> {
  const apiEndpoint = setup.endpoints.api
 
  const localUcan = await ucanInternal.lookupFilesystemUcan("*")
  if (localUcan === null) {
    throw new Error("Could not find your local UCAN")
  }
 
  const jwt = ucan.encode(await ucan.build({
    audience: await api.did(),
    issuer: await did.ucan(),
    proof: localUcan, 
    potency: null
  }))
 
  await fetch(`${apiEndpoint}/app/associated/${url}`, {
    method: 'DELETE',
    headers: {
      'authorization': `Bearer ${jwt}`
    }
  })
}