vue-viacep-plugin 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +105 -0
- package/dist/components/ViaCepLookup.vue.d.ts +20 -0
- package/dist/composables/useViaCep.d.ts +94 -0
- package/dist/index.d.ts +7 -0
- package/dist/plugin.d.ts +15 -0
- package/dist/types.d.ts +35 -0
- package/dist/viacep.d.ts +10 -0
- package/dist/vue-viacep-plugin.css +1 -0
- package/dist/vue-viacep-plugin.js +197 -0
- package/dist/vue-viacep-plugin.umd.cjs +1 -0
- package/package.json +47 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Guilherme Gomes
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
# Vue ViaCEP Plugin
|
|
2
|
+
|
|
3
|
+
Plugin Vue 3 para consulta de CEP e pesquisa de endereco usando o Web Service gratuito do ViaCEP.
|
|
4
|
+
|
|
5
|
+
## Recursos
|
|
6
|
+
|
|
7
|
+
- Consulta por CEP com validacao local de 8 digitos.
|
|
8
|
+
- Tratamento de CEP inexistente retornado como `{ "erro": true }`.
|
|
9
|
+
- Pesquisa por UF, cidade e logradouro.
|
|
10
|
+
- Componente `<ViaCepLookup />` pronto para uso.
|
|
11
|
+
- Composable `useViaCep()` para fluxos customizados.
|
|
12
|
+
- Cliente standalone `createViaCepClient()` para uso fora de componentes.
|
|
13
|
+
|
|
14
|
+
## Instalar
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install
|
|
18
|
+
npm run dev
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Para publicar ou usar como biblioteca:
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
npm run build -- --mode lib
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Registrar no Vue
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
import { createApp } from 'vue';
|
|
31
|
+
import ViaCepPlugin from 'vue-viacep-plugin';
|
|
32
|
+
import 'vue-viacep-plugin/style.css';
|
|
33
|
+
import App from './App.vue';
|
|
34
|
+
|
|
35
|
+
createApp(App)
|
|
36
|
+
.use(ViaCepPlugin)
|
|
37
|
+
.mount('#app');
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Com URL customizada:
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
import { createViaCepPlugin } from 'vue-viacep-plugin';
|
|
44
|
+
|
|
45
|
+
app.use(createViaCepPlugin({
|
|
46
|
+
baseUrl: 'https://viacep.com.br/ws'
|
|
47
|
+
}));
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Componente
|
|
51
|
+
|
|
52
|
+
```vue
|
|
53
|
+
<template>
|
|
54
|
+
<ViaCepLookup v-model="cep" @found="address = $event" @error="message = $event" />
|
|
55
|
+
</template>
|
|
56
|
+
|
|
57
|
+
<script setup lang="ts">
|
|
58
|
+
import { ref } from 'vue';
|
|
59
|
+
import type { ViaCepAddress } from 'vue-viacep-plugin';
|
|
60
|
+
|
|
61
|
+
const cep = ref('');
|
|
62
|
+
const message = ref('');
|
|
63
|
+
const address = ref<ViaCepAddress | null>(null);
|
|
64
|
+
</script>
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Composable
|
|
68
|
+
|
|
69
|
+
```vue
|
|
70
|
+
<script setup lang="ts">
|
|
71
|
+
import { useViaCep } from 'vue-viacep-plugin';
|
|
72
|
+
|
|
73
|
+
const { loading, error, address, results, searchByCep, searchByAddress } = useViaCep();
|
|
74
|
+
|
|
75
|
+
await searchByCep('01001000');
|
|
76
|
+
|
|
77
|
+
await searchByAddress({
|
|
78
|
+
uf: 'RS',
|
|
79
|
+
cidade: 'Porto Alegre',
|
|
80
|
+
logradouro: 'Domingos Jose'
|
|
81
|
+
});
|
|
82
|
+
</script>
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## Cliente standalone
|
|
86
|
+
|
|
87
|
+
```ts
|
|
88
|
+
import { createViaCepClient } from 'vue-viacep-plugin';
|
|
89
|
+
|
|
90
|
+
const viaCep = createViaCepClient();
|
|
91
|
+
const address = await viaCep.fetchCep('01001000');
|
|
92
|
+
const list = await viaCep.searchAddress({
|
|
93
|
+
uf: 'RS',
|
|
94
|
+
cidade: 'Porto Alegre',
|
|
95
|
+
logradouro: 'Domingos Jose'
|
|
96
|
+
});
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## Validacoes implementadas
|
|
100
|
+
|
|
101
|
+
- CEP aceita somente 8 digitos apos remover mascara.
|
|
102
|
+
- Pesquisa por endereco exige UF com 2 letras.
|
|
103
|
+
- Cidade e logradouro exigem no minimo 3 caracteres.
|
|
104
|
+
- Respostas HTTP 400 sao convertidas em erro de validacao.
|
|
105
|
+
- Resposta `{ "erro": true }` vira erro `NOT_FOUND`.
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { ViaCepAddress } from '../types';
|
|
2
|
+
type __VLS_Props = {
|
|
3
|
+
label?: string;
|
|
4
|
+
buttonText?: string;
|
|
5
|
+
modelValue?: string;
|
|
6
|
+
};
|
|
7
|
+
declare const _default: import("vue").DefineComponent<__VLS_Props, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {
|
|
8
|
+
"update:modelValue": (value: string) => any;
|
|
9
|
+
found: (address: ViaCepAddress) => any;
|
|
10
|
+
error: (message: string) => any;
|
|
11
|
+
}, string, import("vue").PublicProps, Readonly<__VLS_Props> & Readonly<{
|
|
12
|
+
"onUpdate:modelValue"?: ((value: string) => any) | undefined;
|
|
13
|
+
onFound?: ((address: ViaCepAddress) => any) | undefined;
|
|
14
|
+
onError?: ((message: string) => any) | undefined;
|
|
15
|
+
}>, {
|
|
16
|
+
label: string;
|
|
17
|
+
buttonText: string;
|
|
18
|
+
modelValue: string;
|
|
19
|
+
}, {}, {}, {}, string, import("vue").ComponentProvideOptions, false, {}, any>;
|
|
20
|
+
export default _default;
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import type { ViaCepAddress, ViaCepClient, ViaCepSearchParams } from '../types';
|
|
2
|
+
export declare function useViaCep(clientOverride?: ViaCepClient): {
|
|
3
|
+
loading: import("vue").Ref<boolean, boolean>;
|
|
4
|
+
error: import("vue").Ref<string | null, string | null>;
|
|
5
|
+
address: import("vue").Ref<{
|
|
6
|
+
cep: string;
|
|
7
|
+
logradouro: string;
|
|
8
|
+
complemento: string;
|
|
9
|
+
unidade: string;
|
|
10
|
+
bairro: string;
|
|
11
|
+
localidade: string;
|
|
12
|
+
uf: string;
|
|
13
|
+
estado: string;
|
|
14
|
+
regiao: string;
|
|
15
|
+
ibge: string;
|
|
16
|
+
gia: string;
|
|
17
|
+
ddd: string;
|
|
18
|
+
siafi: string;
|
|
19
|
+
} | null, ViaCepAddress | {
|
|
20
|
+
cep: string;
|
|
21
|
+
logradouro: string;
|
|
22
|
+
complemento: string;
|
|
23
|
+
unidade: string;
|
|
24
|
+
bairro: string;
|
|
25
|
+
localidade: string;
|
|
26
|
+
uf: string;
|
|
27
|
+
estado: string;
|
|
28
|
+
regiao: string;
|
|
29
|
+
ibge: string;
|
|
30
|
+
gia: string;
|
|
31
|
+
ddd: string;
|
|
32
|
+
siafi: string;
|
|
33
|
+
} | null>;
|
|
34
|
+
results: import("vue").Ref<{
|
|
35
|
+
cep: string;
|
|
36
|
+
logradouro: string;
|
|
37
|
+
complemento: string;
|
|
38
|
+
unidade: string;
|
|
39
|
+
bairro: string;
|
|
40
|
+
localidade: string;
|
|
41
|
+
uf: string;
|
|
42
|
+
estado: string;
|
|
43
|
+
regiao: string;
|
|
44
|
+
ibge: string;
|
|
45
|
+
gia: string;
|
|
46
|
+
ddd: string;
|
|
47
|
+
siafi: string;
|
|
48
|
+
}[], ViaCepAddress[] | {
|
|
49
|
+
cep: string;
|
|
50
|
+
logradouro: string;
|
|
51
|
+
complemento: string;
|
|
52
|
+
unidade: string;
|
|
53
|
+
bairro: string;
|
|
54
|
+
localidade: string;
|
|
55
|
+
uf: string;
|
|
56
|
+
estado: string;
|
|
57
|
+
regiao: string;
|
|
58
|
+
ibge: string;
|
|
59
|
+
gia: string;
|
|
60
|
+
ddd: string;
|
|
61
|
+
siafi: string;
|
|
62
|
+
}[]>;
|
|
63
|
+
searchByCep: (cep: string) => Promise<{
|
|
64
|
+
cep: string;
|
|
65
|
+
logradouro: string;
|
|
66
|
+
complemento: string;
|
|
67
|
+
unidade: string;
|
|
68
|
+
bairro: string;
|
|
69
|
+
localidade: string;
|
|
70
|
+
uf: string;
|
|
71
|
+
estado: string;
|
|
72
|
+
regiao: string;
|
|
73
|
+
ibge: string;
|
|
74
|
+
gia: string;
|
|
75
|
+
ddd: string;
|
|
76
|
+
siafi: string;
|
|
77
|
+
}>;
|
|
78
|
+
searchByAddress: (params: ViaCepSearchParams) => Promise<{
|
|
79
|
+
cep: string;
|
|
80
|
+
logradouro: string;
|
|
81
|
+
complemento: string;
|
|
82
|
+
unidade: string;
|
|
83
|
+
bairro: string;
|
|
84
|
+
localidade: string;
|
|
85
|
+
uf: string;
|
|
86
|
+
estado: string;
|
|
87
|
+
regiao: string;
|
|
88
|
+
ibge: string;
|
|
89
|
+
gia: string;
|
|
90
|
+
ddd: string;
|
|
91
|
+
siafi: string;
|
|
92
|
+
}[]>;
|
|
93
|
+
client: ViaCepClient;
|
|
94
|
+
};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import './style.css';
|
|
2
|
+
export { default } from './plugin';
|
|
3
|
+
export { createViaCepPlugin, viaCepKey } from './plugin';
|
|
4
|
+
export { useViaCep } from './composables/useViaCep';
|
|
5
|
+
export { default as ViaCepLookup } from './components/ViaCepLookup.vue';
|
|
6
|
+
export { ViaCepError, createViaCepClient, formatCep, isValidCep, sanitizeCep } from './viacep';
|
|
7
|
+
export type { ViaCepAddress, ViaCepClient, ViaCepErrorResponse, ViaCepPluginOptions, ViaCepResponse, ViaCepSearchParams } from './types';
|
package/dist/plugin.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { App, InjectionKey } from 'vue';
|
|
2
|
+
import type { ViaCepClient, ViaCepPluginOptions } from './types';
|
|
3
|
+
export declare const viaCepKey: InjectionKey<ViaCepClient>;
|
|
4
|
+
export declare function createViaCepPlugin(options?: ViaCepPluginOptions): {
|
|
5
|
+
install(app: App): void;
|
|
6
|
+
};
|
|
7
|
+
declare const _default: {
|
|
8
|
+
install(app: App): void;
|
|
9
|
+
};
|
|
10
|
+
export default _default;
|
|
11
|
+
declare module 'vue' {
|
|
12
|
+
interface ComponentCustomProperties {
|
|
13
|
+
$viaCep: ViaCepClient;
|
|
14
|
+
}
|
|
15
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export type ViaCepFormat = 'json' | 'xml';
|
|
2
|
+
export interface ViaCepAddress {
|
|
3
|
+
cep: string;
|
|
4
|
+
logradouro: string;
|
|
5
|
+
complemento: string;
|
|
6
|
+
unidade: string;
|
|
7
|
+
bairro: string;
|
|
8
|
+
localidade: string;
|
|
9
|
+
uf: string;
|
|
10
|
+
estado: string;
|
|
11
|
+
regiao: string;
|
|
12
|
+
ibge: string;
|
|
13
|
+
gia: string;
|
|
14
|
+
ddd: string;
|
|
15
|
+
siafi: string;
|
|
16
|
+
}
|
|
17
|
+
export interface ViaCepErrorResponse {
|
|
18
|
+
erro: true;
|
|
19
|
+
}
|
|
20
|
+
export type ViaCepResponse = ViaCepAddress | ViaCepErrorResponse;
|
|
21
|
+
export interface ViaCepSearchParams {
|
|
22
|
+
uf: string;
|
|
23
|
+
cidade: string;
|
|
24
|
+
logradouro: string;
|
|
25
|
+
}
|
|
26
|
+
export interface ViaCepPluginOptions {
|
|
27
|
+
baseUrl?: string;
|
|
28
|
+
}
|
|
29
|
+
export interface ViaCepClient {
|
|
30
|
+
fetchCep(cep: string): Promise<ViaCepAddress>;
|
|
31
|
+
searchAddress(params: ViaCepSearchParams): Promise<ViaCepAddress[]>;
|
|
32
|
+
formatCep(cep: string): string;
|
|
33
|
+
isValidCep(cep: string): boolean;
|
|
34
|
+
sanitizeCep(cep: string): string;
|
|
35
|
+
}
|
package/dist/viacep.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { ViaCepClient, ViaCepPluginOptions } from './types';
|
|
2
|
+
export declare class ViaCepError extends Error {
|
|
3
|
+
readonly code: 'INVALID_CEP' | 'NOT_FOUND' | 'INVALID_ADDRESS' | 'REQUEST_FAILED';
|
|
4
|
+
readonly status?: number | undefined;
|
|
5
|
+
constructor(message: string, code: 'INVALID_CEP' | 'NOT_FOUND' | 'INVALID_ADDRESS' | 'REQUEST_FAILED', status?: number | undefined);
|
|
6
|
+
}
|
|
7
|
+
export declare function sanitizeCep(cep: string): string;
|
|
8
|
+
export declare function isValidCep(cep: string): boolean;
|
|
9
|
+
export declare function formatCep(cep: string): string;
|
|
10
|
+
export declare function createViaCepClient(options?: ViaCepPluginOptions): ViaCepClient;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
:root{color-scheme:light}.via-cep[data-v-e1da2fc8]{display:grid;gap:8px;max-width:420px;font-family:system-ui,-apple-system,BlinkMacSystemFont,Segoe UI,sans-serif}.via-cep__label[data-v-e1da2fc8]{color:#27333f;font-size:14px;font-weight:700}.via-cep__row[data-v-e1da2fc8]{display:grid;grid-template-columns:minmax(0,1fr) auto;gap:8px}.via-cep__input[data-v-e1da2fc8],.via-cep__button[data-v-e1da2fc8]{min-height:40px;border-radius:6px;font:inherit}.via-cep__input[data-v-e1da2fc8]{width:100%;border:1px solid #9aa8b5;padding:0 12px}.via-cep__button[data-v-e1da2fc8]{border:0;background:#136f63;color:#fff;cursor:pointer;font-weight:700;padding:0 14px}.via-cep__button[data-v-e1da2fc8]:disabled{cursor:not-allowed;opacity:.55}.via-cep__error[data-v-e1da2fc8]{color:#b42318;font-size:14px;margin:0}.via-cep__result[data-v-e1da2fc8]{display:grid;gap:3px;border:1px solid #cbd5df;border-radius:6px;padding:12px;color:#27333f;background:#f8fafc;font-size:14px}@media(max-width:420px){.via-cep__row[data-v-e1da2fc8]{grid-template-columns:1fr}}
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import { inject as U, ref as v, defineComponent as N, computed as S, openBlock as _, createElementBlock as g, withModifiers as $, createElementVNode as d, toDisplayString as p, withDirectives as L, vModelText as k, unref as u, createCommentVNode as V } from "vue";
|
|
2
|
+
const x = "https://viacep.com.br/ws";
|
|
3
|
+
class m extends Error {
|
|
4
|
+
constructor(n, a, r) {
|
|
5
|
+
super(n), this.code = a, this.status = r, this.name = "ViaCepError";
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
function h(t) {
|
|
9
|
+
return t.replace(/\D/g, "");
|
|
10
|
+
}
|
|
11
|
+
function b(t) {
|
|
12
|
+
return /^\d{8}$/.test(h(t));
|
|
13
|
+
}
|
|
14
|
+
function T(t) {
|
|
15
|
+
const n = h(t);
|
|
16
|
+
return n.length !== 8 ? t : `${n.slice(0, 5)}-${n.slice(5)}`;
|
|
17
|
+
}
|
|
18
|
+
function w(t = {}) {
|
|
19
|
+
const n = (t.baseUrl ?? x).replace(/\/$/, "");
|
|
20
|
+
async function a(o) {
|
|
21
|
+
const e = await fetch(o, {
|
|
22
|
+
headers: {
|
|
23
|
+
Accept: "application/json"
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
if (!e.ok)
|
|
27
|
+
throw new m(
|
|
28
|
+
`Falha ao consultar ViaCEP. HTTP ${e.status}.`,
|
|
29
|
+
e.status === 400 ? "INVALID_ADDRESS" : "REQUEST_FAILED",
|
|
30
|
+
e.status
|
|
31
|
+
);
|
|
32
|
+
return e.json();
|
|
33
|
+
}
|
|
34
|
+
async function r(o) {
|
|
35
|
+
const e = h(o);
|
|
36
|
+
if (!b(e))
|
|
37
|
+
throw new m("CEP invalido. Informe exatamente 8 digitos.", "INVALID_CEP");
|
|
38
|
+
const l = await a(`${n}/${e}/json/`);
|
|
39
|
+
if ("erro" in l && l.erro)
|
|
40
|
+
throw new m("CEP nao encontrado na base do ViaCEP.", "NOT_FOUND");
|
|
41
|
+
return l;
|
|
42
|
+
}
|
|
43
|
+
async function i(o) {
|
|
44
|
+
const e = o.uf.trim().toUpperCase(), l = o.cidade.trim(), c = o.logradouro.trim();
|
|
45
|
+
if (!/^[A-Z]{2}$/.test(e) || l.length < 3 || c.length < 3)
|
|
46
|
+
throw new m(
|
|
47
|
+
"Pesquisa invalida. Informe UF com 2 letras, cidade e logradouro com no minimo 3 caracteres.",
|
|
48
|
+
"INVALID_ADDRESS"
|
|
49
|
+
);
|
|
50
|
+
const f = [
|
|
51
|
+
n,
|
|
52
|
+
encodeURIComponent(e),
|
|
53
|
+
encodeURIComponent(l),
|
|
54
|
+
encodeURIComponent(c),
|
|
55
|
+
"json"
|
|
56
|
+
].join("/"), s = await a(`${f}/`);
|
|
57
|
+
if (!Array.isArray(s))
|
|
58
|
+
throw new m("Endereco nao encontrado na base do ViaCEP.", "NOT_FOUND");
|
|
59
|
+
return s;
|
|
60
|
+
}
|
|
61
|
+
return {
|
|
62
|
+
fetchCep: r,
|
|
63
|
+
searchAddress: i,
|
|
64
|
+
formatCep: T,
|
|
65
|
+
isValidCep: b,
|
|
66
|
+
sanitizeCep: h
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
function j(t) {
|
|
70
|
+
const n = U(I, null), a = t ?? n ?? w(), r = v(!1), i = v(null), o = v(null), e = v([]);
|
|
71
|
+
async function l(f) {
|
|
72
|
+
r.value = !0, i.value = null, o.value = null;
|
|
73
|
+
try {
|
|
74
|
+
return o.value = await a.fetchCep(f), o.value;
|
|
75
|
+
} catch (s) {
|
|
76
|
+
throw i.value = s instanceof m ? s.message : "Nao foi possivel consultar o CEP.", s;
|
|
77
|
+
} finally {
|
|
78
|
+
r.value = !1;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
async function c(f) {
|
|
82
|
+
r.value = !0, i.value = null, e.value = [];
|
|
83
|
+
try {
|
|
84
|
+
return e.value = await a.searchAddress(f), e.value;
|
|
85
|
+
} catch (s) {
|
|
86
|
+
throw i.value = s instanceof m ? s.message : "Nao foi possivel pesquisar o endereco.", s;
|
|
87
|
+
} finally {
|
|
88
|
+
r.value = !1;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return {
|
|
92
|
+
loading: r,
|
|
93
|
+
error: i,
|
|
94
|
+
address: o,
|
|
95
|
+
results: e,
|
|
96
|
+
searchByCep: l,
|
|
97
|
+
searchByAddress: c,
|
|
98
|
+
client: a
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
const B = { class: "via-cep__row" }, R = ["disabled"], F = {
|
|
102
|
+
key: 0,
|
|
103
|
+
class: "via-cep__error",
|
|
104
|
+
role: "alert"
|
|
105
|
+
}, O = {
|
|
106
|
+
key: 1,
|
|
107
|
+
class: "via-cep__result",
|
|
108
|
+
"aria-live": "polite"
|
|
109
|
+
}, q = /* @__PURE__ */ N({
|
|
110
|
+
__name: "ViaCepLookup",
|
|
111
|
+
props: {
|
|
112
|
+
label: { default: "CEP" },
|
|
113
|
+
buttonText: { default: "Consultar" },
|
|
114
|
+
modelValue: { default: "" }
|
|
115
|
+
},
|
|
116
|
+
emits: ["update:modelValue", "found", "error"],
|
|
117
|
+
setup(t, { emit: n }) {
|
|
118
|
+
const a = t, r = n, i = `via-cep-${Math.random().toString(36).slice(2)}`, o = v(a.modelValue), { address: e, loading: l, error: c, searchByCep: f, client: s } = j(), y = S(() => s.isValidCep(o.value));
|
|
119
|
+
function A() {
|
|
120
|
+
const C = s.sanitizeCep(o.value).slice(0, 8);
|
|
121
|
+
o.value = s.formatCep(C), r("update:modelValue", o.value);
|
|
122
|
+
}
|
|
123
|
+
async function D() {
|
|
124
|
+
if (!y.value) {
|
|
125
|
+
c.value = "Informe um CEP com 8 digitos.", r("error", c.value);
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
try {
|
|
129
|
+
const C = await f(o.value);
|
|
130
|
+
r("found", C);
|
|
131
|
+
} catch {
|
|
132
|
+
r("error", c.value ?? "Nao foi possivel consultar o CEP.");
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return (C, E) => (_(), g("form", {
|
|
136
|
+
class: "via-cep",
|
|
137
|
+
onSubmit: $(D, ["prevent"])
|
|
138
|
+
}, [
|
|
139
|
+
d("label", {
|
|
140
|
+
class: "via-cep__label",
|
|
141
|
+
for: i
|
|
142
|
+
}, p(t.label), 1),
|
|
143
|
+
d("div", B, [
|
|
144
|
+
L(d("input", {
|
|
145
|
+
id: i,
|
|
146
|
+
"onUpdate:modelValue": E[0] || (E[0] = (P) => o.value = P),
|
|
147
|
+
class: "via-cep__input",
|
|
148
|
+
inputmode: "numeric",
|
|
149
|
+
maxlength: "9",
|
|
150
|
+
placeholder: "01001-000",
|
|
151
|
+
autocomplete: "postal-code",
|
|
152
|
+
onInput: A
|
|
153
|
+
}, null, 544), [
|
|
154
|
+
[k, o.value]
|
|
155
|
+
]),
|
|
156
|
+
d("button", {
|
|
157
|
+
class: "via-cep__button",
|
|
158
|
+
type: "submit",
|
|
159
|
+
disabled: u(l) || !y.value
|
|
160
|
+
}, p(u(l) ? "Consultando..." : t.buttonText), 9, R)
|
|
161
|
+
]),
|
|
162
|
+
u(c) ? (_(), g("p", F, p(u(c)), 1)) : V("", !0),
|
|
163
|
+
u(e) ? (_(), g("section", O, [
|
|
164
|
+
d("strong", null, p(u(e).cep), 1),
|
|
165
|
+
d("span", null, p(u(e).logradouro || "Logradouro nao informado"), 1),
|
|
166
|
+
d("span", null, p(u(e).bairro || "Bairro nao informado"), 1),
|
|
167
|
+
d("span", null, p(u(e).localidade) + " - " + p(u(e).uf), 1)
|
|
168
|
+
])) : V("", !0)
|
|
169
|
+
], 32));
|
|
170
|
+
}
|
|
171
|
+
}), M = (t, n) => {
|
|
172
|
+
const a = t.__vccOpts || t;
|
|
173
|
+
for (const [r, i] of n)
|
|
174
|
+
a[r] = i;
|
|
175
|
+
return a;
|
|
176
|
+
}, z = /* @__PURE__ */ M(q, [["__scopeId", "data-v-e1da2fc8"]]), I = Symbol("viaCep");
|
|
177
|
+
function H(t = {}) {
|
|
178
|
+
const n = w(t);
|
|
179
|
+
return {
|
|
180
|
+
install(a) {
|
|
181
|
+
a.provide(I, n), a.config.globalProperties.$viaCep = n, a.component("ViaCepLookup", z);
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
const K = H();
|
|
186
|
+
export {
|
|
187
|
+
m as ViaCepError,
|
|
188
|
+
z as ViaCepLookup,
|
|
189
|
+
w as createViaCepClient,
|
|
190
|
+
H as createViaCepPlugin,
|
|
191
|
+
K as default,
|
|
192
|
+
T as formatCep,
|
|
193
|
+
b as isValidCep,
|
|
194
|
+
h as sanitizeCep,
|
|
195
|
+
j as useViaCep,
|
|
196
|
+
I as viaCepKey
|
|
197
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
(function(i,e){typeof exports=="object"&&typeof module<"u"?e(exports,require("vue")):typeof define=="function"&&define.amd?define(["exports","vue"],e):(i=typeof globalThis<"u"?globalThis:i||self,e(i.VueViaCepPlugin={},i.Vue))})(this,(function(i,e){"use strict";const S="https://viacep.com.br/ws";class p extends Error{constructor(a,r,s){super(a),this.code=r,this.status=s,this.name="ViaCepError"}}function m(n){return n.replace(/\D/g,"")}function h(n){return/^\d{8}$/.test(m(n))}function g(n){const a=m(n);return a.length!==8?n:`${a.slice(0,5)}-${a.slice(5)}`}function V(n={}){const a=(n.baseUrl??S).replace(/\/$/,"");async function r(o){const t=await fetch(o,{headers:{Accept:"application/json"}});if(!t.ok)throw new p(`Falha ao consultar ViaCEP. HTTP ${t.status}.`,t.status===400?"INVALID_ADDRESS":"REQUEST_FAILED",t.status);return t.json()}async function s(o){const t=m(o);if(!h(t))throw new p("CEP invalido. Informe exatamente 8 digitos.","INVALID_CEP");const u=await r(`${a}/${t}/json/`);if("erro"in u&&u.erro)throw new p("CEP nao encontrado na base do ViaCEP.","NOT_FOUND");return u}async function c(o){const t=o.uf.trim().toUpperCase(),u=o.cidade.trim(),d=o.logradouro.trim();if(!/^[A-Z]{2}$/.test(t)||u.length<3||d.length<3)throw new p("Pesquisa invalida. Informe UF com 2 letras, cidade e logradouro com no minimo 3 caracteres.","INVALID_ADDRESS");const f=[a,encodeURIComponent(t),encodeURIComponent(u),encodeURIComponent(d),"json"].join("/"),l=await r(`${f}/`);if(!Array.isArray(l))throw new p("Endereco nao encontrado na base do ViaCEP.","NOT_FOUND");return l}return{fetchCep:s,searchAddress:c,formatCep:g,isValidCep:h,sanitizeCep:m}}function y(n){const a=e.inject(_,null),r=n??a??V(),s=e.ref(!1),c=e.ref(null),o=e.ref(null),t=e.ref([]);async function u(f){s.value=!0,c.value=null,o.value=null;try{return o.value=await r.fetchCep(f),o.value}catch(l){throw c.value=l instanceof p?l.message:"Nao foi possivel consultar o CEP.",l}finally{s.value=!1}}async function d(f){s.value=!0,c.value=null,t.value=[];try{return t.value=await r.searchAddress(f),t.value}catch(l){throw c.value=l instanceof p?l.message:"Nao foi possivel pesquisar o endereco.",l}finally{s.value=!1}}return{loading:s,error:c,address:o,results:t,searchByCep:u,searchByAddress:d,client:r}}const N={class:"via-cep__row"},I=["disabled"],P={key:0,class:"via-cep__error",role:"alert"},A={key:1,class:"via-cep__result","aria-live":"polite"},E=((n,a)=>{const r=n.__vccOpts||n;for(const[s,c]of a)r[s]=c;return r})(e.defineComponent({__name:"ViaCepLookup",props:{label:{default:"CEP"},buttonText:{default:"Consultar"},modelValue:{default:""}},emits:["update:modelValue","found","error"],setup(n,{emit:a}){const r=n,s=a,c=`via-cep-${Math.random().toString(36).slice(2)}`,o=e.ref(r.modelValue),{address:t,loading:u,error:d,searchByCep:f,client:l}=y(),w=e.computed(()=>l.isValidCep(o.value));function v(){const C=l.sanitizeCep(o.value).slice(0,8);o.value=l.formatCep(C),s("update:modelValue",o.value)}async function U(){if(!w.value){d.value="Informe um CEP com 8 digitos.",s("error",d.value);return}try{const C=await f(o.value);s("found",C)}catch{s("error",d.value??"Nao foi possivel consultar o CEP.")}}return(C,D)=>(e.openBlock(),e.createElementBlock("form",{class:"via-cep",onSubmit:e.withModifiers(U,["prevent"])},[e.createElementVNode("label",{class:"via-cep__label",for:c},e.toDisplayString(n.label),1),e.createElementVNode("div",N,[e.withDirectives(e.createElementVNode("input",{id:c,"onUpdate:modelValue":D[0]||(D[0]=T=>o.value=T),class:"via-cep__input",inputmode:"numeric",maxlength:"9",placeholder:"01001-000",autocomplete:"postal-code",onInput:v},null,544),[[e.vModelText,o.value]]),e.createElementVNode("button",{class:"via-cep__button",type:"submit",disabled:e.unref(u)||!w.value},e.toDisplayString(e.unref(u)?"Consultando...":n.buttonText),9,I)]),e.unref(d)?(e.openBlock(),e.createElementBlock("p",P,e.toDisplayString(e.unref(d)),1)):e.createCommentVNode("",!0),e.unref(t)?(e.openBlock(),e.createElementBlock("section",A,[e.createElementVNode("strong",null,e.toDisplayString(e.unref(t).cep),1),e.createElementVNode("span",null,e.toDisplayString(e.unref(t).logradouro||"Logradouro nao informado"),1),e.createElementVNode("span",null,e.toDisplayString(e.unref(t).bairro||"Bairro nao informado"),1),e.createElementVNode("span",null,e.toDisplayString(e.unref(t).localidade)+" - "+e.toDisplayString(e.unref(t).uf),1)])):e.createCommentVNode("",!0)],32))}}),[["__scopeId","data-v-e1da2fc8"]]),_=Symbol("viaCep");function b(n={}){const a=V(n);return{install(r){r.provide(_,a),r.config.globalProperties.$viaCep=a,r.component("ViaCepLookup",E)}}}const k=b();i.ViaCepError=p,i.ViaCepLookup=E,i.createViaCepClient=V,i.createViaCepPlugin=b,i.default=k,i.formatCep=g,i.isValidCep=h,i.sanitizeCep=m,i.useViaCep=y,i.viaCepKey=_,Object.defineProperties(i,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}));
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "vue-viacep-plugin",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Plugin Vue 3 para consulta de CEP e endereco no Web Service ViaCEP.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"vue",
|
|
8
|
+
"vue3",
|
|
9
|
+
"viacep",
|
|
10
|
+
"cep",
|
|
11
|
+
"brasil",
|
|
12
|
+
"plugin"
|
|
13
|
+
],
|
|
14
|
+
"type": "module",
|
|
15
|
+
"main": "./dist/vue-viacep-plugin.umd.cjs",
|
|
16
|
+
"module": "./dist/vue-viacep-plugin.js",
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"import": "./dist/vue-viacep-plugin.js",
|
|
22
|
+
"require": "./dist/vue-viacep-plugin.umd.cjs"
|
|
23
|
+
},
|
|
24
|
+
"./style.css": "./dist/vue-viacep-plugin.css"
|
|
25
|
+
},
|
|
26
|
+
"files": [
|
|
27
|
+
"dist"
|
|
28
|
+
],
|
|
29
|
+
"publishConfig": {
|
|
30
|
+
"access": "public"
|
|
31
|
+
},
|
|
32
|
+
"scripts": {
|
|
33
|
+
"dev": "vite --host 127.0.0.1",
|
|
34
|
+
"build": "vite build --mode lib && vue-tsc -p tsconfig.build.json",
|
|
35
|
+
"typecheck": "vue-tsc --noEmit"
|
|
36
|
+
},
|
|
37
|
+
"peerDependencies": {
|
|
38
|
+
"vue": "^3.4.0 || ^3.5.0"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"@vitejs/plugin-vue": "^5.2.4",
|
|
42
|
+
"typescript": "^5.8.3",
|
|
43
|
+
"vite": "^6.3.5",
|
|
44
|
+
"vue": "^3.5.17",
|
|
45
|
+
"vue-tsc": "^2.2.10"
|
|
46
|
+
}
|
|
47
|
+
}
|