UNPKG

zater-cep

Version:

ZAter cep correios e via cep

33 lines (30 loc) 915 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 = 'http://apps.widenet.com.br/busca-cep/api/cep/'; this.basetype = '.json'; this.options = { method: 'GET', mode: 'cors', headers: { 'content-type': 'application/json', }, }; } requestCep() { try { return fetch(`${this.baseurl}${this.cep}${this.basetype}`, this.options) .then((response) => { if (response.ok !== true) throw ErrorCep('Cep não lacalizado', 400, 'ZTR_CEP_002'); return response.json(); }) .then(res => FormatCep().makeAddres('BUSCACEP', this.cep, res.state, res.city, res.district, res.address)); } catch (err) { return Promise.reject(err); } } } export default cep => new ViaCep(cep);