Repository navigation
Expand file tree
/
Copy pathfilesystemClient.ts
More file actions
177 lines (157 loc) · 6.16 KB
/
Copy pathfilesystemClient.ts
File metadata and controls
177 lines (157 loc) · 6.16 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/**
* HTTP client for the FilesystemApi container.
*
* The transport is injected (`fetch`), so tests drive the real request/response
* handling against a double instead of a live backend.
*/
import type { FileContent, FolderListing } from './types'
export interface FilesystemClient {
/**
* §REQ:TWO-PNL-NVGT-OOX-010 a panel's current folder content
* §REQ:TWO-PNL-NVGT-OOX-030 the same listing, at the subfolder's path
*/
listFolder(path: string): Promise<FolderListing>
/**
* §REQ:TWO-PNL-NVGT-OOX-040 copy the item at `sourcePath` INTO the folder
* `destinationFolderPath` — the path of the destination FOLDER, not of the
* copied item.
*/
copyEntry(sourcePath: string, destinationFolderPath: string): Promise<void>
/**
* §REQ:TWO-PNL-NVGT-OOX-050 move the item at `sourcePath` INTO the folder
* `destinationFolderPath`; the source path stops existing.
*/
moveEntry(sourcePath: string, destinationFolderPath: string): Promise<void>
/** §REQ:TWO-PNL-NVGT-OOX-060 delete the item at `path`, once the user confirmed it. */
deleteEntry(path: string): Promise<void>
/**
* §REQ:TWO-PNL-NVGT-OOX-070 create a folder named `name` inside the folder
* `parentPath`, and return that folder's refreshed listing.
*/
createFolder(parentPath: string, name: string): Promise<FolderListing>
/**
* §REQ:TWO-PNL-NVGT-OOX-080 rename the item at `path` to `newName`, leaving it
* in the same parent folder.
*/
renameEntry(path: string, newName: string): Promise<void>
/** §REQ:TWO-PNL-NVGT-OOX-090 the textual content of the file at `path`. */
readContent(path: string): Promise<FileContent>
}
export interface FilesystemClientOptions {
/** Injected transport; defaults to the global `fetch`. */
fetch?: typeof fetch
/** Origin of the FilesystemApi; empty for a same-origin / proxied deployment. */
baseUrl?: string
}
export class FilesystemApiError extends Error {
readonly status: number
constructor(status: number, message: string) {
super(message)
this.name = 'FilesystemApiError'
this.status = status
}
}
export function createFilesystemClient(
options: FilesystemClientOptions = {},
): FilesystemClient {
const doFetch = options.fetch ?? globalThis.fetch.bind(globalThis)
const baseUrl = (options.baseUrl ?? '').replace(/\/$/, '')
return {
// §REQ:TWO-PNL-NVGT-OOX-010 each panel reads the content of its own current folder
// §REQ:TWO-PNL-NVGT-OOX-030 opening a subfolder is the same read, at the child path
async listFolder(path: string): Promise<FolderListing> {
const query = new URLSearchParams({ path }).toString()
const response = await doFetch(`${baseUrl}/fs?${query}`, {
method: 'GET',
headers: { Accept: 'application/json' },
})
if (!response.ok) {
throw new FilesystemApiError(
response.status,
await readErrorDetail(response),
)
}
return (await response.json()) as FolderListing
},
// §REQ:TWO-PNL-NVGT-OOX-040 the copy command of the active panel
async copyEntry(sourcePath: string, destinationFolderPath: string): Promise<void> {
await postJson(doFetch, `${baseUrl}/fs/copy`, {
sourcePath,
destinationPath: destinationFolderPath,
})
},
// §REQ:TWO-PNL-NVGT-OOX-050 the move command of the active panel
async moveEntry(sourcePath: string, destinationFolderPath: string): Promise<void> {
await postJson(doFetch, `${baseUrl}/fs/move`, {
sourcePath,
destinationPath: destinationFolderPath,
})
},
// §REQ:TWO-PNL-NVGT-OOX-060 issued ONLY after the user confirmed the deletion
async deleteEntry(path: string): Promise<void> {
const query = new URLSearchParams({ path }).toString()
const response = await doFetch(`${baseUrl}/fs?${query}`, {
method: 'DELETE',
headers: { Accept: 'application/json' },
})
if (!response.ok) {
throw new FilesystemApiError(response.status, await readErrorDetail(response))
}
},
// §REQ:TWO-PNL-NVGT-OOX-070 the create-folder command of the active panel
async createFolder(parentPath: string, name: string): Promise<FolderListing> {
return (await postJson(doFetch, `${baseUrl}/fs/folders`, {
path: parentPath,
name,
})) as FolderListing
},
// §REQ:TWO-PNL-NVGT-OOX-080 the rename command of the active panel
async renameEntry(path: string, newName: string): Promise<void> {
const response = await doFetch(`${baseUrl}/fs`, {
method: 'PATCH',
headers: { 'Content-Type': 'application/json', Accept: 'application/json' },
body: JSON.stringify({ path, newName }),
})
if (!response.ok) {
throw new FilesystemApiError(response.status, await readErrorDetail(response))
}
},
// §REQ:TWO-PNL-NVGT-OOX-090 the view command reads the file's text content
async readContent(path: string): Promise<FileContent> {
const query = new URLSearchParams({ path }).toString()
const response = await doFetch(`${baseUrl}/fs/content?${query}`, {
method: 'GET',
headers: { Accept: 'application/json' },
})
if (!response.ok) {
throw new FilesystemApiError(response.status, await readErrorDetail(response))
}
return (await response.json()) as FileContent
},
}
}
/** Shared POST-with-JSON-body plumbing for the filesystem commands. */
async function postJson(
doFetch: typeof fetch,
url: string,
body: Record<string, unknown>,
): Promise<unknown> {
const response = await doFetch(url, {
method: 'POST',
headers: { 'Content-Type': 'application/json', Accept: 'application/json' },
body: JSON.stringify(body),
})
if (!response.ok) {
throw new FilesystemApiError(response.status, await readErrorDetail(response))
}
return response.json().catch(() => null)
}
async function readErrorDetail(response: Response): Promise<string> {
try {
const body = (await response.json()) as { detail?: unknown }
if (typeof body.detail === 'string') return body.detail
} catch {
/* a non-JSON error body is reported by status alone */
}
return `Request failed with status ${response.status}`
}