Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FF-3407 spike on client-side edge code #125

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/edge/edge-init.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { applicationLogger } from '..';
import ApiEndpoints from '../api-endpoints';
import { DEFAULT_REQUEST_TIMEOUT_MS } from '../constants';
import FetchHttpClient from '../http-client';
import { Variation } from '../interfaces';
import { AttributeType } from '../types';

import { EppoClientEdge } from './eppo-client-edge';

type VariationValue = Variation['value'] | object;

export interface IEdgeConfig {
apiKey: string;
subjectKey: string;
subjectAttributes: Record<string, AttributeType>;
throwOnFailedInitialization?: boolean;
baseUrl?: string;
}

export async function commonEdgeInit(
config: IEdgeConfig,
sdkName: string,
sdkVersion: string,
): Promise<EppoClientEdge> {
const { apiKey, baseUrl, throwOnFailedInitialization } = config;
try {
const apiEndpoints = new ApiEndpoints({
baseUrl,
queryParams: { apiKey, sdkName, sdkVersion },
});
const httpClient = new FetchHttpClient(apiEndpoints, DEFAULT_REQUEST_TIMEOUT_MS);
const assignments = await httpClient.rawGet<Record<string, VariationValue>>(
apiEndpoints.endpoint('/flag-config/v1/edge-config'),
);
if (!assignments) {
throw new Error('Unable to get assignments');
}
return new EppoClientEdge(assignments);
} catch (error) {
applicationLogger.warn(
'Eppo SDK encountered an error initializing, assignment calls will return the default value',
);
if (throwOnFailedInitialization) {
throw error;
}
return new EppoClientEdge({});
}
}
39 changes: 39 additions & 0 deletions src/edge/eppo-client-edge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Variation } from '../interfaces';

type VariationValue = Variation['value'] | object;

export class EppoClientEdge {
constructor(private cachedAssignments: Record<string, VariationValue>) {}

getAssignments() {
return this.cachedAssignments;
}

getStringAssignment(flagKey: string, defaultValue: string): string {
if (typeof this.cachedAssignments[flagKey] === 'string') {
return this.cachedAssignments[flagKey];

Check failure on line 14 in src/edge/eppo-client-edge.ts

View workflow job for this annotation

GitHub Actions / typecheck

Type 'VariationValue' is not assignable to type 'string'.

Check failure on line 14 in src/edge/eppo-client-edge.ts

View workflow job for this annotation

GitHub Actions / lint-test-sdk

Type 'VariationValue' is not assignable to type 'string'.
}
return defaultValue;
}

getBooleanAssignment(flagKey: string, defaultValue: boolean): boolean {
if (typeof this.cachedAssignments[flagKey] === 'boolean') {
return this.cachedAssignments[flagKey];

Check failure on line 21 in src/edge/eppo-client-edge.ts

View workflow job for this annotation

GitHub Actions / typecheck

Type 'VariationValue' is not assignable to type 'boolean'.

Check failure on line 21 in src/edge/eppo-client-edge.ts

View workflow job for this annotation

GitHub Actions / lint-test-sdk

Type 'VariationValue' is not assignable to type 'boolean'.
}
return defaultValue;
}

getNumericAssignment(flagKey: string, defaultValue: number): number {
if (typeof this.cachedAssignments[flagKey] === 'number') {
return this.cachedAssignments[flagKey];

Check failure on line 28 in src/edge/eppo-client-edge.ts

View workflow job for this annotation

GitHub Actions / typecheck

Type 'VariationValue' is not assignable to type 'number'.

Check failure on line 28 in src/edge/eppo-client-edge.ts

View workflow job for this annotation

GitHub Actions / lint-test-sdk

Type 'VariationValue' is not assignable to type 'number'.
}
return defaultValue;
}

getJSONAssignment(flagKey: string, defaultValue: object): object {
if (typeof this.cachedAssignments[flagKey] === 'object') {
return this.cachedAssignments[flagKey];

Check failure on line 35 in src/edge/eppo-client-edge.ts

View workflow job for this annotation

GitHub Actions / typecheck

Type 'VariationValue' is not assignable to type 'object'.

Check failure on line 35 in src/edge/eppo-client-edge.ts

View workflow job for this annotation

GitHub Actions / lint-test-sdk

Type 'VariationValue' is not assignable to type 'object'.
}
return defaultValue;
}
}
Loading