UNPKG

zenodraft

Version:

CLI to manage depositions on Zenodo and Zenodo Sandbox

59 lines 2.72 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 }); exports.metadata_update = void 0; const node_fetch_1 = require("node-fetch"); const details_1 = require("../deposition/show/details"); const get_api_1 = require("../helpers/get-api"); const fs = require("fs"); const validate_1 = require("../metadata/validate"); const metadata_update = (token, sandbox, version_id, filename, verbose = false) => __awaiter(void 0, void 0, void 0, function* () { const msg_clearing = `clearing metadata from deposition with id ${version_id}...`; const msg_updating = `adding metadata from ${filename} to deposition with id ${version_id}...`; if (verbose) { if (filename === undefined) { console.log(msg_clearing); } else { console.log(msg_updating); } } if (filename !== undefined) { validate_1.metadata_validate(filename, verbose); } const deposition = yield details_1.deposition_show_details(token, sandbox, version_id, verbose); const api = get_api_1.helpers_get_api(sandbox); const url = `${api}/deposit/depositions/${version_id}`; const user_metadata = filename === undefined ? {} : JSON.parse(fs.readFileSync(filename, 'utf8')); const response = yield node_fetch_1.default(url, { method: 'PUT', headers: { 'Authorization': `Bearer ${token}`, 'Content-Type': 'application/json', }, body: JSON.stringify({ metadata: Object.assign({ "title": `Untitled in ${deposition.conceptrecid}` }, user_metadata) }) }); if (response.ok !== true) { throw new Error(`(errid 13) Something went wrong on PUT to ${url}: ${response.status} - ${response.statusText}`); } if (verbose) { if (filename === undefined) { console.log(`${msg_clearing}done`); } else { console.log(`${msg_updating}done`); } } }); exports.metadata_update = metadata_update; //# sourceMappingURL=update.js.map