@f-o-t/brasil-api 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 +243 -0
- package/dist/index.d.ts +1442 -0
- package/dist/index.js +523 -0
- package/package.json +63 -0
package/README.md
ADDED
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
# @f-o-t/brasil-api
|
|
2
|
+
|
|
3
|
+
Type-safe wrapper for [Brasil API](https://brasilapi.com.br/) with Zod validation.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
bun add @f-o-t/brasil-api
|
|
9
|
+
# or
|
|
10
|
+
npm install @f-o-t/brasil-api
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Features
|
|
14
|
+
|
|
15
|
+
- 🔒 **Full TypeScript type safety** with Zod validation
|
|
16
|
+
- 🌳 **Tree-shakeable** - import only what you need
|
|
17
|
+
- ⚡ **Built for Bun and Node.js 18+**
|
|
18
|
+
- 🎯 **Covers all 14 Brasil API endpoint categories**
|
|
19
|
+
- 🛠️ **Flexible configuration** (global or context-based)
|
|
20
|
+
- 🚨 **Custom error hierarchy** for precise error handling
|
|
21
|
+
|
|
22
|
+
## Quick Start
|
|
23
|
+
|
|
24
|
+
```typescript
|
|
25
|
+
import { getCep, getCnpj, getBanks } from "@f-o-t/brasil-api";
|
|
26
|
+
|
|
27
|
+
// Fetch address by postal code
|
|
28
|
+
const address = await getCep("01310-100");
|
|
29
|
+
console.log(address.city); // "São Paulo"
|
|
30
|
+
|
|
31
|
+
// Get company info
|
|
32
|
+
const company = await getCnpj("00000000000191");
|
|
33
|
+
console.log(company.razao_social); // "BANCO DO BRASIL S.A."
|
|
34
|
+
|
|
35
|
+
// List all banks
|
|
36
|
+
const banks = await getBanks();
|
|
37
|
+
console.log(banks.length); // 200+
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Configuration
|
|
41
|
+
|
|
42
|
+
### Global Configuration
|
|
43
|
+
|
|
44
|
+
```typescript
|
|
45
|
+
import { configureBrasilApi } from "@f-o-t/brasil-api";
|
|
46
|
+
|
|
47
|
+
configureBrasilApi({
|
|
48
|
+
timeout: 5000, // 5 seconds
|
|
49
|
+
baseUrl: "https://brasilapi.com.br/api", // custom URL
|
|
50
|
+
});
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### Context Configuration
|
|
54
|
+
|
|
55
|
+
```typescript
|
|
56
|
+
import { withConfig, getCep, getCnpj } from "@f-o-t/brasil-api";
|
|
57
|
+
|
|
58
|
+
const api = withConfig({ timeout: 15000 }, { getCep, getCnpj });
|
|
59
|
+
|
|
60
|
+
await api.getCep("01310-100"); // uses 15s timeout
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## API Reference
|
|
64
|
+
|
|
65
|
+
### CEP (Postal Codes)
|
|
66
|
+
|
|
67
|
+
```typescript
|
|
68
|
+
import { getCep, getCepV2 } from "@f-o-t/brasil-api";
|
|
69
|
+
|
|
70
|
+
// Basic CEP lookup
|
|
71
|
+
const address = await getCep("01310-100");
|
|
72
|
+
// { cep, state, city, neighborhood, street, service }
|
|
73
|
+
|
|
74
|
+
// CEP with geolocation
|
|
75
|
+
const addressWithCoords = await getCepV2("01310-100");
|
|
76
|
+
// { ..., location: { coordinates: { latitude, longitude } } }
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### Banks
|
|
80
|
+
|
|
81
|
+
```typescript
|
|
82
|
+
import { getBanks, getBank } from "@f-o-t/brasil-api";
|
|
83
|
+
|
|
84
|
+
// All banks
|
|
85
|
+
const banks = await getBanks();
|
|
86
|
+
|
|
87
|
+
// Specific bank by code
|
|
88
|
+
const bb = await getBank(1); // Banco do Brasil
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### CNPJ
|
|
92
|
+
|
|
93
|
+
```typescript
|
|
94
|
+
import { getCnpj } from "@f-o-t/brasil-api";
|
|
95
|
+
|
|
96
|
+
const company = await getCnpj("00000000000191");
|
|
97
|
+
// { cnpj, razao_social, uf, municipio, qsa, ... }
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### DDD (Area Codes)
|
|
101
|
+
|
|
102
|
+
```typescript
|
|
103
|
+
import { getDdd } from "@f-o-t/brasil-api";
|
|
104
|
+
|
|
105
|
+
const result = await getDdd(11); // or "11"
|
|
106
|
+
// { state: "SP", cities: ["São Paulo", "Guarulhos", ...] }
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### Holidays
|
|
110
|
+
|
|
111
|
+
```typescript
|
|
112
|
+
import { getFeriados } from "@f-o-t/brasil-api";
|
|
113
|
+
|
|
114
|
+
const holidays = await getFeriados(2024);
|
|
115
|
+
// [{ date: "2024-01-01", name: "Confraternização Universal", type: "national" }]
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### IBGE
|
|
119
|
+
|
|
120
|
+
```typescript
|
|
121
|
+
import { getEstados, getMunicipios } from "@f-o-t/brasil-api";
|
|
122
|
+
|
|
123
|
+
// All states
|
|
124
|
+
const states = await getEstados();
|
|
125
|
+
|
|
126
|
+
// Municipalities by state
|
|
127
|
+
const cities = await getMunicipios("SP");
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### ISBN
|
|
131
|
+
|
|
132
|
+
```typescript
|
|
133
|
+
import { getIsbn } from "@f-o-t/brasil-api";
|
|
134
|
+
|
|
135
|
+
const book = await getIsbn("9788545702870");
|
|
136
|
+
// { isbn, title, authors, publisher, year, ... }
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
### NCM
|
|
140
|
+
|
|
141
|
+
```typescript
|
|
142
|
+
import { getNcms, getNcm } from "@f-o-t/brasil-api";
|
|
143
|
+
|
|
144
|
+
const codes = await getNcms();
|
|
145
|
+
const specific = await getNcm("01012100");
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
### PIX
|
|
149
|
+
|
|
150
|
+
```typescript
|
|
151
|
+
import { getPixParticipants } from "@f-o-t/brasil-api";
|
|
152
|
+
|
|
153
|
+
const participants = await getPixParticipants();
|
|
154
|
+
// [{ ispb, nome, modalidade_participacao, ... }]
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
### Domain Status
|
|
158
|
+
|
|
159
|
+
```typescript
|
|
160
|
+
import { getDomainStatus } from "@f-o-t/brasil-api";
|
|
161
|
+
|
|
162
|
+
const status = await getDomainStatus("google.com.br");
|
|
163
|
+
// { status, fqdn, hosts, expires_at, ... }
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
### Interest Rates
|
|
167
|
+
|
|
168
|
+
```typescript
|
|
169
|
+
import { getTaxas, getTaxa } from "@f-o-t/brasil-api";
|
|
170
|
+
|
|
171
|
+
const rates = await getTaxas();
|
|
172
|
+
const cdi = await getTaxa("CDI");
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
### Brokers
|
|
176
|
+
|
|
177
|
+
```typescript
|
|
178
|
+
import { getCorretoras, getCorretora } from "@f-o-t/brasil-api";
|
|
179
|
+
|
|
180
|
+
const brokers = await getCorretoras();
|
|
181
|
+
const xp = await getCorretora("02332886000104");
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
### Weather (CPTEC)
|
|
185
|
+
|
|
186
|
+
```typescript
|
|
187
|
+
import { getCidades, getPrevisao, getPrevisaoOndas } from "@f-o-t/brasil-api";
|
|
188
|
+
|
|
189
|
+
const cities = await getCidades();
|
|
190
|
+
const forecast = await getPrevisao(244, 3); // São Paulo, 3 days
|
|
191
|
+
const waves = await getPrevisaoOndas(1234); // Ocean forecast
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
### Currency Exchange
|
|
195
|
+
|
|
196
|
+
```typescript
|
|
197
|
+
import { getMoedas, getCotacao } from "@f-o-t/brasil-api";
|
|
198
|
+
|
|
199
|
+
const currencies = await getMoedas();
|
|
200
|
+
const usd = await getCotacao("USD", "2024-01-15");
|
|
201
|
+
// { simbolo, cotacaoCompra, cotacaoVenda, ... }
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
### FIPE (Vehicle Prices)
|
|
205
|
+
|
|
206
|
+
```typescript
|
|
207
|
+
import { getFipeMarcas, getFipePreco, getFipeTabelas } from "@f-o-t/brasil-api";
|
|
208
|
+
|
|
209
|
+
const brands = await getFipeMarcas("carros");
|
|
210
|
+
const price = await getFipePreco("001004-1");
|
|
211
|
+
const tables = await getFipeTabelas();
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
## Error Handling
|
|
215
|
+
|
|
216
|
+
```typescript
|
|
217
|
+
import {
|
|
218
|
+
BrasilApiError,
|
|
219
|
+
BrasilApiNetworkError,
|
|
220
|
+
BrasilApiValidationError,
|
|
221
|
+
BrasilApiResponseError,
|
|
222
|
+
} from "@f-o-t/brasil-api";
|
|
223
|
+
|
|
224
|
+
try {
|
|
225
|
+
const address = await getCep("invalid");
|
|
226
|
+
} catch (error) {
|
|
227
|
+
if (error instanceof BrasilApiValidationError) {
|
|
228
|
+
console.log("Input validation failed:", error.zodError);
|
|
229
|
+
} else if (error instanceof BrasilApiNetworkError) {
|
|
230
|
+
console.log("Network error:", error.statusCode, error.endpoint);
|
|
231
|
+
} else if (error instanceof BrasilApiResponseError) {
|
|
232
|
+
console.log("Invalid API response:", error.zodError);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
## License
|
|
238
|
+
|
|
239
|
+
MIT
|
|
240
|
+
|
|
241
|
+
## Credits
|
|
242
|
+
|
|
243
|
+
Built on top of [Brasil API](https://brasilapi.com.br/) by [@filipedeschamps](https://github.com/filipedeschamps).
|