Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions handlers/webpac-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ const {
} = require('../lib/utils')
const logger = require('../lib/logger')
const requests = require('../lib/requests')
const { NyplSourceMapper, config } = require('@nypl/node-utils')

let nyplSourceMapperInstance = null
const getNyplSourceMapper = async () => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is totally fine, but the idea of the NyplSourceMapper is to load the instance in some initializer method at a high level in the app. Then the NyplSourceMapper.instance() is available as a synchronous method without having to deal with building your own singleton method. The module itself is already handling that.

if (!nyplSourceMapperInstance) {
await config.loadConfig()
await NyplSourceMapper.loadInstance()
nyplSourceMapperInstance = NyplSourceMapper.instance()
}
return nyplSourceMapperInstance
}

const expressions = {
/**
Expand Down Expand Up @@ -112,9 +123,24 @@ const expressions = {
},

recordReg: {
expr: /\/record=b(\d{8})/,
expr: /\/record=([a-zA-Z0-9-]+)/,
handler: async (match, request) => {
const num = match[1]
const identifier = match[1]

let num
try {
const mapper = await getNyplSourceMapper()
const mapping = mapper.splitIdentifier(identifier)
if (mapping && mapping.id) {
num = mapping.id
} else {
return `${process.env.RC_BASE_URL}/404/redirect`
}
} catch (e) {
logger.warn(`NyplSourceMapper could not split identifier ${identifier}`)
return `${process.env.RC_BASE_URL}/404/redirect`
}

const { collection } = request.query
const circCollection = Array.isArray(collection) && collection.includes('circ')
const vegaUrl = `${process.env.VEGA_HOST}/search/card?recordId=${num.replace(/\D/g, '')}`
Expand All @@ -123,7 +149,7 @@ const expressions = {
}
const { isResearch } = await requests.queryIsResearch(num, 'id')
if (isResearch) {
return `${process.env.RC_BASE_URL}/bib/b${num}`
return `${process.env.RC_BASE_URL}/bib/${identifier}`
} else return vegaUrl
}
},
Expand Down
Loading
Loading