-
Notifications
You must be signed in to change notification settings - Fork 0
/
pubmed.js
66 lines (51 loc) · 1.97 KB
/
pubmed.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const apiKey = process.env.SENDGRID_API_KEY
const API_KEY = process.env.PUBMED_API_KEY
const BASE_URL = `https://eutils.ncbi.nlm.nih.gov/entrez/eutils/`
const SEARCH_ENDPOINT = `esearch.fcgi?db=pubmed&retmode=json&sort=pub_date&term=`
const SUMMARY_ENDPOINT = `esummary.fcgi?db=pubmed&version=2.0&retmode=json&api_key=${API_KEY}&id=`
const SPELLING_ENDPOINT = `espell.fcgi?db=pubmed&term=rhuinitis`
const RELATED_ENDPOINT = `elink.fcgi?dbfrom=pubmed&db=pubmed&id=`
const getPmids = async (term) => {
// Get the pubMed Ids associated with the search
const url = `${BASE_URL}${SEARCH_ENDPOINT}${term}+randomized+controlled+trial[pubt]`
// const url = `${BASE_URL}${SEARCH_ENDPOINT}${term}+review[pubt]`
const response = await fetch(url)
const search = await response.json()
console.log(search)
return search
}
const getSummary = async (idlist) => {
const joined = idlist.toString();
const url = `${BASE_URL}${SUMMARY_ENDPOINT}${joined}`
const response = await fetch(url)
const summary = await response.json()
return summary
}
export default async (req, res) => {
try {
console.log('you areached here')
const { term } = req.query
// console.log("REQ.BODY", req.body);
const { esearchresult, header } = await getPmids(term)
const { idlist } = esearchresult
if (idlist.length > 0){
const summary = await getSummary(idlist)
const { result } = summary
const resultValues = Object.values(result)
let summaries = []
resultValues.map(r => {
if (r.uid != undefined){
// This wil eliminate the last part of the array
// which is nothing but the list uids
summaries.push(r)
}
})
console.log(summaries)
res.json({ summaries , error: ""})
}
res.json({error: "No PMIDs were found for that search"})
} catch (error) {
console.log('you landed here')
return res.status(error.statusCode || 500).json({ error: error.message });
}
};