Skip to content

Commit

Permalink
feat: create cities endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
tcelestino committed Dec 1, 2019
1 parent 23ce3ec commit b7d3067
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions pages/api/cities/[id].js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import fetch from 'isomorphic-unfetch';

const hidrateCities = (cities) => {
return cities.map(({ cidade_id, cidade }) => {
return {
label: cidade,
value: cidade_id
}
});
}

export default async (req, res) => {
const { query: { id } } = req;
const citiesApi = await fetch(`https://www.catho.com.br/util/localidade/ajaxCidadesByEstadoId/${id}/`);
const cities = await citiesApi.json();

if (cities.length > 1) {
const hidrate = hidrateCities(cities);
res.status(200).json(hidrate);
} else {
res.status(404).json({
message: 'not cities found'
})
}

}

0 comments on commit b7d3067

Please sign in to comment.