Skip to content

Commit

Permalink
Merge pull request #5 from Akord-com/logger
Browse files Browse the repository at this point in the history
add custom logger for debugging purposes
  • Loading branch information
jarrvis authored Sep 27, 2022
2 parents ef4f52a + 9228506 commit c01553f
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/__tests__/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ export async function initInstance(email: string, password: string) : Promise<Ak
const jwtToken = await apiAuthenticator.getJWTToken(email, password);
const userAttributes = await apiAuthenticator.getUserAttributes(email, password);
const wallet = await AkordWallet.importFromEncBackupPhrase(password, userAttributes["custom:encBackupPhrase"]);
return new Akord({}, wallet, jwtToken);
return new Akord({ debug: true }, wallet, jwtToken);
}
6 changes: 4 additions & 2 deletions src/akord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ServiceInterface, ServiceFactory } from "./service";
import { Wallet } from "@akord/crypto";
import { reactionEmoji, actionRefs, status, objectTypes } from "./constants";
import { v4 as uuidv4 } from "uuid";
import { Logger } from "./logger";

export default class Akord {
static readonly reactionEmoji = reactionEmoji;
Expand All @@ -20,6 +21,7 @@ export default class Akord {
* @param {string} [jwtToken]
*/
constructor(config: ClientConfig, wallet?: Wallet, jwtToken?: string) {
Logger.debug = config.debug;
this.api = new ApiFactory(config, wallet, jwtToken).apiInstance();
this.service = new ServiceFactory(config.ledgerVersion, wallet, this.api).serviceInstance();
}
Expand Down Expand Up @@ -848,7 +850,7 @@ export default class Akord {
currentChunk++;
}
} catch (e) {
console.log(e);
Logger.log(e);
throw new Error(
"Failed to download. Please check your network connection." +
" Please upload the file again if problem persists and/or contact Akord support."
Expand Down Expand Up @@ -883,7 +885,7 @@ export default class Akord {
currentChunk++;
}
} catch (e) {
console.log(e);
Logger.log(e);
throw new Error(
"Failed to download. Please check your network connection." +
" Please upload the file again if problem persists and/or contact Akord support."
Expand Down
19 changes: 13 additions & 6 deletions src/api/akord/akord-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Api } from "../api";
import { awsConfig, AWSConfig } from "./aws-config";
import * as queries from "./graphql/graphql";
import { PermapostExecutor } from "./permapost";
import { Logger } from "../../logger";

let client: any;

Expand Down Expand Up @@ -45,21 +46,23 @@ export default class AkordApi extends Api {
.bundle(shouldBundleTransaction)
.metadata(item.metadata)
.uploadState()
console.log(`File uploaded successfully.`);
Logger.log("Uploaded state with id: " + resource.id);
resources[index] = resource;
}));
return resources;
};

public async postContractTransaction(contractId: string, input: any, tags: any, metadata?: any): Promise<string> {
return await new PermapostExecutor()
const txId = await new PermapostExecutor()
.env(this.config.env, this.config.domain)
.auth(this.jwtToken)
.contractId(contractId)
.input(JSON.stringify(input))
.metadata(JSON.stringify(metadata ? metadata : {}))
.tags(tags)
.transaction()
Logger.log("Uploaded contract interaction with id: " + txId);
return txId;
};

public async postLedgerTransaction(transactions: any): Promise<any> {
Expand All @@ -75,11 +78,13 @@ export default class AkordApi extends Api {
};

public async initContractId(tags: any): Promise<string> {
return await new PermapostExecutor()
const contractId = await new PermapostExecutor()
.env(this.config.env, this.config.domain)
.auth(this.jwtToken)
.tags(tags)
.contract()
Logger.log("Created contract with id: " + contractId);
return contractId;
};

public async getUserFromEmail(email: string): Promise<any> {
Expand Down Expand Up @@ -110,7 +115,7 @@ export default class AkordApi extends Api {
.progressHook(progressHook)
.cancelHook(cancelHook)
.uploadFile()
console.log(`File uploaded successfully.`);
Logger.log("Uploaded file with id: " + resource.id);

return resource;
};
Expand Down Expand Up @@ -211,7 +216,8 @@ export default class AkordApi extends Api {
});
return response;
} catch (err) {
console.log("Error while trying to mutate data", err);
Logger.log("Error while trying to mutate data");
Logger.log(err);
throw Error(JSON.stringify(err));
}
};
Expand Down Expand Up @@ -257,7 +263,8 @@ export default class AkordApi extends Api {
});
return response;
} catch (err) {
console.log("Error while trying to query data", err);
Logger.log("Error while trying to query data");
Logger.log(err);
throw new Error(JSON.stringify(err));
}
};
Expand Down
11 changes: 7 additions & 4 deletions src/api/akord/api-authenticator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'cross-fetch/polyfill';
import * as AmazonCognitoIdentity from 'amazon-cognito-identity-js';
import { EnvType } from "../../client-config";
import { awsConfig, AWSConfig } from "./aws-config";
import { Logger } from '../../logger';

