Skip to content

Commit

Permalink
Merge branch 'main' into feat/test-release-script
Browse files Browse the repository at this point in the history
  • Loading branch information
FredrikOseberg authored Aug 10, 2022
2 parents efb1605 + 5d2b8aa commit 8e04524
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 53 deletions.
40 changes: 0 additions & 40 deletions examples/index.html

This file was deleted.

4 changes: 2 additions & 2 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { FetchMock } from 'jest-fetch-mock';
import 'jest-localstorage-mock';
import * as data from './tests/example-data.json';
import * as data from './test/testdata.json';
import IStorageProvider from './storage-provider';
import { EVENTS, IConfig, IMutableContext, UnleashClient } from './index';
import { getTypeSafeRequest, getTypeSafeRequestUrl } from './tests/util';
import { getTypeSafeRequest, getTypeSafeRequestUrl } from './test';

jest.useFakeTimers();

Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ export class UnleashClient extends TinyEmitter {
this.bootstrapOverride = bootstrapOverride;

this.metrics = new Metrics({
onError: this.emit.bind(this, EVENTS.ERROR),
appName,
metricsInterval,
disableMetrics,
Expand Down
7 changes: 6 additions & 1 deletion src/metrics.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FetchMock } from 'jest-fetch-mock';
import Metrics from './metrics';
import { getTypeSafeRequest, parseRequestBodyWithType } from './tests/util';
import { getTypeSafeRequest, parseRequestBodyWithType } from './test';

jest.useFakeTimers();

Expand All @@ -13,6 +13,7 @@ afterEach(() => {

test('should be disabled by flag disableMetrics', async () => {
const metrics = new Metrics({
onError: console.error,
appName: 'test',
metricsInterval: 0,
disableMetrics: true,
Expand All @@ -31,6 +32,7 @@ test('should be disabled by flag disableMetrics', async () => {

test('should send metrics', async () => {
const metrics = new Metrics({
onError: console.error,
appName: 'test',
metricsInterval: 0,
disableMetrics: false,
Expand Down Expand Up @@ -62,6 +64,7 @@ test('should send metrics', async () => {

test('should send metrics under custom header', async () => {
const metrics = new Metrics({
onError: console.error,
appName: 'test',
metricsInterval: 0,
disableMetrics: false,
Expand All @@ -84,6 +87,7 @@ test('should send metrics under custom header', async () => {

test('Should send initial metrics after 2 seconds', () => {
const metrics = new Metrics({
onError: console.error,
appName: 'test',
metricsInterval: 5,
disableMetrics: false,
Expand All @@ -106,6 +110,7 @@ test('Should send initial metrics after 2 seconds', () => {

test('should send metrics based on timer interval', async () => {
const metrics = new Metrics({
onError: console.error,
appName: 'test',
metricsInterval: 5,
disableMetrics: false,
Expand Down
32 changes: 22 additions & 10 deletions src/metrics.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Simplified version of: https://github.com/Unleash/unleash-client-node/blob/main/src/metrics.ts

export interface MetricsOptions {
onError: OnError;
appName: string;
metricsInterval: number;
disableMetrics?: boolean;
Expand All @@ -21,7 +23,10 @@ interface Payload {
instanceId: string;
}

type OnError = (error: unknown) => void;

export default class Metrics {
private onError: OnError;
private bucket: Bucket;
private appName: string;
private metricsInterval: number;
Expand All @@ -33,6 +38,7 @@ export default class Metrics {
private headerName: string;

constructor({
onError,
appName,
metricsInterval,
disableMetrics = false,
Expand All @@ -41,6 +47,7 @@ export default class Metrics {
fetch,
headerName,
}: MetricsOptions) {
this.onError = onError;
this.disabled = disableMetrics;
this.metricsInterval = metricsInterval * 1000;
this.appName = appName;
Expand Down Expand Up @@ -93,16 +100,21 @@ export default class Metrics {
return;
}

await this.fetch(url, {
cache: 'no-cache',
method: 'POST',
headers: {
[this.headerName]: this.clientKey,
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(payload),
});
try {
await this.fetch(url, {
cache: 'no-cache',
method: 'POST',
headers: {
[this.headerName]: this.clientKey,
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(payload),
});
} catch (e) {
console.error('Unleash: unable to send feature metrics', e);
this.onError(e);
}
}

public count(name: string, enabled: boolean): boolean {
Expand Down
27 changes: 27 additions & 0 deletions src/test/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8' />
<title>unleash-proxy-client-js manual test</title>
<script src='../../build/main.min.js' type='text/javascript'></script>
<script type='module'>
const client = new unleash.UnleashClient({
url: 'http://localhost:3000/proxy',
clientKey: 'a',
appName: 'a',
refreshInterval: 1,
metricsInterval: 1,
disableMetrics: false
});

client.start();
client.on('initialized', () => console.log('initialized'));
client.on('error', (error) => console.log('error', error));
client.on('ready', () => console.log('ready'));
client.on('impression', () => console.log('impression'));
client.on('update', () => console.log('update', client.isEnabled('a')));
</script>
</head>
<body>
</body>
</html>
File renamed without changes.
File renamed without changes.

0 comments on commit 8e04524

Please sign in to comment.