UNPKG

@jupyterlab/services

Version:

Client APIs for the Jupyter services REST APIs

169 lines 5.67 kB
"use strict"; // Copyright (c) Jupyter Development Team. // Distributed under the terms of the Modified BSD License. Object.defineProperty(exports, "__esModule", { value: true }); exports.TerminalAPIClient = exports.TERMINAL_SERVICE_URL = void 0; exports.isAvailable = isAvailable; exports.startNew = startNew; exports.listRunning = listRunning; exports.shutdownTerminal = shutdownTerminal; const coreutils_1 = require("@jupyterlab/coreutils"); const serverconnection_1 = require("../serverconnection"); /** * The url for the terminal service. */ exports.TERMINAL_SERVICE_URL = 'api/terminals'; /** * Whether the terminal service is available. */ function isAvailable() { const available = String(coreutils_1.PageConfig.getOption('terminalsAvailable')); return available.toLowerCase() === 'true'; } /** * Start a new terminal session. * * @param settings - The server settings to use. * * @param name - The name of the target terminal. * * @param cwd - The path in which the terminal will start. * * @returns A promise that resolves with the session model. */ async function startNew(settings = serverconnection_1.ServerConnection.makeSettings(), name, cwd) { Private.errorIfNotAvailable(); const url = coreutils_1.URLExt.join(settings.baseUrl, exports.TERMINAL_SERVICE_URL); const init = { method: 'POST', body: JSON.stringify({ name, cwd }) }; const response = await serverconnection_1.ServerConnection.makeRequest(url, init, settings); if (response.status !== 200) { const err = await serverconnection_1.ServerConnection.ResponseError.create(response); throw err; } const data = await response.json(); // TODO: Validate model return data; } /** * List the running terminal sessions. * * @param settings - The server settings to use. * * @returns A promise that resolves with the list of running session models. */ async function listRunning(settings = serverconnection_1.ServerConnection.makeSettings()) { Private.errorIfNotAvailable(); const url = coreutils_1.URLExt.join(settings.baseUrl, exports.TERMINAL_SERVICE_URL); const response = await serverconnection_1.ServerConnection.makeRequest(url, {}, settings); if (response.status !== 200) { const err = await serverconnection_1.ServerConnection.ResponseError.create(response); throw err; } const data = await response.json(); if (!Array.isArray(data)) { throw new Error('Invalid terminal list'); } // TODO: validate each model return data; } /** * Shut down a terminal session by name. * * @param name - The name of the target session. * * @param settings - The server settings to use. * * @returns A promise that resolves when the session is shut down. */ async function shutdownTerminal(name, settings = serverconnection_1.ServerConnection.makeSettings()) { var _a; Private.errorIfNotAvailable(); const workspacesBase = coreutils_1.URLExt.join(settings.baseUrl, exports.TERMINAL_SERVICE_URL); const url = coreutils_1.URLExt.join(workspacesBase, name); if (!url.startsWith(workspacesBase)) { throw new Error('Can only be used for terminal requests'); } const init = { method: 'DELETE' }; const response = await serverconnection_1.ServerConnection.makeRequest(url, init, settings); if (response.status === 404) { const data = await response.json(); const msg = (_a = data.message) !== null && _a !== void 0 ? _a : `The terminal session "${name}"" does not exist on the server`; console.warn(msg); } else if (response.status !== 204) { const err = await serverconnection_1.ServerConnection.ResponseError.create(response); throw err; } } /** * The Terminal API client. * * #### Notes * Use this class to interact with the Jupyter Server Terminal API. * This class adheres to the Jupyter Server API endpoints. */ class TerminalAPIClient { /** * Create a new Terminal API client. */ constructor(options = {}) { var _a; this.serverSettings = (_a = options.serverSettings) !== null && _a !== void 0 ? _a : serverconnection_1.ServerConnection.makeSettings(); } /** * Whether terminals are available. */ get isAvailable() { return isAvailable(); } /** * Start a new terminal session. * * @param options - The options used to create the terminal. * * @returns A promise that resolves with the terminal session model. */ async startNew(options = {}) { const { name, cwd } = options; return startNew(this.serverSettings, name, cwd); } /** * List the running terminal sessions. * * @returns A promise that resolves with the list of running session models. */ async listRunning() { return listRunning(this.serverSettings); } /** * Shut down a terminal session by name. * * @param name - The name of the target session. * * @returns A promise that resolves when the session is shut down. */ async shutdown(name) { return shutdownTerminal(name, this.serverSettings); } } exports.TerminalAPIClient = TerminalAPIClient; /** * Namespace for private statics. */ var Private; (function (Private) { /** * Throw an error if terminals are not available. */ function errorIfNotAvailable() { if (!isAvailable()) { throw new Error('Terminals Unavailable'); } } Private.errorIfNotAvailable = errorIfNotAvailable; })(Private || (Private = {})); //# sourceMappingURL=restapi.js.map