UNPKG

3.78 kBJavaScriptView Raw
1import * as did from "../did/index.js";
2import * as ucan from "../ucan/index.js";
3import { api, isString } from "../common/index.js";
4import { setup } from "../setup/internal.js";
5/**
6 * Get A list of all of your apps and their associated domain names
7 */
8export async function index() {
9 const apiEndpoint = setup.endpoints.api;
10 const localUcan = await ucan.dictionary.lookupAppUcan("*");
11 if (localUcan === null) {
12 throw "Could not find your local UCAN";
13 }
14 const jwt = ucan.encode(await ucan.build({
15 audience: await api.did(),
16 issuer: await did.ucan(),
17 proof: localUcan,
18 potency: null
19 }));
20 const response = await fetch(`${apiEndpoint}/app`, {
21 method: 'GET',
22 headers: {
23 'authorization': `Bearer ${jwt}`
24 }
25 });
26 const data = await response.json();
27 return Object
28 .values(data)
29 .filter(v => v.length > 0)
30 .map(v => ({ domain: v[0] }));
31}
32/**
33 * Creates a new app, assigns an initial subdomain, and sets an asset placeholder
34 *
35 * @param subdomain Subdomain to create the fission app with
36 */
37export async function create(subdomain) {
38 const apiEndpoint = setup.endpoints.api;
39 const localUcan = await ucan.dictionary.lookupAppUcan("*");
40 if (localUcan === null) {
41 throw "Could not find your local UCAN";
42 }
43 const jwt = ucan.encode(await ucan.build({
44 audience: await api.did(),
45 issuer: await did.ucan(),
46 proof: localUcan,
47 potency: null
48 }));
49 const url = isString(subdomain)
50 ? `${apiEndpoint}/app?subdomain=${encodeURIComponent(subdomain)}`
51 : `${apiEndpoint}/app`;
52 const response = await fetch(url, {
53 method: 'POST',
54 headers: {
55 'authorization': `Bearer ${jwt}`
56 }
57 });
58 const data = await response.json();
59 return { domain: data };
60}
61/**
62 * Destroy app by any associated domain
63 *
64 * @param domain The domain associated with the app we want to delete
65 */
66export async function deleteByDomain(domain) {
67 const apiEndpoint = setup.endpoints.api;
68 const localUcan = await ucan.dictionary.lookupAppUcan(domain);
69 if (localUcan === null) {
70 throw new Error("Could not find your local UCAN");
71 }
72 const jwt = ucan.encode(await ucan.build({
73 audience: await api.did(),
74 issuer: await did.ucan(),
75 proof: localUcan,
76 potency: null
77 }));
78 const appIndexResponse = await fetch(`${apiEndpoint}/app`, {
79 method: 'GET',
80 headers: {
81 'authorization': `Bearer ${jwt}`
82 }
83 });
84 const index = await appIndexResponse.json();
85 const appToDelete = Object.entries(index).find(([_, domains]) => domains.includes(domain));
86 if (appToDelete == null) {
87 throw new Error(`Couldn't find an app with domain ${domain}`);
88 }
89 await fetch(`${apiEndpoint}/app/${encodeURIComponent(appToDelete[0])}`, {
90 method: 'DELETE',
91 headers: {
92 'authorization': `Bearer ${jwt}`
93 }
94 });
95}
96/**
97 * Updates an app by CID
98 *
99 * @param subdomain Subdomain to create the fission app with
100 */
101export async function publish(domain, cid) {
102 const apiEndpoint = setup.endpoints.api;
103 const localUcan = await ucan.dictionary.lookupAppUcan(domain);
104 if (localUcan === null) {
105 throw "Could not find your local UCAN";
106 }
107 const jwt = ucan.encode(await ucan.build({
108 audience: await api.did(),
109 issuer: await did.ucan(),
110 proof: localUcan,
111 potency: null
112 }));
113 const url = `${apiEndpoint}/app/${domain}/${cid}`;
114 await fetch(url, {
115 method: 'PATCH',
116 headers: {
117 'authorization': `Bearer ${jwt}`
118 }
119 });
120}
121//# sourceMappingURL=index.js.map
\No newline at end of file