@eusilvio/cep-lookup-react 0.4.1 → 2.6.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/README.md +64 -65
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,107 +1,106 @@
|
|
|
1
1
|
# @eusilvio/cep-lookup-react
|
|
2
2
|
|
|
3
|
-
React
|
|
4
|
-
|
|
5
|
-
Provides a flexible and easy-to-use React hook (`useCepLookup`) to look up Brazilian postal codes (CEPs), with built-in debounce, caching, and full configuration via a React Context Provider.
|
|
3
|
+
React hooks/provider for [`@eusilvio/cep-lookup`](https://www.npmjs.com/package/@eusilvio/cep-lookup).
|
|
6
4
|
|
|
7
5
|
## Installation
|
|
8
6
|
|
|
9
7
|
```bash
|
|
10
|
-
npm install @eusilvio/cep-lookup @eusilvio/cep-lookup-react react
|
|
11
|
-
# or
|
|
12
|
-
yarn add @eusilvio/cep-lookup @eusilvio/cep-lookup-react react
|
|
8
|
+
npm install @eusilvio/cep-lookup @eusilvio/cep-lookup-react react react-dom
|
|
13
9
|
```
|
|
14
10
|
|
|
15
11
|
## Compatibility
|
|
16
12
|
|
|
17
13
|
- React: `>= 16.8`
|
|
18
|
-
- Node.js (
|
|
19
|
-
-
|
|
14
|
+
- Node.js (tooling/tests): `20.x`, `22.x`, `24.x`
|
|
15
|
+
- Core peer: `@eusilvio/cep-lookup ^2.6.0`
|
|
20
16
|
|
|
21
17
|
## Basic Usage
|
|
22
18
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
import React from 'react';
|
|
27
|
-
import { CepProvider, useCepLookup } from '@eusilvio/cep-lookup-react';
|
|
19
|
+
```tsx
|
|
20
|
+
import React from "react";
|
|
21
|
+
import { CepProvider, useCepLookup } from "@eusilvio/cep-lookup-react";
|
|
28
22
|
|
|
29
|
-
|
|
30
|
-
const [cep, setCep] = React.useState(
|
|
31
|
-
const { address, loading, error } = useCepLookup(cep);
|
|
23
|
+
function CepField() {
|
|
24
|
+
const [cep, setCep] = React.useState("01001000");
|
|
25
|
+
const { address, loading, error, warmup } = useCepLookup(cep);
|
|
32
26
|
|
|
33
27
|
return (
|
|
34
28
|
<div>
|
|
35
|
-
<input
|
|
29
|
+
<input
|
|
30
|
+
value={cep}
|
|
31
|
+
onFocus={() => warmup()}
|
|
32
|
+
onChange={(e) => setCep(e.target.value)}
|
|
33
|
+
/>
|
|
36
34
|
{loading && <p>Loading...</p>}
|
|
37
|
-
{error && <p>
|
|
38
|
-
{address && (
|
|
39
|
-
<pre>{JSON.stringify(address, null, 2)}</pre>
|
|
40
|
-
)}
|
|
35
|
+
{error && <p>{error.message}</p>}
|
|
36
|
+
{address && <pre>{JSON.stringify(address, null, 2)}</pre>}
|
|
41
37
|
</div>
|
|
42
38
|
);
|
|
43
|
-
}
|
|
39
|
+
}
|
|
44
40
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
<
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
41
|
+
export default function App() {
|
|
42
|
+
return (
|
|
43
|
+
<CepProvider>
|
|
44
|
+
<CepField />
|
|
45
|
+
</CepProvider>
|
|
46
|
+
);
|
|
47
|
+
}
|
|
52
48
|
```
|
|
53
49
|
|
|
54
|
-
##
|
|
50
|
+
## Resilience Example (Provider Options)
|
|
55
51
|
|
|
56
|
-
|
|
52
|
+
```tsx
|
|
53
|
+
import { CepProvider } from "@eusilvio/cep-lookup-react";
|
|
54
|
+
import { viaCepProvider, brasilApiProvider } from "@eusilvio/cep-lookup/providers";
|
|
57
55
|
|
|
58
|
-
|
|
56
|
+
<CepProvider
|
|
57
|
+
providers={[viaCepProvider, brasilApiProvider]}
|
|
58
|
+
circuitBreaker={{ enabled: true, failureThreshold: 3, cooldownMs: 30000 }}
|
|
59
|
+
retries={1}
|
|
60
|
+
rateLimit={{ requests: 60, per: 60_000 }}
|
|
61
|
+
>
|
|
62
|
+
{/* app */}
|
|
63
|
+
</CepProvider>
|
|
64
|
+
```
|
|
59
65
|
|
|
60
|
-
|
|
61
|
-
import { CepProvider } from '@eusilvio/cep-lookup-react';
|
|
62
|
-
import { viaCepProvider } from '@eusilvio/cep-lookup/providers';
|
|
66
|
+
## Events Example
|
|
63
67
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
)
|
|
68
|
+
```tsx
|
|
69
|
+
<CepProvider
|
|
70
|
+
onSuccess={({ provider, duration }) => console.log("success", provider, duration)}
|
|
71
|
+
onFailure={({ provider, error }) => console.log("failure", provider, error.message)}
|
|
72
|
+
onCacheHit={({ cep }) => console.log("cache hit", cep)}
|
|
73
|
+
>
|
|
74
|
+
{/* app */}
|
|
75
|
+
</CepProvider>
|
|
69
76
|
```
|
|
70
77
|
|
|
71
78
|
## API
|
|
72
79
|
|
|
73
|
-
### `<CepProvider />`
|
|
74
|
-
|
|
75
|
-
A React component that provides the `CepLookup` instance to its children.
|
|
76
|
-
|
|
77
|
-
**Props**
|
|
78
|
-
|
|
79
|
-
It accepts all options from `CepLookupOptions` as props:
|
|
80
|
-
|
|
81
|
-
- `providers` (optional): `Provider[]` - An array of CEP providers.
|
|
82
|
-
- `cache` (optional): `Cache` - A cache instance. Defaults to a persistent `InMemoryCache`.
|
|
83
|
-
- `rateLimit` (optional): `RateLimitOptions` - Options for rate limiting.
|
|
84
|
-
- `mapper` (optional): `(address: Address) => T` - A function to transform the address object globally.
|
|
85
|
-
- `onSuccess` (optional): `(event) => void` - Callback triggered on successful lookups.
|
|
86
|
-
- `onFailure` (optional): `(event) => void` - Callback triggered on lookup failures.
|
|
87
|
-
- `onCacheHit` (optional): `(event) => void` - Callback triggered on cache hits.
|
|
88
|
-
|
|
89
80
|
### `useCepLookup<T = Address>(cep: string, delay?: number)`
|
|
90
81
|
|
|
91
|
-
|
|
82
|
+
Returns `{ address, error, loading, warmup }`.
|
|
92
83
|
|
|
93
|
-
|
|
84
|
+
### `useBulkCepLookup<T = Address>(ceps: string[], options?)`
|
|
94
85
|
|
|
95
|
-
|
|
96
|
-
- `delay` (optional): `number` - The debounce delay in milliseconds. Defaults to `500`.
|
|
86
|
+
Returns `{ results, error, loading, refresh }`.
|
|
97
87
|
|
|
98
|
-
|
|
88
|
+
### `<CepProvider />`
|
|
99
89
|
|
|
100
|
-
|
|
90
|
+
Accepts `CepLookupOptions` as props (`providers`, `cache`, `rateLimit`, `retries`, `retryDelay`, `circuitBreaker`, `staggerDelay`, `fetcher`) plus:
|
|
101
91
|
|
|
102
|
-
- `
|
|
92
|
+
- `mapper`
|
|
93
|
+
- `onSuccess`
|
|
94
|
+
- `onFailure`
|
|
95
|
+
- `onCacheHit`
|
|
103
96
|
|
|
104
97
|
## Community
|
|
105
98
|
|
|
106
|
-
-
|
|
107
|
-
-
|
|
99
|
+
- [CONTRIBUTING.md](../../CONTRIBUTING.md)
|
|
100
|
+
- [CODE_OF_CONDUCT.md](../../CODE_OF_CONDUCT.md)
|
|
101
|
+
- [SECURITY.md](../../SECURITY.md)
|
|
102
|
+
|
|
103
|
+
## Production docs
|
|
104
|
+
|
|
105
|
+
- [Best Practices](../../docs/BEST_PRACTICES.md)
|
|
106
|
+
- [Cookbook](../../docs/COOKBOOK.md)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eusilvio/cep-lookup-react",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.6.0",
|
|
4
4
|
"description": "React hooks and provider for @eusilvio/cep-lookup",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
}
|
|
13
13
|
},
|
|
14
14
|
"peerDependencies": {
|
|
15
|
-
"@eusilvio/cep-lookup": "^2.
|
|
15
|
+
"@eusilvio/cep-lookup": "^2.6.0",
|
|
16
16
|
"react": ">=16.8.0",
|
|
17
17
|
"react-dom": ">=16.8.0"
|
|
18
18
|
},
|