@jupyterlab/git
Version:
A JupyterLab extension for version control using git
65 lines (64 loc) • 2.26 kB
JavaScript
import { URLExt } from '@jupyterlab/coreutils';
import { ServerConnection } from '@jupyterlab/services';
import { Git } from './tokens';
/**
* Array of Git Auth Error Messages
*/
export const AUTH_ERROR_MESSAGES = [
'Invalid username or password',
'could not read Username',
'could not read Password',
'Authentication error'
];
/**
* Call the API extension
*
* @param endPoint API REST end point for the extension; default ''
* @param method HTML method; default 'GET'
* @param body JSON object to be passed as body or null; default null
* @param namespace API namespace; default 'git'
* @param serverSettings Optional server connection settings; if not provided, uses ServerConnection.makeSettings()
* @returns The response body interpreted as JSON
*
* @throws {Git.GitResponseError} If the server response is not ok
* @throws {ServerConnection.NetworkError} If the request cannot be made
*/
export async function requestAPI(endPoint = '', method = 'GET', body = null, namespace = 'git', serverSettings) {
// Make request to Jupyter API
const settings = serverSettings !== null && serverSettings !== void 0 ? serverSettings : ServerConnection.makeSettings();
const requestUrl = URLExt.join(settings.baseUrl, namespace, // API Namespace
endPoint);
const init = {
method,
body: body ? JSON.stringify(body) : undefined
};
let response;
try {
response = await ServerConnection.makeRequest(requestUrl, init, settings);
}
catch (error) {
throw new ServerConnection.NetworkError(error);
}
let data = await response.text();
let isJSON = false;
if (data.length > 0) {
try {
data = JSON.parse(data);
isJSON = true;
}
catch (_error) {
console.log('Not a JSON response body.', response);
}
}
if (!response.ok) {
if (isJSON) {
const { message, traceback, ...json } = data;
throw new Git.GitResponseError(response, message ||
`Invalid response: ${response.status} ${response.statusText}`, traceback || '', json);
}
else {
throw new Git.GitResponseError(response, data);
}
}
return data;
}