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/test/client.test.ts
DELETED
|
@@ -1,226 +0,0 @@
|
|
|
1
|
-
import { expect, describe, it, vi, afterEach, beforeEach } from 'vitest';
|
|
2
|
-
import {
|
|
3
|
-
Client,
|
|
4
|
-
Frequency,
|
|
5
|
-
InvalidCredentialsError,
|
|
6
|
-
InvalidSeriesError,
|
|
7
|
-
InvalidFrequencyError,
|
|
8
|
-
} from '../src';
|
|
9
|
-
import { reverseDate } from '../src/utils';
|
|
10
|
-
import fetchMock from './mocks/fetch.mock';
|
|
11
|
-
import fixtures from './fixtures';
|
|
12
|
-
|
|
13
|
-
const fetchSpy = vi.spyOn(global, 'fetch').mockImplementation(fetchMock);
|
|
14
|
-
|
|
15
|
-
describe('Client', () => {
|
|
16
|
-
const client = new Client({
|
|
17
|
-
user: 'test',
|
|
18
|
-
pass: 'test',
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
const invalidClient = new Client({
|
|
22
|
-
user: '',
|
|
23
|
-
pass: '',
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
it('should create a Client instance', () => {
|
|
27
|
-
expect(client).toBeInstanceOf(Client);
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
describe('getSeries', () => {
|
|
31
|
-
beforeEach(() => {
|
|
32
|
-
fetchSpy.mockImplementation(fetchMock);
|
|
33
|
-
});
|
|
34
|
-
afterEach(() => {
|
|
35
|
-
fetchSpy.mockReset();
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
it('should throw an error if series is not a non-empty string', async () => {
|
|
39
|
-
await expect(
|
|
40
|
-
client.getSeries({
|
|
41
|
-
series: undefined as unknown as string,
|
|
42
|
-
}),
|
|
43
|
-
).rejects.toThrow('series must be a non-empty string');
|
|
44
|
-
await expect(
|
|
45
|
-
client.getSeries({
|
|
46
|
-
series: '',
|
|
47
|
-
}),
|
|
48
|
-
).rejects.toThrow('series must be a non-empty string');
|
|
49
|
-
|
|
50
|
-
expect(fetchSpy).not.toHaveBeenCalled();
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
it('should throw an error if date range is invalid', async () => {
|
|
54
|
-
await expect(
|
|
55
|
-
client.getSeries({
|
|
56
|
-
series: 'TEST',
|
|
57
|
-
since: 'invalid',
|
|
58
|
-
}),
|
|
59
|
-
).rejects.toThrow('"since" is not a valid date string or Date object');
|
|
60
|
-
await expect(
|
|
61
|
-
client.getSeries({
|
|
62
|
-
series: 'TEST',
|
|
63
|
-
since: new Date('invalid'),
|
|
64
|
-
}),
|
|
65
|
-
).rejects.toThrow('"since" is not a valid date string or Date object');
|
|
66
|
-
|
|
67
|
-
await expect(
|
|
68
|
-
client.getSeries({
|
|
69
|
-
series: 'TEST',
|
|
70
|
-
until: 'invalid',
|
|
71
|
-
}),
|
|
72
|
-
).rejects.toThrow('"until" is not a valid date string or Date object');
|
|
73
|
-
await expect(
|
|
74
|
-
client.getSeries({
|
|
75
|
-
series: 'TEST',
|
|
76
|
-
until: new Date('invalid'),
|
|
77
|
-
}),
|
|
78
|
-
).rejects.toThrow('"until" is not a valid date string or Date object');
|
|
79
|
-
|
|
80
|
-
await expect(
|
|
81
|
-
client.getSeries({
|
|
82
|
-
series: 'TEST',
|
|
83
|
-
since: new Date(2020, 0, 1),
|
|
84
|
-
until: new Date(2000, 0, 1),
|
|
85
|
-
}),
|
|
86
|
-
).rejects.toThrow('invalid date range');
|
|
87
|
-
|
|
88
|
-
expect(fetchSpy).not.toHaveBeenCalled();
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
it('should throw error if credentials are invalid', async () => {
|
|
92
|
-
await expect(invalidClient.getSeries({ series: 'TEST' })).rejects.toThrow(
|
|
93
|
-
InvalidCredentialsError,
|
|
94
|
-
);
|
|
95
|
-
expect(fetchSpy).toHaveBeenCalledTimes(1);
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
it('should throw error if series does not exist', async () => {
|
|
99
|
-
await expect(client.getSeries({ series: 'invalid' })).rejects.toThrow(InvalidSeriesError);
|
|
100
|
-
expect(fetchSpy).toHaveBeenCalledTimes(1);
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
it('should return the series history information', async () => {
|
|
104
|
-
const series = await client.getSeries({ series: 'TEST' });
|
|
105
|
-
|
|
106
|
-
expect(fetchSpy).toHaveBeenCalledTimes(1);
|
|
107
|
-
|
|
108
|
-
expect(series).toBeDefined();
|
|
109
|
-
expect(series.seriesId).toBe('TEST');
|
|
110
|
-
expect(series.description).toBe('Test');
|
|
111
|
-
expect(series.data).toBeInstanceOf(Array);
|
|
112
|
-
expect(series.data).toHaveLength(15);
|
|
113
|
-
|
|
114
|
-
for (let i = 0; i < series.data.length; i += 1) {
|
|
115
|
-
expect(series.data[i].date).toBe(
|
|
116
|
-
reverseDate(fixtures.response.getSeriesSuccess.Series.Obs[i].indexDateString),
|
|
117
|
-
);
|
|
118
|
-
expect(series.data[i].value.toFixed(2)).toBe(
|
|
119
|
-
fixtures.response.getSeriesSuccess.Series.Obs[i].value,
|
|
120
|
-
);
|
|
121
|
-
|
|
122
|
-
if (Number.isNaN(series.data[i].value)) {
|
|
123
|
-
expect(fixtures.response.getSeriesSuccess.Series.Obs[i].statusCode).toBe('ND');
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
it('should filter the series history by date range', async () => {
|
|
129
|
-
const series = await client.getSeries({
|
|
130
|
-
series: 'TEST',
|
|
131
|
-
until: '2020-12-05',
|
|
132
|
-
});
|
|
133
|
-
|
|
134
|
-
expect(fetchSpy).toHaveBeenCalledTimes(1);
|
|
135
|
-
|
|
136
|
-
expect(series).toBeDefined();
|
|
137
|
-
expect(series.seriesId).toBe('TEST');
|
|
138
|
-
expect(series.description).toBe('Test');
|
|
139
|
-
expect(series.data).toBeInstanceOf(Array);
|
|
140
|
-
expect(series.data).toHaveLength(5);
|
|
141
|
-
|
|
142
|
-
for (let i = 0; i < series.data.length; i += 1) {
|
|
143
|
-
expect(series.data[i].date).toBe(
|
|
144
|
-
reverseDate(fixtures.response.getSeriesSuccess.Series.Obs[i].indexDateString),
|
|
145
|
-
);
|
|
146
|
-
expect(series.data[i].value.toFixed(2)).toBe(
|
|
147
|
-
fixtures.response.getSeriesSuccess.Series.Obs[i].value,
|
|
148
|
-
);
|
|
149
|
-
|
|
150
|
-
if (Number.isNaN(series.data[i].value)) {
|
|
151
|
-
expect(fixtures.response.getSeriesSuccess.Series.Obs[i].statusCode).toBe('ND');
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
});
|
|
155
|
-
|
|
156
|
-
it('should return empty results if series has no data', async () => {
|
|
157
|
-
const series = await client.getSeries({
|
|
158
|
-
series: 'TEST',
|
|
159
|
-
since: '2020-12-16',
|
|
160
|
-
});
|
|
161
|
-
|
|
162
|
-
expect(fetchSpy).toHaveBeenCalledTimes(1);
|
|
163
|
-
|
|
164
|
-
expect(series).toBeDefined();
|
|
165
|
-
expect(series.seriesId).toBe('TEST');
|
|
166
|
-
expect(series.description).toBe('Test');
|
|
167
|
-
expect(series.data).toBeInstanceOf(Array);
|
|
168
|
-
expect(series.data).toHaveLength(0);
|
|
169
|
-
});
|
|
170
|
-
});
|
|
171
|
-
|
|
172
|
-
describe('searchSeries', () => {
|
|
173
|
-
beforeEach(() => {
|
|
174
|
-
fetchSpy.mockImplementation(fetchMock);
|
|
175
|
-
});
|
|
176
|
-
afterEach(() => {
|
|
177
|
-
fetchSpy.mockReset();
|
|
178
|
-
});
|
|
179
|
-
|
|
180
|
-
it('should throw an error if frequency is not a non-empty string', async () => {
|
|
181
|
-
await expect(
|
|
182
|
-
client.searchSeries({
|
|
183
|
-
frequency: undefined as unknown as Frequency,
|
|
184
|
-
}),
|
|
185
|
-
).rejects.toThrow('frequency must be a non-empty string');
|
|
186
|
-
await expect(
|
|
187
|
-
client.searchSeries({
|
|
188
|
-
frequency: '' as Frequency,
|
|
189
|
-
}),
|
|
190
|
-
).rejects.toThrow('frequency must be a non-empty string');
|
|
191
|
-
|
|
192
|
-
expect(fetchSpy).not.toHaveBeenCalled();
|
|
193
|
-
});
|
|
194
|
-
|
|
195
|
-
it('should throw error if credentials are invalid', async () => {
|
|
196
|
-
await expect(
|
|
197
|
-
invalidClient.searchSeries({
|
|
198
|
-
frequency: Frequency.Daily,
|
|
199
|
-
}),
|
|
200
|
-
).rejects.toThrow(InvalidCredentialsError);
|
|
201
|
-
|
|
202
|
-
expect(fetchSpy).toHaveBeenCalledTimes(1);
|
|
203
|
-
});
|
|
204
|
-
|
|
205
|
-
it('should throw error if frequency is invalid', async () => {
|
|
206
|
-
await expect(
|
|
207
|
-
client.searchSeries({
|
|
208
|
-
frequency: 'invalid' as Frequency,
|
|
209
|
-
}),
|
|
210
|
-
).rejects.toThrow(InvalidFrequencyError);
|
|
211
|
-
|
|
212
|
-
expect(fetchSpy).toHaveBeenCalledTimes(1);
|
|
213
|
-
});
|
|
214
|
-
|
|
215
|
-
it('should return the series metadata information', async () => {
|
|
216
|
-
const series = await client.searchSeries({
|
|
217
|
-
frequency: Frequency.Daily,
|
|
218
|
-
});
|
|
219
|
-
|
|
220
|
-
expect(fetchSpy).toHaveBeenCalledTimes(1);
|
|
221
|
-
|
|
222
|
-
expect(series).toBeInstanceOf(Array);
|
|
223
|
-
expect(series).toHaveLength(1);
|
|
224
|
-
});
|
|
225
|
-
});
|
|
226
|
-
});
|
package/test/fixtures/index.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import getSeriesSuccess from './responses/getseries.success.json';
|
|
2
|
-
import getSeriesInvalid from './responses/getseries.invalid.json';
|
|
3
|
-
import searchSeriesSuccess from './responses/searchseries.success.json';
|
|
4
|
-
import searchSeriesInvalid from './responses/searchseries.invalid.json';
|
|
5
|
-
import credentialsInvalid from './responses/credentials.invalid.json';
|
|
6
|
-
|
|
7
|
-
export default {
|
|
8
|
-
response: {
|
|
9
|
-
credentialsInvalid,
|
|
10
|
-
getSeriesInvalid,
|
|
11
|
-
getSeriesSuccess,
|
|
12
|
-
searchSeriesInvalid,
|
|
13
|
-
searchSeriesSuccess,
|
|
14
|
-
},
|
|
15
|
-
};
|
|
@@ -1,87 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"Codigo": 0,
|
|
3
|
-
"Descripcion": "Success",
|
|
4
|
-
"Series": {
|
|
5
|
-
"seriesId": "TEST",
|
|
6
|
-
"descripEsp": "Test",
|
|
7
|
-
"descripIng": "Test",
|
|
8
|
-
"Obs": [
|
|
9
|
-
{
|
|
10
|
-
"indexDateString": "01-12-2020",
|
|
11
|
-
"value": "12.01",
|
|
12
|
-
"statusCode": "OK"
|
|
13
|
-
},
|
|
14
|
-
{
|
|
15
|
-
"indexDateString": "02-12-2020",
|
|
16
|
-
"value": "12.02",
|
|
17
|
-
"statusCode": "OK"
|
|
18
|
-
},
|
|
19
|
-
{
|
|
20
|
-
"indexDateString": "03-12-2020",
|
|
21
|
-
"value": "12.03",
|
|
22
|
-
"statusCode": "OK"
|
|
23
|
-
},
|
|
24
|
-
{
|
|
25
|
-
"indexDateString": "04-12-2020",
|
|
26
|
-
"value": "12.04",
|
|
27
|
-
"statusCode": "OK"
|
|
28
|
-
},
|
|
29
|
-
{
|
|
30
|
-
"indexDateString": "05-12-2020",
|
|
31
|
-
"value": "NaN",
|
|
32
|
-
"statusCode": "ND"
|
|
33
|
-
},
|
|
34
|
-
{
|
|
35
|
-
"indexDateString": "06-12-2020",
|
|
36
|
-
"value": "NaN",
|
|
37
|
-
"statusCode": "ND"
|
|
38
|
-
},
|
|
39
|
-
{
|
|
40
|
-
"indexDateString": "07-12-2020",
|
|
41
|
-
"value": "12.07",
|
|
42
|
-
"statusCode": "OK"
|
|
43
|
-
},
|
|
44
|
-
{
|
|
45
|
-
"indexDateString": "08-12-2020",
|
|
46
|
-
"value": "NaN",
|
|
47
|
-
"statusCode": "ND"
|
|
48
|
-
},
|
|
49
|
-
{
|
|
50
|
-
"indexDateString": "09-12-2020",
|
|
51
|
-
"value": "12.09",
|
|
52
|
-
"statusCode": "OK"
|
|
53
|
-
},
|
|
54
|
-
{
|
|
55
|
-
"indexDateString": "10-12-2020",
|
|
56
|
-
"value": "12.10",
|
|
57
|
-
"statusCode": "OK"
|
|
58
|
-
},
|
|
59
|
-
{
|
|
60
|
-
"indexDateString": "11-12-2020",
|
|
61
|
-
"value": "12.11",
|
|
62
|
-
"statusCode": "OK"
|
|
63
|
-
},
|
|
64
|
-
{
|
|
65
|
-
"indexDateString": "12-12-2020",
|
|
66
|
-
"value": "NaN",
|
|
67
|
-
"statusCode": "ND"
|
|
68
|
-
},
|
|
69
|
-
{
|
|
70
|
-
"indexDateString": "13-12-2020",
|
|
71
|
-
"value": "NaN",
|
|
72
|
-
"statusCode": "ND"
|
|
73
|
-
},
|
|
74
|
-
{
|
|
75
|
-
"indexDateString": "14-12-2020",
|
|
76
|
-
"value": "12.14",
|
|
77
|
-
"statusCode": "OK"
|
|
78
|
-
},
|
|
79
|
-
{
|
|
80
|
-
"indexDateString": "15-12-2020",
|
|
81
|
-
"value": "12.15",
|
|
82
|
-
"statusCode": "OK"
|
|
83
|
-
}
|
|
84
|
-
]
|
|
85
|
-
},
|
|
86
|
-
"SeriesInfo": []
|
|
87
|
-
}
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"Codigo": 0,
|
|
3
|
-
"Descripcion": "Success",
|
|
4
|
-
"Series": {
|
|
5
|
-
"descripEsp": null,
|
|
6
|
-
"descripIng": null,
|
|
7
|
-
"seriesId": null,
|
|
8
|
-
"Obs": null
|
|
9
|
-
},
|
|
10
|
-
"SeriesInfos": [
|
|
11
|
-
{
|
|
12
|
-
"seriesId": "TEST",
|
|
13
|
-
"frequencyCode": "DAILY",
|
|
14
|
-
"spanishTitle": "Test",
|
|
15
|
-
"englishTitle": "Test",
|
|
16
|
-
"firstObservation": "01-12-2020",
|
|
17
|
-
"lastObservation": "15-12-2020",
|
|
18
|
-
"updatedAt": "19-01-2024",
|
|
19
|
-
"createdAt": "19-01-2024"
|
|
20
|
-
}
|
|
21
|
-
]
|
|
22
|
-
}
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect } from 'vitest';
|
|
2
|
-
import * as Errors from '../../src/errors';
|
|
3
|
-
import { handleGetSeriesResponse } from '../../src/handlers';
|
|
4
|
-
import { ApiResponse } from '../../src/types';
|
|
5
|
-
import fixtures from '../fixtures';
|
|
6
|
-
|
|
7
|
-
describe('handleGetSeriesResponse', () => {
|
|
8
|
-
it('should parse correctly a valid response', () => {
|
|
9
|
-
const parsed = handleGetSeriesResponse(
|
|
10
|
-
fixtures.response.getSeriesSuccess as unknown as ApiResponse,
|
|
11
|
-
);
|
|
12
|
-
|
|
13
|
-
expect(parsed).toBeDefined();
|
|
14
|
-
expect(parsed.seriesId).toBe(fixtures.response.getSeriesSuccess.Series.seriesId);
|
|
15
|
-
expect(parsed.description).toBe(fixtures.response.getSeriesSuccess.Series.descripIng);
|
|
16
|
-
expect(parsed.data).toHaveLength(fixtures.response.getSeriesSuccess.Series.Obs.length);
|
|
17
|
-
|
|
18
|
-
for (let i = 0; i < parsed.data.length; i += 1) {
|
|
19
|
-
expect(parsed.data[i].date).toMatch(/^\d{4}-\d{2}-\d{2}$/);
|
|
20
|
-
expect(parsed.data[i].value.toFixed(2)).toBe(
|
|
21
|
-
fixtures.response.getSeriesSuccess.Series.Obs[i].value,
|
|
22
|
-
);
|
|
23
|
-
|
|
24
|
-
if (Number.isNaN(parsed.data[i].value)) {
|
|
25
|
-
expect(fixtures.response.getSeriesSuccess.Series.Obs[i].statusCode).toBe('ND');
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
it('should throw an InvalidCredentialsError if the response code is -5', () => {
|
|
31
|
-
expect(() =>
|
|
32
|
-
handleGetSeriesResponse(fixtures.response.credentialsInvalid as unknown as ApiResponse),
|
|
33
|
-
).toThrow(Errors.InvalidCredentialsError);
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
it('should throw an InvalidSeriesError if the response code is -50', () => {
|
|
37
|
-
expect(() =>
|
|
38
|
-
handleGetSeriesResponse(fixtures.response.getSeriesInvalid as unknown as ApiResponse),
|
|
39
|
-
).toThrow(Errors.InvalidSeriesError);
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
it('should throw a ResponseError if the response code is unknown', () => {
|
|
43
|
-
expect(() => handleGetSeriesResponse({} as ApiResponse)).toThrow(Errors.WebServiceError);
|
|
44
|
-
});
|
|
45
|
-
});
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect } from 'vitest';
|
|
2
|
-
import * as Errors from '../../src/errors';
|
|
3
|
-
import { ApiResponse } from '../../src/types';
|
|
4
|
-
import { handleSearchSeriesResponse } from '../../src/handlers';
|
|
5
|
-
import fixtures from '../fixtures';
|
|
6
|
-
|
|
7
|
-
describe('handleSearchSeriesResponse', () => {
|
|
8
|
-
it('should parse correctly a valid response', () => {
|
|
9
|
-
const parsed = handleSearchSeriesResponse(
|
|
10
|
-
fixtures.response.searchSeriesSuccess as unknown as ApiResponse,
|
|
11
|
-
);
|
|
12
|
-
|
|
13
|
-
expect(parsed).toBeInstanceOf(Array);
|
|
14
|
-
expect(parsed).toHaveLength(fixtures.response.searchSeriesSuccess.SeriesInfos.length);
|
|
15
|
-
|
|
16
|
-
for (let i = 0; i < parsed.length; i += 1) {
|
|
17
|
-
expect(parsed[i].seriesId).toBe(
|
|
18
|
-
fixtures.response.searchSeriesSuccess.SeriesInfos[i].seriesId,
|
|
19
|
-
);
|
|
20
|
-
expect(parsed[i].frequency).toBe(
|
|
21
|
-
fixtures.response.searchSeriesSuccess.SeriesInfos[i].frequencyCode,
|
|
22
|
-
);
|
|
23
|
-
expect(parsed[i].title).toBe(
|
|
24
|
-
fixtures.response.searchSeriesSuccess.SeriesInfos[i].englishTitle,
|
|
25
|
-
);
|
|
26
|
-
expect(parsed[i].firstObservedAt).toMatch(/^\d{4}-\d{2}-\d{2}$/);
|
|
27
|
-
expect(parsed[i].lastObservedAt).toMatch(/^\d{4}-\d{2}-\d{2}$/);
|
|
28
|
-
expect(parsed[i].updatedAt).toMatch(/^\d{4}-\d{2}-\d{2}$/);
|
|
29
|
-
expect(parsed[i].createdAt).toMatch(/^\d{4}-\d{2}-\d{2}$/);
|
|
30
|
-
}
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
it('should throw an InvalidCredentialsError if the response code is -5', () => {
|
|
34
|
-
expect(() =>
|
|
35
|
-
handleSearchSeriesResponse(fixtures.response.credentialsInvalid as unknown as ApiResponse),
|
|
36
|
-
).toThrow(Errors.InvalidCredentialsError);
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
it('should throw an InvalidFrequencyError if the response code is -1', () => {
|
|
40
|
-
expect(() =>
|
|
41
|
-
handleSearchSeriesResponse(fixtures.response.searchSeriesInvalid as unknown as ApiResponse),
|
|
42
|
-
).toThrow(Errors.InvalidFrequencyError);
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
it('should throw a ResponseError if the response code is unknown', () => {
|
|
46
|
-
expect(() => handleSearchSeriesResponse({} as ApiResponse)).toThrow(Errors.WebServiceError);
|
|
47
|
-
});
|
|
48
|
-
});
|
package/test/mocks/fetch.mock.ts
DELETED
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
import { Frequency } from '../../src/client';
|
|
2
|
-
import { reverseDate } from '../../src/utils';
|
|
3
|
-
import fixtures from '../fixtures';
|
|
4
|
-
|
|
5
|
-
export default (input: string | URL | Request) => {
|
|
6
|
-
const params = new URL(input.toString()).searchParams;
|
|
7
|
-
const func = params.get('function') || 'GetSeries';
|
|
8
|
-
|
|
9
|
-
if (params.get('user') !== 'test' || params.get('pass') !== 'test') {
|
|
10
|
-
return Promise.resolve({
|
|
11
|
-
json: () => Promise.resolve(fixtures.response.credentialsInvalid),
|
|
12
|
-
} as Response);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
if (func === 'GetSeries') {
|
|
16
|
-
if (fixtures.response.getSeriesSuccess.Series.seriesId !== params.get('timeseries')) {
|
|
17
|
-
return Promise.resolve({
|
|
18
|
-
json: () => Promise.resolve(fixtures.response.getSeriesInvalid),
|
|
19
|
-
} as Response);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
const firstDate = params.get('firstdate');
|
|
23
|
-
const lastDate = params.get('lastdate');
|
|
24
|
-
const obs = fixtures.response.getSeriesSuccess.Series.Obs.filter((o) => {
|
|
25
|
-
if (firstDate && lastDate) {
|
|
26
|
-
return (
|
|
27
|
-
reverseDate(o.indexDateString) >= firstDate && reverseDate(o.indexDateString) <= lastDate
|
|
28
|
-
);
|
|
29
|
-
}
|
|
30
|
-
if (firstDate) {
|
|
31
|
-
return reverseDate(o.indexDateString) >= firstDate;
|
|
32
|
-
}
|
|
33
|
-
if (lastDate) {
|
|
34
|
-
return reverseDate(o.indexDateString) <= lastDate;
|
|
35
|
-
}
|
|
36
|
-
return true;
|
|
37
|
-
});
|
|
38
|
-
const series = {
|
|
39
|
-
...fixtures.response.getSeriesSuccess.Series,
|
|
40
|
-
Obs: obs,
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
return Promise.resolve({
|
|
44
|
-
json: () =>
|
|
45
|
-
Promise.resolve({
|
|
46
|
-
...fixtures.response.getSeriesSuccess,
|
|
47
|
-
Series: series,
|
|
48
|
-
}),
|
|
49
|
-
} as Response);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
if (func === 'SearchSeries') {
|
|
53
|
-
const frequency = params.get('frequency');
|
|
54
|
-
|
|
55
|
-
if (!Object.values(Frequency).includes(frequency as Frequency)) {
|
|
56
|
-
return Promise.resolve({
|
|
57
|
-
json: () => Promise.resolve(fixtures.response.searchSeriesInvalid),
|
|
58
|
-
} as Response);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
const response = {
|
|
62
|
-
...fixtures.response.searchSeriesSuccess,
|
|
63
|
-
SeriesInfo: fixtures.response.searchSeriesSuccess.SeriesInfos.filter(
|
|
64
|
-
(series) => series.frequencyCode === frequency,
|
|
65
|
-
),
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
return Promise.resolve({
|
|
69
|
-
json: () => Promise.resolve(response),
|
|
70
|
-
} as Response);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
return Promise.resolve({
|
|
74
|
-
json: () => Promise.resolve({}),
|
|
75
|
-
} as Response);
|
|
76
|
-
};
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect } from 'vitest';
|
|
2
|
-
import { isValidDate } from '../../src/utils';
|
|
3
|
-
|
|
4
|
-
describe('isValidDate', () => {
|
|
5
|
-
it('should return true if the date is valid', () => {
|
|
6
|
-
expect(isValidDate('2020-01-01')).toBe(true);
|
|
7
|
-
expect(isValidDate(new Date())).toBe(true);
|
|
8
|
-
});
|
|
9
|
-
|
|
10
|
-
it('should return false if the date is invalid', () => {
|
|
11
|
-
expect(isValidDate('invalid')).toBe(false);
|
|
12
|
-
expect(isValidDate({})).toBe(false);
|
|
13
|
-
expect(isValidDate(undefined)).toBe(false);
|
|
14
|
-
expect(isValidDate(null)).toBe(false);
|
|
15
|
-
expect(isValidDate(new Date('invalid'))).toBe(false);
|
|
16
|
-
});
|
|
17
|
-
});
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect } from 'vitest';
|
|
2
|
-
import { reverseDate } from '../../src/utils';
|
|
3
|
-
|
|
4
|
-
describe('reverseDate', () => {
|
|
5
|
-
it('should reverse correctly a date in DD-MM-YYYY format', () => {
|
|
6
|
-
const date = '01-02-2020';
|
|
7
|
-
const reversed = reverseDate(date);
|
|
8
|
-
|
|
9
|
-
expect(reversed).toBe('2020-02-01');
|
|
10
|
-
});
|
|
11
|
-
});
|
package/tsconfig.eslint.json
DELETED
package/tsconfig.json
DELETED