Skip to content

Commit

Permalink
Merge branch 'main' into qa
Browse files Browse the repository at this point in the history
  • Loading branch information
nonword committed Oct 31, 2024
2 parents 5934979 + c97d1cd commit e2b1233
Show file tree
Hide file tree
Showing 7 changed files with 371 additions and 512 deletions.
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

0 comments on commit e2b1233

Please sign in to comment.