@eusilvio/cep-lookup 2.4.0 → 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.
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.openCepProvider = void 0;
4
+ /**
5
+ * @const {Provider} openCepProvider
6
+ * @description Provider for the OpenCEP service.
7
+ * @property {string} name - "OpenCEP".
8
+ * @property {(cep: string) => string} buildUrl - Constructs the URL for OpenCEP API.
9
+ * @property {(response: any) => Address} transform - Transforms OpenCEP's response into a standardized `Address` object.
10
+ * @throws {Error} If OpenCEP response indicates an error.
11
+ */
12
+ exports.openCepProvider = {
13
+ name: "OpenCEP",
14
+ buildUrl: (cep) => `https://opencep.com/v1/${cep}`,
15
+ transform: (response) => {
16
+ if (!response || response.error) {
17
+ throw new Error("CEP not found");
18
+ }
19
+ // OpenCEP returns status code 404 for not found, which fetcher catches.
20
+ // But sometimes APIs return 200 with error data.
21
+ // Assuming standard JSON return based on description.
22
+ return {
23
+ cep: (response.cep || "").replace("-", ""),
24
+ state: response.uf || "",
25
+ city: response.localidade || "",
26
+ neighborhood: response.bairro || "",
27
+ street: response.logradouro || "",
28
+ service: "OpenCEP",
29
+ ibge: response.ibge || undefined,
30
+ };
31
+ },
32
+ };
@@ -23,6 +23,8 @@ exports.viaCepProvider = {
23
23
  neighborhood: response.bairro || "",
24
24
  street: response.logradouro || "",
25
25
  service: "ViaCEP",
26
+ ibge: response.ibge || undefined,
27
+ ddd: response.ddd || undefined,
26
28
  };
27
29
  },
28
30
  };
@@ -10,6 +10,8 @@ export interface Address {
10
10
  neighborhood: string;
11
11
  street: string;
12
12
  service: string;
13
+ ibge?: string;
14
+ ddd?: string;
13
15
  }
14
16
  /**
15
17
  * @interface Provider
@@ -44,14 +46,24 @@ export interface CepLookupOptions {
44
46
  cache?: Cache;
45
47
  rateLimit?: RateLimitOptions;
46
48
  staggerDelay?: number;
49
+ /** Number of retries after all providers fail. Default: 0 */
50
+ retries?: number;
51
+ /** Base delay in ms between retries (exponential backoff). Default: 1000 */
52
+ retryDelay?: number;
53
+ /** Optional logger for debug output */
54
+ logger?: {
55
+ debug: (msg: string, data?: Record<string, unknown>) => void;
56
+ };
57
+ /** Circuit breaker options for provider resilience */
58
+ circuitBreaker?: CircuitBreakerOptions;
47
59
  }
48
60
  /**
49
61
  * @interface BulkCepResult
50
62
  * @description Represents the result for a single CEP in a bulk lookup operation.
51
63
  */
52
- export interface BulkCepResult {
64
+ export interface BulkCepResult<T = Address> {
53
65
  cep: string;
54
- data: Address | null;
66
+ data: T | null;
55
67
  provider?: string;
56
68
  error?: Error;
57
69
  }
@@ -77,3 +89,30 @@ export interface EventMap {
77
89
  'cache:hit': CacheHitPayload;
78
90
  }
79
91
  export type EventListener<T extends EventName> = (payload: EventMap[T]) => void;
92
+ export interface CircuitBreakerOptions {
93
+ /** Consecutive failures required to open the circuit. Default: 3 */
94
+ failureThreshold?: number;
95
+ /** Cooldown in ms before trying a provider again. Default: 30000 */
96
+ cooldownMs?: number;
97
+ /** Enable/disable circuit breaker. Default: true */
98
+ enabled?: boolean;
99
+ }
100
+ export interface ProviderHealth {
101
+ provider: string;
102
+ score: number;
103
+ isOpen: boolean;
104
+ openUntil?: number;
105
+ consecutiveFailures: number;
106
+ successCount: number;
107
+ failureCount: number;
108
+ avgLatencyMs: number;
109
+ }
110
+ export interface ProviderMetrics {
111
+ provider: string;
112
+ requests: number;
113
+ successes: number;
114
+ failures: number;
115
+ timeoutErrors: number;
116
+ notFoundErrors: number;
117
+ avgLatencyMs: number;
118
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eusilvio/cep-lookup",
3
- "version": "2.4.0",
3
+ "version": "2.6.0",
4
4
  "description": "A agnostic, performant and flexible CEP lookup library with race strategy and caching.",
5
5
  "license": "MIT",
6
6
  "author": "Silvio Souza",
@@ -34,7 +34,8 @@
34
34
  "scripts": {
35
35
  "clean": "rm -rf dist tsconfig.tsbuildinfo",
36
36
  "build": "npm run clean && tsc --emitDeclarationOnly --outDir dist && esbuild src/index.ts src/providers/index.ts --bundle --platform=neutral --format=cjs --outdir=dist --out-extension:.js=.cjs --minify && esbuild src/index.ts src/providers/index.ts --bundle --platform=neutral --format=esm --outdir=dist --out-extension:.js=.mjs --minify",
37
- "test": "jest"
37
+ "test": "jest",
38
+ "test:html": "npx http-server . -o examples/index.html -c-1"
38
39
  },
39
40
  "devDependencies": {
40
41
  "@types/jest": "^30.0.0",