@eusilvio/cep-lookup 1.2.3 → 1.3.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 CHANGED
@@ -99,6 +99,46 @@ cepLookup.lookup("01001-000", myMapper).then((customAddress: CustomAddress) => {
99
99
  });
100
100
  ```
101
101
 
102
+ ### Example 3: Bulk CEP Lookup
103
+
104
+ For scenarios where you need to query multiple CEPs at once, you can use the `lookupCeps` function. It processes the CEPs in parallel with a configurable concurrency limit to avoid overwhelming the providers.
105
+
106
+ ```typescript
107
+ import { lookupCeps, BulkCepResult } from "@eusilvio/cep-lookup";
108
+ import {
109
+ viaCepProvider,
110
+ brasilApiProvider,
111
+ } from "@eusilvio/cep-lookup/providers";
112
+
113
+ const cepsToLookup = ["01001-000", "99999-999", "04538-132"];
114
+
115
+ lookupCeps({
116
+ ceps: cepsToLookup,
117
+ providers: [viaCepProvider, brasilApiProvider],
118
+ concurrency: 5, // Optional: Number of parallel requests
119
+ }).then((results: BulkCepResult[]) => {
120
+ console.log("Bulk lookup results:", results);
121
+ // Output:
122
+ // [
123
+ // {
124
+ // cep: '01001-000',
125
+ // data: { cep: '01001-000', state: 'SP', city: 'São Paulo', ... },
126
+ // provider: 'ViaCEP'
127
+ // },
128
+ // {
129
+ // cep: '99999-999',
130
+ // data: null,
131
+ // error: [Error: All providers failed to find the CEP]
132
+ // },
133
+ // {
134
+ // cep: '04538-132',
135
+ // data: { cep: '04538-132', state: 'SP', city: 'São Paulo', ... },
136
+ // provider: 'BrasilAPI'
137
+ // }
138
+ // ]
139
+ });
140
+ ```
141
+
102
142
  ## API
103
143
 
104
144
  ### `new CepLookup(options)`
@@ -117,6 +157,17 @@ Returns a `Promise` that resolves to the address in the default format (`Address
117
157
  - `cep` (string, **required**): The CEP to be queried.
118
158
  - `mapper` ((address: Address) => T, _optional_): A function that receives the default `Address` object and transforms it into a new format `T`.
119
159
 
160
+ ### `lookupCeps(options): Promise<BulkCepResult[]>`
161
+
162
+ Looks up multiple CEPs in bulk. Returns a `Promise` that resolves to an array of `BulkCepResult` objects, one for each queried CEP.
163
+
164
+ - `options`: A configuration object extending the `CepLookup` options.
165
+ - `ceps` (string[], **required**): An array of CEP strings to be queried.
166
+ - `providers` (Provider[], **required**): An array of providers.
167
+ - `concurrency` (number, _optional_): The number of parallel requests to make. Defaults to `5`.
168
+ - `fetcher` (Fetcher, _optional_): A custom fetch function.
169
+ - `cache` (Cache, _optional_): A cache instance.
170
+
120
171
  ## Examples
121
172
 
122
173
  You can find more detailed examples in the `examples/` directory:
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
- import { Address, Fetcher, Provider, CepLookupOptions } from "./types";
1
+ import { Address, Fetcher, Provider, CepLookupOptions, BulkCepResult } from "./types";
2
2
  import { Cache, InMemoryCache } from "./cache";
