Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scc 4327 #97

Merged
merged 4 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
39 changes: 35 additions & 4 deletions lib/utils/nypl-source-mapper.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const axios = require('axios')
class NyplSourceMapper {
constructor (mapping) {
this.nyplSourceMap = mapping
Expand Down Expand Up @@ -50,13 +49,45 @@ class NyplSourceMapper {
}
}

/**
* Create a NyplSourceMapper instance
*/
const createInstance = async () => {
const sourceMappingUrl = `https://raw.githubusercontent.com/NYPL/nypl-core/${process.env.NYPL_CORE_VERSION || 'master'}/mappings/recap-discovery/nypl-source-mapping.json`

// Retrieve json file:
const resp = await fetch(sourceMappingUrl)
.catch((e) => {
throw new Error(`Error retrieving ${sourceMappingUrl}: ${e}`)
})

// Assert 2xx status:
if (!resp?.ok) {
throw new Error(`Error retrieving ${sourceMappingUrl} - got status ${resp?.status}`)
}

// Parse JSON:
const data = await resp.json()
.catch((e) => {
throw new Error(`Error parsing ${sourceMappingUrl}: ${e}`)
})

// Check for invalid data structure:
if (!data || !data['sierra-nypl']) {
throw new Error(`Error parsing data at ${sourceMappingUrl}`)
}

return new NyplSourceMapper(data)
}

let sourceMapperInstance = null

/**
* Get singleton NyplSourceMapper instance
*/
NyplSourceMapper.instance = async () => {
if (!sourceMapperInstance) {
sourceMapperInstance = await axios.get(`https://raw.githubusercontent.com/NYPL/nypl-core/${process.env.NYPL_CORE_VERSION || 'master'}/mappings/recap-discovery/nypl-source-mapping.json`).then((resp) => {
return new NyplSourceMapper(resp.data)
})
sourceMapperInstance = createInstance()
}
return sourceMapperInstance
}
Expand Down
Loading
Loading