UNPKG

@chameleoncloud/jupyterlab_zenodo

Version:
135 lines (134 loc) 5.6 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const services_1 = require("@jupyterlab/services"); const coreutils_1 = require("@jupyterlab/coreutils"); class ZenodoRegistry { constructor() { this._serverSettings = services_1.ServerConnection.makeSettings(); this._records = []; this._recordsFetched = false; } createDeposition(path, post) { return __awaiter(this, void 0, void 0, function* () { const res = yield services_1.ServerConnection.makeRequest(Private.getUrl('upload', this._serverSettings), { method: 'POST', body: JSON.stringify(post) }, this._serverSettings); const record = yield Private.handleCreateResponse(path, res); this._updateRecords(record); return record; }); } newDepositionVersion(path) { return __awaiter(this, void 0, void 0, function* () { const res = yield services_1.ServerConnection.makeRequest(Private.getUrl('update', this._serverSettings), { method: 'POST' }, this._serverSettings); const record = yield Private.handleUpdateResponse(path, res); this._updateRecords(record); return record; }); } getDepositions() { return __awaiter(this, void 0, void 0, function* () { if (!this._recordsFetched) { if (!this._recordsFetchPromise) { this._recordsFetchPromise = services_1.ServerConnection.makeRequest(Private.getUrl('status', this._serverSettings), { method: 'GET' }, this._serverSettings).then(Private.handleListResponse); } try { this._records = yield this._recordsFetchPromise; this._recordsFetched = true; } catch (err) { throw err; // Re-raise } finally { delete this._recordsFetchPromise; } } return this._records; }); } getDeposition(path) { return __awaiter(this, void 0, void 0, function* () { const depositions = yield this.getDepositions(); return depositions.find(record => record.path === path); }); } getDepositionSync(path) { return this._records.find(r => r.path === path); } hasDepositionSync(path) { return !!this.getDepositionSync(path); } _updateRecords(record) { const indexOf = this._records.findIndex(({ path }) => path === record.path); if (indexOf >= 0) { this._records = this._records .slice(0, indexOf) .concat([record], this._records.slice(indexOf + 1)); } else { this._records = this._records.concat([record]); } } } exports.ZenodoRegistry = ZenodoRegistry; var Private; (function (Private) { function getUrl(name, settings) { const parts = [settings.baseUrl, 'zenodo', name]; return coreutils_1.URLExt.join.apply(coreutils_1.URLExt, parts); } Private.getUrl = getUrl; function handleListResponse(res) { return __awaiter(this, void 0, void 0, function* () { const { records } = yield res.json(); if (!records || !Array.isArray(records)) { throw Error('Malformed response'); } return records; }); } Private.handleListResponse = handleListResponse; function handleUpdateResponse(path, res) { return __awaiter(this, void 0, void 0, function* () { if (res.status > 299) { const message = 'An error occurred updating the Zenodo deposition'; throw new services_1.ServerConnection.ResponseError(res, message); } const json = yield res.json(); const { doi, redirect } = json; if (!doi) { throw new Error('Missing DOI'); } if (redirect) { window.open(redirect); } return { path, doi }; }); } Private.handleUpdateResponse = handleUpdateResponse; function handleCreateResponse(path, res) { return __awaiter(this, void 0, void 0, function* () { if (res.status > 299) { const message = 'An error occurred creating the Zenodo deposition'; throw new services_1.ServerConnection.ResponseError(res, message); } const json = yield res.json(); const { doi, redirect } = json; if (!doi) { throw new Error('Missing DOI'); } if (redirect) { window.open(redirect); } return { path, doi }; }); } Private.handleCreateResponse = handleCreateResponse; })(Private || (Private = {}));