├── .github └── workflows │ └── npm-publish.yml ├── .gitignore ├── .npmignore ├── package.json ├── readme.md └── src └── index.js /.github/workflows/npm-publish.yml: -------------------------------------------------------------------------------- 1 | # This workflow will run tests using node and then publish a package to GitHub Packages when a release is created 2 | # For more information see: https://help.github.com/actions/language-and-framework-guides/publishing-nodejs-packages 3 | 4 | name: Node.js Package 5 | 6 | on: 7 | release: 8 | types: [created] 9 | 10 | jobs: 11 | build: 12 | runs-on: ubuntu-latest 13 | steps: 14 | - uses: actions/checkout@v2 15 | - uses: actions/setup-node@v2 16 | with: 17 | node-version: 12 18 | - run: npm build 19 | 20 | publish-npm: 21 | needs: build 22 | runs-on: ubuntu-latest 23 | steps: 24 | - uses: actions/checkout@v2 25 | - uses: actions/setup-node@v2 26 | with: 27 | node-version: 12 28 | registry-url: https://registry.npmjs.org/ 29 | - run: npm build 30 | - run: npm publish 31 | env: 32 | NODE_AUTH_TOKEN: ${{secrets.npm_token}} 33 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | lerna-debug.log* 8 | 9 | # Directory for instrumented libs generated by jscoverage/JSCover 10 | lib-cov 11 | 12 | # Coverage directory used by tools like istanbul 13 | coverage 14 | *.lcov 15 | 16 | # nyc test coverage 17 | .nyc_output 18 | 19 | # node-waf configuration 20 | .lock-wscript 21 | 22 | # Compiled binary addons (https://nodejs.org/api/addons.html) 23 | build/ 24 | 25 | # Dependency directories 26 | node_modules/ 27 | jspm_packages/ 28 | 29 | # TypeScript v1 declaration files 30 | typings/ 31 | 32 | # TypeScript cache 33 | *.tsbuildinfo 34 | 35 | # Optional npm cache directory 36 | .npm 37 | 38 | # Optional eslint cache 39 | .eslintcache 40 | 41 | # Output of 'npm pack' 42 | *.tgz 43 | 44 | # Yarn Integrity file 45 | .yarn-integrity 46 | 47 | # generate output 48 | dist -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | dist 2 | .babelrc 3 | .storybook 4 | .gitignore 5 | .prettierrc 6 | rollup.config.js 7 | tsconfig.json -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-hook-usecep", 3 | "version": "1.0.6", 4 | "description": "a react hook to get address information", 5 | "main": "src/index.js", 6 | "scripts": { 7 | }, 8 | "repository": { 9 | "type": "git", 10 | "url": "git+https://github.com/tawsbob/react-hook-useCep.git" 11 | }, 12 | "keywords": [ 13 | "react", 14 | "cep" 15 | ], 16 | "author": "Dellean Santos", 17 | "license": "ISC", 18 | "bugs": { 19 | "url": "https://github.com/tawsbob/react-hook-useCep/issues" 20 | }, 21 | "homepage": "https://github.com/tawsbob/react-hook-useCep#readme", 22 | "peerDependencies": { 23 | "react": ">= 17.0.2", 24 | "react-dom": ">= 17.0.2" 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # useCep react hook 2 | 3 | useCep is a react **hook** for get address information from [viacep](https://viacep.com.br/), 4 | 5 | ## Installation 6 | 7 | ```bash 8 | npm install react-hook-usecep -S 9 | ``` 10 | 11 | ## Import 12 | 13 | ```javascript 14 | import useCep from 'react-hook-usecep'; 15 | ``` 16 | 17 | ## Usage 18 | 19 | ```javascript 20 | function MyReactComponent(){ 21 | 22 | 23 | const [status, setCep] = useCep() 24 | return ({ setCep(e.target.value) }} />) 25 | } 26 | ``` 27 | ### Status object 28 | ```json 29 | { 30 | "loading": false, 31 | "error": false, 32 | "data": { 33 | "cep": "01001-000", 34 | "logradouro": "Praça da Sé", 35 | "complemento": "lado ímpar", 36 | "bairro": "Sé", 37 | "localidade": "São Paulo", 38 | "uf": "SP", 39 | "ibge": "3550308", 40 | "gia": "1004", 41 | "ddd": "11", 42 | "siafi": "7107" 43 | } 44 | } 45 | ``` -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import { useState, useEffect } from "react"; 2 | 3 | const initialStatus = { loading: false, error: false, data: {} }; 4 | 5 | async function getCep(cep) { 6 | const response = await fetch(`https://viacep.com.br/ws/${cep}/json/`); 7 | return await response.json(); 8 | } 9 | 10 | function useCep() { 11 | const [cep, setCep] = useState(null); 12 | const [status, setStatus] = useState(initialStatus); 13 | 14 | useEffect(() => { 15 | async function fetchData() { 16 | if (cep) { 17 | setStatus({ ...status, loading: true }); 18 | try { 19 | const data = await getCep(cep); 20 | setStatus({ ...status, loading: false, data }); 21 | } catch (e) { 22 | setStatus({ loading: false, error: true, data: {} }); 23 | throw e; 24 | } 25 | } 26 | } 27 | fetchData(); 28 | }, [cep]); 29 | 30 | return [status, setCep]; 31 | } 32 | 33 | export default useCep; 34 | --------------------------------------------------------------------------------