diff --git a/src/fetch-and-parse.ts b/src/fetch-and-parse.ts index b73bf0b..c48cbd1 100644 --- a/src/fetch-and-parse.ts +++ b/src/fetch-and-parse.ts @@ -3,17 +3,36 @@ import { exit } from 'process'; import { DocMap } from './docmap'; import { parsePreprintDocMap } from './docmap-parser'; -fetch('https://data-hub-api--stg.elifesciences.org/enhanced-preprints/docmaps/v1/index') - .then((data) => data.json()) - .then((data) => { - data.docmaps.forEach((docMapStruct: DocMap) => { - const docMapEditableStruct = JSON.parse(JSON.stringify(docMapStruct)); - // You can make edits here +const input = process.argv[2] || ''; - const docMapJson = JSON.stringify(docMapEditableStruct); - const parsedDocMap = parsePreprintDocMap(docMapJson); - console.log(JSON.stringify(docMapEditableStruct, undefined, ' ')); - console.log(JSON.stringify(parsedDocMap, undefined, ' ')); - exit(1); +const msid = (input.match(/^[0-9]+$/)) ? input : null; +const docmapUrl = `https://data-hub-api--stg.elifesciences.org/enhanced-preprints/docmaps/v2/${msid ? `by-publisher/elife/get-by-manuscript-id?manuscript_id=${msid}` : 'index'}`; + +const processDocmap = (docMapStruct: DocMap) => { + const docMapEditableStruct = JSON.parse(JSON.stringify(docMapStruct)); + // You can make edits here + + const docMapJson = JSON.stringify(docMapEditableStruct); + const parsedDocMap = parsePreprintDocMap(docMapJson); + console.log(JSON.stringify(docMapEditableStruct, undefined, ' ')); + console.log(JSON.stringify(parsedDocMap, undefined, ' ')); +}; + +if (msid || input.length === 0) { + fetch(docmapUrl) + .then((data) => data.json()) + .then((data) => { + if (msid) { + processDocmap(data); + exit(1); + } + + data.docmaps.forEach((docMapStruct: DocMap) => { + processDocmap(docMapStruct); + exit(1); + }); }); - }); +} else { + processDocmap(JSON.parse(input)); + exit(1); +}