@defra/wls-connectors-lib
Version:
A library extracting the various connectivity functions for the wildlife service
45 lines (40 loc) • 1.13 kB
JavaScript
import * as https from 'https'
import { checkResponseOkElseThrow, httpFetch } from './fetch-helper.js'
import Config from './config.js'
import { AWS } from './aws.js'
const addressUrl = search => {
const url = new URL(Config.address.url)
url.searchParams.append('postcode', search)
return url.href
}
let keyAndCertsBuffers = {}
export const ADDRESS = ({
initialize: async () => {
console.log('Initializing the address lookup...')
try {
const { getSecret } = AWS.secretsManager()
const certificate = await getSecret(Config.address.certificateParam)
const key = await getSecret(Config.address.keyParam)
keyAndCertsBuffers = {
key: Buffer.from(key, 'utf8'),
cert: Buffer.from(certificate, 'utf8')
}
} catch (err) {
console.error(err)
}
},
lookup: async search =>
httpFetch(addressUrl(search),
'GET',
null,
null,
checkResponseOkElseThrow,
Config.address.timeout,
{
agent: new https.Agent({
key: keyAndCertsBuffers.key,
cert: keyAndCertsBuffers.cert
})
}
)
})