Skip to content

Commit

Permalink
feat!: add a custom baseURL for sidecar resolves
Browse files Browse the repository at this point in the history
  • Loading branch information
nickybondarenko committed Sep 10, 2024
1 parent 85b8b15 commit 3a0f252
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
5 changes: 5 additions & 0 deletions packages/sdk/src/Confidence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ export interface ConfidenceOptions {
timeout: number;
/** Debug logger */
logger?: Logger;
/** Backend only option. Sets an alternative resolve url */
APIResolveBaseUrl?: string;
}

/**
Expand Down Expand Up @@ -323,6 +325,7 @@ export class Confidence implements EventSender, Trackable, FlagResolver {
* @param environment - can be either "client" or "backend"
* @param fetchImplementation - fetch implementation
* @param logger - debug logger
* @param APIResolveBaseUrl - custom backend resolve URL
* @returns
*/
static create({
Expand All @@ -332,6 +335,7 @@ export class Confidence implements EventSender, Trackable, FlagResolver {
environment,
fetchImplementation = defaultFetchImplementation(),
logger = defaultLogger(),
APIResolveBaseUrl,
}: ConfidenceOptions): Confidence {
const sdk = {
id: SdkId.SDK_ID_JS_CONFIDENCE,
Expand All @@ -344,6 +348,7 @@ export class Confidence implements EventSender, Trackable, FlagResolver {
environment,
resolveTimeout: timeout,
region,
APIResolveBaseUrl,
});
if (environment === 'client') {
flagResolverClient = new CachingFlagResolverClient(flagResolverClient, Number.POSITIVE_INFINITY);
Expand Down
13 changes: 11 additions & 2 deletions packages/sdk/src/FlagResolverClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ import { SimpleFetch } from './types';
const FLAG_PREFIX = 'flags/';

export class ResolveError extends Error {
constructor(public readonly code: FlagEvaluation.ErrorCode, message: string) {
constructor(
public readonly code: FlagEvaluation.ErrorCode,
message: string,
) {
super(message);
}
}
Expand Down Expand Up @@ -84,6 +87,7 @@ export type FlagResolverClientOptions = {
resolveTimeout: number;
environment: 'client' | 'backend';
region?: 'eu' | 'us';
APIResolveBaseUrl?: string;
};

export class FetchingFlagResolverClient implements FlagResolverClient {
Expand All @@ -103,13 +107,18 @@ export class FetchingFlagResolverClient implements FlagResolverClient {
// todo refactor to move out environment
environment,
region,
APIResolveBaseUrl,
}: FlagResolverClientOptions) {
// TODO think about both resolve and apply request logic for backends
this.fetchImplementation = environment === 'backend' ? fetchImplementation : withRequestLogic(fetchImplementation);
this.clientSecret = clientSecret;
this.sdk = sdk;
this.applyTimeout = applyTimeout;
this.baseUrl = region ? `https://resolver.${region}.confidence.dev/v1` : 'https://resolver.confidence.dev/v1';
if (environment === 'backend' && APIResolveBaseUrl) {
this.baseUrl = APIResolveBaseUrl;
} else {
this.baseUrl = region ? `https://resolver.${region}.confidence.dev/v1` : 'https://resolver.confidence.dev/v1';
}
this.resolveTimeout = resolveTimeout;
}

Expand Down

0 comments on commit 3a0f252

Please sign in to comment.