Skip to content

Commit 61d1be1

Browse files
committed
dev: add logs to client;api-call-builder
1 parent 076f149 commit 61d1be1

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

deno.lock

Lines changed: 13 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/builders/api-call-builder.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
import { getApi, type GetApiError } from "../common/api.ts";
22
import { createQueryParams, type QueryParams } from "../common/api.ts";
3+
import { getLogger, type Logger } from "@logtape/logtape";
34

45
export default class ApiCallBuilder<T> {
56
private readonly token: string;
67
private readonly getApi: typeof getApi;
78
private endpoint?: string;
89
private queryParams?: QueryParams;
910
private id?: string;
11+
private logger: Logger;
1012

1113
constructor({
1214
token,
1315
}: {
1416
token: string;
1517
}) {
18+
this.logger = getLogger(["czech-covid-data-api-lib", "api-call-builder"]);
1619
this.token = token;
1720
this.getApi = getApi;
1821
this.endpoint = undefined;
@@ -22,23 +25,39 @@ export default class ApiCallBuilder<T> {
2225

2326
public provideQueryParams(queryParams: QueryParams) {
2427
this.queryParams = queryParams;
28+
this.logger.debug("Provided query parameters.", {
29+
method: "provideQueryParams",
30+
data: queryParams,
31+
});
2532
return this;
2633
}
2734

2835
public provideEndpoint(endpoint: string) {
2936
this.endpoint = endpoint;
37+
this.logger.debug("Provided endpoint.", {
38+
method: "provideEndpoint",
39+
data: endpoint,
40+
});
3041
return this;
3142
}
3243

3344
public provideId(id: string) {
3445
this.id = id;
46+
this.logger.debug("Provided endpoint subpath id.", {
47+
method: "provideId",
48+
data: id,
49+
});
3550
return this;
3651
}
3752

3853
public async build(): Promise<[T, null] | [null, GetApiError]> {
3954
let queryParams: URLSearchParams | undefined;
4055

4156
if (typeof this.endpoint === "undefined") {
57+
this.logger.error("Endpoint argument is of type undefined.", {
58+
method: "build",
59+
data: this.endpoint,
60+
});
4261
throw new Error("Property endpoint has to have a defined value!");
4362
}
4463

@@ -50,6 +69,10 @@ export default class ApiCallBuilder<T> {
5069
this.endpoint = `${this.endpoint}/${this.id}`;
5170
}
5271

72+
this.logger.debug("Attempting to call the api.", {
73+
method: "build",
74+
data: { endpoint: this.endpoint, token: "***", queryParams },
75+
});
5376
return await this.getApi<T>(
5477
this.endpoint,
5578
this.token,

src/client/client.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import Deaths from "../deaths/deaths.ts";
55
import VaccinationAggregated from "../vaccination/vaccination-aggregated.ts";
66
import VaccinationPlaces from "../vaccination/vaccination-places.ts";
77

8+
import { getLogger, type Logger } from "@logtape/logtape";
9+
810
/**
911
* Represents single entrypoint for calling supported
1012
* COVID REST APIs.
@@ -28,6 +30,8 @@ export default class Client {
2830
public readonly vaccinationPlaces: VaccinationPlaces;
2931
/** @property instance - holds the singleton instance of the class */
3032
private static instance: Client;
33+
/** @property logger - holds instance of the logger */
34+
private logger: Logger;
3135

3236
/**
3337
* Represents single entrypoint for calling the supported
@@ -41,6 +45,7 @@ export default class Client {
4145
* @param param0.token - your personal token
4246
*/
4347
private constructor({ token }: { token?: string }) {
48+
this.logger = getLogger(["czech-covid-data-api-lib", "client"]);
4449
this.token = this.addToken(token);
4550

4651
this.hospitalization = new Hospitalizations(this.token);
@@ -59,9 +64,15 @@ export default class Client {
5964
*/
6065
private addToken(token?: string) {
6166
if (typeof token === "string") {
67+
Client.instance.logger.debug(
68+
"Token of valid type string was provided and will be returned.",
69+
);
6270
return token;
6371
}
64-
72+
Client.instance.logger.fatal(
73+
"Provided token was not of type string. Method will throw.",
74+
{ method: "addToken" },
75+
);
6576
throw new Error("Provided token must be of the type string.");
6677
}
6778

@@ -71,7 +82,16 @@ export default class Client {
7182
}
7283
if (!Client.instance) {
7384
Client.instance = new Client({ token });
85+
Client.instance.logger.debug(
86+
"Client instance was not found. New instance was created.",
87+
{ method: "getInstance" },
88+
);
7489
}
90+
91+
Client.instance.logger.debug(
92+
"Client instance exists and will be returned.",
93+
{ method: "getInstance" },
94+
);
7595
return Client.instance;
7696
}
7797
}

0 commit comments

Comments
 (0)