Skip to content

Commit 825d2cd

Browse files
authored
Merge pull request #85 from usherlabs/FIET-364
Fiet 364
2 parents dcf7d82 + 75be147 commit 825d2cd

File tree

3 files changed

+37
-13
lines changed

3 files changed

+37
-13
lines changed

ts/verity-client/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@
3131
"dependencies": {
3232
"@types/eventsource": "^1.1.15",
3333
"axios": "^1.9.0",
34-
"uuid": "^11.1.0",
35-
"eventsource": "^2.0.2"
34+
"eventsource": "^2.0.2",
35+
"tslog": "^4.10.2",
36+
"uuid": "^11.1.0"
3637
},
3738
"devDependencies": {
3839
"@babel/preset-env": "^7.23.2",

ts/verity-client/src/index.ts

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type {
77
AxiosResponseHeaders,
88
RawAxiosResponseHeaders,
99
} from "axios";
10+
import { log } from "./logger";
1011

1112
// Dynamic EventSource import for cross-platform compatibility
1213
let EventSource: any;
@@ -22,7 +23,7 @@ function initializeEventSource() {
2223
// Use require for Node.js environments
2324
EventSource = require("eventsource");
2425
} catch (error) {
25-
console.warn(
26+
log.error(
2627
"eventsource package not available, EventSource functionality may not work in Node.js environment",
2728
);
2829
EventSource = null;
@@ -51,6 +52,7 @@ class VerityRequest<T> {
5152
private axiosInstance: AxiosInstance;
5253
private redacted: string | null = null;
5354
public requestId: string;
55+
public strictProof = false;
5456
private url: string;
5557
private sse_is_ready = false;
5658
private proof: Promise<string>;
@@ -60,18 +62,20 @@ class VerityRequest<T> {
6062
method: string,
6163
Url: string,
6264
config?: AxiosRequestConfig,
65+
strictProof?: boolean,
6366
data?: any,
6467
) {
6568
const { url, ...rest } = config ?? {};
6669
this.config = rest || {};
6770
this.requestId = uuidv4().toString();
6871
this.url = Url;
72+
this.strictProof = strictProof || false;
6973

7074
this.axiosInstance = axiosInstance;
7175
this.proof = this.subscribeToProof().catch((err) => {
72-
console.error(`Proof SSE failed for ${this.requestId}:`, err);
76+
log.error(`Proof SSE failed for ${this.requestId}:`, err);
7377
// re-throw so downstream still sees the error
74-
throw err;
78+
return strictProof ? Promise.reject(err) : "";
7579
});
7680

7781
const instance = axios.create();
@@ -90,7 +94,7 @@ class VerityRequest<T> {
9094
response.proof = data.slice(index + 1);
9195
return response;
9296
} catch (error) {
93-
console.log({ error });
97+
log.error({ error });
9498
return response;
9599
}
96100
},
@@ -183,9 +187,9 @@ class VerityRequest<T> {
183187

184188
es.onerror = (err: any) => {
185189
clearTimeout(timeout);
186-
console.error("SSE error:", err);
190+
log.error("SSE error:", err);
187191
es.close();
188-
reject(err);
192+
this.strictProof ? reject(err) : resolve("");
189193
};
190194
});
191195
}
@@ -204,14 +208,25 @@ export class VerityClient {
204208
});
205209
}
206210

207-
get<T>(url: string, config?: AxiosRequestConfig) {
208-
return new VerityRequest<T>(this.axios, "get", url, config);
211+
get<T>(url: string, config?: AxiosRequestConfig, strictProof = false) {
212+
return new VerityRequest<T>(this.axios, "get", url, config, strictProof);
209213
}
210214

211-
post<T>(url: string, config?: AxiosRequestConfig, data?: any) {
212-
return new VerityRequest<T>(this.axios, "post", url, config, data);
215+
post<T>(
216+
url: string,
217+
config?: AxiosRequestConfig,
218+
data?: any,
219+
strictProof = false,
220+
) {
221+
return new VerityRequest<T>(
222+
this.axios,
223+
"post",
224+
url,
225+
config,
226+
strictProof,
227+
data,
228+
);
213229
}
214-
215230
/// Get the information of the connected notary
216231
async get_notary_info() {
217232
const response = await this.axios.get<INotaryInformation>("/notaryinfo");

ts/verity-client/src/logger.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { Logger } from "tslog";
2+
3+
const logLevel = Number.parseInt(process.env.LOG_LEVEL || "3") || 3;
4+
5+
export const log = new Logger({
6+
name: "VerityTsClient",
7+
minLevel: logLevel,
8+
});

0 commit comments

Comments
 (0)