-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
index.ts
245 lines (221 loc) · 6.01 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
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
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { URLExt } from '@jupyterlab/coreutils';
import { JSONObject, JSONValue } from '@lumino/coreutils';
import { ServerConnection } from '..';
/**
* The url for the config service.
*/
const SERVICE_CONFIG_URL = 'api/config';
/**
* A Configurable data section.
*/
export interface IConfigSection {
/**
* The data for this section.
*/
readonly data: JSONObject;
/**
* Modify the stored config values.
*
* #### Notes
* Updates the local data immediately, sends the change to the server,
* and updates the local data with the response, and fulfils the promise
* with that data.
*/
update(newdata: JSONObject): Promise<JSONObject>;
/**
* The server settings for the section.
*/
readonly serverSettings: ServerConnection.ISettings;
}
/**
* The namespace for ConfigSection statics.
*/
export namespace ConfigSection {
/**
* Create a config section.
*
* @returns A Promise that is fulfilled with the config section is loaded.
*/
export function create(
options: ConfigSection.IOptions
): Promise<IConfigSection> {
const section = new DefaultConfigSection(options);
return section.load().then(() => {
return section;
});
}
/**
* The options used to create a config section.
*/
export interface IOptions {
/**
* The section name.
*/
name: string;
/**
* The optional server settings.
*/
serverSettings?: ServerConnection.ISettings;
}
}
/**
* Implementation of the Configurable data section.
*/
class DefaultConfigSection implements IConfigSection {
/**
* Construct a new config section.
*/
constructor(options: ConfigSection.IOptions) {
const settings = (this.serverSettings =
options.serverSettings ?? ServerConnection.makeSettings());
this._url = URLExt.join(
settings.baseUrl,
SERVICE_CONFIG_URL,
encodeURIComponent(options.name)
);
}
/**
* The server settings for the section.
*/
readonly serverSettings: ServerConnection.ISettings;
/**
* Get the data for this section.
*/
get data(): JSONObject {
return this._data;
}
/**
* Load the initial data for this section.
*
* #### Notes
* Uses the [Jupyter Server API](https://petstore.swagger.io/?url=https://raw.githubusercontent.com/jupyter-server/jupyter_server/main/jupyter_server/services/api/api.yaml#!/config).
*
* The promise is fulfilled on a valid response and rejected otherwise.
*/
async load(): Promise<void> {
const response = await ServerConnection.makeRequest(
this._url,
{},
this.serverSettings
);
if (response.status !== 200) {
const err = await ServerConnection.ResponseError.create(response);
throw err;
}
this._data = await response.json();
}
/**
* Modify the stored config values.
*
* #### Notes
* Uses the [Jupyter Server API](https://petstore.swagger.io/?url=https://raw.githubusercontent.com/jupyter-server/jupyter_server/main/jupyter_server/services/api/api.yaml#!/config).
*
* The promise is fulfilled on a valid response and rejected otherwise.
*
* Updates the local data immediately, sends the change to the server,
* and updates the local data with the response, and fulfils the promise
* with that data.
*/
async update(newdata: JSONObject): Promise<JSONObject> {
this._data = { ...this._data, ...newdata };
const init = {
method: 'PATCH',
body: JSON.stringify(newdata)
};
const response = await ServerConnection.makeRequest(
this._url,
init,
this.serverSettings
);
if (response.status !== 200) {
const err = await ServerConnection.ResponseError.create(response);
throw err;
}
this._data = await response.json();
return this._data;
}
private _url = 'unknown';
private _data: JSONObject;
}
/**
* Configurable object with defaults.
*/
export class ConfigWithDefaults {
/**
* Create a new config with defaults.
*/
constructor(options: ConfigWithDefaults.IOptions) {
this._section = options.section;
this._defaults = options.defaults ?? {};
this._className = options.className ?? '';
}
/**
* Get data from the config section or fall back to defaults.
*/
get(key: string): JSONValue {
const data = this._classData();
return key in data ? data[key] : this._defaults[key];
}
/**
* Set a config value.
*
* #### Notes
* Uses the [Jupyter Server API](https://petstore.swagger.io/?url=https://raw.githubusercontent.com/jupyter-server/jupyter_server/main/jupyter_server/services/api/api.yaml#!/config).
*
* The promise is fulfilled on a valid response and rejected otherwise.
*
* Sends the update to the server, and changes our local copy of the data
* immediately.
*/
set(key: string, value: JSONValue): Promise<JSONValue> {
const d: JSONObject = {};
d[key] = value;
if (this._className) {
const d2: JSONObject = {};
d2[this._className] = d;
return this._section.update(d2);
} else {
return this._section.update(d);
}
}
/**
* Get data from the Section with our classname, if available.
*
* #### Notes
* If we have no classname, get all of the data in the Section
*/
private _classData(): JSONObject {
const data = this._section.data;
if (this._className && this._className in data) {
return data[this._className] as JSONObject;
}
return data;
}
private _section: IConfigSection;
private _defaults: JSONObject;
private _className = '';
}
/**
* A namespace for ConfigWithDefaults statics.
*/
export namespace ConfigWithDefaults {
/**
* The options used to initialize a ConfigWithDefaults object.
*/
export interface IOptions {
/**
* The configuration section.
*/
section: IConfigSection;
/**
* The default values.
*/
defaults?: JSONObject;
/**
* The optional classname namespace.
*/
className?: string;
}
}