make api key optional to follow appo11y config convention

This commit is contained in:
teodosii 2022-12-22 00:02:11 +02:00
parent 07a7941c6c
commit d92ab4bca6
2 changed files with 28 additions and 17 deletions

View file

@ -30,20 +30,23 @@ describe('Faro', () => {
beforeEach(() => {
jest.resetModules();
process.env = { ...OLD_ENV };
FaroHelper.faro = undefined;
jest.clearAllMocks();
});
afterAll(() => {
process.env = OLD_ENV;
const getDefaultValues = () => ({
faroUrl: 'localhost:12345/collect',
apiKey: 'secret',
enabled: 'true',
});
const getProcessEnv = (
config: { faroUrl?: string; apiKey?: string; enabled?: string } = {
faroUrl: 'localhost:12345/collect',
apiKey: 'secret',
enabled: 'true',
}
) => {
const { faroUrl, apiKey, enabled } = config;
const getProcessEnv = (config: { faroUrl?: string; apiKey?: string; enabled?: string } = {}) => {
const configObject = {
...getDefaultValues(),
...config,
};
const { faroUrl, apiKey, enabled } = configObject;
return {
FARO_URL: faroUrl,
@ -52,6 +55,12 @@ describe('Faro', () => {
};
};
test('It initializes without api key', () => {
process.env = getProcessEnv({ apiKey: '' });
const faro = FaroHelper.initializeFaro();
expect(faro).toBeDefined();
});
test('It initializes faro ENABLED === true', () => {
process.env = getProcessEnv();
const faro = FaroHelper.initializeFaro();
@ -73,10 +82,6 @@ describe('Faro', () => {
faro = FaroHelper.initializeFaro();
expect(faro).toBeUndefined();
process.env = getProcessEnv({ apiKey: undefined });
faro = FaroHelper.initializeFaro();
expect(faro).toBeUndefined();
process.env = getProcessEnv({ enabled: undefined });
faro = FaroHelper.initializeFaro();
expect(faro).toBeUndefined();

View file

@ -32,12 +32,12 @@ class FaroHelper {
environment: FARO_ENV ? `${ONCALL} ${FARO_ENV}` : ONCALL,
};
if (!faroConfig?.enabled || !faroConfig?.url || !faroConfig?.apiKey || this.faro) {
if (!faroConfig?.enabled || !faroConfig?.url || this.faro) {
return undefined;
}
try {
this.faro = initializeFaro({
const faroOptions = {
url: faroConfig.url,
apiKey: faroConfig.apiKey,
isolate: true,
@ -59,7 +59,13 @@ class FaroHelper {
name: faroConfig.environment,
version: plugin?.version,
},
});
};
if (!faroConfig.apiKey) {
delete faroOptions.apiKey; // appo11y has the key in the API instead
}
this.faro = initializeFaro(faroOptions);
this.faro.api.pushLog([`Faro was initialized for ${faroConfig.environment}`]);
} catch (ex) {}