Skip to content

Commit

Permalink
Merge pull request #116 from skyflowapi/release/23.7.1
Browse files Browse the repository at this point in the history
SK-861/Release/23.7.1
  • Loading branch information
skyflow-vivek authored Jul 19, 2023
2 parents 503a20e + f481aec commit 89db705
Show file tree
Hide file tree
Showing 11 changed files with 711 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "skyflow-node",
"version": "1.10.0",
"version": "1.10.0-dev.9757c98",
"description": "Skyflow SDK for Node.js",
"main": "./lib/index.js",
"module": "./lib/index.js",
Expand Down
31 changes: 30 additions & 1 deletion src/vault-api/Controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
import Client from './client';

import {
validateConnectionConfig, validateInsertRecords, validateDetokenizeInput, validateGetByIdInput, validateInitConfig, validateUpsertOptions, validateUpdateInput, validateGetInput,
validateConnectionConfig, validateInsertRecords, validateDetokenizeInput, validateGetByIdInput, validateInitConfig, validateUpsertOptions, validateUpdateInput, validateGetInput, validateDeleteInputAndOptions
} from './utils/validators';

import {
ContentType,
IDeleteInput,
IDeleteOptions,
IGetInput,
IInsertOptions,
IUpdateInput,
Expand Down Expand Up @@ -40,6 +42,7 @@ import {
import { isTokenValid } from './utils/jwt-utils';
import SKYFLOW_ERROR_CODE from './utils/constants';
import SkyflowError from './libs/SkyflowError';
import { deleteRecordsBySkyflowID } from './core/Delete';

class Controller {
#client: Client;
Expand Down Expand Up @@ -243,6 +246,32 @@ class Controller {
});
}

delete(deleteInput: IDeleteInput, options?: IDeleteOptions) {
return new Promise((resolve, reject) => {
try {
validateInitConfig(this.#client.config);
validateDeleteInputAndOptions(deleteInput, options);
this.getToken().then((authToken) => {
deleteRecordsBySkyflowID(deleteInput.records, options = {}, this.#client, authToken)
.then((response) => {
printLog(logs.infoLogs.DELETE_REQUEST_RESOLVED, MessageType.LOG);
resolve(response);
}).catch((error) => {
printLog(logs.errorLogs.DELETE_REQUEST_REJECTED, MessageType.ERROR);
reject(error);
});
}).catch((err) => {
reject(err);
})

} catch (error) {
if (error instanceof Error)
printLog(error.message, MessageType.ERROR);
reject(error);
}
})
}

insertData(records, options) {
const requestBody = constructInsertRecordRequest(records, options);
return new Promise((rootResolve, rootReject) => {
Expand Down
7 changes: 7 additions & 0 deletions src/vault-api/Skyflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import {
IUpdateInput,
IUpdateOptions,
IGetInput,
IDeleteInput,
IDeleteOptions,
} from './utils/common';
import { formatVaultURL } from './utils/helpers';

Expand Down Expand Up @@ -94,6 +96,11 @@ class Skyflow {
return this.#Controller.update(updateInput,options);
}

delete(deleteInput: IDeleteInput, options?: IDeleteOptions) {
printLog(logs.infoLogs.UPDATE_TRIGGERED, MessageType.LOG);
return this.#Controller.delete(deleteInput, options)
}

static get RedactionType() {
return RedactionType;
}
Expand Down
83 changes: 83 additions & 0 deletions src/vault-api/core/Delete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import Client from "../client";
import SkyflowError from "../libs/SkyflowError";
import { IDeleteOptions, IDeleteRecord } from "../utils/common";

const formatForPureJsFailure = (cause, skyflowId: string) => ({
id: skyflowId,
...new SkyflowError({
code: cause?.error?.code,
description: cause?.error?.description,
}, [], true),
});

const deleteRecordInVault = (
deleteRecord: IDeleteRecord,
options: IDeleteOptions,
client: Client,
authToken: string,
): Promise<any> => {
const vaultEndPointURL: string = `${client.config.vaultURL}/v1/vaults/${client.config.vaultID}/${deleteRecord.table}/${deleteRecord.id}`;
return client.request({
requestMethod: 'DELETE',
url: vaultEndPointURL,
headers: {
Authorization: `Bearer ${authToken}`,
'Content-Type': 'application/json',
}
});
};

/** Delete By Skyflow ID */
export const deleteRecordsBySkyflowID = (
deleteRecords: IDeleteRecord[],
options: IDeleteOptions,
client: Client,
authToken: String
) => {
return new Promise((rootResolve, rootReject) => {
const vaultResponseSet: Promise<any>[] = deleteRecords.map(
(deleteRecord) =>
new Promise((resolve) => {
const deleteResponse: any = [];
deleteRecordInVault(
deleteRecord,
options,
client,
authToken as string
)
.then(
(response: any) => {
deleteResponse.push(response);
},
(cause: any) => {
deleteResponse.push(formatForPureJsFailure(cause, deleteRecord.id));
}
)
.finally(() => {
resolve(deleteResponse);
});
})
);

Promise.allSettled(vaultResponseSet).then((resultSet) => {
const recordsResponse: Record<string, any>[] = [];
const errorResponse: Record<string, any>[] = [];
resultSet.forEach((result) => {
if (result.status === "fulfilled") {
result.value.forEach((res: Record<string, any>) => {
if (Object.prototype.hasOwnProperty.call(res, "error")) {
errorResponse.push(res);
} else {
recordsResponse.push(res);
}
});
}
});
if (errorResponse.length === 0) {
rootResolve({ records: recordsResponse });
} else if (recordsResponse.length === 0) {
rootReject({ errors: errorResponse });
}else rootReject({ records: recordsResponse, errors: errorResponse });
});
});
};
13 changes: 13 additions & 0 deletions src/vault-api/utils/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,17 @@ export interface IUpdateOptions{
tokens: boolean
}

export interface IDeleteRecord {
id: string;
table: string;
}

export interface IDeleteInput {
records: IDeleteRecord[];
}

export interface IDeleteOptions {

}

export const SDK_METRICS_HEADER_KEY = "sky-metadata";
24 changes: 24 additions & 0 deletions src/vault-api/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,30 @@ const SKYFLOW_ERROR_CODE = {
code: 400,
description: logs.errorLogs.INVALID_GET_BY_ID_INPUT,
},
MISSING_ID_IN_DELETE: {
code: 400,
description: logs.errorLogs.MISSING_ID_IN_DELETE,
},
INVALID_ID_IN_DELETE: {
code: 400,
description: logs.errorLogs.INVALID_ID_IN_DELETE,
},
MISSING_TABLE_IN_DELETE: {
code: 400,
description: logs.errorLogs.MISSING_TABLE_IN_DELETE,
},
INVALID_TABLE_IN_DELETE: {
code: 400,
description: logs.errorLogs.INVALID_TABLE_IN_DELETE,
},
INVALID_DELETE_INPUT: {
code: 400,
description: logs.errorLogs.INVALID_DELETE_INPUT,
},
INVLAID_DELETE_RECORDS_INPUT: {
code: 400,
description: logs.errorLogs.INVLAID_DELETE_RECORDS_INPUT
},
DETOKENIZE_INVALID_REDACTION_TYPE:{
code: 400,
description: logs.errorLogs.DETOKENIZE_INVALID_REDACTION_TYPE,
Expand Down
9 changes: 9 additions & 0 deletions src/vault-api/utils/logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ const logs = {
GET_BY_ID_TRIGGERED: 'Get by ID triggered.',
GET_CALL_TRIGGERED: 'Get call triggered.',
INVOKE_CONNECTION_TRIGGERED: 'Invoke connection triggered.',
DELETE_TRIGGERED: 'Delete method Triggered',
DELETE_REQUEST_RESOLVED: 'Delete method is resolved',
EMIT_REQUEST: 'Emitted %s1 request.',
FETCH_RECORDS_RESOLVED: 'Detokenize request is resolved.',
INSERT_RECORDS_RESOLVED: 'Insert request is resolved.',
Expand Down Expand Up @@ -126,6 +128,13 @@ const logs = {
INVALID_UPDATE_INPUT: 'Interface: update method - Invalid argument , object with records key is required.',
INVALID_RECORDS_UPDATE_INPUT: 'Interface: update method - Invalid records type, records should be an array of objects.',
INVALID_GET_BY_ID_INPUT: 'Interface: getById method - columnName or columnValues cannot be passed, use get method instead.',
INVALID_DELETE_INPUT: 'Interface: delete method - Invalid argument , object with records key is required.',
INVLAID_DELETE_RECORDS_INPUT: 'Interface: delete method - Invalid records type, records should be an array of objects.',
MISSING_ID_IN_DELETE: 'Interface: delete method - id key is required in records object at index %s1',
INVALID_ID_IN_DELETE: 'Interface: delete method - Invalid id in records object at index %s1, id of type non empty string is required.',
MISSING_TABLE_IN_DELETE: 'Interface: delete method - table key is required in records object at index %s1',
INVALID_TABLE_IN_DELETE: 'Interface: delete method - Invalid table in records object at index %s1, table of type non empty string is required.',
DELETE_REQUEST_REJECTED: 'Delete request is rejected.',
DETOKENIZE_INVALID_REDACTION_TYPE:'Interface: detokenize method - Invalid redaction type, use Skyflow.RedactionType enum.',
},
warnLogs: {
Expand Down
44 changes: 42 additions & 2 deletions src/vault-api/utils/validators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import SkyflowError from '../../libs/SkyflowError';
import { ISkyflow } from '../../Skyflow';
import {
IInsertRecordInput, IDetokenizeInput, RedactionType, IGetByIdInput, IConnectionConfig, RequestMethod, IUpdateInput, IGetInput,
IInsertRecordInput, IDetokenizeInput, RedactionType, IGetByIdInput, IConnectionConfig, RequestMethod, IUpdateInput, IGetInput, IDeleteInput, IDeleteOptions,
} from '../common';
import SKYFLOW_ERROR_CODE from '../constants';

Expand Down Expand Up @@ -264,4 +264,44 @@ export const validateUpdateInput = (updateInput: IUpdateInput) => {
} else {
throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_UPDATE_INPUT);
}
};
};

export const validateDeleteInputAndOptions = (deleteInput: IDeleteInput, options?: IDeleteOptions) => {
if (deleteInput) {
if (!Object.prototype.hasOwnProperty.call(deleteInput, 'records')) {
throw new SkyflowError(SKYFLOW_ERROR_CODE.MISSING_RECORDS);
}

if (!Array.isArray(deleteInput.records)) {
throw new SkyflowError(SKYFLOW_ERROR_CODE.INVLAID_DELETE_RECORDS_INPUT)
}
const { records } = deleteInput;
if (records.length === 0) {
throw new SkyflowError(SKYFLOW_ERROR_CODE.EMPTY_RECORDS);
}
records.forEach((deleteRecord, index) => {
if (Object.keys(deleteRecord).length === 0) {
throw new SkyflowError(SKYFLOW_ERROR_CODE.EMPTY_RECORDS);
}

if (!Object.prototype.hasOwnProperty.call(deleteRecord, 'id')){
throw new SkyflowError(SKYFLOW_ERROR_CODE.MISSING_ID_IN_DELETE, [index]);
}

if (typeof deleteRecord.id !== 'string' || deleteRecord.id.trim().length === 0) {
throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_ID_IN_DELETE, [index]);
}

if (!Object.prototype.hasOwnProperty.call(deleteRecord, 'table')) {
throw new SkyflowError(SKYFLOW_ERROR_CODE.MISSING_TABLE_IN_DELETE, [index]);
}

const recordTable = deleteRecord.table;
if (typeof recordTable !== 'string' || recordTable.trim().length === 0) {
throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_TABLE_IN_DELETE, [index]);
}
});
} else {
throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_DELETE_INPUT);
}
}
Loading

0 comments on commit 89db705

Please sign in to comment.