-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.ts
129 lines (111 loc) · 3.69 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
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 renameFileEntity = async (
fileEntityID: string,
fileEntityNewName: string
): Promise<void> => {
const rename_resp = await fetch('/api/filesystem/rename', {
method: 'POST',
body: new URLSearchParams({
EntityID: fileEntityID,
NewName: fileEntityNewName,
}),
});
if (!rename_resp.ok) {
const message = `An error has occured: ${rename_resp.status}`;
throw new Error(message);
}
const response = await rename_resp.json();
console.log(response);
return;
};
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);
return;
};