Last active
November 11, 2021 16:48
-
-
Save SidneyAllen/c8d2c081bdd3ef94a3a03526a6ba6f16 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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