1 | "use strict";
|
2 |
|
3 |
|
4 | Object.defineProperty(exports, "__esModule", { value: true });
|
5 | exports.SettingManager = void 0;
|
6 | const coreutils_1 = require("@jupyterlab/coreutils");
|
7 | const statedb_1 = require("@jupyterlab/statedb");
|
8 | const serverconnection_1 = require("../serverconnection");
|
9 |
|
10 |
|
11 |
|
12 | const SERVICE_SETTINGS_URL = 'api/settings';
|
13 |
|
14 |
|
15 |
|
16 | class SettingManager extends statedb_1.DataConnector {
|
17 | |
18 |
|
19 |
|
20 | constructor(options = {}) {
|
21 | var _a;
|
22 | super();
|
23 | this.serverSettings = (_a = options.serverSettings) !== null && _a !== void 0 ? _a : serverconnection_1.ServerConnection.makeSettings();
|
24 | }
|
25 | |
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | async fetch(id) {
|
33 | if (!id) {
|
34 | throw new Error('Plugin `id` parameter is required for settings fetch.');
|
35 | }
|
36 | const { serverSettings } = this;
|
37 | const { baseUrl, appUrl } = serverSettings;
|
38 | const { makeRequest, ResponseError } = serverconnection_1.ServerConnection;
|
39 | const base = baseUrl + appUrl;
|
40 | const url = Private.url(base, id);
|
41 | const response = await makeRequest(url, {}, serverSettings);
|
42 | if (response.status !== 200) {
|
43 | const err = await ResponseError.create(response);
|
44 | throw err;
|
45 | }
|
46 |
|
47 | return response.json();
|
48 | }
|
49 | |
50 |
|
51 |
|
52 |
|
53 |
|
54 | async list() {
|
55 | var _a, _b;
|
56 | const { serverSettings } = this;
|
57 | const { baseUrl, appUrl } = serverSettings;
|
58 | const { makeRequest, ResponseError } = serverconnection_1.ServerConnection;
|
59 | const base = baseUrl + appUrl;
|
60 | const url = Private.url(base, '');
|
61 | const response = await makeRequest(url, {}, serverSettings);
|
62 | if (response.status !== 200) {
|
63 | throw new ResponseError(response);
|
64 | }
|
65 | const json = await response.json();
|
66 | const values = (_b = (_a = json === null || json === void 0 ? void 0 : json['settings']) === null || _a === void 0 ? void 0 : _a.map((plugin) => {
|
67 | plugin.data = { composite: {}, user: {} };
|
68 | return plugin;
|
69 | })) !== null && _b !== void 0 ? _b : [];
|
70 | const ids = values.map(plugin => plugin.id);
|
71 | return { ids, values };
|
72 | }
|
73 | |
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 | async save(id, raw) {
|
83 | const { serverSettings } = this;
|
84 | const { baseUrl, appUrl } = serverSettings;
|
85 | const { makeRequest, ResponseError } = serverconnection_1.ServerConnection;
|
86 | const base = baseUrl + appUrl;
|
87 | const url = Private.url(base, id);
|
88 |
|
89 | const init = { body: JSON.stringify({ raw }), method: 'PUT' };
|
90 | const response = await makeRequest(url, init, serverSettings);
|
91 | if (response.status !== 204) {
|
92 | throw new ResponseError(response);
|
93 | }
|
94 | }
|
95 | }
|
96 | exports.SettingManager = SettingManager;
|
97 |
|
98 |
|
99 |
|
100 | var Private;
|
101 | (function (Private) {
|
102 | |
103 |
|
104 |
|
105 | function url(base, id) {
|
106 | return coreutils_1.URLExt.join(base, SERVICE_SETTINGS_URL, id);
|
107 | }
|
108 | Private.url = url;
|
109 | })(Private || (Private = {}));
|
110 |
|
\ | No newline at end of file |