skyflow-node
Version:
Skyflow SDK for Node.js
319 lines • 17.3 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateDeleteInputAndOptions = exports.validateUpdateInput = exports.validateUpsertOptions = exports.validateInitConfig = exports.validateConnectionConfig = exports.isValidURL = exports.validateGetInput = exports.validateGetByIdInput = exports.validateDetokenizeInput = exports.validateInsertRecords = void 0;
const SkyflowError_1 = __importDefault(require("../../libs/SkyflowError"));
const common_1 = require("../common");
const constants_1 = __importDefault(require("../constants"));
const validateInsertRecords = (recordObj) => {
if (!('records' in recordObj)) {
throw new SkyflowError_1.default(constants_1.default.RECORDS_KEY_NOT_FOUND, [], true);
}
const { records } = recordObj;
if (records.length === 0) {
throw new SkyflowError_1.default(constants_1.default.EMPTY_RECORDS, [], true);
}
records.forEach((record, index) => {
if (!('table' in record && 'fields' in record)) {
throw new SkyflowError_1.default(constants_1.default.EMPTY_TABLE_AND_FIELDS, [`${index}`], true);
}
if (record.table === '') {
throw new SkyflowError_1.default(constants_1.default.EMPTY_TABLE, [`${index}`], true);
}
});
};
exports.validateInsertRecords = validateInsertRecords;
const validateDetokenizeInput = (detokenizeInput) => {
if (!Object.prototype.hasOwnProperty.call(detokenizeInput, 'records'))
throw new SkyflowError_1.default(constants_1.default.MISSING_RECORDS);
const { records } = detokenizeInput;
if (records.length === 0)
throw new SkyflowError_1.default(constants_1.default.EMPTY_RECORDS);
records.forEach((record) => {
if (Object.keys(record).length === 0) {
throw new SkyflowError_1.default(constants_1.default.EMPTY_RECORDS);
}
const recordToken = record.token;
if (!recordToken) {
throw new SkyflowError_1.default(constants_1.default.MISSING_TOKEN);
}
if (recordToken === '' || typeof recordToken !== 'string') {
throw new SkyflowError_1.default(constants_1.default.INVALID_TOKEN_ID);
}
if (Object.prototype.hasOwnProperty.call(record, 'redaction')) {
if (!Object.values(common_1.RedactionType).includes(record.redaction)) {
throw new SkyflowError_1.default(constants_1.default.DETOKENIZE_INVALID_REDACTION_TYPE);
}
}
});
};
exports.validateDetokenizeInput = validateDetokenizeInput;
const validateGetByIdInput = (getByIdInput) => {
if (!Object.prototype.hasOwnProperty.call(getByIdInput, 'records')) {
throw new SkyflowError_1.default(constants_1.default.MISSING_RECORDS);
}
const { records } = getByIdInput;
if (records.length === 0) {
throw new SkyflowError_1.default(constants_1.default.EMPTY_RECORDS);
}
records.forEach((record) => {
if (Object.keys(record).length === 0) {
throw new SkyflowError_1.default(constants_1.default.EMPTY_RECORDS);
}
const recordIds = record.ids;
if (!recordIds) {
throw new SkyflowError_1.default(constants_1.default.MISSING_IDS);
}
if ((recordIds === null || recordIds === void 0 ? void 0 : recordIds.length) === 0)
throw new SkyflowError_1.default(constants_1.default.EMPTY_RECORD_IDS);
recordIds === null || recordIds === void 0 ? void 0 : recordIds.forEach((skyflowId) => {
if (typeof skyflowId !== 'string')
throw new SkyflowError_1.default(constants_1.default.INVALID_RECORD_ID_TYPE);
});
const recordRedaction = record.redaction;
if (!recordRedaction)
throw new SkyflowError_1.default(constants_1.default.MISSING_REDACTION);
if (!Object.values(common_1.RedactionType).includes(recordRedaction)) {
throw new SkyflowError_1.default(constants_1.default.INVALID_REDACTION_TYPE);
}
const recordTable = record.table;
if (!Object.prototype.hasOwnProperty.call(record, 'table')) {
throw new SkyflowError_1.default(constants_1.default.MISSING_TABLE);
}
if (recordTable === '' || typeof recordTable !== 'string') {
throw new SkyflowError_1.default(constants_1.default.INVALID_RECORD_TABLE_VALUE);
}
if (record.hasOwnProperty('columnName') || record.hasOwnProperty('columnValues')) {
throw new SkyflowError_1.default(constants_1.default.INVALID_GET_BY_ID_INPUT);
}
});
};
exports.validateGetByIdInput = validateGetByIdInput;
const validateGetInput = (getInput, options) => {
if (!Object.prototype.hasOwnProperty.call(getInput, 'records')) {
throw new SkyflowError_1.default(constants_1.default.MISSING_RECORDS);
}
const { records } = getInput;
if (records.length === 0) {
throw new SkyflowError_1.default(constants_1.default.EMPTY_RECORDS);
}
if (Object.prototype.hasOwnProperty.call(options, 'tokens') && (typeof (options === null || options === void 0 ? void 0 : options.tokens) !== 'boolean')) {
throw new SkyflowError_1.default(constants_1.default.INVALID_TOKENS_IN_GET);
}
if (Object.prototype.hasOwnProperty.call(options, 'encodeURI') && (typeof (options === null || options === void 0 ? void 0 : options.encodeURI) !== 'boolean')) {
throw new SkyflowError_1.default(constants_1.default.INVALID_ENCODE_URI_IN_GET);
}
records.forEach((record) => {
if (Object.keys(record).length === 0) {
throw new SkyflowError_1.default(constants_1.default.EMPTY_RECORDS);
}
const recordIds = record.ids;
if ((recordIds === null || recordIds === void 0 ? void 0 : recordIds.length) === 0)
throw new SkyflowError_1.default(constants_1.default.EMPTY_RECORD_IDS);
recordIds === null || recordIds === void 0 ? void 0 : recordIds.forEach((skyflowId) => {
if (typeof skyflowId !== 'string')
throw new SkyflowError_1.default(constants_1.default.INVALID_RECORD_ID_TYPE);
});
const recordRedaction = record.redaction;
if (!(options && Object.prototype.hasOwnProperty.call(options, 'tokens') && (options === null || options === void 0 ? void 0 : options.tokens) === true)) {
if (!recordRedaction)
throw new SkyflowError_1.default(constants_1.default.MISSING_REDACTION);
if (!Object.values(common_1.RedactionType).includes(recordRedaction)) {
throw new SkyflowError_1.default(constants_1.default.INVALID_REDACTION_TYPE);
}
}
const recordTable = record.table;
if (!Object.prototype.hasOwnProperty.call(record, 'table')) {
throw new SkyflowError_1.default(constants_1.default.MISSING_TABLE);
}
if (recordTable === '' || typeof recordTable !== 'string') {
throw new SkyflowError_1.default(constants_1.default.INVALID_RECORD_TABLE_VALUE);
}
if (record.columnName === undefined && record.columnValues === undefined && recordIds === undefined) {
throw new SkyflowError_1.default(constants_1.default.MISSING_ID_AND_COLUMN_NAME);
}
if (record.columnName != undefined && record.columnValues === undefined) {
throw new SkyflowError_1.default(constants_1.default.MISSING_RECORD_COLUMN_VALUE);
}
if (record.columnName === undefined && record.columnValues !== undefined) {
throw new SkyflowError_1.default(constants_1.default.MISSING_RECORD_COLUMN_NAME);
}
const columnName = record.columnName;
if (columnName != null && typeof columnName !== 'string') {
throw new SkyflowError_1.default(constants_1.default.INVALID_RECORD_COLUMN_VALUE);
}
if (columnName != null && columnName === '') {
throw new SkyflowError_1.default(constants_1.default.EMPTY_COLUMN_NAME);
}
const columnValues = record.columnValues;
if (columnValues != null && !(columnValues && Array.isArray(columnValues)))
throw new SkyflowError_1.default(constants_1.default.INVALID_COLUMN_VALUES_OPTION_TYPE);
if (columnValues != null) {
if (columnValues.length === 0)
throw new SkyflowError_1.default(constants_1.default.EMPTY_RECORD_COLUMN_VALUES);
columnValues.forEach((eachColumnValue) => {
if (typeof eachColumnValue !== 'string')
throw new SkyflowError_1.default(constants_1.default.INVALID_RECORD_COLUMN_VALUE_TYPE);
if (eachColumnValue === '')
throw new SkyflowError_1.default(constants_1.default.EMPTY_COLUMN_VALUE);
});
}
if (options && Object.prototype.hasOwnProperty.call(options, 'tokens') && (options === null || options === void 0 ? void 0 : options.tokens) === true) {
if (columnName || columnValues)
throw new SkyflowError_1.default(constants_1.default.TOKENS_GET_COLUMN_NOT_SUPPORTED);
if (recordRedaction)
throw new SkyflowError_1.default(constants_1.default.REDACTION_WITH_TOKENS_NOT_SUPPORTED);
}
});
};
exports.validateGetInput = validateGetInput;
const isValidURL = (url) => {
if (url.substring(0, 5).toLowerCase() !== 'https') {
return false;
}
try {
const tempUrl = new URL(url);
if (tempUrl)
return true;
}
catch (err) {
return false;
}
return true;
};
exports.isValidURL = isValidURL;
const validateConnectionConfig = (config) => {
if (!Object.prototype.hasOwnProperty.call(config, 'connectionURL')) {
throw new SkyflowError_1.default(constants_1.default.MISSING_CONNECTION_URL);
}
if (typeof config.connectionURL !== 'string') {
throw new SkyflowError_1.default(constants_1.default.INVALID_CONNECTION_URL_TYPE);
}
if (!(0, exports.isValidURL)(config.connectionURL)) {
throw new SkyflowError_1.default(constants_1.default.INVALID_CONNECTION_URL);
}
if (!Object.prototype.hasOwnProperty.call(config, 'methodName')) {
throw new SkyflowError_1.default(constants_1.default.MISSING_METHODNAME_KEY);
}
if (!Object.values(common_1.RequestMethod).includes(config.methodName)) {
throw new SkyflowError_1.default(constants_1.default.INVALID_METHODNAME_VALUE);
}
};
exports.validateConnectionConfig = validateConnectionConfig;
const validateInitConfig = (initConfig) => {
if (!Object.prototype.hasOwnProperty.call(initConfig, 'vaultID')) {
throw new SkyflowError_1.default(constants_1.default.VAULTID_IS_REQUIRED, [], true);
}
if (!initConfig.vaultID) {
throw new SkyflowError_1.default(constants_1.default.EMPTY_VAULTID_IN_INIT, [], true);
}
if (!Object.prototype.hasOwnProperty.call(initConfig, 'vaultURL')) {
throw new SkyflowError_1.default(constants_1.default.VAULTURL_IS_REQUIRED, [], true);
}
if (!initConfig.vaultURL) {
throw new SkyflowError_1.default(constants_1.default.EMPTY_VAULTURL_IN_INIT, [], true);
}
if (initConfig.vaultURL && !(0, exports.isValidURL)(initConfig.vaultURL)) {
throw new SkyflowError_1.default(constants_1.default.INVALID_VAULTURL_IN_INIT, [], true);
}
if (!Object.prototype.hasOwnProperty.call(initConfig, 'getBearerToken')) {
throw new SkyflowError_1.default(constants_1.default.GET_BEARER_TOKEN_IS_REQUIRED, [], true);
}
};
exports.validateInitConfig = validateInitConfig;
const validateUpsertOptions = (upsertOptions) => {
if (!(upsertOptions && Array.isArray(upsertOptions)))
throw new SkyflowError_1.default(constants_1.default.INVALID_UPSERT_OPTION_TYPE, [], true);
if (!upsertOptions.length)
throw new SkyflowError_1.default(constants_1.default.EMPTY_UPSERT_OPTIONS_ARRAY, [], true);
upsertOptions.forEach((upsertOption, index) => {
if (!(upsertOption && typeof upsertOption === 'object')) {
throw new SkyflowError_1.default(constants_1.default.INVALID_UPSERT_OPTION_OBJECT_TYPE, [index], true);
}
if (!Object.prototype.hasOwnProperty.call(upsertOption, 'table')) {
throw new SkyflowError_1.default(constants_1.default.MISSING_TABLE_IN_UPSERT_OPTION, [index], true);
}
if (!(upsertOption.table && typeof upsertOption.table === 'string' && upsertOption.table.length)) {
throw new SkyflowError_1.default(constants_1.default.INVALID_TABLE_IN_UPSERT_OPTION, [index], true);
}
if (!Object.prototype.hasOwnProperty.call(upsertOption, 'column')) {
throw new SkyflowError_1.default(constants_1.default.MISSING_COLUMN_IN_UPSERT_OPTION, [index], true);
}
if (!(upsertOption.column && typeof upsertOption.column === 'string' && upsertOption.column.length)) {
throw new SkyflowError_1.default(constants_1.default.INVALID_COLUMN_IN_UPSERT_OPTION, [index], true);
}
});
};
exports.validateUpsertOptions = validateUpsertOptions;
const validateUpdateInput = (updateInput) => {
if (updateInput) {
if (!Object.prototype.hasOwnProperty.call(updateInput, 'records'))
throw new SkyflowError_1.default(constants_1.default.MISSING_RECORDS);
if (!Array.isArray(updateInput.records))
throw new SkyflowError_1.default(constants_1.default.INVALID_RECORDS_UPDATE_INPUT);
const { records } = updateInput;
if (records.length === 0) {
throw new SkyflowError_1.default(constants_1.default.EMPTY_RECORDS);
}
records.forEach((updateRecord, index) => {
if (Object.keys(updateRecord).length === 0)
throw new SkyflowError_1.default(constants_1.default.EMPTY_RECORDS);
if (!Object.prototype.hasOwnProperty.call(updateRecord, 'id'))
throw new SkyflowError_1.default(constants_1.default.MISSING_ID_IN_UPDATE, [index]);
if (typeof updateRecord.id !== 'string' || updateRecord.id.trim().length === 0)
throw new SkyflowError_1.default(constants_1.default.INVALID_ID_IN_UPDATE, [index]);
if (!Object.prototype.hasOwnProperty.call(updateRecord, 'table'))
throw new SkyflowError_1.default(constants_1.default.MISSING_TABLE_IN_IN_UPDATE, [index]);
const recordTable = updateRecord.table;
if (typeof recordTable !== 'string' || recordTable.trim().length === 0)
throw new SkyflowError_1.default(constants_1.default.INVALID_TABLE_IN_UPDATE, [index]);
if (!Object.prototype.hasOwnProperty.call(updateRecord, 'fields'))
throw new SkyflowError_1.default(constants_1.default.MISSING_FIELDS_IN_IN_UPDATE, [index]);
if (typeof (updateRecord === null || updateRecord === void 0 ? void 0 : updateRecord.fields) !== 'object' || Object.keys(updateRecord).length === 0)
throw new SkyflowError_1.default(constants_1.default.INVALID_FIELDS_IN_UPDATE, [index]);
});
}
else {
throw new SkyflowError_1.default(constants_1.default.INVALID_UPDATE_INPUT);
}
};
exports.validateUpdateInput = validateUpdateInput;
const validateDeleteInputAndOptions = (deleteInput, options) => {
if (deleteInput) {
if (!Object.prototype.hasOwnProperty.call(deleteInput, 'records')) {
throw new SkyflowError_1.default(constants_1.default.MISSING_RECORDS);
}
if (!Array.isArray(deleteInput.records)) {
throw new SkyflowError_1.default(constants_1.default.INVLAID_DELETE_RECORDS_INPUT);
}
const { records } = deleteInput;
if (records.length === 0) {
throw new SkyflowError_1.default(constants_1.default.EMPTY_RECORDS);
}
records.forEach((deleteRecord, index) => {
if (Object.keys(deleteRecord).length === 0) {
throw new SkyflowError_1.default(constants_1.default.EMPTY_RECORDS);
}
if (!Object.prototype.hasOwnProperty.call(deleteRecord, 'id')) {
throw new SkyflowError_1.default(constants_1.default.MISSING_ID_IN_DELETE, [index]);
}
if (typeof deleteRecord.id !== 'string' || deleteRecord.id.trim().length === 0) {
throw new SkyflowError_1.default(constants_1.default.INVALID_ID_IN_DELETE, [index]);
}
if (!Object.prototype.hasOwnProperty.call(deleteRecord, 'table')) {
throw new SkyflowError_1.default(constants_1.default.MISSING_TABLE_IN_DELETE, [index]);
}
const recordTable = deleteRecord.table;
if (typeof recordTable !== 'string' || recordTable.trim().length === 0) {
throw new SkyflowError_1.default(constants_1.default.INVALID_TABLE_IN_DELETE, [index]);
}
});
}
else {
throw new SkyflowError_1.default(constants_1.default.INVALID_DELETE_INPUT);
}
};
exports.validateDeleteInputAndOptions = validateDeleteInputAndOptions;
//# sourceMappingURL=index.js.map