Skip to content

Instantly share code, notes, and snippets.

@SidneyAllen
Last active November 11, 2021 16:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SidneyAllen/c8d2c081bdd3ef94a3a03526a6ba6f16 to your computer and use it in GitHub Desktop.
Save SidneyAllen/c8d2c081bdd3ef94a3a03526a6ba6f16 to your computer and use it in GitHub Desktop.
export default function () {
async function getSuggestions(val) {
const config = {
method: 'POST',
headers: new Headers({
'Authorization': 'Basic ' + btoa(`${import.meta.env.VITE_LOB_API_KEY}:`),
'Content-Type': 'application/json'
}),
body: JSON.stringify({
"address_prefix": import.meta.env.PROD ? val : '5 sugg'
})
}
// insert try/catch
try {
const res = await fetch('https://api.lob.com/v1/us_autocompletions', config)
const data = await res.json()
return data.suggestions.map((suggestion, index)
=> ({ id: index,
data: suggestion,
name: [suggestion.primary_line, suggestion.city, suggestion.state, suggestion.zip_code].join(' ')
}))
} catch (err) {
console.log(err)
}
}
return { getSuggestions }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment