24 lines
559 B
TypeScript
24 lines
559 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}`, {
|
|
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.logradouro,
|
|
address2: json.complemento,
|
|
neighborhood: json.bairro,
|
|
city: json.localidade,
|
|
state: json.uf
|
|
})
|
|
}
|