-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.ts
108 lines (93 loc) · 2.95 KB
/
index.ts
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
107
108
import { toFileOrFolder } from './helpers';
import { JSONFileFormat } from './types';
const DEFAULT_OWNER_GROUP = '1';
// Given a file ID (if no ID is provided root is assumed), returns
// a FileFormat of that file from the backend
export async function getFolder(id?: string) {
const ending = id === undefined ? '' : `?EntityID=${id}`;
const folder_resp = await fetch(`/api/filesystem/info${ending}`);
if (!folder_resp.ok) {
const message = `An error has occured: ${folder_resp.status}`;
throw new Error(message);
}
const folder_json = await folder_resp.json();
return toFileOrFolder(folder_json.Response);
}
// Given a file ID, sets the `contents` state variable to the children
// of that file
export async function updateContents(id: string) {
// const id = getCurrentID();
const children_resp = await fetch(`/api/filesystem/info?EntityID=${id}`);
if (!children_resp.ok) {
const message = `An error has occured: ${children_resp.status}`;
throw new Error(message);
}
const children_json = await children_resp.json();
const children = children_json.Response.Children.map(
(child: JSONFileFormat) => {
return toFileOrFolder(child);
}
);
return children;
}
export const newFile = async (
name: string,
parentID: string
): Promise<string> => {
// This isn't attached to the parent folder yet,
// TODO: patch once auth is finished
const create_resp = await fetch('/api/filesystem/create', {
method: 'POST',
body: new URLSearchParams({
LogicalName: name,
Parent: parentID.toString(),
OwnerGroup: DEFAULT_OWNER_GROUP,
IsDocument: 'true',
}),
});
if (!create_resp.ok) {
const message = `An error has occured: ${create_resp.status}`;
throw new Error(message);
}
const response = await create_resp.json();
console.log(response);
console.log(JSON.stringify(response));
return response.Response.NewID;
};
export const newFolder = async (
name: string,
parentID: string
): Promise<string> => {
// TODO: patch once auth is finished
const create_resp = await fetch('/api/filesystem/create', {
method: 'POST',
body: new URLSearchParams({
LogicalName: name,
Parent: parentID.toString(),
OwnerGroup: DEFAULT_OWNER_GROUP,
IsDocument: 'false',
}),
});
if (!create_resp.ok) {
const message = `An error has occured: ${create_resp.status}`;
throw new Error(message);
}
const response = await create_resp.json();
return response.Response.NewID;
};
export const deleteFileEntity = async (fileEntityID: string): Promise<void> => {
const delete_resp = await fetch('/api/filesystem/delete', {
method: 'POST',
body: new URLSearchParams({
EntityID: fileEntityID,
}),
});
if (!delete_resp.ok) {
const message = `An error has occured: ${delete_resp.status}`;
throw new Error(message);
}
const response = await delete_resp.json();
console.log(response);
console.log(JSON.stringify(response));
return;
};