Skip to content

Commit

Permalink
SK-891 add custom domain functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
yaswanth-pula-skyflow committed Aug 1, 2023
1 parent b89fb87 commit 817d8ed
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "skyflow-js",
"preferGlobal": true,
"analyze": false,
"version": "1.29.5-dev.86b6724",
"version": "1.29.5-dev.56212ef",
"author": "Skyflow",
"description": "Skyflow JavaScript SDK",
"homepage": "https://github.com/skyflowapi/skyflow-js",
Expand Down
3 changes: 2 additions & 1 deletion src/skyflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import {
IDeleteRecordInput,
IDeleteOptions,
} from './utils/common';
import { formatVaultURL } from './utils/helpers';
import { formatVaultURL, checkAndSetForCustomUrl } from './utils/helpers';
import ComposableContainer from './core/external/collect/compose-collect-container';
import { validateComposableContainerOptions } from './utils/validators';

Expand Down Expand Up @@ -136,6 +136,7 @@ class Skyflow {

static init(config: ISkyflow): Skyflow {
const logLevel = config?.options?.logLevel || LogLevel.ERROR;
checkAndSetForCustomUrl(config);
printLog(parameterizedString(logs.infoLogs.INITIALIZE_CLIENT, CLASS_NAME), MessageType.LOG,
logLevel);

Expand Down
20 changes: 18 additions & 2 deletions src/utils/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import {
} from '../../core/constants';
import { IRevealElementOptions } from '../../core/external/reveal/reveal-container';
import SkyflowError from '../../libs/skyflow-error';
import { ContainerType } from '../../skyflow';
import { ContainerType, ISkyflow } from '../../skyflow';
import SKYFLOW_ERROR_CODE from '../constants';
import { detectCardType, validateBooleanOptions } from '../validators';
import { detectCardType, isValidURL, validateBooleanOptions } from '../validators';
import properties from '../../properties';

export const flattenObject = (obj, roots = [] as any, sep = '.') => Object.keys(obj).reduce((memo, prop: any) => ({ ...memo, ...(Object.prototype.toString.call(obj[prop]) === '[object Object]' ? flattenObject(obj[prop], roots.concat([prop])) : { [roots.concat([prop]).join(sep)]: obj[prop] }) }), {});

Expand Down Expand Up @@ -290,3 +291,18 @@ export function getMetaObject(sdkDetails: any, metaData: any, navigator: any) {
};
return metaObject;
}

export function checkAndSetForCustomUrl(config: ISkyflow) {
if (
config?.options?.customElementsURL
&& isValidURL(config?.options?.customElementsURL)
) {
const urlString = config?.options?.customElementsURL;
const url = new URL(urlString);
const protocol = url.protocol;
const domain = url.hostname;
const fullDomain = `${protocol}//${domain}`;
properties.IFRAME_SECURE_ORGIN = fullDomain;
properties.IFRAME_SECURE_SITE = config?.options?.customElementsURL;
}
}
12 changes: 12 additions & 0 deletions tests/skyflow.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ describe('Skyflow initialization', () => {
expect(skyflow.constructor === Skyflow).toBe(true);
});

test('should initialize the skyflow object with custom url ', () => {
const skyflow = Skyflow.init({
vaultID: 'vault_id',
vaultURL: 'https://vault.test.com',
getBearerToken: jest.fn(),
options: {
customElementsURL: "https://js.skyflow.com/v1/elements/index.html",
}
});
expect(skyflow.constructor === Skyflow).toBe(true);
});

test('invalid vaultURL testing', async () => {
try {
const skyflow = Skyflow.init({
Expand Down
32 changes: 29 additions & 3 deletions tests/utils/helpers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
getOSDetails,
getSdkVersionName,
getMetaObject,
checkAndSetForCustomUrl
} from '../../src/utils/helpers/index';
import {
parameterizedString
Expand All @@ -30,7 +31,7 @@ import {
detectCardType
} from '../../src/utils/validators/index';
import successIcon from '../../assets/path.svg'

import { isValidURL } from '../../src/utils/validators/index';

describe('bin data for for all card number except AMEX element type on CHANGE event', () => {
test("in PROD return bin data only for card number element", () => {
Expand Down Expand Up @@ -522,11 +523,36 @@ describe('getOSDetails', () => {
expect(osDetails.os).toEqual('iOS');
expect(osDetails.version).toEqual('15.0');
});

it('should correctly parse iOS user agent string and version as null', () => {
const userAgentString = 'Mozilla/5.0 (iPhone; CPU iPhone OS like Mac OS X) AppleWebKit/ (KHTML, like Gecko) Version/ Mobile/ Safari/';
const osDetails = getOSDetails(userAgentString);
expect(osDetails.os).toEqual('iOS');
expect(osDetails.version).toEqual(null);
});
});
});
describe('checkAndSetForCustomUrl', () => {
it('should correctly parse url string and set IFRAME_SECURE origin and site', () => {
const config = {
getBearerToken: () => { },
options: {
customElementsURL: 'https://js.skyflow.com'
},
};
checkAndSetForCustomUrl(config);
const isValid = isValidURL(config.options.customElementsURL);
expect(isValid).toEqual(true);
});

it('should not parse url string and set IFRAME_SECURE origin and site', () => {
const config = {
getBearerToken: () => { },
options: {
customElementsURL: 'wrong_url'
},
};
checkAndSetForCustomUrl(config);
const isValid = isValidURL(config.options.customElementsURL);
expect(isValid).toEqual(false);
});
});

0 comments on commit 817d8ed

Please sign in to comment.