Commit 3e247782 authored by nickstiffler's avatar nickstiffler Committed by Yannis Barlas

feat(api): add pubmed api lookup

parent 8b439260
......@@ -279,12 +279,12 @@ const InitialSubmission = props => {
<FieldWrapper>
<ReferenceField
// handleBlur={handleBlur}
// handleChange={handleChange}
handleBlur={handleBlur}
handleChange={handleChange}
label="Pubmed ID"
name={`references[${i}].pubmedId`}
placeholder={`Pubmed IDs are required unless they don't exist`}
// touched={touched}
touched={touched}
value={item.pubmedId}
// {...props}
/>
......@@ -310,6 +310,7 @@ const InitialSubmission = props => {
</div>
)
})}
{!props.readOnly && (
<Button onClick={handleAddReference} primary>
Add another reference
......
import config from 'config'
const { baseUrl } = config['pubsweet-client']
const apiUrl = `${baseUrl}/api/pubmed`
const get = async (endpoint, value) => {
const url = `${apiUrl}/${endpoint}?id=${value}`
return fetch(url)
}
const validate = async (endpoint, value) => {
const { id } = value
const url = `${apiUrl}/validate/${endpoint}?id=${id}`
return fetch(url)
}
const getReference = value => get('reference', value)
const validateReference = value => validate('reference', value)
export { getReference, validateReference }
const request = require('request')
const { head } = require('lodash')
const baseUrl = 'https://eutils.ncbi.nlm.nih.gov/entrez/eutils/'
// const apiKey = config.get('pubmed.apiKey')
const makeRequest = (req, res) => {
const reqUrl = `${baseUrl}esummary.fcgi`
request(
{
qs: {
db: 'pubmed',
id: req.query.id,
retmode: 'json',
},
url: reqUrl,
},
(error, response, body) => {
// eslint-disable-next-line no-console
if (error) return console.error(error)
return res.send({ reference: makeReference(JSON.parse(body)) })
},
)
}
const makeValidateRequest = (req, res) => {
const validateUrl = `${baseUrl}esearch.fcgi`
request(
{
qs: {
db: 'pubmed',
term: `${req.query.id}[UID]`,
retmode: 'json',
},
url: validateUrl,
},
(error, response, body) => {
// eslint-disable-next-line no-console
if (error) return console.error(error)
const json = JSON.parse(body)
const result = json.esearchresult.count === '1'
return res.send({ data: { result } })
},
)
}
const makeReference = data => {
if (data.result.uids.length === 0) {
return ''
}
const uid = data.result[head(data.result.uids)]
const authors = uid.authors.map(author => author.name).join(', ')
const year = head(uid.pubdate.split(' '))
const { title, source, volume, pages } = uid
return `${authors}. ${year}. ${title} ${source} ${volume}: ${pages}.`
}
const PubMedApi = app => {
/**
* GET ENDPOINTS
*/
app.get('/api/pubmed/fetch/reference', (req, res) => {
makeRequest(req, res)
})
/**
* VALIDATION ENDPOINTS
*/
app.get('/api/pubmed/validate/reference', (req, res) => {
makeValidateRequest(req, res)
})
}
module.exports = PubMedApi
/* eslint-disable global-require */
module.exports = {
server: () => app => require('./PubMedApi')(app),
}
......@@ -15,4 +15,5 @@ module.exports = [
'./server/models/user',
'./app/wbApi',
'./server/export',
'./app/pubMedApi',
]
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment