25 lines
626 B
TypeScript
25 lines
626 B
TypeScript
import { data } from 'react-router'
|
|
import type { Route } from './+types/route'
|
|
|
|
export async function loader({ params }: Route.LoaderArgs) {
|
|
// const r = await fetch(`https://opencep.com/v1/${params.cep}`, {
|
|
const r = await fetch(`https://brasilapi.com.br/api/cep/v1/${params.cep}`, {
|
|
method: 'GET'
|
|
})
|
|
|
|
if (!r.ok) {
|
|
throw new Response(await r.text(), { status: r.status })
|
|
}
|
|
|
|
const json = (await r.json()) as any
|
|
|
|
return data({
|
|
postcode: json.cep.replace(/\D/g, ''),
|
|
address1: json.street,
|
|
address2: '',
|
|
neighborhood: json.neighborhood,
|
|
city: json.city,
|
|
state: json.state
|
|
})
|
|
}
|