Skip to content

Commit a1918be

Browse files
committed
Fix logLevels and add missing export
1 parent ef8f81a commit a1918be

5 files changed

Lines changed: 32 additions & 16 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ yarn add @boilingdata/node-boilingdata
1818
`execQueryPromise()` method can be used to await for the results directly.
1919

2020
```typescript
21-
import { BoilingData, isDataResponse } from "@boilingdata/node-boilingdata";
21+
import { BoilingData } from "@boilingdata/node-boilingdata";
2222

2323
async function main() {
2424
const bdInstance = new BoilingData({ username: process.env["BD_USERNAME"], password: process.env["BD_PASSWORD"] });

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"license": "MIT",
1010
"private": false,
1111
"files": [
12-
"dist/"
12+
"dist/*"
1313
],
1414
"repository": {
1515
"type": "git",

src/boilingdata/boilingdata.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,20 @@ enum ECallbackNames {
9999
QUERY_FINISHED = "onQueryFinished",
100100
}
101101

102-
const createLogger = (_props: any): Console => console;
102+
const createLogger = (props: any): Console => {
103+
const logLevel = props.logLevel ?? "info";
104+
105+
// logLevel == "error"
106+
// ==> log.info("") --> NA
107+
return {
108+
...console,
109+
debug: (a, ...rest) => (["debug"].includes(logLevel) ? console.log(a, ...rest) : undefined),
110+
log: (a, ...rest) => (["debug", "info"].includes(logLevel) ? console.log(a, ...rest) : undefined),
111+
info: (a, ...rest) => (["debug", "info"].includes(logLevel) ? console.log(a, ...rest) : undefined),
112+
warn: (a, ...rest) => (["debug", "info", "warn"].includes(logLevel) ? console.log(a, ...rest) : undefined),
113+
error: (a, ...rest) => console.log(a, ...rest),
114+
};
115+
};
103116

104117
function mapEventToCallbackName(event: IEvent): ECallbackNames {
105118
const entry = Object.entries(ECallbackNames).find(([key, _value]) => key === event.eventType);
@@ -115,9 +128,10 @@ export class BoilingData {
115128
private region: BDAWSRegion;
116129
private creds?: BDCredentials;
117130
private socketInstance: ISocketInstance;
118-
private logger = createLogger({ name: "boilingdata", level: this.props.logLevel ?? "info" });
131+
private logger: Console;
119132

120133
constructor(public props: IBoilingData) {
134+
this.logger = createLogger({ name: "boilingdata", logLevel: this.props.logLevel ?? "info" });
121135
this.region = this.props.region ? this.props.region : "eu-west-1";
122136
this.socketInstance = {
123137
queries: new Map(), // no queries yet
@@ -169,6 +183,7 @@ export class BoilingData {
169183
this.props.endpointUrl,
170184
this.props.mfa,
171185
this.props.authcontext,
186+
this.logger,
172187
)
173188
.then(creds => {
174189
this.creds = creds;
@@ -294,7 +309,7 @@ export class BoilingData {
294309

295310
public async execQuery(params: IBDQuery): Promise<void> {
296311
this.validateJsHooks(params);
297-
this.logger.info("execQuery:", params);
312+
this.logger.debug("execQuery:", params);
298313
this.socketInstance.bumpActivity();
299314
const requestId = params.requestId ?? uuidv4();
300315
const payload: IBDDataQuery = {

src/common/identity.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,27 @@ export interface BDCredentials {
2222
signedWebsocketUrl: string;
2323
}
2424

25-
function getIdToken(Username: string, Password: string, mfa?: number): Promise<CognitoIdToken> {
25+
function getIdToken(Username: string, Password: string, mfa?: number, logger?: Console): Promise<CognitoIdToken> {
2626
return new Promise((resolve, reject) => {
2727
try {
2828
const loginDetails = { Username, Password };
29-
console.log("====", Pool.getUserPoolId(), Pool.getClientId(), Pool.getCurrentUser());
29+
logger?.debug("====", Pool.getUserPoolId(), Pool.getClientId(), Pool.getCurrentUser());
3030
const userData = { Username, Pool };
3131
const cognitoUser = new CognitoUser(userData);
32-
console.log("====", cognitoUser.getUsername());
32+
logger?.debug("====", cognitoUser.getUsername());
3333
const authenticationDetails = new AuthenticationDetails(loginDetails);
3434
cognitoUser.authenticateUser(authenticationDetails, {
3535
mfaRequired: async function (challengeName: ChallengeName, challengeParameters: any) {
36-
console.log({ callback: "mfaRequired", challengeName, challengeParameters });
36+
logger?.debug({ callback: "mfaRequired", challengeName, challengeParameters });
3737
if (!mfa) return reject("MFA required");
3838
cognitoUser.sendMFACode(`${mfa}`, this);
3939
},
4040
totpRequired: async function (challengeName: ChallengeName, challengeParameters: any) {
41-
console.log({ callback: "totpRequired", challengeName, challengeParameters });
41+
logger?.debug({ callback: "totpRequired", challengeName, challengeParameters });
4242
if (!mfa) return reject("TOTP required");
4343
cognitoUser.sendMFACode(`${mfa}`, this, "SOFTWARE_TOKEN_MFA");
4444
},
45-
onSuccess: (result: any) => resolve(result?.getIdToken()),
45+
onSuccess: (result: any) => resolve(result?.getIdToken(undefined, undefined, undefined, logger)),
4646
onFailure: (err: any) => reject(err),
4747
});
4848
} catch (err) {
@@ -63,7 +63,7 @@ async function refreshCredsWithToken(jwtIdToken: string): Promise<any> {
6363
}),
6464
});
6565
const creds = await cognitoidentity.config.credentials();
66-
console.log(creds.expiration);
66+
// console.log(creds.expiration);
6767
return creds;
6868
}
6969

@@ -83,15 +83,16 @@ export async function getBoilingDataCredentials(
8383
endpointUrl?: string,
8484
mfa?: number,
8585
authContext?: { idToken?: any; accessToken?: any; refreshToken?: any },
86+
logger?: Console,
8687
): Promise<BDCredentials> {
8788
const webSocketHost = getWsApiDomain(region, endpointUrl);
8889
let idToken: CognitoIdToken | undefined = undefined;
8990
try {
9091
if (!authContext && username && password) {
91-
console.log("Fetching ID token with username and pw");
92-
idToken = await getIdToken(username, password, mfa);
92+
logger?.debug("Fetching ID token with username and pw");
93+
idToken = await getIdToken(username, password, mfa, logger);
9394
} else if (authContext && authContext.idToken?.jwtToken) {
94-
console.log("Using existing ID token");
95+
logger?.debug("Using existing ID token");
9596
idToken = new CognitoIdToken({ IdToken: authContext.idToken?.jwtToken });
9697
}
9798
if (!idToken) throw new Error("No credentials for creating signed WS URL");

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { IBoilingData, BoilingData, IBDQuery, IBDCallbacks } from "./boilingdata/boilingdata";
1+
export { IBoilingData, BoilingData, IBDQuery, IBDCallbacks, IJsHooks, isDataResponse } from "./boilingdata/boilingdata";

0 commit comments

Comments
 (0)