UNPKG

3.95 kBJavaScriptView Raw
1"use strict";
2// Copyright (c) Jupyter Development Team.
3// Distributed under the terms of the Modified BSD License.
4Object.defineProperty(exports, "__esModule", { value: true });
5exports.SettingManager = void 0;
6const coreutils_1 = require("@jupyterlab/coreutils");
7const statedb_1 = require("@jupyterlab/statedb");
8const serverconnection_1 = require("../serverconnection");
9/**
10 * The url for the lab settings service.
11 */
12const SERVICE_SETTINGS_URL = 'api/settings';
13/**
14 * The settings API service manager.
15 */
16class SettingManager extends statedb_1.DataConnector {
17 /**
18 * Create a new setting manager.
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 * Fetch a plugin's settings.
27 *
28 * @param id - The plugin's ID.
29 *
30 * @returns A promise that resolves if successful.
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 // Assert what type the server response is returning.
47 return response.json();
48 }
49 /**
50 * Fetch the list of all plugin setting bundles.
51 *
52 * @returns A promise that resolves if successful.
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 * Save a plugin's settings.
75 *
76 * @param id - The plugin's ID.
77 *
78 * @param raw - The user setting values as a raw string of JSON with comments.
79 *
80 * @returns A promise that resolves if successful.
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 // NOTE: 'raw' is JSON5 (not valid JSON), so we encode it as a string in a valid JSON body
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}
96exports.SettingManager = SettingManager;
97/**
98 * A namespace for private data.
99 */
100var Private;
101(function (Private) {
102 /**
103 * Get the url for a plugin's settings.
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//# sourceMappingURL=index.js.map
\No newline at end of file