bcchapi 1.0.2 → 1.0.3
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 +1 -1
- package/package.json +1 -1
- package/src/client.ts +1 -2
- package/src/types.ts +5 -5
- package/src/{helpers/parsers.ts → utils.ts} +41 -9
- package/test/client.test.ts +1 -1
- package/test/mocks/fetch.mock.ts +1 -1
- package/test/{helpers → utils}/is-valid-date.test.ts +1 -1
- package/test/{helpers → utils}/parse-get-series.test.ts +1 -1
- package/test/{helpers → utils}/parse-search-series.test.ts +1 -1
- package/test/{helpers → utils}/reverse-date.test.ts +1 -1
- package/src/helpers/is-valid-date.ts +0 -9
- package/src/helpers/reverse-date.ts +0 -3
package/README.md
CHANGED
package/package.json
CHANGED
package/src/client.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import * as querystring from 'node:querystring';
|
|
2
2
|
import * as assert from 'node:assert/strict';
|
|
3
3
|
import { GetSeriesResponse, SearchSeriesResponse, ApiResponse } from './types';
|
|
4
|
-
import { parseGetSeriesResponse, parseSearchSeriesResponse } from './
|
|
5
|
-
import isValidDate from './helpers/is-valid-date';
|
|
4
|
+
import { isValidDate, parseGetSeriesResponse, parseSearchSeriesResponse } from './utils';
|
|
6
5
|
|
|
7
6
|
export interface ClientConfig {
|
|
8
7
|
/**
|
package/src/types.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
interface
|
|
1
|
+
export interface SeriesObservation {
|
|
2
2
|
/**
|
|
3
3
|
* Series observed date in DD-MM-YYYY format.
|
|
4
4
|
*/
|
|
@@ -13,7 +13,7 @@ interface SeriesValue {
|
|
|
13
13
|
statusCode: 'OK' | 'ND';
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
-
interface SeriesHistory {
|
|
16
|
+
export interface SeriesHistory {
|
|
17
17
|
/**
|
|
18
18
|
* Series identifier.
|
|
19
19
|
*/
|
|
@@ -29,14 +29,14 @@ interface SeriesHistory {
|
|
|
29
29
|
/**
|
|
30
30
|
* List of series observed values.
|
|
31
31
|
*/
|
|
32
|
-
Obs:
|
|
32
|
+
Obs: SeriesObservation[];
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
-
type NullSeries = {
|
|
35
|
+
export type NullSeries = {
|
|
36
36
|
[key in keyof SeriesHistory]: null;
|
|
37
37
|
};
|
|
38
38
|
|
|
39
|
-
interface SeriesMetadata {
|
|
39
|
+
export interface SeriesMetadata {
|
|
40
40
|
/**
|
|
41
41
|
* Series identifier.
|
|
42
42
|
*/
|
|
@@ -1,20 +1,49 @@
|
|
|
1
|
-
import reverseDate from './reverse-date';
|
|
2
1
|
import {
|
|
3
2
|
ErrorCodes,
|
|
4
3
|
InvalidFrequencyError,
|
|
5
4
|
InvalidCredentialsError,
|
|
6
5
|
InvalidSeriesError,
|
|
7
6
|
WebServiceError,
|
|
8
|
-
} from '
|
|
9
|
-
import {
|
|
7
|
+
} from './errors';
|
|
8
|
+
import {
|
|
9
|
+
ApiResponse,
|
|
10
|
+
SeriesObservation,
|
|
11
|
+
SeriesMetadata,
|
|
12
|
+
ErrorResponse,
|
|
13
|
+
GetSeriesResponse,
|
|
14
|
+
SearchSeriesResponse,
|
|
15
|
+
} from './types';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Determines wether a given value can be parsed to a valid date.
|
|
19
|
+
*/
|
|
20
|
+
export function isValidDate(date: unknown): boolean {
|
|
21
|
+
if (typeof date === 'string') {
|
|
22
|
+
return !Number.isNaN(Date.parse(date));
|
|
23
|
+
}
|
|
24
|
+
if (date instanceof Date) {
|
|
25
|
+
return !Number.isNaN(date.getTime());
|
|
26
|
+
}
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Reverses a date string from DD-MM-YYYY to YYYY-MM-DD format.
|
|
32
|
+
*/
|
|
33
|
+
export function reverseDate(date: string): string {
|
|
34
|
+
return date.split('-').reverse().join('-');
|
|
35
|
+
}
|
|
10
36
|
|
|
37
|
+
/**
|
|
38
|
+
* Parses the GetSeries function API response.
|
|
39
|
+
*/
|
|
11
40
|
export function parseGetSeriesResponse<T extends ApiResponse>(response: T): GetSeriesResponse {
|
|
12
41
|
if (response.Codigo !== 0) {
|
|
13
42
|
switch (response.Codigo) {
|
|
14
43
|
case ErrorCodes.InvalidCredentials:
|
|
15
|
-
throw new InvalidCredentialsError();
|
|
44
|
+
throw new InvalidCredentialsError(response as ApiResponse as ErrorResponse);
|
|
16
45
|
case ErrorCodes.InvalidSeries:
|
|
17
|
-
throw new InvalidSeriesError();
|
|
46
|
+
throw new InvalidSeriesError(response as ApiResponse as ErrorResponse);
|
|
18
47
|
default:
|
|
19
48
|
throw new WebServiceError(response as ApiResponse as ErrorResponse);
|
|
20
49
|
}
|
|
@@ -23,28 +52,31 @@ export function parseGetSeriesResponse<T extends ApiResponse>(response: T): GetS
|
|
|
23
52
|
return {
|
|
24
53
|
seriesId: response.Series.seriesId || '',
|
|
25
54
|
description: response.Series.descripIng || '',
|
|
26
|
-
data: (response.Series.Obs || []).map((obs) => ({
|
|
55
|
+
data: (response.Series.Obs || []).map((obs: SeriesObservation) => ({
|
|
27
56
|
date: reverseDate(obs.indexDateString),
|
|
28
57
|
value: parseFloat(obs.value),
|
|
29
58
|
})),
|
|
30
59
|
};
|
|
31
60
|
}
|
|
32
61
|
|
|
62
|
+
/**
|
|
63
|
+
* Parses the SearchSeries function API response.
|
|
64
|
+
*/
|
|
33
65
|
export function parseSearchSeriesResponse<T extends ApiResponse>(
|
|
34
66
|
response: T,
|
|
35
67
|
): SearchSeriesResponse {
|
|
36
68
|
if (response.Codigo !== 0) {
|
|
37
69
|
switch (response.Codigo) {
|
|
38
70
|
case ErrorCodes.InvalidCredentials:
|
|
39
|
-
throw new InvalidCredentialsError();
|
|
71
|
+
throw new InvalidCredentialsError(response as ApiResponse as ErrorResponse);
|
|
40
72
|
case ErrorCodes.InvalidFrequency:
|
|
41
|
-
throw new InvalidFrequencyError();
|
|
73
|
+
throw new InvalidFrequencyError(response as ApiResponse as ErrorResponse);
|
|
42
74
|
default:
|
|
43
75
|
throw new WebServiceError(response as ApiResponse as ErrorResponse);
|
|
44
76
|
}
|
|
45
77
|
}
|
|
46
78
|
|
|
47
|
-
return response.SeriesInfos.map((series) => ({
|
|
79
|
+
return response.SeriesInfos.map((series: SeriesMetadata) => ({
|
|
48
80
|
seriesId: series.seriesId,
|
|
49
81
|
frequency: series.frequencyCode,
|
|
50
82
|
title: series.englishTitle,
|
package/test/client.test.ts
CHANGED
|
@@ -6,9 +6,9 @@ import {
|
|
|
6
6
|
InvalidSeriesError,
|
|
7
7
|
InvalidFrequencyError,
|
|
8
8
|
} from '../src';
|
|
9
|
+
import { reverseDate } from '../src/utils';
|
|
9
10
|
import fetchMock from './mocks/fetch.mock';
|
|
10
11
|
import fixtures from './fixtures';
|
|
11
|
-
import reverseDate from '../src/helpers/reverse-date';
|
|
12
12
|
|
|
13
13
|
const fetchSpy = vi.spyOn(global, 'fetch').mockImplementation(fetchMock);
|
|
14
14
|
|
package/test/mocks/fetch.mock.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { describe, it, expect } from 'vitest';
|
|
2
2
|
import * as Errors from '../../src/errors';
|
|
3
|
-
import { parseGetSeriesResponse } from '../../src/
|
|
3
|
+
import { parseGetSeriesResponse } from '../../src/utils';
|
|
4
4
|
import { ApiResponse } from '../../src/types';
|
|
5
5
|
import fixtures from '../fixtures';
|
|
6
6
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { describe, it, expect } from 'vitest';
|
|
2
2
|
import * as Errors from '../../src/errors';
|
|
3
3
|
import { ApiResponse } from '../../src/types';
|
|
4
|
-
import { parseSearchSeriesResponse } from '../../src/
|
|
4
|
+
import { parseSearchSeriesResponse } from '../../src/utils';
|
|
5
5
|
import fixtures from '../fixtures';
|
|
6
6
|
|
|
7
7
|
describe('parseSearchSeriesResponse', () => {
|