-
-
Notifications
You must be signed in to change notification settings - Fork 552
/
Copy pathcontents.ts
258 lines (240 loc) Β· 6.9 KB
/
contents.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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import { Notebook } from "@nteract/commutable";
import querystring from "querystring";
import { Observable } from "rxjs";
import { ajax, AjaxResponse } from "rxjs/ajax";
import urljoin from "url-join";
import { ServerConfig } from "@nteract/types";
import { createAJAXSettings, JupyterAjaxResponse } from "./base";
const formURI = (path: string) => urljoin("/api/contents/", path);
const formCheckpointURI = (path: string, checkpointID: string) =>
urljoin("/api/contents/", path, "checkpoints", checkpointID);
export type FileType = "directory" | "file" | "notebook";
/*********************************************
* Contents API request and response payloads
*********************************************/
/**
* Just the Stat call portion of the contents API
* (no content property)
*/
export interface IStatContent<FT extends FileType = FileType> {
name: string;
path: string;
type: FT;
writable: boolean;
created: string;
last_modified: string;
mimetype: string;
format: string;
}
/**
* For directory listings and when a GET is performed against content with ?content=0
* the content field is null
*/
export interface IEmptyContent<FT extends FileType = FileType>
extends IStatContent<FT> {
content: null;
}
/**
* Full Payloads from the contents API
*/
export interface IContent<FT extends FileType = FileType>
extends IStatContent<FT> {
content: FT extends "file"
? string
: FT extends "notebook"
? Notebook
: FT extends "directory"
? Array<IEmptyContent<FT>>
: null;
}
/**
* Creates an AjaxObservable for removing content.
*
* @param serverConfig The server configuration
* @param path The path to the content
*
* @returns An Observable with the request response
*/
export const remove = (serverConfig: ServerConfig, path: string) =>
ajax(
createAJAXSettings(serverConfig, formURI(path), {
method: "DELETE"
})
);
interface IGetParams {
type: "file" | "directory" | "notebook";
format: "text" | "base64" | string;
content: 0 | 1;
}
/**
* Creates an AjaxObservable for getting content at a path
*
* @param serverConfig The server configuration
* @param path The content to fetch
* @param params type, format, content
* @param params.type file type, one of 'file', 'directory', 'notebook'
* @param params.format How file content should be returned, e.g. 'text', 'base64'
* @param params.content Return content or not (0 => no content, 1 => content please)
*
* @returns An Observable with the request response
*/
export function get(
serverConfig: ServerConfig,
path: string,
params: Partial<IGetParams> = {}
) {
let uri = formURI(path);
const query = querystring.stringify(params);
if (query.length > 0) {
uri = `${uri}?${query}`;
}
// NOTE: If the user requests with params.content === 0
// Then the response is IEmptyContent
return ajax(
createAJAXSettings(serverConfig, uri, { cache: false })
) as Observable<JupyterAjaxResponse<IContent<FileType>>>;
}
/**
* Creates an AjaxObservable for renaming a file.
*
* @param serverConfig The server configuration
* @param path The content to rename.
* @param model The data to send in the server request
*
* @returns An Observable with the request response
*/
export function update<FT extends FileType>(
serverConfig: ServerConfig,
path: string,
model: Partial<IContent>
) {
return ajax(
createAJAXSettings(serverConfig, formURI(path), {
body: model,
headers: {
"Content-Type": "application/json"
},
method: "PATCH"
})
);
}
/**
* Creates an AjaxObservable for creating content
*
* @param serverConfig The server configuration
* @param path The path to the content
* @param model The data to send in the server request
*
* @returns An Observable with the request response
*/
export function create<FT extends FileType>(
serverConfig: ServerConfig,
path: string,
model: Partial<IContent<FT>> & { type: FT }
): Observable<AjaxResponse> {
return ajax(
createAJAXSettings(serverConfig, formURI(path), {
body: model,
headers: {
"Content-Type": "application/json"
},
method: "POST"
})
);
}
/**
* Creates an AjaxObservable for saving the file in the location specified by
* name and path in the model.
*
* @param serverConfig The server configuration
* @param path The path to the content
* @param model The data to send in the server request
*
* @returns An Observable with the request response
*/
export function save<FT extends FileType>(
serverConfig: ServerConfig,
path: string,
model: Partial<IContent<FT>>
) {
return ajax(
createAJAXSettings(serverConfig, formURI(path), {
body: model,
headers: {
"Content-Type": "application/json"
},
method: "PUT"
})
) as Observable<
JupyterAjaxResponse<{ path: string; [property: string]: string }>
>;
}
/**
* Creates an AjaxObservable for listing checkpoints for a given file.
*
* @param serverConfig The server configuration
* @param path The content containing checkpoints to be listed.
*
* @returns An Observable with the request response
*/
export const listCheckpoints = (serverConfig: ServerConfig, path: string) =>
ajax(
createAJAXSettings(serverConfig, formCheckpointURI(path, ""), {
cache: false,
method: "GET"
})
);
/**
* Creates an AjaxObservable for creating a new checkpoint with the current state of a file.
* With the default Jupyter FileContentsManager, only one checkpoint is supported,
* so creating new checkpoints clobbers existing ones.
*
* @param serverConfig The server configuration
* @param path The content containing the checkpoint to be created
*
* @returns An Observable with the request response
*/
export const createCheckpoint = (serverConfig: ServerConfig, path: string) =>
ajax(
createAJAXSettings(serverConfig, formCheckpointURI(path, ""), {
method: "POST"
})
);
/**
* Creates an AjaxObservable for deleting a checkpoint for a given file.
*
* @param serverConfig The server configuration
* @param path The content containing the checkpoint to be deleted
* @param checkpointID ID of checkpoint to be deleted
*
* @returns An Observable with the request response
*/
export const deleteCheckpoint = (
serverConfig: ServerConfig,
path: string,
checkpointID: string
) =>
ajax(
createAJAXSettings(serverConfig, formCheckpointURI(path, checkpointID), {
method: "DELETE"
})
);
/**
* Creates an AjaxObservable for restoring a file to a specified checkpoint.
*
* @param serverConfig The server configuration
* @param path The content to restore to a previous checkpoint
* @param checkpointID ID of checkpoint to be used for restoration
*
* @returns An Observable with the request response
*/
export const restoreFromCheckpoint = (
serverConfig: ServerConfig,
path: string,
checkpointID: string
) =>
ajax(
createAJAXSettings(serverConfig, formCheckpointURI(path, checkpointID), {
method: "POST"
})
);