Skip to content

Commit

Permalink
refactor(client-http,web,server): use a serializable and more simple …
Browse files Browse the repository at this point in the history
…configuration object
  • Loading branch information
Billlynch committed Oct 23, 2023
1 parent 0353749 commit ce6e5da
Show file tree
Hide file tree
Showing 12 changed files with 230 additions and 494 deletions.
15 changes: 13 additions & 2 deletions packages/client-http/src/client/ConfidenceClient.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { AppliedFlag, ConfidenceClient } from './ConfidenceClient';
import { Configuration, ResolveContext } from './Configuration';
import { ConfidenceFlag } from './ConfidenceFlag';

describe('ConfidenceClient', () => {
const mockFetch = jest.fn();
Expand Down Expand Up @@ -97,7 +96,19 @@ describe('ConfidenceClient', () => {
const config = await instanceUnderTest.resolve(context);

expect(config).toEqual({
flags: { ['test-flag']: new ConfidenceFlag(fakeFlag) },
flags: {
['test-flag']: {
flagName: 'test-flag',
schema: {
str: 'string',
},
value: {
str: 'test',
},
reason: Configuration.ResolveReason.Match,
variant: 'test',
},
},
resolveToken: 'resolve-token',
context,
});
Expand Down
63 changes: 61 additions & 2 deletions packages/client-http/src/client/ConfidenceClient.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Configuration, ResolveContext } from './Configuration';
import { ConfidenceFlag, ResolvedFlag } from './ConfidenceFlag';

type ApplyRequest = {
clientSecret: string;
Expand All @@ -13,6 +12,13 @@ type ResolveRequest = {
apply?: boolean;
flags?: string[];
};
export type ResolvedFlag<T = any> = {
flag: string;
variant: string;
value?: T;
flagSchema?: ConfidenceFlagSchema;
reason: Configuration.ResolveReason;
};
type ResolveResponse = {
resolvedFlags: ResolvedFlag[];
resolveToken: string;
Expand All @@ -29,6 +35,12 @@ export type AppliedFlag = {
flag: string;
applyTime: string;
};
type ConfidenceBaseTypes = { boolSchema: {} } | { doubleSchema: {} } | { intSchema: {} } | { stringSchema: {} };
type ConfidenceFlagSchema = {
schema: {
[key: string]: ConfidenceBaseTypes | { structSchema: ConfidenceFlagSchema };
};
};

export class ConfidenceClient {
private readonly backendApplyEnabled: boolean;
Expand Down Expand Up @@ -58,9 +70,10 @@ export class ConfidenceClient {
body: JSON.stringify(payload),
});
const responseBody: ResolveResponse = await response.json();

return {
flags: responseBody.resolvedFlags.reduce((acc, flag) => {
return { ...acc, [flag.flag]: new ConfidenceFlag(flag) };
return { ...acc, [flag.flag]: resolvedFlagToFlag(flag) };
}, {}),
resolveToken: responseBody.resolveToken,
context,
Expand All @@ -80,3 +93,49 @@ export class ConfidenceClient {
});
}
}

function resolvedFlagToFlag(flag: ResolvedFlag): Configuration.Flag {
return {
flagName: flag.flag,
reason: flag.reason,
variant: flag.variant,
value: flag.value,
schema: parseSchema(flag.flagSchema),
};
}

function parseBaseType(obj: ConfidenceBaseTypes): Configuration.FlagSchema {
if ('boolSchema' in obj) {
return 'boolean';
}
if ('doubleSchema' in obj) {
return 'number';
}
if ('intSchema' in obj) {
return 'number';
}
if ('stringSchema' in obj) {
return 'string';
}

throw new Error(`Confidence: cannot parse schema. unknown schema: ${JSON.stringify(obj)}`);
}
function parseSchema(schema: ConfidenceFlagSchema | undefined): Configuration.FlagSchema {
if (!schema) {
return {};
}

return Object.keys(schema.schema).reduce((acc: Record<string, Configuration.FlagSchema>, key) => {
const obj = schema.schema[key];
if ('structSchema' in obj) {
return {
...acc,
[key]: parseSchema(obj.structSchema),
};
}
return {
...acc,
[key]: parseBaseType(obj),
};
}, {});
}
137 changes: 0 additions & 137 deletions packages/client-http/src/client/ConfidenceFlag.test.ts

This file was deleted.

113 changes: 0 additions & 113 deletions packages/client-http/src/client/ConfidenceFlag.ts

This file was deleted.

Loading

0 comments on commit ce6e5da

Please sign in to comment.