Skip to content

Commit

Permalink
chore: features json to typescript with ENV overwrite support (#3848)
Browse files Browse the repository at this point in the history
  • Loading branch information
vinayteki95 authored Nov 6, 2024
2 parents 7a26459 + bbd31a1 commit 2c22ba7
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 115 deletions.
2 changes: 1 addition & 1 deletion src/controllers/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class MiscController {
}

public static features(ctx: Context) {
ctx.body = MiscService.getFetaures();
ctx.body = MiscService.getFeatures();
ctx.status = 200;
return ctx;
}
Expand Down
105 changes: 0 additions & 105 deletions src/features.json

This file was deleted.

116 changes: 116 additions & 0 deletions src/features.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
interface FeaturesConfig {
routerTransform: Record<string, boolean>;
regulations: string[];
supportSourceTransformV1: boolean;
supportTransformerProxyV1: boolean;
upgradedToSourceTransformV2?: boolean;
}

const defaultFeaturesConfig: FeaturesConfig = {
routerTransform: {
ACTIVE_CAMPAIGN: true,
ALGOLIA: true,
CANDU: true,
DELIGHTED: true,
DRIP: true,
FB_CUSTOM_AUDIENCE: true,
GA: true,
GAINSIGHT: true,
GAINSIGHT_PX: true,
GOOGLESHEETS: true,
GOOGLE_ADWORDS_ENHANCED_CONVERSIONS: true,
GOOGLE_ADWORDS_REMARKETING_LISTS: true,
GOOGLE_ADWORDS_OFFLINE_CONVERSIONS: true,
HS: true,
ITERABLE: true,
KLAVIYO: true,
KUSTOMER: true,
MAILCHIMP: true,
MAILMODO: true,
MARKETO: true,
OMETRIA: true,
PARDOT: true,
PINTEREST_TAG: true,
PROFITWELL: true,
SALESFORCE: true,
SALESFORCE_OAUTH: true,
SALESFORCE_OAUTH_SANDBOX: true,
SFMC: true,
SNAPCHAT_CONVERSION: true,
TIKTOK_ADS: true,
TRENGO: true,
YAHOO_DSP: true,
CANNY: true,
LAMBDA: true,
WOOTRIC: true,
GOOGLE_CLOUD_FUNCTION: true,
BQSTREAM: true,
CLICKUP: true,
FRESHMARKETER: true,
FRESHSALES: true,
MONDAY: true,
CUSTIFY: true,
USER: true,
REFINER: true,
FACEBOOK_OFFLINE_CONVERSIONS: true,
MAILJET: true,
SNAPCHAT_CUSTOM_AUDIENCE: true,
MARKETO_STATIC_LIST: true,
CAMPAIGN_MANAGER: true,
SENDGRID: true,
SENDINBLUE: true,
ZENDESK: true,
MP: true,
TIKTOK_ADS_OFFLINE_EVENTS: true,
CRITEO_AUDIENCE: true,
CUSTOMERIO: true,
BRAZE: true,
OPTIMIZELY_FULLSTACK: true,
TWITTER_ADS: true,
CLEVERTAP: true,
ORTTO: true,
GLADLY: true,
ONE_SIGNAL: true,
TIKTOK_AUDIENCE: true,
REDDIT: true,
THE_TRADE_DESK: true,
INTERCOM: true,
NINETAILED: true,
KOALA: true,
LINKEDIN_ADS: true,
BLOOMREACH: true,
MOVABLE_INK: true,
EMARSYS: true,
KODDI: true,
WUNDERKIND: true,
CLICKSEND: true,
ZOHO: true,
CORDIAL: true,
X_AUDIENCE: true,
BLOOMREACH_CATALOG: true,
SMARTLY: true,
HTTP: true,
AMAZON_AUDIENCE: true,
INTERCOM_V2: true,
},
regulations: [
'BRAZE',
'AM',
'INTERCOM',
'CLEVERTAP',
'AF',
'MP',
'GA',
'ITERABLE',
'ENGAGE',
'CUSTIFY',
'SENDGRID',
'SPRIG',
'EMARSYS',
],
supportSourceTransformV1: true,
supportTransformerProxyV1: true,
upgradedToSourceTransformV2: process.env.UPGRADED_TO_SOURCE_TRANSFORM_V2 === 'true' || false, // redundant but required to show that the default is false
};

export default defaultFeaturesConfig;
40 changes: 40 additions & 0 deletions src/services/__tests__/misc.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { DestHandlerMap } from '../../constants/destinationCanonicalNames';
import defaultFeaturesConfig from '../../features';
import { MiscService } from '../misc';

describe('Misc tests', () => {
Expand All @@ -24,3 +25,42 @@ describe('Misc tests', () => {
);
});
});

