UNPKG

zater-cep

Version:

ZAter cep correios e via cep

34 lines (30 loc) 882 B
import fetch from 'node-fetch'; import ErrorCep from '../../lib/ErrorCep'; import FormatCep from '../../lib/FormatCep'; class ViaCep { constructor(cep) { this.cep = cep; this.baseurl = 'https://viacep.com.br/ws/'; this.basetype = '/json'; this.options = { method: 'GET', mode: 'cors', headers: { 'content-type': 'application/json', }, }; } requestCep() { try { return fetch(`${this.baseurl}${this.cep}${this.basetype}`) .then((response) => { if (response.ok !== true) throw ErrorCep('Cep não lacalizado', 400,'ZTR_CEP_002'); return response.json(); }) .then(res => FormatCep().makeAddres('VIACEP', res.cep, res.uf, res.localidade, res.bairro, res.logradouro)); } catch (err) { return Promise.reject(err); } } } export default cep => new ViaCep(cep);