From 9b62b1406930c433bef17ad08ee398337bd10660 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Camith-skyflow=E2=80=9D?= <“amit@skyflow.com”> Date: Fri, 25 Oct 2024 17:46:56 +0530 Subject: [PATCH] SK-1621: add log support --- samples/vault-api/credentials-options.ts | 81 +++++++++++++++++++ samples/vault-api/data-residency.ts | 6 +- .../{Delete.ts => delete-records.ts} | 0 .../{detokenzie.ts => detokenzie-records.ts} | 0 samples/vault-api/{Get.ts => get-records.ts} | 0 .../{Insert.ts => insert-records.ts} | 2 +- .../{invoke.ts => invoke-connection.ts} | 0 .../vault-api/{query.ts => query-records.ts} | 0 .../{tokenize.ts => tokenize-records.ts} | 0 .../vault-api/{Update.ts => update-record.ts} | 0 src/utils/index.ts | 14 +++- src/utils/logs/index.ts | 29 +++++-- src/vault/client/index.ts | 4 + src/vault/controller/vault/index.ts | 50 ++++++------ src/vault/skyflow/index.ts | 11 ++- test/vault/skyflow/skyflow.test.js | 7 ++ test/vault/utils/utils.test.js | 23 +++--- 17 files changed, 175 insertions(+), 52 deletions(-) create mode 100644 samples/vault-api/credentials-options.ts rename samples/vault-api/{Delete.ts => delete-records.ts} (100%) rename samples/vault-api/{detokenzie.ts => detokenzie-records.ts} (100%) rename samples/vault-api/{Get.ts => get-records.ts} (100%) rename samples/vault-api/{Insert.ts => insert-records.ts} (99%) rename samples/vault-api/{invoke.ts => invoke-connection.ts} (100%) rename samples/vault-api/{query.ts => query-records.ts} (100%) rename samples/vault-api/{tokenize.ts => tokenize-records.ts} (100%) rename samples/vault-api/{Update.ts => update-record.ts} (100%) diff --git a/samples/vault-api/credentials-options.ts b/samples/vault-api/credentials-options.ts new file mode 100644 index 0000000..9fbf543 --- /dev/null +++ b/samples/vault-api/credentials-options.ts @@ -0,0 +1,81 @@ +import { DeleteRequest, Env, LogLevel, Skyflow } from "skyflow-node"; + +//NOTE : If you don't specify credentials at config level credentials or skyflow credentials, SDK will check SKYFLOW_CREDENTIALS key in your env +// To generate Bearer Token from credentials string. +const cred = { + clientID: '', + clientName: '', + keyID: '', + tokenURI: '', + privateKey: '', +}; + +// please pass one of apiKey, token, credentialsString & path +const skyflowCredentials = { + credentialsString: JSON.stringify(cred) +} + +const credentials = { + token: "BEARER_TOKEN", // bearer token + // apiKey: "API_KEY", //API_KEY + // path: "PATH", //PATH to credentials json + // credentialsString: "CREDENTIAL_STRING", // credentials as string +} + +const skyflow_client = new Skyflow({ + vaultConfigs: [ + { + vaultId: "VAULT_ID1", // primary vault + clusterId: "CLUSTER_ID1", // ID from your vault URL Eg https://{clusterId}.vault.skyflowapis.com + env: Env.PROD, // Env by deault it is set to PROD + }, + { + vaultId: "VAULT_ID2", // primary vault + clusterId: "CLUSTER_ID2", // ID from your vault URL Eg https://{clusterId}.vault.skyflowapis.com + env: Env.PROD, // Env by deault it is set to PROD + credentials: credentials, + } + ], + skyflowCredentials: skyflowCredentials, // skyflow credentials will be used if no individual creds are passed + logLevel:LogLevel.ERROR // set loglevel by deault it is set to PROD +}); + +const primaryDeleteIds = [ + 'SKYFLOW_ID1', + 'SKYFLOW_ID2', + 'SKYFLOW_ID3', +] + +const primaryDeleteRequest = new DeleteRequest( + "TABLE_NAME1", // TABLE_NAME + primaryDeleteIds +); + +// VAULT_ID1 will use skyflowCredentials if you don't specify individual credentials at config level +skyflow_client.vault("VAULT_ID1").delete( + primaryDeleteRequest +).then(resp=>{ + console.log(resp); +}).catch(err=>{ + console.log(JSON.stringify(err)); +}); + +const secondaryDeleteIds = [ + 'SKYFLOW_ID4', + 'SKYFLOW_ID5', + 'SKYFLOW_ID6', +] + +const secondaryDeleteRequest = new DeleteRequest( + "TABLE_NAME2", // TABLE_NAME + secondaryDeleteIds +); + +// VAULT_ID1 will use individual credentials at config level +skyflow_client.vault("VAULT_ID2").delete( + secondaryDeleteRequest +).then(resp=>{ + console.log(resp); +}).catch(err=>{ + console.log(JSON.stringify(err)); +}); \ No newline at end of file diff --git a/samples/vault-api/data-residency.ts b/samples/vault-api/data-residency.ts index 6973788..f1aeb5e 100644 --- a/samples/vault-api/data-residency.ts +++ b/samples/vault-api/data-residency.ts @@ -32,12 +32,12 @@ const skyflow_client = new Skyflow({ logLevel:LogLevel.ERROR // set loglevel by deault it is set to PROD }); -//add vault config from Sandbox env +//add vault config from prod env skyflow_client.addVaultConfig( { vaultId: "VAULT_ID2", // secondary vault clusterId: "CLUSTER_ID2", // ID from your vault URL Eg https://{clusterId}.vault.skyflowapis.com - env: Env.SANDBOX, // Env by deault it is set to PROD + env: Env.PROD, // Env by deault it is set to PROD // if you dont specify individual creds, skyflow creds will be used } ); @@ -61,7 +61,7 @@ skyflow_client.vault("VALUT_ID1").get(getRequest) console.log(getResponse.data); const insertData = getResponse.data; //parse insertData to remove skyflow_id - //get data from prod vault and insert data to SANDBOX vault + //get data from one vault and insert data to another vault const insertRequest = new InsertRequest( "TABLE_NAME", insertData!, diff --git a/samples/vault-api/Delete.ts b/samples/vault-api/delete-records.ts similarity index 100% rename from samples/vault-api/Delete.ts rename to samples/vault-api/delete-records.ts diff --git a/samples/vault-api/detokenzie.ts b/samples/vault-api/detokenzie-records.ts similarity index 100% rename from samples/vault-api/detokenzie.ts rename to samples/vault-api/detokenzie-records.ts diff --git a/samples/vault-api/Get.ts b/samples/vault-api/get-records.ts similarity index 100% rename from samples/vault-api/Get.ts rename to samples/vault-api/get-records.ts diff --git a/samples/vault-api/Insert.ts b/samples/vault-api/insert-records.ts similarity index 99% rename from samples/vault-api/Insert.ts rename to samples/vault-api/insert-records.ts index 4ae72c7..1fcf44f 100644 --- a/samples/vault-api/Insert.ts +++ b/samples/vault-api/insert-records.ts @@ -40,7 +40,7 @@ const insertData = [ const insertReq = new InsertRequest( "TABLE_NAME", - insertData + insertData, ) const insertOptions = new InsertOptions() diff --git a/samples/vault-api/invoke.ts b/samples/vault-api/invoke-connection.ts similarity index 100% rename from samples/vault-api/invoke.ts rename to samples/vault-api/invoke-connection.ts diff --git a/samples/vault-api/query.ts b/samples/vault-api/query-records.ts similarity index 100% rename from samples/vault-api/query.ts rename to samples/vault-api/query-records.ts diff --git a/samples/vault-api/tokenize.ts b/samples/vault-api/tokenize-records.ts similarity index 100% rename from samples/vault-api/tokenize.ts rename to samples/vault-api/tokenize-records.ts diff --git a/samples/vault-api/Update.ts b/samples/vault-api/update-record.ts similarity index 100% rename from samples/vault-api/Update.ts rename to samples/vault-api/update-record.ts diff --git a/src/utils/index.ts b/src/utils/index.ts index 5df8cd8..d8ad2ba 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -177,6 +177,7 @@ export async function getBearerToken(credentials?: Credentials, logLevel?: LogLe } // If credentials are missing but environment variable exists, use it if (!credentials && process.env.SKYFLOW_CREDENTIALS) { + printLog(logs.infoLogs.USING_SKYFLOW_CREDENTIALS_ENV, MessageType.LOG, logLevel); credentials = { credentialsString: process.env.SKYFLOW_CREDENTIALS } @@ -184,11 +185,13 @@ export async function getBearerToken(credentials?: Credentials, logLevel?: LogLe // If token already exists, resolve immediately if (credentials?.apiKey && credentials.apiKey.trim().length > 0) { + printLog(logs.infoLogs.USING_API_KEY, MessageType.LOG, logLevel); return { type: AuthType.API_KEY, key: credentials.apiKey }; } // If token already exists, resolve immediately if (credentials?.token) { + printLog(logs.infoLogs.USING_BEARER_TOKEN, MessageType.LOG, logLevel); return { type: AuthType.TOKEN, key: validateToken(credentials.token) }; } @@ -197,6 +200,8 @@ export async function getBearerToken(credentials?: Credentials, logLevel?: LogLe // Generate token based on provided credentials const token = await getToken(credentials, logLevel); + printLog(logs.infoLogs.BEARER_TOKEN_RESOLVED, MessageType.LOG, logLevel); + return { type: AuthType.TOKEN, key: token.accessToken }; } catch (err) { @@ -255,18 +260,19 @@ export const printLog = (message: string, messageType: MessageType, logLevel: Lo const { showDebugLogs, showInfoLogs, showWarnLogs, showErrorLogs, } = LogLevelOptions[logLevel]; + const version = sdkDetails?.version ? `v${sdkDetails?.version}` : ''; if (messageType === MessageType.LOG && showDebugLogs) { // eslint-disable-next-line no-console - console.log("DEBUG: [Skyflow] " + message); + console.log(`DEBUG: [Skyflow Node SDK ${version}] ` + message); } else if (messageType === MessageType.LOG && showInfoLogs) { // eslint-disable-next-line no-console - console.log("INFO: [Skyflow] " + message); + console.log(`INFO: [Skyflow Node SDK ${version}] ` + message); } else if (messageType === MessageType.WARN && showWarnLogs) { // eslint-disable-next-line no-console - console.warn("WARN: [Skyflow] " + message); + console.warn(`WARN: [Skyflow Node SDK ${version}] ` + message); } else if (messageType === MessageType.ERROR && showErrorLogs) { // eslint-disable-next-line no-console - console.error("ERROR: [Skyflow] " + message); + console.error(`ERROR: [Skyflow Node SDK ${version}] ` + message); } }; diff --git a/src/utils/logs/index.ts b/src/utils/logs/index.ts index ba051a4..f699975 100644 --- a/src/utils/logs/index.ts +++ b/src/utils/logs/index.ts @@ -6,6 +6,15 @@ const logs = { GENERATE_BEARER_TOKEN_SUCCESS: "BearerToken is generated", GENERATE_SIGNED_DATA_TOKEN_SUCCESS: 'Signed Data tokens are generated', INITIALIZE_CLIENT: 'Initializing skyflow client.', + VALIDATING_VAULT_CONFIG: 'Validating vault config.', + VALIDATING_CONNECTION_CONFIG: 'Validating connection config.', + VAULT_CONTROLLER_INITIALIZED: 'Initialized vault controller with vault ID %s1.', + CONNECTION_CONTROLLER_INITIALIZED: 'Intitialized connection controller with connection ID %s1.', + VAULT_ID_CONFIG_EXISTS: 'Vault config with vault ID %s1 already exists.', + VAULT_ID_CONFIG_DOES_NOT_EXIST: `Vault config with vault ID %s1 doesn't exist.`, + CONNECTION_ID_CONFIG_EXISTS: `Connection config with connection ID %s1 already exists.`, + CONNECTION_ID_CONFIG_DOES_NOT_EXIST: `Connection config with connection ID %s1 doesn't exist.`, + CURRENT_LOG_LEVEL: 'Current log level is %s1.', CLIENT_INITIALIZED: 'Initialized skyflow client successfully.', VALIDATE_INSERT_INPUT: 'Validating insert input.', VALIDATE_DETOKENIZE_INPUT: 'Validating detokenize input.', @@ -16,12 +25,18 @@ const logs = { VALIDATE_DELETE_INPUT: 'Validating delete method input.', VALIDATE_UPDATE_INPUT: 'Validating update method input.', VALIDATE_CONNECTION_CONFIG: 'Validating connection config.', - INSERT_DATA_SUCCESS: 'Data has been inserted successfully.', - DETOKENIZE_SUCCESS: 'Data has been revealed successfully.', - GET_BY_ID_SUCCESS: 'Data has been revealed successfully.', + INSERT_DATA_SUCCESS: 'Data inserted.', + FILE_UPLOAD_DATA_SUCCESS: 'File uploaded.', + DETOKENIZE_SUCCESS: 'Data detokenized.', + GET_SUCCESS: 'Data revealed.', + UPDATE_SUCCESS: 'Data updated.', + DELETE_SUCCESS: 'Data deleted.', + TOKENIZE_SUCCESS: 'Data tokenized.', + QUERY_SUCCESS: 'Query executed.', BEARER_TOKEN_LISTENER: 'Get bearer token listener added.', BEARER_TOKEN_RESOLVED: 'GetBearerToken promise resolved successfully.', - REUSE_BEARER_TOKEN: 'Reusing the bearer token.', + REUSE_BEARER_TOKEN: 'Reusing bearer token.', + REUSE_API_KEY: 'Reusing api key.', CONTROLLER_INITIALIZED: 'SkyflowController initialized.', INSERT_TRIGGERED: 'Insert method triggered.', DETOKENIZE_TRIGGERED: 'Detokenize method triggered.', @@ -44,8 +59,10 @@ const logs = { GENERATE_SIGNED_DATA_TOKENS_TRIGGERED: "generateSignedDataTokens is triggered", UPDATE_TRIGGERED: 'Update method triggered.', UPDATE_REQUEST_RESOLVED: 'Update request is resolved.', - UNABLE_TO_GENERATE_SDK_METRIC: 'Unable to generate %s1 metric.' - + UNABLE_TO_GENERATE_SDK_METRIC: 'Unable to generate %s1 metric.', + USING_BEARER_TOKEN: 'Using token from credentials', + USING_API_KEY: 'Using api key from credentials', + USING_SKYFLOW_CREDENTIALS_ENV: 'Using SKYFLOW_CREDENTIALS from env' }, errorLogs: { diff --git a/src/vault/client/index.ts b/src/vault/client/index.ts index 803ae41..d123316 100644 --- a/src/vault/client/index.ts +++ b/src/vault/client/index.ts @@ -4,6 +4,7 @@ import SkyflowError from "../../error"; import errorMessages from "../../error/messages"; import { AuthInfo, AuthType, LogLevel, MessageType, printLog, TYPES } from "../../utils/index"; import { isExpired } from "../../utils/jwt-utils"; +import logs from "../../utils/logs"; import Credentials from "../config/credentials"; class VaultClient { @@ -84,8 +85,10 @@ class VaultClient { if (this.authInfo?.key && !this.updateTriggered) { switch (this.authInfo.type) { case AuthType.API_KEY: + printLog(logs.infoLogs.REUSE_API_KEY, MessageType.LOG, this.logLevel); return { apiKey: this.authInfo.key } as Credentials; case AuthType.TOKEN: + printLog(logs.infoLogs.REUSE_BEARER_TOKEN, MessageType.LOG, this.logLevel); if (!isExpired(this.authInfo.key)) { return { token: this.authInfo.key } as Credentials; } @@ -154,6 +157,7 @@ class VaultClient { grpcCode?: number, details?: any ) { + printLog(description, MessageType.ERROR, this.getLogLevel()); reject(new SkyflowError({ http_code: err?.response?.status || 400, message: description, diff --git a/src/vault/controller/vault/index.ts b/src/vault/controller/vault/index.ts index 5e1531d..c1dcaf4 100644 --- a/src/vault/controller/vault/index.ts +++ b/src/vault/controller/vault/index.ts @@ -230,17 +230,18 @@ class VaultController { : this.client.vaultAPI.recordServiceInsertRecord(this.client.vaultId, tableName, requestBody as RecordServiceInsertRecordBody, headers), operationType ).then((resp: any) => { + printLog(logs.infoLogs.INSERT_DATA_SUCCESS, MessageType.LOG, this.client.getLogLevel()); const parsedResponse = isContinueOnError ? this.parseInsertBatchResponse(resp) : this.parseBulkInsertResponse(resp); resolve(parsedResponse); }) .catch(error => { - if (error instanceof Error) - printLog(error.message, MessageType.ERROR, this.client.getLogLevel()); reject(error); }); } catch (error) { + if (error instanceof Error) + printLog(error.message, MessageType.ERROR, this.client.getLogLevel()); reject(error); } }); @@ -272,6 +273,7 @@ class VaultController { ), TYPES.UPDATE ).then(data => { + printLog(logs.infoLogs.UPDATE_SUCCESS, MessageType.LOG, this.client.getLogLevel()); const updatedRecord = { skyflowId: data.skyflow_id, ...data?.tokens @@ -279,12 +281,11 @@ class VaultController { resolve(new UpdateResponse({ updatedField: updatedRecord, errors: [] })); }) .catch(error => { - if (error instanceof Error) - printLog(error.message, MessageType.ERROR, this.client.getLogLevel()); - // throw Skyflow Error reject(error); }); } catch (error) { + if (error instanceof Error) + printLog(error.message, MessageType.ERROR, this.client.getLogLevel()); reject(error); } }); @@ -311,14 +312,15 @@ class VaultController { ), TYPES.DELETE ).then(data => { + printLog(logs.infoLogs.DELETE_SUCCESS, MessageType.LOG, this.client.getLogLevel()); resolve(data); }) .catch(error => { - if (error instanceof Error) - printLog(error.message, MessageType.ERROR, this.client.getLogLevel()); reject(error); }); } catch (error: any) { + if (error instanceof Error) + printLog(error.message, MessageType.ERROR, this.client.getLogLevel()); reject(error); } }); @@ -368,18 +370,18 @@ class VaultController { ), TYPES.GET ).then(records => { + printLog(logs.infoLogs.GET_SUCCESS, MessageType.LOG, this.client.getLogLevel()); const processedRecords = records.map(record => ({ ...record.fields, })); resolve(new GetResponse({ data: processedRecords, errors: [] })); }) .catch(error => { - if (error instanceof Error) - printLog(error.message, MessageType.ERROR, this.client.getLogLevel()); - // throw Skyflow Error reject(error); }); } catch (error) { + if (error instanceof Error) + printLog(error.message, MessageType.ERROR, this.client.getLogLevel()); reject(error); } }); @@ -410,15 +412,15 @@ class VaultController { ), TYPES.FILE_UPLOAD ).then(data => { + printLog(logs.infoLogs.FILE_UPLOAD_DATA_SUCCESS, MessageType.LOG, this.client.getLogLevel()); resolve(new FileUploadResponse({ skyflowId: data.skyflow_id, errors: [] })); }) .catch(error => { - if (error instanceof Error) - printLog(error.message, MessageType.ERROR, this.client.getLogLevel()); - // throw Skyflow Error reject(error); }); } catch (error) { + if (error instanceof Error) + printLog(error.message, MessageType.ERROR, this.client.getLogLevel()); reject(error); } }); @@ -445,6 +447,7 @@ class VaultController { ), TYPES.QUERY ).then(records => { + printLog(logs.infoLogs.QUERY_SUCCESS, MessageType.LOG, this.client.getLogLevel()); const processedRecords = records.map(record => ({ ...record?.fields, tokenizedData: { @@ -454,12 +457,11 @@ class VaultController { resolve(new QueryResponse({ fields: processedRecords, errors: [] })); }) .catch(error => { - if (error instanceof Error) - printLog(error.message, MessageType.ERROR, this.client.getLogLevel()); - // throw Skyflow Error reject(error); }); } catch (error) { + if (error instanceof Error) + printLog(error.message, MessageType.ERROR, this.client.getLogLevel()); reject(error); } }); @@ -481,16 +483,16 @@ class VaultController { (headers: RawAxiosRequestConfig | undefined) => this.client.tokensAPI.recordServiceDetokenize(this.client.vaultId, detokenizePayload, headers), TYPES.DETOKENIZE ).then(records => { + printLog(logs.infoLogs.DETOKENIZE_SUCCESS, MessageType.LOG, this.client.getLogLevel()); const parsedResponse: ParsedDetokenizeResponse = this.parseDetokenizeResponse(records); resolve(new DetokenizeResponse({ detokenizedFields: parsedResponse.success, errors: parsedResponse.errors })); }) .catch(error => { - if (error instanceof Error) - printLog(error.message, MessageType.ERROR, this.client.getLogLevel()); - // throw Skyflow Error reject(error); }); } catch (error) { + if (error instanceof Error) + printLog(error.message, MessageType.ERROR, this.client.getLogLevel()); reject(error); } }); @@ -511,14 +513,16 @@ class VaultController { this.handleRequest( () => this.client.tokensAPI.recordServiceTokenize(this.client.vaultId, tokenizePayload), TYPES.TOKENIZE - ).then(records => resolve(new TokenizeResponse({ tokens: records, errors: [] }))) + ).then(records => { + printLog(logs.infoLogs.TOKENIZE_SUCCESS, MessageType.LOG, this.client.getLogLevel()); + resolve(new TokenizeResponse({ tokens: records, errors: [] })) + }) .catch(error => { - if (error instanceof Error) - printLog(error.message, MessageType.ERROR, this.client.getLogLevel()); - // throw Skyflow Error reject(error); }); } catch (error) { + if (error instanceof Error) + printLog(error.message, MessageType.ERROR, this.client.getLogLevel()); reject(error); } }); diff --git a/src/vault/skyflow/index.ts b/src/vault/skyflow/index.ts index d50411c..1ac829d 100644 --- a/src/vault/skyflow/index.ts +++ b/src/vault/skyflow/index.ts @@ -1,4 +1,4 @@ -import { CONNECTION_ID, CREDENTIALS, Env, getVaultURL, ISkyflowError, LOGLEVEL, LogLevel, MessageType, printLog, VAULT_ID } from "../../utils"; +import { CONNECTION_ID, CREDENTIALS, Env, getVaultURL, ISkyflowError, LOGLEVEL, LogLevel, MessageType, parameterizedString, printLog, VAULT_ID } from "../../utils"; import ConnectionConfig from "../config/connection"; import VaultConfig from "../config/vault"; import { SkyflowConfig, ClientObj } from "../types"; @@ -23,19 +23,22 @@ class Skyflow { constructor(config: SkyflowConfig) { validateSkyflowConfig(config); + printLog(logs.infoLogs.INITIALIZE_CLIENT, MessageType.LOG, this.logLevel); if (config?.logLevel && !isLogLevel(config?.logLevel)) throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_LOG_LEVEL); this.logLevel = config.logLevel || LogLevel.ERROR; - printLog(logs.infoLogs.INITIALIZE_CLIENT, MessageType.LOG, this.logLevel); + printLog(parameterizedString(logs.infoLogs.CURRENT_LOG_LEVEL,[this.logLevel]), MessageType.LOG, this.logLevel); if (config?.skyflowCredentials) validateSkyflowCredentials(config.skyflowCredentials) this.commonCredentials = config?.skyflowCredentials; + printLog(logs.infoLogs.VALIDATING_VAULT_CONFIG, MessageType.LOG, this.logLevel); config.vaultConfigs.map(vaultConfig => { this.addVaultConfig(vaultConfig); }); + printLog(logs.infoLogs.VALIDATING_CONNECTION_CONFIG, MessageType.LOG, this.logLevel); config.connectionConfigs?.map(connectionConfig => { this.addConnectionConfig(connectionConfig); }); @@ -47,12 +50,14 @@ class Skyflow { const vaultUrl = getVaultURL(config.clusterId, env); const client = new VaultClient(vaultUrl, config.vaultId, config?.credentials, this.commonCredentials, this.logLevel); const controller = new VaultController(client); + printLog(parameterizedString(logs.infoLogs.VAULT_CONTROLLER_INITIALIZED, [config.vaultId]), MessageType.LOG, this.logLevel); clients[config.vaultId] = { config, client, controller }; } private addConnectionClient(config: ConnectionConfig, clients: ClientObj) { const client = new VaultClient(config.connectionUrl, '', config?.credentials, this.commonCredentials, this.logLevel); const controller = new ConnectionController(client); + printLog(parameterizedString(logs.infoLogs.CONNECTION_CONTROLLER_INITIALIZED, [config.connectionId]), MessageType.LOG, this.logLevel); clients[config.connectionId] = { config, client, controller }; } @@ -130,6 +135,7 @@ class Skyflow { [CONNECTION_ID]: SKYFLOW_ERROR_CODE.CONNECTION_ID_EXITS_IN_CONFIG_LIST, }; if(Object.keys(clients).includes(id)){ + printLog(parameterizedString(logs.infoLogs[`${idKey}_CONFIG_EXISTS`], [id]), MessageType.LOG, this.logLevel); this.throwSkyflowError(idKey, errorMapping, [id]); } } @@ -139,6 +145,7 @@ class Skyflow { [VAULT_ID]: SKYFLOW_ERROR_CODE.VAULT_ID_NOT_IN_CONFIG_LIST, [CONNECTION_ID]: SKYFLOW_ERROR_CODE.CONNECTION_ID_NOT_IN_CONFIG_LIST, }; + printLog(parameterizedString(logs.infoLogs[`${idKey}_CONFIG_DOES_NOT_EXIST`], [id]), MessageType.LOG, this.logLevel); this.throwSkyflowError(idKey, errorMapping, [id]); } diff --git a/test/vault/skyflow/skyflow.test.js b/test/vault/skyflow/skyflow.test.js index 6677b95..afaabb2 100644 --- a/test/vault/skyflow/skyflow.test.js +++ b/test/vault/skyflow/skyflow.test.js @@ -273,6 +273,13 @@ describe('Skyflow initialization', () => { expect(() => skyflow.vault("ID")).toThrowError(noConfigFound); }); + test('should throw error when getting vault config with empty vaultID', () => { + const skyflow = new Skyflow({ + vaultConfigs: [validVaultConfig], + }); + expect(() => skyflow.getVaultConfig()).toThrowError(invalidVaultIdError); + }); + test('should throw error when adding vault config', () => { const skyflow = new Skyflow({ vaultConfigs: [validVaultConfig], diff --git a/test/vault/utils/utils.test.js b/test/vault/utils/utils.test.js index 417c318..a62dda1 100644 --- a/test/vault/utils/utils.test.js +++ b/test/vault/utils/utils.test.js @@ -308,9 +308,6 @@ describe('generateSDKMetrics With errors', () => { Object.defineProperty(sdkDetails, 'name', { get: () => { throw new Error('mock error'); } }); - Object.defineProperty(sdkDetails, 'version', { - get: () => { throw new Error('mock error'); } - }); const metrics = generateSDKMetrics(logLevel); @@ -332,52 +329,52 @@ describe('printLog', () => { test('should log DEBUG messages when level is DEBUG', () => { printLog('This is a debug message', MessageType.LOG, LogLevel.DEBUG); - expect(console.log).toHaveBeenCalledWith("DEBUG: [Skyflow] This is a debug message"); + expect(console.log).toHaveBeenCalledWith(`DEBUG: [Skyflow Node SDK v${sdkDetails.version}] This is a debug message`); }); test('should log INFO messages when level is DEBUG', () => { printLog('This is an info message', MessageType.LOG, LogLevel.DEBUG); - expect(console.log).toHaveBeenCalledWith("DEBUG: [Skyflow] This is an info message"); + expect(console.log).toHaveBeenCalledWith(`DEBUG: [Skyflow Node SDK v${sdkDetails.version}] This is an info message`); }); test('should log WARN messages when level is DEBUG', () => { printLog('This is a warning message', MessageType.WARN, LogLevel.DEBUG); - expect(console.warn).toHaveBeenCalledWith("WARN: [Skyflow] This is a warning message"); + expect(console.warn).toHaveBeenCalledWith(`WARN: [Skyflow Node SDK v${sdkDetails.version}] This is a warning message`); }); test('should log ERROR messages when level is DEBUG', () => { printLog('This is an error message', MessageType.ERROR, LogLevel.DEBUG); - expect(console.error).toHaveBeenCalledWith("ERROR: [Skyflow] This is an error message"); + expect(console.error).toHaveBeenCalledWith(`ERROR: [Skyflow Node SDK v${sdkDetails.version}] This is an error message`); }); test('should log INFO messages when level is INFO', () => { printLog('This is an info message', MessageType.LOG, LogLevel.INFO); - expect(console.log).toHaveBeenCalledWith("INFO: [Skyflow] This is an info message"); + expect(console.log).toHaveBeenCalledWith(`INFO: [Skyflow Node SDK v${sdkDetails.version}] This is an info message`); }); test('should log WARN messages when level is INFO', () => { printLog('This is a warning message', MessageType.WARN, LogLevel.INFO); - expect(console.warn).toHaveBeenCalledWith("WARN: [Skyflow] This is a warning message"); + expect(console.warn).toHaveBeenCalledWith(`WARN: [Skyflow Node SDK v${sdkDetails.version}] This is a warning message`); }); test('should log ERROR messages when level is INFO', () => { printLog('This is an error message', MessageType.ERROR, LogLevel.INFO); - expect(console.error).toHaveBeenCalledWith("ERROR: [Skyflow] This is an error message"); + expect(console.error).toHaveBeenCalledWith(`ERROR: [Skyflow Node SDK v${sdkDetails.version}] This is an error message`); }); test('should log WARN messages when level is WARN', () => { printLog('This is a warning message', MessageType.WARN, LogLevel.WARN); - expect(console.warn).toHaveBeenCalledWith("WARN: [Skyflow] This is a warning message"); + expect(console.warn).toHaveBeenCalledWith(`WARN: [Skyflow Node SDK v${sdkDetails.version}] This is a warning message`); }); test('should log ERROR messages when level is WARN', () => { printLog('This is an error message', MessageType.ERROR, LogLevel.WARN); - expect(console.error).toHaveBeenCalledWith("ERROR: [Skyflow] This is an error message"); + expect(console.error).toHaveBeenCalledWith(`ERROR: [Skyflow Node SDK v${sdkDetails.version}] This is an error message`); }); test('should log ERROR messages when level is ERROR', () => { printLog('This is an error message', MessageType.ERROR, LogLevel.ERROR); - expect(console.error).toHaveBeenCalledWith("ERROR: [Skyflow] This is an error message"); + expect(console.error).toHaveBeenCalledWith(`ERROR: [Skyflow Node SDK v${sdkDetails.version}] This is an error message`); }); test('should not log anything when level is OFF', () => {