From 532891934cd86d386670934bb0767b8b33c14b89 Mon Sep 17 00:00:00 2001 From: Nathan Lisgo Date: Mon, 11 Dec 2023 20:07:03 +0000 Subject: [PATCH 1/2] feat: add support for fetching specific DocMap by manuscript ID This commit updates the fetch-and-parse method to allow fetching of specific DocMap data by manuscript ID. It also refactors the processing of DocMap data into a separate function for better code organization. --- src/fetch-and-parse.ts | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/src/fetch-and-parse.ts b/src/fetch-and-parse.ts index b73bf0b..d378b7a 100644 --- a/src/fetch-and-parse.ts +++ b/src/fetch-and-parse.ts @@ -3,17 +3,29 @@ 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') +const msid = process.argv[2] || 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, ' ')); +}; + +fetch(docmapUrl) .then((data) => data.json()) .then((data) => { - data.docmaps.forEach((docMapStruct: DocMap) => { - const docMapEditableStruct = JSON.parse(JSON.stringify(docMapStruct)); - // You can make edits here + if (msid) { + processDocmap(data); + exit(1); + } - const docMapJson = JSON.stringify(docMapEditableStruct); - const parsedDocMap = parsePreprintDocMap(docMapJson); - console.log(JSON.stringify(docMapEditableStruct, undefined, ' ')); - console.log(JSON.stringify(parsedDocMap, undefined, ' ')); + data.docmaps.forEach((docMapStruct: DocMap) => { + processDocmap(docMapStruct); exit(1); }); }); From e7ccc26083e1685131636cc343a3c4a7abeb14bd Mon Sep 17 00:00:00 2001 From: Nathan Lisgo Date: Tue, 12 Dec 2023 09:30:04 +0000 Subject: [PATCH 2/2] feat: enhance input handling in fetch-and-parse.ts Improve the handling of command-line arguments in fetch-and-parse.ts. Now, the script accepts only numeric manuscript IDs and raw JSON input. If the input is not a numeric ID, it is treated as a JSON object, parsed and processed. --- src/fetch-and-parse.ts | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/src/fetch-and-parse.ts b/src/fetch-and-parse.ts index d378b7a..c48cbd1 100644 --- a/src/fetch-and-parse.ts +++ b/src/fetch-and-parse.ts @@ -3,7 +3,9 @@ import { exit } from 'process'; import { DocMap } from './docmap'; import { parsePreprintDocMap } from './docmap-parser'; -const msid = process.argv[2] || null; +const input = process.argv[2] || ''; + +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) => { @@ -16,16 +18,21 @@ const processDocmap = (docMapStruct: DocMap) => { console.log(JSON.stringify(parsedDocMap, undefined, ' ')); }; -fetch(docmapUrl) - .then((data) => data.json()) - .then((data) => { - if (msid) { - processDocmap(data); - exit(1); - } +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); + data.docmaps.forEach((docMapStruct: DocMap) => { + processDocmap(docMapStruct); + exit(1); + }); }); - }); +} else { + processDocmap(JSON.parse(input)); + exit(1); +}