@eusilvio/cep-lookup-react 0.1.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 +95 -0
- package/dist/CepProvider.d.ts +18 -0
- package/dist/CepProvider.d.ts.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1 -0
- package/dist/useBulkCepLookup.d.ts +10 -0
- package/dist/useBulkCepLookup.d.ts.map +1 -0
- package/dist/useCepLookup.d.ts +6 -0
- package/dist/useCepLookup.d.ts.map +1 -0
- package/package.json +58 -0
package/README.md
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
# @eusilvio/cep-lookup-react
|
|
2
|
+
|
|
3
|
+
React hook for [`@eusilvio/cep-lookup`](https://www.npmjs.com/package/@eusilvio/cep-lookup).
|
|
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.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```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
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Basic Usage
|
|
16
|
+
|
|
17
|
+
Wrap your application or component tree with the `CepProvider` and use the `useCepLookup` hook anywhere inside it.
|
|
18
|
+
|
|
19
|
+
```jsx
|
|
20
|
+
import React from 'react';
|
|
21
|
+
import { CepProvider, useCepLookup } from '@eusilvio/cep-lookup-react';
|
|
22
|
+
|
|
23
|
+
const CepDisplay = () => {
|
|
24
|
+
const [cep, setCep] = React.useState('01001000');
|
|
25
|
+
const { address, loading, error } = useCepLookup(cep);
|
|
26
|
+
|
|
27
|
+
return (
|
|
28
|
+
<div>
|
|
29
|
+
<input value={cep} onChange={(e) => setCep(e.target.value)} />
|
|
30
|
+
{loading && <p>Loading...</p>}
|
|
31
|
+
{error && <p>Error: {error.message}</p>}
|
|
32
|
+
{address && (
|
|
33
|
+
<pre>{JSON.stringify(address, null, 2)}</pre>
|
|
34
|
+
)}
|
|
35
|
+
</div>
|
|
36
|
+
);
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const App = () => (
|
|
40
|
+
<CepProvider>
|
|
41
|
+
<CepDisplay />
|
|
42
|
+
</CepProvider>
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
export default App;
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Advanced Configuration
|
|
49
|
+
|
|
50
|
+
You can pass any `CepLookupOptions` to the `CepProvider` to customize its behavior, such as changing providers, adding a custom cache, or setting a rate limit.
|
|
51
|
+
|
|
52
|
+
### Example: Using only the ViaCEP provider
|
|
53
|
+
|
|
54
|
+
```jsx
|
|
55
|
+
import { CepProvider } from '@eusilvio/cep-lookup-react';
|
|
56
|
+
import { viaCepProvider } from '@eusilvio/cep-lookup/providers';
|
|
57
|
+
|
|
58
|
+
const App = () => (
|
|
59
|
+
<CepProvider providers={[viaCepProvider]}>
|
|
60
|
+
{/* Your components here */}
|
|
61
|
+
</CepProvider>
|
|
62
|
+
);
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## API
|
|
66
|
+
|
|
67
|
+
### `<CepProvider />`
|
|
68
|
+
|
|
69
|
+
A React component that provides the `CepLookup` instance to its children.
|
|
70
|
+
|
|
71
|
+
**Props**
|
|
72
|
+
|
|
73
|
+
It accepts all options from `CepLookupOptions` as props:
|
|
74
|
+
|
|
75
|
+
- `providers` (optional): `Provider[]` - An array of CEP providers. Defaults to `[viaCepProvider, brasilApiProvider, apicepProvider]`.
|
|
76
|
+
- `cache` (optional): `Cache` - A cache instance (e.g., `new InMemoryCache()`). Defaults to a new `InMemoryCache` instance.
|
|
77
|
+
- `rateLimit` (optional): `RateLimitOptions` - Options for rate limiting.
|
|
78
|
+
- `fetcher` (optional): `Fetcher` - A custom fetch implementation.
|
|
79
|
+
|
|
80
|
+
### `useCepLookup(cep: string, debounceTime?: number)`
|
|
81
|
+
|
|
82
|
+
A React hook that performs the CEP lookup.
|
|
83
|
+
|
|
84
|
+
**Parameters**
|
|
85
|
+
|
|
86
|
+
- `cep`: `string` - The CEP to look up.
|
|
87
|
+
- `debounceTime` (optional): `number` - The debounce time in milliseconds before the lookup is triggered. Defaults to `500`.
|
|
88
|
+
|
|
89
|
+
**Returns**
|
|
90
|
+
|
|
91
|
+
An object with the following properties:
|
|
92
|
+
|
|
93
|
+
- `address`: `Address | null` - The found address object, or `null` if not found or not yet loaded.
|
|
94
|
+
- `loading`: `boolean` - `true` when a lookup is in progress.
|
|
95
|
+
- `error`: `Error | null` - An `Error` object if the lookup fails, otherwise `null`.
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import React, { ReactNode } from "react";
|
|
2
|
+
import { Address, CepLookup, CepLookupOptions, EventMap } from "@eusilvio/cep-lookup";
|
|
3
|
+
export interface CepContextValue {
|
|
4
|
+
instance: CepLookup;
|
|
5
|
+
mapper?: (address: Address) => any;
|
|
6
|
+
options: CepLookupOptions;
|
|
7
|
+
}
|
|
8
|
+
interface CepProviderProps extends Partial<CepLookupOptions> {
|
|
9
|
+
children: ReactNode;
|
|
10
|
+
mapper?: (address: Address) => any;
|
|
11
|
+
onSuccess?: (event: EventMap['success']) => void;
|
|
12
|
+
onFailure?: (event: EventMap['failure']) => void;
|
|
13
|
+
onCacheHit?: (event: EventMap['cache:hit']) => void;
|
|
14
|
+
}
|
|
15
|
+
export declare const CepProvider: React.FC<CepProviderProps>;
|
|
16
|
+
export declare const useCepLookupInstance: () => CepContextValue;
|
|
17
|
+
export {};
|
|
18
|
+
//# sourceMappingURL=CepProvider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CepProvider.d.ts","sourceRoot":"","sources":["../src/CepProvider.tsx"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,EAEZ,SAAS,EAIV,MAAM,OAAO,CAAC;AACf,OAAO,EACL,OAAO,EACP,SAAS,EACT,gBAAgB,EAEhB,QAAQ,EACT,MAAM,sBAAsB,CAAC;AAS9B,MAAM,WAAW,eAAe;IAC9B,QAAQ,EAAE,SAAS,CAAC;IACpB,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,GAAG,CAAC;IAEnC,OAAO,EAAE,gBAAgB,CAAC;CAC3B;AAQD,UAAU,gBAAiB,SAAQ,OAAO,CAAC,gBAAgB,CAAC;IAC1D,QAAQ,EAAE,SAAS,CAAC;IACpB,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,GAAG,CAAC;IAEnC,SAAS,CAAC,EAAE,CAAC,KAAK,EAAE,QAAQ,CAAC,SAAS,CAAC,KAAK,IAAI,CAAC;IACjD,SAAS,CAAC,EAAE,CAAC,KAAK,EAAE,QAAQ,CAAC,SAAS,CAAC,KAAK,IAAI,CAAC;IACjD,UAAU,CAAC,EAAE,CAAC,KAAK,EAAE,QAAQ,CAAC,WAAW,CAAC,KAAK,IAAI,CAAC;CACrD;AAED,eAAO,MAAM,WAAW,EAAE,KAAK,CAAC,EAAE,CAAC,gBAAgB,CA2DlD,CAAC;AAEF,eAAO,MAAM,oBAAoB,uBAOhC,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,eAAe,CAAC;AAC9B,cAAc,oBAAoB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var x=Object.defineProperty;var b=Object.getOwnPropertyDescriptor;var I=Object.getOwnPropertyNames;var A=Object.prototype.hasOwnProperty;var M=(e,t)=>{for(var o in t)x(e,o,{get:t[o],enumerable:!0})},O=(e,t,o,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let n of I(t))!A.call(e,n)&&n!==o&&x(e,n,{get:()=>t[n],enumerable:!(r=b(t,n))||r.enumerable});return e};var B=e=>O(x({},"__esModule",{value:!0}),e);var F={};M(F,{CepProvider:()=>N,useBulkCepLookup:()=>D,useCepLookup:()=>V,useCepLookupInstance:()=>L});module.exports=B(F);var f=require("react");var v=require("@eusilvio/cep-lookup/providers"),p=require("react"),d=require("@eusilvio/cep-lookup"),w=require("react/jsx-runtime"),y=[v.viaCepProvider,v.brasilApiProvider,v.apicepProvider],P=new d.CepLookup({providers:y,cache:new d.InMemoryCache}),g=(0,p.createContext)({instance:P,options:{providers:y,cache:new d.InMemoryCache}}),N=({children:e,mapper:t,onSuccess:o,onFailure:r,onCacheHit:n,...s})=>{let a=(0,p.useMemo)(()=>Object.keys(s).length?new d.CepLookup({providers:y,cache:new d.InMemoryCache,...s}):P,[s]);(0,p.useEffect)(()=>(o&&a.on("success",o),r&&a.on("failure",r),n&&a.on("cache:hit",n),()=>{o&&a.off("success",o),r&&a.off("failure",r),n&&a.off("cache:hit",n)}),[a,o,r,n]);let c=(0,p.useMemo)(()=>({instance:a,mapper:t,options:{providers:y,cache:new d.InMemoryCache,...s}}),[a,t,s]);return(0,w.jsx)(g.Provider,{value:c,children:e})},L=()=>{let e=(0,p.useContext)(g);if(!e)throw new Error("useCepLookupInstance must be used within a CepProvider");return e};function T(e,t){let o;return(...r)=>new Promise(n=>{o&&clearTimeout(o),o=setTimeout(()=>n(e(...r)),t)})}var V=(e,t=500)=>{let[o,r]=(0,f.useState)(null),[n,s]=(0,f.useState)(null),[a,c]=(0,f.useState)(!1),{instance:h,mapper:C}=L(),u=(0,f.useCallback)(T(async i=>{c(!0),s(null);try{let l=await h.lookup(i);r(C?C(l):l)}catch(l){s(l),r(null)}finally{c(!1)}},t),[t,h,C]);return(0,f.useEffect)(()=>{let i=e.replace(/\D/g,"");i.length===8?u(i):(r(null),s(null))},[e,u]),{address:o,error:n,loading:a}};var m=require("react"),E=require("@eusilvio/cep-lookup");var D=(e,t)=>{let[o,r]=(0,m.useState)([]),[n,s]=(0,m.useState)(!1),[a,c]=(0,m.useState)(null),{instance:h,mapper:C,options:u}=L(),i=(0,m.useCallback)(async()=>{if(!e||e.length===0){r([]),s(!1),c(null);return}s(!0),c(null);try{let l=e.map(k=>k.replace(/\D/g,"")),R=(await(0,E.lookupCeps)({ceps:l,providers:u.providers,cache:u.cache,fetcher:u.fetcher,rateLimit:u.rateLimit,concurrency:t?.concurrency})).map(k=>({...k,data:k.data&&C?C(k.data):k.data}));r(R)}catch(l){c(l),r([])}finally{s(!1)}},[e,t,h,C,u]);return(0,m.useEffect)(()=>{i()},[i]),{results:o,loading:n,error:a,performBulkLookup:i}};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { BulkCepResult } from '@eusilvio/cep-lookup';
|
|
2
|
+
export declare const useBulkCepLookup: (ceps: string[], options?: {
|
|
3
|
+
concurrency?: number;
|
|
4
|
+
}) => {
|
|
5
|
+
results: BulkCepResult[];
|
|
6
|
+
loading: boolean;
|
|
7
|
+
error: Error;
|
|
8
|
+
performBulkLookup: () => Promise<void>;
|
|
9
|
+
};
|
|
10
|
+
//# sourceMappingURL=useBulkCepLookup.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useBulkCepLookup.d.ts","sourceRoot":"","sources":["../src/useBulkCepLookup.ts"],"names":[],"mappings":"AACA,OAAO,EAAc,aAAa,EAAoB,MAAM,sBAAsB,CAAC;AAGnF,eAAO,MAAM,gBAAgB,GAC3B,MAAM,MAAM,EAAE,EACd,UAAU;IAAE,WAAW,CAAC,EAAE,MAAM,CAAA;CAAE;;;;;CAgDnC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useCepLookup.d.ts","sourceRoot":"","sources":["../src/useCepLookup.ts"],"names":[],"mappings":"AAkBA,eAAO,MAAM,YAAY,GAAI,KAAK,MAAM,EAAE,qBAAkB;;;;CAmC3D,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@eusilvio/cep-lookup-react",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "React hook for cep-lookup.",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"clean": "rm -rf dist && rm tsconfig.tsbuildinfo",
|
|
9
|
+
"build": "npm run clean && tsc --build && esbuild src/index.ts --bundle --platform=neutral --format=cjs --outdir=dist --minify --external:react --external:@eusilvio/cep-lookup",
|
|
10
|
+
"test": "jest"
|
|
11
|
+
},
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"require": "./dist/index.js",
|
|
15
|
+
"import": "./dist/index.js",
|
|
16
|
+
"types": "./dist/index.d.ts"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"peerDependencies": {
|
|
20
|
+
"@eusilvio/cep-lookup": "^2.0.1",
|
|
21
|
+
"react": ">=16.8.0",
|
|
22
|
+
"react-dom": ">=16.8.0"
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"dist"
|
|
26
|
+
],
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@types/react": "^18.3.3",
|
|
29
|
+
"@types/react-dom": "^18.3.0",
|
|
30
|
+
"jest": "^30.1.3",
|
|
31
|
+
"ts-jest": "^29.4.4",
|
|
32
|
+
"@types/jest": "^30.0.0",
|
|
33
|
+
"jest-environment-jsdom": "^29.7.0",
|
|
34
|
+
"@testing-library/react": "^15.0.7",
|
|
35
|
+
"@testing-library/jest-dom": "^6.9.1",
|
|
36
|
+
"esbuild": "^0.20.2"
|
|
37
|
+
},
|
|
38
|
+
"license": "MIT",
|
|
39
|
+
"publishConfig": {
|
|
40
|
+
"access": "public"
|
|
41
|
+
},
|
|
42
|
+
"repository": {
|
|
43
|
+
"type": "git",
|
|
44
|
+
"url": "git+https://github.com/eusilvio/cep-lookup.git"
|
|
45
|
+
},
|
|
46
|
+
"bugs": {
|
|
47
|
+
"url": "https://github.com/eusilvio/cep-lookup/issues"
|
|
48
|
+
},
|
|
49
|
+
"homepage": "https://github.com/eusilvio/cep-lookup#readme",
|
|
50
|
+
"keywords": [
|
|
51
|
+
"cep",
|
|
52
|
+
"lookup",
|
|
53
|
+
"react",
|
|
54
|
+
"hook"
|
|
55
|
+
],
|
|
56
|
+
"author": "Silvio Souza",
|
|
57
|
+
"sideEffects": false
|
|
58
|
+
}
|