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 CHANGED
@@ -78,7 +78,7 @@ _Output_
78
78
 
79
79
  ### SearchSeries
80
80
 
81
- Allows you to search for series by their frequency code (ie: daily, weekly, monthly, yearly).
81
+ Allows you to search for series by their frequency code (ie: daily, monthly, quarterly, yearly).
82
82
 
83
83
  #### Parameters
84
84
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bcchapi",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "API para acceder al Web Service del Banco Central de Chile.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
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 './helpers/parsers';
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 SeriesValue {
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: SeriesValue[];
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 '../errors';
9
- import { ApiResponse, ErrorResponse, GetSeriesResponse, SearchSeriesResponse } from '../types';
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,
@@ -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
 
@@ -1,5 +1,5 @@
1
1
  import { Frequency } from '../../src/client';
2
- import reverseDate from '../../src/helpers/reverse-date';
2
+ import { reverseDate } from '../../src/utils';
3
3
  import fixtures from '../fixtures';
4
4
 
5
5
  export default (input: string | URL | Request) => {
@@ -1,5 +1,5 @@
1
1
  import { describe, it, expect } from 'vitest';
2
- import isValidDate from '../../src/helpers/is-valid-date';
2
+ import { isValidDate } from '../../src/utils';
3
3
 
4
4
  describe('isValidDate', () => {
5
5
  it('should return true if the date is valid', () => {
@@ -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/helpers/parsers';
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/helpers/parsers';
4
+ import { parseSearchSeriesResponse } from '../../src/utils';
5
5
  import fixtures from '../fixtures';
6
6
 
7
7
  describe('parseSearchSeriesResponse', () => {
@@ -1,5 +1,5 @@
1
1
  import { describe, it, expect } from 'vitest';
2
- import reverseDate from '../../src/helpers/reverse-date';
2
+ import { reverseDate } from '../../src/utils';
3
3
 
4
4
  describe('reverseDate', () => {
5
5
  it('should reverse correctly a date in DD-MM-YYYY format', () => {
@@ -1,9 +0,0 @@
1
- export default function isValidDate(date: unknown): boolean {
2
- if (typeof date === 'string') {
3
- return !Number.isNaN(Date.parse(date));
4
- }
5
- if (date instanceof Date) {
6
- return !Number.isNaN(date.getTime());
7
- }
8
- return false;
9
- }
@@ -1,3 +0,0 @@
1
- export default function reverseDate(date: string): string {
2
- return date.split('-').reverse().join('-');
3
- }