export default class ApiAuthenticator {
public config!: AWSConfig;
Expand Down Expand Up @@ -45,8 +46,9 @@ export default class ApiAuthenticator {
resolve({ user: cognitoUser, session: result })
},
onFailure: function (err) {
console.log(err);
return reject;
Logger.log(err.message);
Logger.log(JSON.stringify(err));
return reject(err.message);
}
})
}
Expand All @@ -58,8 +60,9 @@ export default class ApiAuthenticator {
return new Promise((resolve, reject) => {
user.getUserAttributes(async function (err, result) {
if (err) {
console.log(err.message || JSON.stringify(err));
reject();
Logger.log(err.message);
Logger.log(JSON.stringify(err));
reject(err.message);
}
const attributes = result.reduce(function (
attributesObject,
Expand Down
3 changes: 2 additions & 1 deletion src/client-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ export interface ClientConfig {
env?: EnvType,
network?: NetworkType,
wallet?: WalletType,
ledgerVersion?: LedgerVersion
ledgerVersion?: LedgerVersion,
debug?: boolean
}

export enum EnvType {
Expand Down
9 changes: 9 additions & 0 deletions src/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export class Logger {
static debug: boolean = false;

static log = function (message: any) {
if (this.debug) {
console.debug(message);
}
};
}
3 changes: 2 additions & 1 deletion src/service/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { reactionEmoji, objectTypes } from '../constants';
import { protocolTags } from './protocol/protocol-constants';
import lodash from "lodash";
import { createThumbnail } from './thumbnail'
import { Logger } from '../logger'

declare const Buffer;
class Service implements ServiceInterface {
Expand Down Expand Up @@ -305,7 +306,7 @@ class Service implements ServiceInterface {
return await filePromise;
}
} catch (e) {
console.log(e);
Logger.log(e);
}
}

Expand Down
15 changes: 9 additions & 6 deletions src/service/thumbnail.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import loadImage from 'blueimp-load-image';
import { Logger } from '../logger';
const pdfjsLib = require("pdfjs-dist/legacy/build/pdf.js");

export const THUMBNAIL_WIDTH = 438
Expand All @@ -25,7 +26,8 @@ const makeImageThumbnail = async (fileUrl: any) => {
})

image.addEventListener('error', error => {
console.log('Make image thumbnail error', error)
Logger.log('Make image thumbnail error')
Logger.log(error)
reject(error)
})
})
Expand All @@ -38,7 +40,8 @@ const makeVideoThumbnail = async (fileUrl: string) => {
video.load()

video.addEventListener('error', error => {
console.log('Make video thumbnail error', error)
Logger.log('Make video thumbnail error')
Logger.log(error)
reject('Make video thumbnail error: ' + error)
})

Expand Down Expand Up @@ -103,7 +106,7 @@ export const createThumbnail = async (file: any) => {
try {
thumbnail = await makePdfThumbnail(fileUrl)
} catch (error) {
console.log('PDF thumbnail error: ' + error)
Logger.log('PDF thumbnail error: ' + error)
}
break
case 'image/jpeg':
Expand All @@ -113,19 +116,19 @@ export const createThumbnail = async (file: any) => {
try {
thumbnail = await makeImageThumbnail(fileUrl)
} catch (error) {
console.log('Image thumbnail error: ' + error)
Logger.log('Image thumbnail error: ' + error)
}
break
case 'video/mp4':
case 'video/quicktime':
try {
thumbnail = await makeVideoThumbnail(fileUrl)
} catch (error) {
console.log('Video thumbnail error: ' + error)
Logger.log('Video thumbnail error: ' + error)
}
break
default:
console.log('Thumbnail not supported for this file type: ' + file.type)
Logger.log('Thumbnail not supported for this file type: ' + file.type)
return null;
break
}
Expand Down

0 comments on commit c01553f

Please sign in to comment.