-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
95 lines (77 loc) · 2.52 KB
/
app.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
const express = require('express')
const cors = require('cors')
const awsServerlessExpressMiddleware = require('aws-serverless-express/middleware')
const app = express()
const logger = require('./lib/logger')
const errors = require('./lib/errors')
const DataApi = require('./lib/data-api')
const scsbXmlFormatter = require('./lib/scsb-xml-formatter')
const swaggerDocs = require('./swagger.v0.1.json')
app.use(cors())
app.use(awsServerlessExpressMiddleware.eventContext())
const dataApi = new DataApi()
app.get('/docs/recap-bibs', function (req, res) {
res.send(swaggerDocs)
})
app.get('/api/v0.1/recap/nypl-bibs', (req, res, next) => {
/**
* This endpoint requires the following params set:
*
* - customercode AND
* - barcode OR bibId
*/
let customerCode = ''
// Preserve backward compatibility for lowercase customer code
if (req.query.customercode) {
customerCode = req.query.customercode
} else {
customerCode = req.query.customerCode
}
const barcode = req.query.barcode
const bibId = req.query.bibId
const includeFullBibTree = (req.query.includeFullBibTree === 'true')
if (!customerCode || !(barcode || bibId)) {
return handleError(new errors.InvalidParameterError('Missing barcode and customerCode paramaters or bibId and customerCode parameter'), req, res)
}
let resolveBibIds = null
if (bibId) {
resolveBibIds = Promise.resolve(bibId)
} else {
resolveBibIds = dataApi.getBibIdsByBarcode(barcode)
}
return resolveBibIds
// Get bib & item records:
.then((bibIds) => {
return dataApi.getBibsAndItemsByBibId(bibIds, barcode, includeFullBibTree)
})
// Format as scsb xml:
.then((bibsAndItems) => {
const [bibs, items] = bibsAndItems
return scsbXmlFormatter.bibsAndItemsToScsbXml(bibs, items, customerCode)
})
// Write response:
.then((scsbXml) => {
res.set('Content-Type', 'text/xml')
res.send(scsbXml)
})
.catch((e) => handleError(e, req, res))
})
function handleError (error, req, res) {
let status = null
res.set('Content-Type', 'application/json')
switch (error.name) {
case 'InvalidParameterError':
case 'NoItemsError':
status = 400
break
case 'NotFoundError':
status = 404
break
case 'ScsbXmlGenerationError':
default:
status = 500
}
logger.error('Error processing request', { error: error.message, path: req.path, query: req.query })
res.status(status).send({ statusCode: status, error: error.message, errorCode: error.name })
}
module.exports = app