Skip to content

Commit

Permalink
Make fs dynamically imported
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesliupenn committed May 17, 2024
1 parent 821b3c1 commit 2b7e1a8
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 14 deletions.
10 changes: 8 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dimo-network/dimo-node-sdk",
"version": "1.1.3",
"version": "1.1.4",
"description": "DIMO SDK for JavaScript",
"main": "dist/index.js",
"author": "James Li",
Expand All @@ -24,6 +24,7 @@
},
"dependencies": {
"axios": "^1.6.7",
"fs": "^0.0.1-security",
"web3": "^4.6.0"
},
"devDependencies": {
Expand Down
41 changes: 32 additions & 9 deletions src/dimo.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { DimoEnvironment } from './environments';
import fs from 'fs';

import {
Identity,
Expand Down Expand Up @@ -54,13 +53,37 @@ export class DIMO {

// Helper Function
async authenticate() {
const credentials = JSON.parse(fs.readFileSync('.credentials.json', 'utf8'));
// Call getToken with credentials
const authHeader = await this.auth.getToken({
client_id: credentials.client_id,
domain: credentials.redirect_uri,
private_key: credentials.private_key,
});
return authHeader;
let fs;
try {
// Dynamically import fs
if (typeof process !== 'undefined' && process.versions && process.versions.node) {
fs = await import('fs');
} else {
// Optionally handle the case where 'fs' is not available, returns null
console.log('Not in Node.js environment; `fs` module is not available.');
return null;
}

if (!fs.existsSync('.credentials.json')) {
throw new Error('Credentials file does not exist');
}

const data = fs.readFileSync('.credentials.json', 'utf8');
const credentials = JSON.parse(data);

const authHeader = await this.auth.getToken({
client_id: credentials.client_id,
domain: credentials.redirect_uri,
private_key: credentials.private_key,
});

return authHeader;

} catch (error: any) {
// Handle file not existing and other errors
console.error('Failed to authenticate:', error.message);
// Decide whether to throw the error or handle it differently
throw new Error('Authentication failed');
}
}
}
5 changes: 3 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
"strict": true, /* Enable all strict type-checking options. */
"noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */
"skipLibCheck": true /* Skip type checking all .d.ts files. */
"skipLibCheck": true, /* Skip type checking all .d.ts files. */
"types": ["node"]
},
"include": [
"src"
"src", "node"
],
"exclude": [],
"ts-node": {
Expand Down

0 comments on commit 2b7e1a8

Please sign in to comment.