3
- export { Address, Fetcher, Provider, CepLookupOptions, Cache, InMemoryCache };
3
+ export { Address, Fetcher, Provider, CepLookupOptions, Cache, InMemoryCache, BulkCepResult };
4
4
  /**
5
5
  * @class CepLookup
6
6
  * @description A class for looking up Brazilian postal codes (CEPs) using multiple providers.
@@ -43,3 +43,13 @@ export declare function lookupCep<T = Address>(options: CepLookupOptions & {
43
43
  cep: string;
44
44
  mapper?: (address: Address) => T;
45
45
  }): Promise<T>;
46
+ /**
47
+ * @function lookupCeps
48
+ * @description Looks up multiple CEPs in bulk with controlled concurrency.
49
+ * @param {CepLookupOptions & { ceps: string[], concurrency?: number }} options - Options for the bulk lookup.
50
+ * @returns {Promise<BulkCepResult[]>} A Promise that resolves to an array of results for each CEP.
51
+ */
52
+ export declare function lookupCeps(options: CepLookupOptions & {
53
+ ceps: string[];
54
+ concurrency?: number;
55
+ }): Promise<BulkCepResult[]>;
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";var u=Object.defineProperty;var A=Object.getOwnPropertyDescriptor;var w=Object.getOwnPropertyNames;var T=Object.prototype.hasOwnProperty;var k=(s,e)=>{for(var t in e)u(s,t,{get:e[t],enumerable:!0})},C=(s,e,t,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of w(e))!T.call(s,r)&&r!==t&&u(s,r,{get:()=>e[r],enumerable:!(o=A(e,r))||o.enumerable});return s};var P=s=>C(u({},"__esModule",{value:!0}),s);var y={};k(y,{CepLookup:()=>d,InMemoryCache:()=>a,lookupCep:()=>E});module.exports=P(y);var a=class{constructor(){this.cache=new Map}get(e){return this.cache.get(e)}set(e,t){this.cache.set(e,t)}clear(){this.cache.clear()}};function b(s){let e=s.replace(/\D/g,"");if(e.length!==8)throw new Error("Invalid CEP. It must have 8 digits.");return e}var d=class{constructor(e){this.providers=e.providers,this.fetcher=e.fetcher||(async(t,o)=>{let r=await fetch(t,{signal:o});if(!r.ok)throw new Error(`HTTP error! status: ${r.status}`);return r.json()}),this.cache=e.cache}async lookup(e,t){let o=b(e);if(this.cache){let n=this.cache.get(o);if(n)return Promise.resolve(t?t(n):n)}let r=new AbortController,{signal:i}=r,p=this.providers.map(n=>{let f=n.buildUrl(o),v=new Promise((c,l)=>{let m,h=()=>{clearTimeout(m),l(new DOMException("Aborted","AbortError"))};n.timeout?(m=setTimeout(()=>{i.removeEventListener("abort",h),l(new Error(`Timeout from ${n.name}`))},n.timeout),i.addEventListener("abort",h,{once:!0})):i.addEventListener("abort",h,{once:!0})}),g=this.fetcher(f,i).then(c=>n.transform(c)).then(c=>(this.cache&&this.cache.set(o,c),t?t(c):c));return Promise.race([g,v])});try{return await Promise.any(p)}finally{r.abort()}}};function E(s){let{cep:e,providers:t,fetcher:o,mapper:r,cache:i}=s;return new d({providers:t,fetcher:o,cache:i}).lookup(e,r)}0&&(module.exports={CepLookup,InMemoryCache,lookupCep});
1
+ "use strict";var v=Object.defineProperty;var w=Object.getOwnPropertyDescriptor;var k=Object.getOwnPropertyNames;var A=Object.prototype.hasOwnProperty;var C=(s,e)=>{for(var r in e)v(s,r,{get:e[r],enumerable:!0})},y=(s,e,r,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let t of k(e))!A.call(s,t)&&t!==r&&v(s,t,{get:()=>e[t],enumerable:!(n=w(e,t))||n.enumerable});return s};var P=s=>y(v({},"__esModule",{value:!0}),s);var x={};C(x,{CepLookup:()=>u,InMemoryCache:()=>p,lookupCep:()=>b,lookupCeps:()=>E});module.exports=P(x);var p=class{constructor(){this.cache=new Map}get(e){return this.cache.get(e)}set(e,r){this.cache.set(e,r)}clear(){this.cache.clear()}};function T(s){let e=s.replace(/\D/g,"");if(e.length!==8)throw new Error("Invalid CEP. It must have 8 digits.");return e}var u=class{constructor(e){this.providers=e.providers,this.fetcher=e.fetcher||(async(r,n)=>{let t=await fetch(r,{signal:n});if(!t.ok)throw new Error(`HTTP error! status: ${t.status}`);return t.json()}),this.cache=e.cache}async lookup(e,r){let n=T(e);if(this.cache){let o=this.cache.get(n);if(o)return Promise.resolve(r?r(o):o)}let t=new AbortController,{signal:i}=t,h=this.providers.map(o=>{let l=o.buildUrl(n),m=new Promise((c,d)=>{let a,g=()=>{clearTimeout(a),d(new DOMException("Aborted","AbortError"))};o.timeout?(a=setTimeout(()=>{i.removeEventListener("abort",g),d(new Error(`Timeout from ${o.name}`))},o.timeout),i.addEventListener("abort",g,{once:!0})):i.addEventListener("abort",g,{once:!0})}),f=this.fetcher(l,i).then(c=>o.transform(c)).then(c=>(this.cache&&this.cache.set(n,c),r?r(c):c));return Promise.race([f,m])});try{return await Promise.any(h)}finally{t.abort()}}};function b(s){let{cep:e,providers:r,fetcher:n,mapper:t,cache:i}=s;return new u({providers:r,fetcher:n,cache:i}).lookup(e,t)}async function E(s){let{ceps:e,providers:r,fetcher:n,cache:t,concurrency:i=5}=s;if(!e||e.length===0)return[];let h=new u({providers:r,fetcher:n,cache:t}),o=new Array(e.length),l=0,m=async()=>{for(;l<e.length;){let c=l++;if(c>=e.length)break;let d=e[c];try{let a=await h.lookup(d);if(a)o[c]={cep:d,data:a,provider:a.service};else throw new Error("No address found")}catch(a){o[c]={cep:d,data:null,error:a}}}},f=Array.from({length:Math.min(i,e.length)},()=>m());return await Promise.all(f),o.filter(Boolean)}0&&(module.exports={CepLookup,InMemoryCache,lookupCep,lookupCeps});
package/dist/meta.json ADDED
@@ -0,0 +1,132 @@
1
+ {
2
+ "inputs": {
3
+ "src/types.ts": {
4
+ "bytes": 2529,
5
+ "imports": [
6
+ {
7
+ "path": "./cache",
8
+ "kind": "import-statement",
9
+ "external": true
10
+ }
11
+ ],
12
+ "format": "esm"
13
+ },
14
+ "src/cache/index.ts": {
15
+ "bytes": 1150,
16
+ "imports": [
17
+ {
18
+ "path": "../types",
19
+ "kind": "import-statement",
20
+ "external": true
21
+ }
22
+ ],
23
+ "format": "esm"
24
+ },
25
+ "src/index.ts": {
26
+ "bytes": 6549,
27
+ "imports": [
28
+ {
29
+ "path": "src/types.ts",
30
+ "kind": "import-statement",
31
+ "original": "./types"
32
+ },
33
+ {
34
+ "path": "src/cache/index.ts",
35
+ "kind": "import-statement",
36
+ "original": "./cache"
37
+ }
38
+ ],
39
+ "format": "esm"
40
+ },
41
+ "src/providers/viacep.ts": {
42
+ "bytes": 917,
43
+ "imports": [
44
+ {
45
+ "path": "../types",
46
+ "kind": "import-statement",
47
+ "external": true
48
+ }
49
+ ],
50
+ "format": "esm"
51
+ },
52
+ "src/providers/brasil-api.ts": {
53
+ "bytes": 790,
54
+ "imports": [
55
+ {
56
+ "path": "../types",
57
+ "kind": "import-statement",
58
+ "external": true
59
+ }
60
+ ],
61
+ "format": "esm"
62
+ },
63
+ "src/providers/apicep.ts": {
64
+ "bytes": 768,
65
+ "imports": [
66
+ {
67
+ "path": "../types",
68
+ "kind": "import-statement",
69
+ "external": true
70
+ }
71
+ ],
72
+ "format": "esm"
73
+ },
74
+ "src/providers/index.ts": {
75
+ "bytes": 83,
76
+ "imports": [
77
+ {
78
+ "path": "src/providers/viacep.ts",
79
+ "kind": "import-statement",
80
+ "original": "./viacep"
81
+ },
82
+ {
83
+ "path": "src/providers/brasil-api.ts",
84
+ "kind": "import-statement",
85
+ "original": "./brasil-api"
86
+ },
87
+ {
88
+ "path": "src/providers/apicep.ts",
89
+ "kind": "import-statement",
90
+ "original": "./apicep"
91
+ }
92
+ ],
93
+ "format": "esm"
94
+ }
95
+ },
96
+ "outputs": {
97
+ "dist/index.js": {
98
+ "imports": [],
99
+ "exports": [],
100
+ "entryPoint": "src/index.ts",
101
+ "inputs": {
102
+ "src/index.ts": {
103
+ "bytesInOutput": 1736
104
+ },
105
+ "src/cache/index.ts": {
106
+ "bytesInOutput": 135
107
+ }
108
+ },
109
+ "bytes": 2364
110
+ },
111
+ "dist/providers/index.js": {
112
+ "imports": [],
113
+ "exports": [],
114
+ "entryPoint": "src/providers/index.ts",
115
+ "inputs": {
116
+ "src/providers/index.ts": {
117
+ "bytesInOutput": 102
118
+ },
119
+ "src/providers/viacep.ts": {
120
+ "bytesInOutput": 236
121
+ },
122
+ "src/providers/brasil-api.ts": {
123
+ "bytesInOutput": 197
124
+ },
125
+ "src/providers/apicep.ts": {
126
+ "bytesInOutput": 193
127
+ }
128
+ },
129
+ "bytes": 1224
130
+ }
131
+ }
132
+ }
package/dist/types.d.ts CHANGED
@@ -46,3 +46,17 @@ export interface CepLookupOptions {
46
46
  fetcher?: Fetcher;
47
47
  cache?: Cache;
48
48
  }
49
+ /**
50
+ * @interface BulkCepResult
51
+ * @description Represents the result for a single CEP in a bulk lookup operation.
52
+ * @property {string} cep - The original CEP string.
53
+ * @property {Address | null} data - The address data if the lookup was successful, otherwise null.
54
+ * @property {string} [provider] - The name of the provider that successfully resolved the address.
55
+ * @property {Error} [error] - An error object if the lookup failed for this specific CEP.
56
+ */
57
+ export interface BulkCepResult {
58
+ cep: string;
59
+ data: Address | null;
60
+ provider?: string;
61
+ error?: Error;
62
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eusilvio/cep-lookup",
3
- "version": "1.2.3",
3
+ "version": "1.3.0",
4
4
  "description": "A modern, flexible, and agnostic CEP lookup library written in TypeScript.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",