@eusilvio/cep-lookup 1.2.2 → 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 +51 -0
- package/dist/index.d.ts +12 -2
- package/dist/index.js +1 -147
- package/dist/meta.json +132 -0
- package/dist/providers/index.js +1 -84
- package/dist/types.d.ts +14 -0
- package/package.json +5 -27
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,147 +1 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/index.ts
|
|
21
|
-
var src_exports = {};
|
|
22
|
-
__export(src_exports, {
|
|
23
|
-
CepLookup: () => CepLookup,
|
|
24
|
-
InMemoryCache: () => InMemoryCache,
|
|
25
|
-
lookupCep: () => lookupCep
|
|
26
|
-
});
|
|
27
|
-
module.exports = __toCommonJS(src_exports);
|
|
28
|
-
|
|
29
|
-
// src/cache/index.ts
|
|
30
|
-
var InMemoryCache = class {
|
|
31
|
-
constructor() {
|
|
32
|
-
this.cache = /* @__PURE__ */ new Map();
|
|
33
|
-
}
|
|
34
|
-
/**
|
|
35
|
-
* @method get
|
|
36
|
-
* @description Retrieves an address from the cache.
|
|
37
|
-
* @param {string} key - The CEP to look up.
|
|
38
|
-
* @returns {Address | undefined} The cached address or undefined if not found.
|
|
39
|
-
*/
|
|
40
|
-
get(key) {
|
|
41
|
-
return this.cache.get(key);
|
|
42
|
-
}
|
|
43
|
-
/**
|
|
44
|
-
* @method set
|
|
45
|
-
* @description Stores an address in the cache.
|
|
46
|
-
* @param {string} key - The CEP to use as the cache key.
|
|
47
|
-
* @param {Address} value - The address to store.
|
|
48
|
-
*/
|
|
49
|
-
set(key, value) {
|
|
50
|
-
this.cache.set(key, value);
|
|
51
|
-
}
|
|
52
|
-
/**
|
|
53
|
-
* @method clear
|
|
54
|
-
* @description Clears the entire cache.
|
|
55
|
-
*/
|
|
56
|
-
clear() {
|
|
57
|
-
this.cache.clear();
|
|
58
|
-
}
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
// src/index.ts
|
|
62
|
-
function validateCep(cep) {
|
|
63
|
-
const cleanedCep = cep.replace(/\D/g, "");
|
|
64
|
-
if (cleanedCep.length !== 8) {
|
|
65
|
-
throw new Error("Invalid CEP. It must have 8 digits.");
|
|
66
|
-
}
|
|
67
|
-
return cleanedCep;
|
|
68
|
-
}
|
|
69
|
-
var CepLookup = class {
|
|
70
|
-
/**
|
|
71
|
-
* @constructor
|
|
72
|
-
* @param {CepLookupOptions} options - The options for initializing the CepLookup instance.
|
|
73
|
-
*/
|
|
74
|
-
constructor(options) {
|
|
75
|
-
this.providers = options.providers;
|
|
76
|
-
this.fetcher = options.fetcher || (async (url, signal) => {
|
|
77
|
-
const response = await fetch(url, { signal });
|
|
78
|
-
if (!response.ok) {
|
|
79
|
-
throw new Error(`HTTP error! status: ${response.status}`);
|
|
80
|
-
}
|
|
81
|
-
return response.json();
|
|
82
|
-
});
|
|
83
|
-
this.cache = options.cache;
|
|
84
|
-
}
|
|
85
|
-
/**
|
|
86
|
-
* @method lookup
|
|
87
|
-
* @description Looks up an address for a given CEP.
|
|
88
|
-
* @template T - The expected return type, defaults to `Address`.
|
|
89
|
-
* @param {string} cep - The CEP to be queried.
|
|
90
|
-
* @param {(address: Address) => T} [mapper] - An optional function to transform the `Address` object into a custom format `T`.
|
|
91
|
-
* @returns {Promise<T>} A Promise that resolves to the address in the default `Address` format or a custom format `T` if a mapper is provided.
|
|
92
|
-
* @throws {Error} If the CEP is invalid or if all providers fail to find the CEP.
|
|
93
|
-
*/
|
|
94
|
-
async lookup(cep, mapper) {
|
|
95
|
-
const cleanedCep = validateCep(cep);
|
|
96
|
-
if (this.cache) {
|
|
97
|
-
const cachedAddress = this.cache.get(cleanedCep);
|
|
98
|
-
if (cachedAddress) {
|
|
99
|
-
return Promise.resolve(mapper ? mapper(cachedAddress) : cachedAddress);
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
const controller = new AbortController();
|
|
103
|
-
const { signal } = controller;
|
|
104
|
-
const promises = this.providers.map((provider) => {
|
|
105
|
-
const url = provider.buildUrl(cleanedCep);
|
|
106
|
-
const timeoutPromise = new Promise((resolve, reject) => {
|
|
107
|
-
let timeoutId;
|
|
108
|
-
const onAbort = () => {
|
|
109
|
-
clearTimeout(timeoutId);
|
|
110
|
-
reject(new DOMException("Aborted", "AbortError"));
|
|
111
|
-
};
|
|
112
|
-
if (provider.timeout) {
|
|
113
|
-
timeoutId = setTimeout(() => {
|
|
114
|
-
signal.removeEventListener("abort", onAbort);
|
|
115
|
-
reject(new Error(`Timeout from ${provider.name}`));
|
|
116
|
-
}, provider.timeout);
|
|
117
|
-
signal.addEventListener("abort", onAbort, { once: true });
|
|
118
|
-
} else {
|
|
119
|
-
signal.addEventListener("abort", onAbort, { once: true });
|
|
120
|
-
}
|
|
121
|
-
});
|
|
122
|
-
const fetchPromise = this.fetcher(url, signal).then((response) => provider.transform(response)).then((address) => {
|
|
123
|
-
if (this.cache) {
|
|
124
|
-
this.cache.set(cleanedCep, address);
|
|
125
|
-
}
|
|
126
|
-
return mapper ? mapper(address) : address;
|
|
127
|
-
});
|
|
128
|
-
return Promise.race([fetchPromise, timeoutPromise]);
|
|
129
|
-
});
|
|
130
|
-
try {
|
|
131
|
-
return await Promise.any(promises);
|
|
132
|
-
} finally {
|
|
133
|
-
controller.abort();
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
};
|
|
137
|
-
function lookupCep(options) {
|
|
138
|
-
const { cep, providers, fetcher, mapper, cache } = options;
|
|
139
|
-
const cepLookup = new CepLookup({ providers, fetcher, cache });
|
|
140
|
-
return cepLookup.lookup(cep, mapper);
|
|
141
|
-
}
|
|
142
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
143
|
-
0 && (module.exports = {
|
|
144
|
-
CepLookup,
|
|
145
|
-
InMemoryCache,
|
|
146
|
-
lookupCep
|
|
147
|
-
});
|
|
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/providers/index.js
CHANGED
|
@@ -1,84 +1 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/providers/index.ts
|
|
21
|
-
var providers_exports = {};
|
|
22
|
-
__export(providers_exports, {
|
|
23
|
-
apicepProvider: () => apicepProvider,
|
|
24
|
-
brasilApiProvider: () => brasilApiProvider,
|
|
25
|
-
viaCepProvider: () => viaCepProvider
|
|
26
|
-
});
|
|
27
|
-
module.exports = __toCommonJS(providers_exports);
|
|
28
|
-
|
|
29
|
-
// src/providers/viacep.ts
|
|
30
|
-
var viaCepProvider = {
|
|
31
|
-
name: "ViaCEP",
|
|
32
|
-
buildUrl: (cep) => `https://viacep.com.br/ws/${cep}/json/`,
|
|
33
|
-
transform: (response) => {
|
|
34
|
-
if (response.erro) {
|
|
35
|
-
throw new Error("CEP not found");
|
|
36
|
-
}
|
|
37
|
-
return {
|
|
38
|
-
cep: response.cep,
|
|
39
|
-
state: response.uf,
|
|
40
|
-
city: response.localidade,
|
|
41
|
-
neighborhood: response.bairro,
|
|
42
|
-
street: response.logradouro,
|
|
43
|
-
service: "ViaCEP"
|
|
44
|
-
};
|
|
45
|
-
}
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
// src/providers/brasil-api.ts
|
|
49
|
-
var brasilApiProvider = {
|
|
50
|
-
name: "BrasilAPI",
|
|
51
|
-
buildUrl: (cep) => `https://brasilapi.com.br/api/cep/v1/${cep}`,
|
|
52
|
-
transform: (response) => {
|
|
53
|
-
return {
|
|
54
|
-
cep: response.cep,
|
|
55
|
-
state: response.state,
|
|
56
|
-
city: response.city,
|
|
57
|
-
neighborhood: response.neighborhood,
|
|
58
|
-
street: response.street,
|
|
59
|
-
service: "BrasilAPI"
|
|
60
|
-
};
|
|
61
|
-
}
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
// src/providers/apicep.ts
|
|
65
|
-
var apicepProvider = {
|
|
66
|
-
name: "ApiCEP",
|
|
67
|
-
buildUrl: (cep) => `https://cdn.apicep.com/file/apicep/${cep}.json`,
|
|
68
|
-
transform: (response) => {
|
|
69
|
-
return {
|
|
70
|
-
cep: response.code,
|
|
71
|
-
state: response.state,
|
|
72
|
-
city: response.city,
|
|
73
|
-
neighborhood: response.district,
|
|
74
|
-
street: response.address,
|
|
75
|
-
service: "ApiCEP"
|
|
76
|
-
};
|
|
77
|
-
}
|
|
78
|
-
};
|
|
79
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
80
|
-
0 && (module.exports = {
|
|
81
|
-
apicepProvider,
|
|
82
|
-
brasilApiProvider,
|
|
83
|
-
viaCepProvider
|
|
84
|
-
});
|
|
1
|
+
"use strict";var e=Object.defineProperty;var a=Object.getOwnPropertyDescriptor;var c=Object.getOwnPropertyNames;var s=Object.prototype.hasOwnProperty;var m=(r,t)=>{for(var o in t)e(r,o,{get:t[o],enumerable:!0})},p=(r,t,o,d)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of c(t))!s.call(r,i)&&i!==o&&e(r,i,{get:()=>t[i],enumerable:!(d=a(t,i))||d.enumerable});return r};var P=r=>p(e({},"__esModule",{value:!0}),r);var f={};m(f,{apicepProvider:()=>l,brasilApiProvider:()=>v,viaCepProvider:()=>n});module.exports=P(f);var n={name:"ViaCEP",buildUrl:r=>`https://viacep.com.br/ws/${r}/json/`,transform:r=>{if(r.erro)throw new Error("CEP not found");return{cep:r.cep,state:r.uf,city:r.localidade,neighborhood:r.bairro,street:r.logradouro,service:"ViaCEP"}}};var v={name:"BrasilAPI",buildUrl:r=>`https://brasilapi.com.br/api/cep/v1/${r}`,transform:r=>({cep:r.cep,state:r.state,city:r.city,neighborhood:r.neighborhood,street:r.street,service:"BrasilAPI"})};var l={name:"ApiCEP",buildUrl:r=>`https://cdn.apicep.com/file/apicep/${r}.json`,transform:r=>({cep:r.code,state:r.state,city:r.city,neighborhood:r.district,street:r.address,service:"ApiCEP"})};0&&(module.exports={apicepProvider,brasilApiProvider,viaCepProvider});
|
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.
|
|
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",
|
|
@@ -12,40 +12,18 @@
|
|
|
12
12
|
"files": [
|
|
13
13
|
"dist"
|
|
14
14
|
],
|
|
15
|
-
"publishConfig": {
|
|
16
|
-
"access": "public"
|
|
17
|
-
},
|
|
18
15
|
"scripts": {
|
|
19
16
|
"clean": "rm -rf dist",
|
|
20
|
-
"build": "npm run clean && tsc --emitDeclarationOnly --outDir dist && esbuild src/index.ts src/providers/index.ts --
|
|
21
|
-
"build:prod": "npm run clean && tsc --emitDeclarationOnly --outDir dist && esbuild src/index.ts src/providers/index.ts --outdir=dist --bundle --platform=node --format=cjs --minify",
|
|
17
|
+
"build": "npm run clean && tsc --emitDeclarationOnly --outDir dist && esbuild src/index.ts src/providers/index.ts --bundle --platform=node --format=cjs --outdir=dist --minify --external:node:*",
|
|
22
18
|
"test": "jest"
|
|
23
19
|
},
|
|
24
|
-
"keywords": [
|
|
25
|
-
"cep",
|
|
26
|
-
"lookup",
|
|
27
|
-
"typescript",
|
|
28
|
-
"address",
|
|
29
|
-
"correios"
|
|
30
|
-
],
|
|
31
|
-
"author": "Silvio Campos",
|
|
32
|
-
"license": "MIT",
|
|
33
|
-
"repository": {
|
|
34
|
-
"type": "git",
|
|
35
|
-
"url": "https://github.com/eusilvio/cep-lookup.git"
|
|
36
|
-
},
|
|
37
|
-
"bugs": {
|
|
38
|
-
"url": "https://github.com/eusilvio/cep-lookup/issues"
|
|
39
|
-
},
|
|
40
|
-
"homepage": "https://github.com/eusilvio/cep-lookup#readme",
|
|
41
20
|
"devDependencies": {
|
|
42
|
-
"@types/jest": "^30.0.0",
|
|
43
21
|
"esbuild": "^0.20.2",
|
|
22
|
+
"typescript": "^5.9.2",
|
|
23
|
+
"@types/jest": "^30.0.0",
|
|
44
24
|
"jest": "^30.1.3",
|
|
45
|
-
"p-limit": "^2.3.0",
|
|
46
25
|
"ts-jest": "^29.4.4",
|
|
47
26
|
"ts-node": "^10.9.2",
|
|
48
|
-
"tsconfig-paths": "^4.2.0"
|
|
49
|
-
"typescript": "^5.9.2"
|
|
27
|
+
"tsconfig-paths": "^4.2.0"
|
|
50
28
|
}
|
|
51
29
|
}
|