bcchapi 1.0.9 → 2.0.1
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/CHANGELOG.md +16 -44
- package/LICENSE +18 -4
- package/README.md +152 -85
- package/dist/client/client.d.ts +60 -0
- package/dist/client/client.d.ts.map +1 -0
- package/dist/client/client.js +126 -0
- package/dist/client/client.js.map +1 -0
- package/dist/client/index.d.ts +4 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +3 -0
- package/dist/client/index.js.map +1 -0
- package/dist/client/types.d.ts +136 -0
- package/dist/client/types.d.ts.map +1 -0
- package/dist/client/types.js +53 -0
- package/dist/client/types.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/dist/series/index.d.ts +2 -0
- package/dist/series/index.d.ts.map +1 -0
- package/dist/series/index.js +2 -0
- package/dist/series/index.js.map +1 -0
- package/dist/series/series.d.ts +213 -0
- package/dist/series/series.d.ts.map +1 -0
- package/dist/series/series.js +213 -0
- package/dist/series/series.js.map +1 -0
- package/dist/utils/dates.d.ts +30 -0
- package/dist/utils/dates.d.ts.map +1 -0
- package/dist/utils/dates.js +67 -0
- package/dist/utils/dates.js.map +1 -0
- package/dist/utils/index.d.ts +4 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +4 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/stats.d.ts +112 -0
- package/dist/utils/stats.d.ts.map +1 -0
- package/dist/utils/stats.js +185 -0
- package/dist/utils/stats.js.map +1 -0
- package/dist/utils/transform.d.ts +79 -0
- package/dist/utils/transform.d.ts.map +1 -0
- package/dist/utils/transform.js +101 -0
- package/dist/utils/transform.js.map +1 -0
- package/package.json +61 -39
- package/.eslintignore +0 -3
- package/.eslintrc.json +0 -21
- package/.github/workflows/npm-publish.yml +0 -38
- package/.prettierrc.json +0 -17
- package/.vscode/extensions.json +0 -3
- package/.vscode/settings.json +0 -4
- package/src/client.ts +0 -108
- package/src/errors.ts +0 -36
- package/src/handlers.ts +0 -69
- package/src/index.ts +0 -8
- package/src/types.ts +0 -115
- package/src/utils.ts +0 -37
- package/test/client.test.ts +0 -226
- package/test/fixtures/index.ts +0 -15
- package/test/fixtures/responses/credentials.invalid.json +0 -11
- package/test/fixtures/responses/getseries.invalid.json +0 -11
- package/test/fixtures/responses/getseries.success.json +0 -87
- package/test/fixtures/responses/searchseries.invalid.json +0 -11
- package/test/fixtures/responses/searchseries.success.json +0 -22
- package/test/handlers/handle-get-series.test.ts +0 -45
- package/test/handlers/handle-search-series.test.ts +0 -48
- package/test/mocks/fetch.mock.ts +0 -76
- package/test/utils/is-valid-date.test.ts +0 -17
- package/test/utils/reverse-date.test.ts +0 -11
- package/tsconfig.eslint.json +0 -5
- package/tsconfig.json +0 -9
- package/vitest.config.mts +0 -9
package/CHANGELOG.md
CHANGED
|
@@ -2,59 +2,31 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
|
|
5
|
-
The format
|
|
6
|
-
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
5
|
+
The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
6
|
|
|
8
|
-
|
|
7
|
+
---
|
|
9
8
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
- Replaced `querystring` with `URLSearchParams` in client
|
|
13
|
-
|
|
14
|
-
## [1.0.8] - 2025-02-19
|
|
15
|
-
|
|
16
|
-
### Fixed
|
|
17
|
-
|
|
18
|
-
- Fixed `extends` missing full path in `tsconfig.json`
|
|
19
|
-
|
|
20
|
-
## [1.0.7] - 2025-02-19
|
|
9
|
+
## [2.0.1] - 2026-03-10
|
|
21
10
|
|
|
22
11
|
### Changed
|
|
23
12
|
|
|
24
|
-
-
|
|
25
|
-
-
|
|
26
|
-
- Fixed date validation in client test
|
|
27
|
-
- Updated dependencies:
|
|
28
|
-
- `@vitest/coverage-v8` from `^1.6.0` to `^3.0.6`
|
|
29
|
-
- `rimraf` from `^5.0.7` to `^6.0.1`
|
|
30
|
-
- `vitest` from `^1.6.0` to `^3.0.6`
|
|
31
|
-
|
|
32
|
-
## [1.0.6] - 2024-06-13
|
|
13
|
+
- Updated README with full API reference and usage examples
|
|
14
|
+
- Cleaned up CHANGELOG to start from v2.0.0
|
|
33
15
|
|
|
34
|
-
|
|
16
|
+
---
|
|
35
17
|
|
|
36
|
-
|
|
37
|
-
- `@types/node` from `20.12.7` to `2.14.2`
|
|
38
|
-
- `@vitest/coverage-v8` from `1.5.1` to `1.6.0`
|
|
39
|
-
- `rimraf` from `5.0.5` to `5.0.7`
|
|
40
|
-
- `prettier` from `3.2.5` to `3.3.2`
|
|
41
|
-
- `vitest` from `1.5.1` to `1.6.0`
|
|
42
|
-
|
|
43
|
-
## [1.0.5] - 2024-04-24
|
|
18
|
+
## [2.0.0] - 2026-03-10
|
|
44
19
|
|
|
45
20
|
### Added
|
|
46
21
|
|
|
47
|
-
-
|
|
22
|
+
- `bcchapi/client` — typed HTTP client (`Client`, `ApiError`, `HttpError`) for the Banco Central de Chile REST API
|
|
23
|
+
- `bcchapi/series` — curated `SERIES` constants covering exchange rates, prices, interest rates, money, national accounts, external sector, employment, public finances, and capital markets
|
|
24
|
+
- `bcchapi/utils` — observation transform functions (`parseValue`, `filterValid`, `toNumbers`, `toMap`, `toArrays`, `parseObservationDate`, `formatQueryDate`) and statistics functions (`mean`, `stdDev`, `min`, `max`, `periodVariation`, `annualVariation`, `rollingMean`)
|
|
25
|
+
- ESM-only output targeting Node.js >= 24
|
|
26
|
+
- Full TypeScript declarations and source maps
|
|
27
|
+
- npm provenance via GitHub Actions (SLSA)
|
|
48
28
|
|
|
49
|
-
|
|
29
|
+
---
|
|
50
30
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
- `@types/node` from `20.11.5` to `2.12.7`
|
|
54
|
-
- `@typescript-eslint/eslint-plugin` from `6.19.0` to `6.21.0`
|
|
55
|
-
- `@typescript-eslint/parser` from `6.19.0` to `6.21.0`
|
|
56
|
-
- `@vitest/coverage-v8` from `1.2.1` to `1.5.1`
|
|
57
|
-
- `eslint` from `8.56.0` to `8.57.0`
|
|
58
|
-
- `prettier` from `3.2.4` to `3.2.5`
|
|
59
|
-
- `typescript` from `5.3.3` to `5.4.5`
|
|
60
|
-
- `vitest` from `1.2.1` to `1.5.1`
|
|
31
|
+
[2.0.1]: https://github.com/airarrazaval/bcchapi/compare/v2.0.0...v2.0.1
|
|
32
|
+
[2.0.0]: https://github.com/airarrazaval/bcchapi/releases/tag/v2.0.0
|
package/LICENSE
CHANGED
|
@@ -1,7 +1,21 @@
|
|
|
1
|
-
|
|
1
|
+
MIT License
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Copyright (c) 2026 airarrazaval
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
6
11
|
|
|
7
|
-
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,124 +1,191 @@
|
|
|
1
|
-
#
|
|
1
|
+
# bcchapi
|
|
2
2
|
|
|
3
|
-
[
|
|
3
|
+
Node.js wrapper for the [Banco Central de Chile REST API](https://si3.bcentral.cl/siete). Features a fully typed HTTP client, curated series ID constants, and utility functions for transforming and analysing macroeconomic observations.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
## Requirements
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
For further information on how to use the API directly, please refer to the [official documentation](https://si3.bcentral.cl/estadisticas/Principal1/Web_Services/doc_en.htm). Usage of the API is subject to the restrictions defined in the [Terms and Conditions](https://si3.bcentral.cl/estadisticas/Principal1/Web_Services/index_BDE_TC.htm) of the Statistical Data Base API of the Central Bank of Chile. As of January of 2024, **the API allows up to 5 requests per second per user**.
|
|
7
|
+
- Node.js >= 24.0.0
|
|
10
8
|
|
|
11
9
|
## Installation
|
|
12
10
|
|
|
13
|
-
```
|
|
14
|
-
npm
|
|
11
|
+
```sh
|
|
12
|
+
npm install bcchapi
|
|
15
13
|
```
|
|
16
14
|
|
|
17
|
-
##
|
|
15
|
+
## Authentication
|
|
18
16
|
|
|
19
|
-
|
|
20
|
-
// ESM
|
|
21
|
-
import { Client } from 'bcchapi';
|
|
17
|
+
Register for free at [si3.bcentral.cl](https://si3.bcentral.cl/siete) to obtain API credentials (email + password).
|
|
22
18
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
### Fetch a time series
|
|
26
22
|
|
|
27
|
-
|
|
23
|
+
```ts
|
|
24
|
+
import { Client } from 'bcchapi/client';
|
|
25
|
+
import { SERIES } from 'bcchapi/series';
|
|
28
26
|
|
|
29
|
-
|
|
27
|
+
const client = new Client({ user: 'me@example.com', pass: 'secret' });
|
|
30
28
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
pass: 'secret',
|
|
29
|
+
const data = await client.getSeries(SERIES.PRICES.UF, {
|
|
30
|
+
firstdate: '2024-01-01',
|
|
31
|
+
lastdate: '2024-12-31',
|
|
35
32
|
});
|
|
33
|
+
|
|
34
|
+
console.log(data.descripIng); // "Unidad de Fomento (UF)"
|
|
35
|
+
console.log(data.observations); // [{ indexDateString, value, statusCode }, ...]
|
|
36
36
|
```
|
|
37
37
|
|
|
38
|
-
###
|
|
38
|
+
### Transform observations
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
import { toNumbers, toArrays, filterValid, mean, rollingMean } from 'bcchapi/utils';
|
|
39
42
|
|
|
40
|
-
|
|
43
|
+
// Parse values to number | null (null for gaps)
|
|
44
|
+
const values = toNumbers(data.observations); // [37000.12, null, 37050.45, ...]
|
|
41
45
|
|
|
42
|
-
|
|
46
|
+
// Get parallel Date[] and (number | null)[] arrays
|
|
47
|
+
const { dates, values } = toArrays(data.observations);
|
|
43
48
|
|
|
44
|
-
|
|
49
|
+
// Filter out gap observations
|
|
50
|
+
const valid = filterValid(data.observations);
|
|
45
51
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
| series | `string` | Yes | The series identifier | `'F072.EUR.USD.N.O.D'` |
|
|
49
|
-
| since | `string` or `Date` | No | The starting date of the series | `'2020-12-01'` |
|
|
50
|
-
| until | `string` or `Date` | No | The ending date of the series | `'2020-12-02'` |
|
|
52
|
+
// Summary statistics (gaps are ignored)
|
|
53
|
+
const avg = mean(data.observations);
|
|
51
54
|
|
|
52
|
-
|
|
55
|
+
// 3-period rolling mean
|
|
56
|
+
const rolling = rollingMean(data.observations, 3);
|
|
57
|
+
```
|
|
53
58
|
|
|
54
|
-
|
|
59
|
+
### Compute variations
|
|
55
60
|
|
|
56
|
-
```
|
|
57
|
-
|
|
58
|
-
series: 'F072.EUR.USD.N.O.D',
|
|
59
|
-
since: '2020-12-01',
|
|
60
|
-
until: '2020-12-02',
|
|
61
|
-
});
|
|
61
|
+
```ts
|
|
62
|
+
import { periodVariation, annualVariation } from 'bcchapi/utils';
|
|
62
63
|
|
|
63
|
-
|
|
64
|
+
// Month-over-month change
|
|
65
|
+
const mom = periodVariation(cpi.observations);
|
|
66
|
+
|
|
67
|
+
// 12-month change for monthly series
|
|
68
|
+
const yoy = annualVariation(cpi.observations, 12);
|
|
64
69
|
```
|
|
65
70
|
|
|
66
|
-
|
|
71
|
+
### Search available series
|
|
72
|
+
|
|
73
|
+
```ts
|
|
74
|
+
const monthlySeries = await client.searchSeries('MONTHLY');
|
|
75
|
+
console.log(monthlySeries.map(s => s.englishTitle));
|
|
76
|
+
```
|
|
67
77
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
78
|
+
### Error handling
|
|
79
|
+
|
|
80
|
+
```ts
|
|
81
|
+
import { ApiError, HttpError } from 'bcchapi/client';
|
|
82
|
+
|
|
83
|
+
try {
|
|
84
|
+
await client.getSeries('INVALID_ID');
|
|
85
|
+
} catch (err) {
|
|
86
|
+
if (err instanceof ApiError) {
|
|
87
|
+
// Non-zero Codigo in the API response body
|
|
88
|
+
console.error(`API error ${err.codigo}: ${err.descripcion}`);
|
|
89
|
+
} else if (err instanceof HttpError) {
|
|
90
|
+
// Non-ok HTTP status
|
|
91
|
+
console.error(`HTTP ${err.status}`);
|
|
92
|
+
} else {
|
|
93
|
+
// Network failure — original error available as err.cause
|
|
94
|
+
throw err;
|
|
95
|
+
}
|
|
76
96
|
}
|
|
77
97
|
```
|
|
78
98
|
|
|
79
|
-
|
|
99
|
+
## API
|
|
80
100
|
|
|
81
|
-
|
|
101
|
+
### `bcchapi/client`
|
|
82
102
|
|
|
83
|
-
####
|
|
103
|
+
#### `new Client(options)`
|
|
84
104
|
|
|
85
|
-
|
|
|
86
|
-
|
|
|
87
|
-
|
|
|
105
|
+
| Option | Type | Description |
|
|
106
|
+
| ------ | ---- | ----------- |
|
|
107
|
+
| `user` | `string` | BCCH account email |
|
|
108
|
+
| `pass` | `string` | BCCH account password |
|
|
109
|
+
| `fetch` | `typeof fetch` | Custom fetch implementation (optional, useful for testing) |
|
|
88
110
|
|
|
89
|
-
####
|
|
111
|
+
#### `client.getSeries(seriesId, options?)`
|
|
90
112
|
|
|
91
|
-
|
|
92
|
-
import { Frequency } from 'bcchapi';
|
|
113
|
+
Fetches observations for a single time series.
|
|
93
114
|
|
|
94
|
-
|
|
115
|
+
| Parameter | Type | Description |
|
|
116
|
+
| --------- | ---- | ----------- |
|
|
117
|
+
| `seriesId` | `string` | BCCH series identifier |
|
|
118
|
+
| `options.firstdate` | `string` | Start date `"YYYY-MM-DD"` (defaults to earliest available) |
|
|
119
|
+
| `options.lastdate` | `string` | End date `"YYYY-MM-DD"` (defaults to most recent) |
|
|
95
120
|
|
|
96
|
-
|
|
97
|
-
|
|
121
|
+
Returns `Promise<SeriesData>`.
|
|
122
|
+
|
|
123
|
+
#### `client.searchSeries(frequency)`
|
|
124
|
+
|
|
125
|
+
Returns metadata for all series with the given frequency. `frequency` is one of `'DAILY' | 'MONTHLY' | 'QUARTERLY' | 'ANNUAL'`.
|
|
98
126
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
127
|
+
Returns `Promise<SeriesInfo[]>`.
|
|
128
|
+
|
|
129
|
+
### `bcchapi/series`
|
|
130
|
+
|
|
131
|
+
`SERIES` is a nested `as const` object of curated series IDs grouped by domain:
|
|
132
|
+
|
|
133
|
+
| Group | Description |
|
|
134
|
+
| ----- | ----------- |
|
|
135
|
+
| `SERIES.EXCHANGE_RATE` | USD/CLP and major currency pairs |
|
|
136
|
+
| `SERIES.PRICES` | UF, UTM, IVP, CPI and components |
|
|
137
|
+
| `SERIES.INTEREST_RATES` | MPR, BCP/BCU sovereign bonds, PDBC rates |
|
|
138
|
+
| `SERIES.MONEY` | M1, M2, M3, bank loans by type |
|
|
139
|
+
| `SERIES.NATIONAL_ACCOUNTS` | Imacec and components |
|
|
140
|
+
| `SERIES.EXTERNAL_SECTOR` | Current account, exports, imports, FDI |
|
|
141
|
+
| `SERIES.EMPLOYMENT` | Unemployment rate, labour force, employed/unemployed |
|
|
142
|
+
| `SERIES.PUBLIC_FINANCES` | Government revenue, expenditure, fiscal balance |
|
|
143
|
+
| `SERIES.CAPITAL_MARKET` | IPSA, stock market capitalisation |
|
|
144
|
+
|
|
145
|
+
Use [si3.bcentral.cl](https://si3.bcentral.cl/siete) or `client.searchSeries()` to discover additional series IDs beyond the curated set.
|
|
146
|
+
|
|
147
|
+
### `bcchapi/utils`
|
|
148
|
+
|
|
149
|
+
#### Transform functions
|
|
150
|
+
|
|
151
|
+
| Function | Description |
|
|
152
|
+
| -------- | ----------- |
|
|
153
|
+
| `parseValue(value)` | Parses a value string to `number \| null` (`null` for empty or non-numeric) |
|
|
154
|
+
| `filterValid(observations)` | Returns only observations with parseable numeric values |
|
|
155
|
+
| `toNumbers(observations)` | Maps observations to `Array<number \| null>` |
|
|
156
|
+
| `toMap(observations)` | Returns a `Map<string, number \| null>` keyed by `indexDateString` |
|
|
157
|
+
| `toArrays(observations)` | Returns `{ dates: Date[], values: Array<number \| null> }` |
|
|
158
|
+
| `parseObservationDate(dateString)` | Parses `"DD-MM-YYYY"` to a UTC `Date` |
|
|
159
|
+
| `formatQueryDate(date)` | Formats a `Date` to `"YYYY-MM-DD"` for use in `getSeries` options |
|
|
160
|
+
|
|
161
|
+
#### Statistics functions
|
|
162
|
+
|
|
163
|
+
All stats functions operate on `Observation[]` and ignore gap values (empty or non-numeric).
|
|
164
|
+
|
|
165
|
+
| Function | Description |
|
|
166
|
+
| -------- | ----------- |
|
|
167
|
+
| `mean(observations)` | Arithmetic mean |
|
|
168
|
+
| `stdDev(observations)` | Sample standard deviation (Bessel's correction) |
|
|
169
|
+
| `min(observations)` | Minimum value |
|
|
170
|
+
| `max(observations)` | Maximum value |
|
|
171
|
+
| `periodVariation(observations)` | Period-over-period fractional change (`value[i] / value[i-1] - 1`) |
|
|
172
|
+
| `annualVariation(observations, periodsPerYear)` | Year-over-year fractional change (`value[i] / value[i - n] - 1`) |
|
|
173
|
+
| `rollingMean(observations, window)` | Rolling mean over a fixed window; `null` if any value in window is a gap |
|
|
174
|
+
|
|
175
|
+
## Releases
|
|
176
|
+
|
|
177
|
+
```sh
|
|
178
|
+
# 1. Regenerate CHANGELOG.md (auto-determines next version)
|
|
179
|
+
npm run changelog
|
|
180
|
+
|
|
181
|
+
# 2. Commit and bump version
|
|
182
|
+
git add CHANGELOG.md && git commit -m "chore: release vX.Y.Z"
|
|
183
|
+
npm version patch # or minor / major
|
|
184
|
+
|
|
185
|
+
# 3. Push — the publish workflow triggers automatically on the version tag
|
|
186
|
+
git push --follow-tags
|
|
124
187
|
```
|
|
188
|
+
|
|
189
|
+
## License
|
|
190
|
+
|
|
191
|
+
MIT
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { ClientOptions, Frequency, GetSeriesOptions, SeriesData, SeriesInfo } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* HTTP client for the Banco Central de Chile REST API.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```ts
|
|
7
|
+
* import { Client } from 'bcch/client';
|
|
8
|
+
*
|
|
9
|
+
* const client = new Client({ user: 'me@example.com', pass: 'secret' });
|
|
10
|
+
* const data = await client.getSeries('F022.TPM.TIN.D001.NO.Z.D', {
|
|
11
|
+
* firstdate: '2024-01-01',
|
|
12
|
+
* });
|
|
13
|
+
* console.log(data.observations);
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export declare class Client {
|
|
17
|
+
private readonly user;
|
|
18
|
+
private readonly pass;
|
|
19
|
+
private readonly fetch;
|
|
20
|
+
constructor(options: ClientOptions);
|
|
21
|
+
/**
|
|
22
|
+
* Fetches observations for a single time series.
|
|
23
|
+
*
|
|
24
|
+
* @param seriesId - BCCH series identifier (e.g. `'F022.TPM.TIN.D001.NO.Z.D'`).
|
|
25
|
+
* Values from the `SERIES` constants in `bcch/series` are accepted directly.
|
|
26
|
+
* @param options - Optional date range filter.
|
|
27
|
+
* @returns Parsed series data including all observations in the requested range.
|
|
28
|
+
* @throws {ApiError} When the API returns a non-zero `Codigo`.
|
|
29
|
+
* @throws {HttpError} When the API returns a non-ok HTTP status.
|
|
30
|
+
* @throws {Error} When a network failure occurs; original error is set as `cause`.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```ts
|
|
34
|
+
* const data = await client.getSeries('F073.UFF.PRE.Z.D', {
|
|
35
|
+
* firstdate: '2024-01-01',
|
|
36
|
+
* lastdate: '2024-12-31',
|
|
37
|
+
* });
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
getSeries(seriesId: string, options?: GetSeriesOptions): Promise<SeriesData>;
|
|
41
|
+
/**
|
|
42
|
+
* Searches for available series filtered by observation frequency.
|
|
43
|
+
*
|
|
44
|
+
* @param frequency - One of `'DAILY'`, `'MONTHLY'`, `'QUARTERLY'`, or `'ANNUAL'`.
|
|
45
|
+
* @returns Array of series metadata matching the given frequency.
|
|
46
|
+
* @throws {ApiError} When the API returns a non-zero `Codigo`.
|
|
47
|
+
* @throws {HttpError} When the API returns a non-ok HTTP status.
|
|
48
|
+
* @throws {Error} When a network failure occurs; original error is set as `cause`.
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```ts
|
|
52
|
+
* const monthlySeries = await client.searchSeries('MONTHLY');
|
|
53
|
+
* console.log(monthlySeries.map(s => s.englishTitle));
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
searchSeries(frequency: Frequency): Promise<SeriesInfo[]>;
|
|
57
|
+
private baseParams;
|
|
58
|
+
private request;
|
|
59
|
+
}
|
|
60
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/client/client.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,aAAa,EACb,SAAS,EACT,gBAAgB,EAEhB,UAAU,EACV,UAAU,EACX,MAAM,YAAY,CAAC;AAyCpB;;;;;;;;;;;;;GAaG;AACH,qBAAa,MAAM;IACjB,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAS;IAC9B,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAS;IAC9B,OAAO,CAAC,QAAQ,CAAC,KAAK,CAA0B;gBAEpC,OAAO,EAAE,aAAa;IAMlC;;;;;;;;;;;;;;;;;;OAkBG;IACG,SAAS,CAAC,QAAQ,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,gBAAgB,GAAG,OAAO,CAAC,UAAU,CAAC;IA+BlF;;;;;;;;;;;;;;OAcG;IACG,YAAY,CAAC,SAAS,EAAE,SAAS,GAAG,OAAO,CAAC,UAAU,EAAE,CAAC;IAuB/D,OAAO,CAAC,UAAU;YAOJ,OAAO;CAgBtB"}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { ApiError, HttpError } from './types.js';
|
|
2
|
+
const API_URL = 'https://si3.bcentral.cl/SieteRestWS/SieteRestWS.ashx';
|
|
3
|
+
/**
|
|
4
|
+
* HTTP client for the Banco Central de Chile REST API.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* import { Client } from 'bcch/client';
|
|
9
|
+
*
|
|
10
|
+
* const client = new Client({ user: 'me@example.com', pass: 'secret' });
|
|
11
|
+
* const data = await client.getSeries('F022.TPM.TIN.D001.NO.Z.D', {
|
|
12
|
+
* firstdate: '2024-01-01',
|
|
13
|
+
* });
|
|
14
|
+
* console.log(data.observations);
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
export class Client {
|
|
18
|
+
user;
|
|
19
|
+
pass;
|
|
20
|
+
fetch;
|
|
21
|
+
constructor(options) {
|
|
22
|
+
this.user = options.user;
|
|
23
|
+
this.pass = options.pass;
|
|
24
|
+
this.fetch = options.fetch ?? globalThis.fetch;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Fetches observations for a single time series.
|
|
28
|
+
*
|
|
29
|
+
* @param seriesId - BCCH series identifier (e.g. `'F022.TPM.TIN.D001.NO.Z.D'`).
|
|
30
|
+
* Values from the `SERIES` constants in `bcch/series` are accepted directly.
|
|
31
|
+
* @param options - Optional date range filter.
|
|
32
|
+
* @returns Parsed series data including all observations in the requested range.
|
|
33
|
+
* @throws {ApiError} When the API returns a non-zero `Codigo`.
|
|
34
|
+
* @throws {HttpError} When the API returns a non-ok HTTP status.
|
|
35
|
+
* @throws {Error} When a network failure occurs; original error is set as `cause`.
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```ts
|
|
39
|
+
* const data = await client.getSeries('F073.UFF.PRE.Z.D', {
|
|
40
|
+
* firstdate: '2024-01-01',
|
|
41
|
+
* lastdate: '2024-12-31',
|
|
42
|
+
* });
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
async getSeries(seriesId, options) {
|
|
46
|
+
const params = this.baseParams();
|
|
47
|
+
params.set('function', 'GetSeries');
|
|
48
|
+
params.set('timeseries', seriesId);
|
|
49
|
+
if (options?.firstdate !== undefined) {
|
|
50
|
+
params.set('firstdate', options.firstdate);
|
|
51
|
+
}
|
|
52
|
+
if (options?.lastdate !== undefined) {
|
|
53
|
+
params.set('lastdate', options.lastdate);
|
|
54
|
+
}
|
|
55
|
+
const raw = await this.request(params);
|
|
56
|
+
if (raw.Codigo !== 0 || raw.Series === null) {
|
|
57
|
+
throw new ApiError(`BCCH API error: ${raw.Descripcion}`, raw.Codigo, raw.Descripcion);
|
|
58
|
+
}
|
|
59
|
+
const observations = raw.Series.Obs.map((obs) => ({
|
|
60
|
+
indexDateString: obs.indexDateString,
|
|
61
|
+
value: obs.value,
|
|
62
|
+
statusCode: obs.statusCode,
|
|
63
|
+
}));
|
|
64
|
+
return {
|
|
65
|
+
seriesId: raw.Series.seriesId,
|
|
66
|
+
descripEsp: raw.Series.descripEsp,
|
|
67
|
+
descripIng: raw.Series.descripIng,
|
|
68
|
+
observations,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Searches for available series filtered by observation frequency.
|
|
73
|
+
*
|
|
74
|
+
* @param frequency - One of `'DAILY'`, `'MONTHLY'`, `'QUARTERLY'`, or `'ANNUAL'`.
|
|
75
|
+
* @returns Array of series metadata matching the given frequency.
|
|
76
|
+
* @throws {ApiError} When the API returns a non-zero `Codigo`.
|
|
77
|
+
* @throws {HttpError} When the API returns a non-ok HTTP status.
|
|
78
|
+
* @throws {Error} When a network failure occurs; original error is set as `cause`.
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* ```ts
|
|
82
|
+
* const monthlySeries = await client.searchSeries('MONTHLY');
|
|
83
|
+
* console.log(monthlySeries.map(s => s.englishTitle));
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
86
|
+
async searchSeries(frequency) {
|
|
87
|
+
const params = this.baseParams();
|
|
88
|
+
params.set('function', 'SearchSeries');
|
|
89
|
+
params.set('frequency', frequency);
|
|
90
|
+
const raw = await this.request(params);
|
|
91
|
+
if (raw.Codigo !== 0) {
|
|
92
|
+
throw new ApiError(`BCCH API error: ${raw.Descripcion}`, raw.Codigo, raw.Descripcion);
|
|
93
|
+
}
|
|
94
|
+
return (raw.SeriesInfos ?? []).map((info) => ({
|
|
95
|
+
seriesId: info.seriesId,
|
|
96
|
+
frequencyCode: info.frequencyCode,
|
|
97
|
+
spanishTitle: info.spanishTitle,
|
|
98
|
+
englishTitle: info.englishTitle,
|
|
99
|
+
firstObservation: info.firstObservation,
|
|
100
|
+
lastObservation: info.lastObservation,
|
|
101
|
+
updatedAt: info.updatedAt,
|
|
102
|
+
createdAt: info.createdAt,
|
|
103
|
+
}));
|
|
104
|
+
}
|
|
105
|
+
baseParams() {
|
|
106
|
+
const params = new URLSearchParams();
|
|
107
|
+
params.set('user', this.user);
|
|
108
|
+
params.set('pass', this.pass);
|
|
109
|
+
return params;
|
|
110
|
+
}
|
|
111
|
+
async request(params) {
|
|
112
|
+
const url = `${API_URL}?${params.toString()}`;
|
|
113
|
+
let response;
|
|
114
|
+
try {
|
|
115
|
+
response = await this.fetch(url);
|
|
116
|
+
}
|
|
117
|
+
catch (err) {
|
|
118
|
+
throw new Error('BCCH request failed', { cause: err });
|
|
119
|
+
}
|
|
120
|
+
if (!response.ok) {
|
|
121
|
+
throw new HttpError(`HTTP error ${response.status}`, response.status);
|
|
122
|
+
}
|
|
123
|
+
return response.json();
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../../src/client/client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAUjD,MAAM,OAAO,GAAG,sDAAsD,CAAC;AAuCvE;;;;;;;;;;;;;GAaG;AACH,MAAM,OAAO,MAAM;IACA,IAAI,CAAS;IACb,IAAI,CAAS;IACb,KAAK,CAA0B;IAEhD,YAAY,OAAsB;QAChC,IAAI,CAAC,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;QACzB,IAAI,CAAC,I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AAI,CAAC,CAAC;QAC9B,MAAM,CAAC,GAAG,CAAC,MAAM,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC;QAC9B,OAAO,MAAM,CAAC;IAChB,CAAC;IAEO,KAAK,CAAC,OAAO,CAAI,MAAuB;QAC9C,MAAM,GAAG,GAAG,GAAG,OAAO,IAAI,MAAM,CAAC,QAAQ,EAAE,EAAE,CAAC;QAC9C,IAAI,QAAkB,CAAC;QAEvB,IAAI,CAAC;YACH,QAAQ,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QACnC,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,MAAM,IAAI,KAAK,CAAC,qBAAqB,EAAE,EAAE,KAAK,EAAE,GAAG,EAAE,CAAC,CAAC;QACzD,CAAC;QAED,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC;YACjB,MAAM,IAAI,SAAS,CAAC,cAAc,QAAQ,CAAC,MAAM,EAAE,EAAE,QAAQ,CAAC,MAAM,CAAC,CAAC;QACxE,CAAC;QAED,OAAO,QAAQ,CAAC,IAAI,EAAgB,CAAC;IACvC,CAAC;CACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/client/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AACjD,YAAY,EACV,aAAa,EACb,SAAS,EACT,gBAAgB,EAChB,WAAW,EACX,UAAU,EACV,UAAU,GACX,MAAM,YAAY,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/client/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC"}
|