describe('Misc | getFeatures', () => {
const originalEnv = process.env;

beforeEach(() => {
// Reset environment variables and module cache before each test
process.env = { ...originalEnv };
jest.resetModules();
});

afterAll(() => {
// Restore the original environment variables after all tests
process.env = originalEnv;
});

function getMiscService() {
// Re-import config and featuresService after environment variables are set
const { MiscService: miscService } = require('../misc');
return miscService;
}

it('should return the default configuration as a JSON string', () => {
const miscService = getMiscService();
const expectedConfig = JSON.stringify(defaultFeaturesConfig);
const result = miscService.getFeatures();
expect(result).toBe(expectedConfig);
});

it('should return configuration with upgradedToSourceTransformV2 overridden by environment variable', () => {
process.env.UPGRADED_TO_SOURCE_TRANSFORM_V2 = 'true';
const expectedConfig = {
...defaultFeaturesConfig,
upgradedToSourceTransformV2: true,
};
const miscService = getMiscService();
const result = miscService.getFeatures();
expect(result).toBe(JSON.stringify(expectedConfig));
});
});
8 changes: 3 additions & 5 deletions src/services/misc.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
/* eslint-disable global-require, import/no-dynamic-require */
import fs from 'fs';
import { Context } from 'koa';
import path from 'path';
import { DestHandlerMap } from '../constants/destinationCanonicalNames';
import { getCPUProfile, getHeapProfile } from '../middleware';
import { Metadata } from '../types';
import defaultFeaturesConfig from '../features';

export class MiscService {
public static getDestHandler(dest: string, version: string) {
Expand Down Expand Up @@ -62,9 +61,8 @@ export class MiscService {
return process.env.npm_package_version || 'Version Info not found';
}

public static getFetaures() {
const obj = JSON.parse(fs.readFileSync(path.resolve(__dirname, '../features.json'), 'utf8'));
return JSON.stringify(obj);
public static getFeatures() {
return JSON.stringify(defaultFeaturesConfig);
}

public static async getCPUProfile(seconds: number) {
Expand Down
6 changes: 2 additions & 4 deletions test/apitests/service.api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import request from 'supertest';
import networkHandlerFactory from '../../src/adapters/networkHandlerFactory';
import { FetchHandler } from '../../src/helpers/fetchHandlers';
import { applicationRoutes } from '../../src/routes';
import defaultFeaturesConfig from '../../src/features';

let server: any;
const OLD_ENV = process.env;
Expand Down Expand Up @@ -43,12 +44,9 @@ const getDataFromPath = (pathInput) => {

describe('features tests', () => {
test('successful features response', async () => {
const expectedData = JSON.parse(
fs.readFileSync(path.resolve(__dirname, '../../src/features.json'), 'utf8'),
);
const response = await request(server).get('/features');
expect(response.status).toEqual(200);
expect(JSON.parse(response.text)).toEqual(expectedData);
expect(JSON.parse(response.text)).toEqual(defaultFeaturesConfig);
});

test('features regulations should be array', async () => {
Expand Down

0 comments on commit 2c22ba7

Please sign in to comment.