/
index.ts
149 lines (131 loc) · 3.88 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
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { DataConnector, ISettingRegistry, URLExt } from '@jupyterlab/coreutils';
import { ServerConnection } from '../serverconnection';
/**
* The url for the lab settings service.
*/
const SERVICE_SETTINGS_URL = 'api/settings';
/**
* The settings API service manager.
*/
export class SettingManager extends DataConnector<
ISettingRegistry.IPlugin,
string
> {
/**
* Create a new setting manager.
*/
constructor(options: SettingManager.IOptions = {}) {
super();
this.serverSettings =
options.serverSettings || ServerConnection.makeSettings();
}
/**
* The server settings used to make API requests.
*/
readonly serverSettings: ServerConnection.ISettings;
/**
* Fetch a plugin's settings.
*
* @param id - The plugin's ID.
*
* @returns A promise that resolves if successful.
*/
async fetch(id: string): Promise<ISettingRegistry.IPlugin> {
if (!id) {
throw new Error('Plugin `id` parameter is required for settings fetch.');
}
const { serverSettings } = this;
const { baseUrl, appUrl } = serverSettings;
const { makeRequest, ResponseError } = ServerConnection;
const base = baseUrl + appUrl;
const url = Private.url(base, id);
const response = await makeRequest(url, {}, serverSettings);
if (response.status !== 200) {
throw new ResponseError(response);
}
// Assert what type the server response is returning.
return response.json() as Promise<ISettingRegistry.IPlugin>;
}
/**
* Fetch the list of all plugin setting bundles.
*
* @returns A promise that resolves if successful.
*/
async list(): Promise<{ ids: string[]; values: ISettingRegistry.IPlugin[] }> {
const { serverSettings } = this;
const { baseUrl, appUrl } = serverSettings;
const { makeRequest, ResponseError } = ServerConnection;
const base = baseUrl + appUrl;
const url = Private.url(base, '');
const response = await makeRequest(url, {}, serverSettings);
if (response.status !== 200) {
throw new ResponseError(response);
}
const json = await response.json();
const values = ((json || {})['settings'] || []).map(
(plugin: ISettingRegistry.IPlugin) => {
plugin.data = { composite: {}, user: {} };
return plugin;
}
) as ISettingRegistry.IPlugin[];
const ids = values.map(plugin => plugin.id);
return { ids, values };
}
/**
* Save a plugin's settings.
*
* @param id - The plugin's ID.
*
* @param raw - The user setting values as a raw string of JSON with comments.
*
* @returns A promise that resolves if successful.
*/
async save(id: string, raw: string): Promise<void> {
const { serverSettings } = this;
const { baseUrl, appUrl } = serverSettings;
const { makeRequest, ResponseError } = ServerConnection;
const base = baseUrl + appUrl;
const url = Private.url(base, id);
const init = { body: raw, method: 'PUT' };
const response = await makeRequest(url, init, serverSettings);
if (response.status !== 204) {
throw new ResponseError(response);
}
}
}
/**
* A namespace for `SettingManager` statics.
*/
export namespace SettingManager {
/**
* The instantiation options for a setting manager.
*/
export interface IOptions {
/**
* The server settings used to make API requests.
*/
serverSettings?: ServerConnection.ISettings;
}
}
/**
* A namespace for setting API interfaces.
*/
export namespace Setting {
/**
* The interface for the setting system manager.
*/
export interface IManager extends SettingManager {}
}
/**
* A namespace for private data.
*/
namespace Private {
/**
* Get the url for a plugin's settings.
*/
export function url(base: string, id: string): string {
return URLExt.join(base, SERVICE_SETTINGS_URL, id);
}
}