Skip to content

Commit b15b539

Browse files
authored
Merge pull request #147 from azriel46d/feature/parse-json-values
Parse json values on getValue
2 parents 55a0c5e + 232c4e3 commit b15b539

File tree

16 files changed

+75
-24
lines changed

16 files changed

+75
-24
lines changed

flagsmith-core.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { IFlags, IFlagsmith, IFlagsmithResponse, IInitConfig, IState, ITraits } from './types';
1+
import { IFlags, IFlagsmith, GetValueOptions, IFlagsmithResponse, IInitConfig, IState, ITraits } from './types';
22
export type LikeFetch = (input: Partial<RequestInfo>, init?: Partial<RequestInit>) => Promise<Partial<Response>>
33
let _fetch: LikeFetch;
44
type RequestOptions = {
@@ -622,16 +622,23 @@ const Flagsmith = class {
622622
this.updateEventStorage();
623623
}
624624

625-
getValue = (key:string) => {
625+
getValue = (key:string, options?: GetValueOptions) => {
626626
const flag = this.flags && this.flags[key.toLowerCase().replace(/ /g, '_')];
627627
let res = null;
628628
if (flag) {
629629
res = flag.value;
630630
}
631+
631632
this.evaluateFlag(key);
632633

634+
if (options?.json) {
635+
try {
636+
return JSON.parse(res as string)
637+
} catch (e) {
638+
return options.fallback
639+
}
640+
}
633641
//todo record check for value
634-
635642
return res;
636643
}
637644

lib/flagsmith-es/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/flagsmith-es/isomorphic.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/flagsmith-es/next-middleware.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/flagsmith-es/src/flagsmith-core.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { IFlags, IFlagsmith, IFlagsmithResponse, IInitConfig, IState, ITraits } from './types';
1+
import { IFlags, IFlagsmith, GetValueOptions, IFlagsmithResponse, IInitConfig, IState, ITraits } from './types';
22
export type LikeFetch = (input: Partial<RequestInfo>, init?: Partial<RequestInit>) => Promise<Partial<Response>>
33
let _fetch: LikeFetch;
44
type RequestOptions = {
@@ -622,16 +622,23 @@ const Flagsmith = class {
622622
this.updateEventStorage();
623623
}
624624

625-
getValue = (key:string) => {
625+
getValue = (key:string, options?: GetValueOptions) => {
626626
const flag = this.flags && this.flags[key.toLowerCase().replace(/ /g, '_')];
627627
let res = null;
628628
if (flag) {
629629
res = flag.value;
630630
}
631+
631632
this.evaluateFlag(key);
632633

634+
if (options?.json) {
635+
try {
636+
return JSON.parse(res as string)
637+
} catch (e) {
638+
return options.fallback
639+
}
640+
}
633641
//todo record check for value
634-
635642
return res;
636643
}
637644

lib/flagsmith-es/types.d.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ export interface IFlagsmithFeature {
77
export declare type IFlagsmithTrait = IFlagsmithValue;
88
export declare type IFlags<F extends string = string> = Record<F, IFlagsmithFeature>;
99
export declare type ITraits<T extends string = string> = Record<T, IFlagsmithTrait>;
10+
export declare type GetValueOptions<T = Array<any> | object> = {
11+
json: boolean;
12+
fallback?: T;
13+
};
1014
export interface IRetrieveInfo {
1115
isFromServer: boolean;
1216
flagsChanged: boolean;
@@ -107,7 +111,7 @@ export interface IFlagsmith<F extends string = string, T extends string = string
107111
/**
108112
* Get the value of a particular remote config e.g. flagsmith.getValue("font_size")
109113
*/
110-
getValue: (key: F) => IFlagsmithValue;
114+
getValue<T>(key: F, options?: GetValueOptions<T>): T | IFlagsmithValue;
111115
/**
112116
* Get the value of a particular trait for the identified user
113117
*/

lib/flagsmith/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/flagsmith/isomorphic.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/flagsmith/next-middleware.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/flagsmith/src/flagsmith-core.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { IFlags, IFlagsmith, IFlagsmithResponse, IInitConfig, IState, ITraits } from './types';
1+
import { IFlags, IFlagsmith, GetValueOptions, IFlagsmithResponse, IInitConfig, IState, ITraits } from './types';
22
export type LikeFetch = (input: Partial<RequestInfo>, init?: Partial<RequestInit>) => Promise<Partial<Response>>
33
let _fetch: LikeFetch;
44
type RequestOptions = {
@@ -622,16 +622,23 @@ const Flagsmith = class {
622622
this.updateEventStorage();
623623
}
624624

625-
getValue = (key:string) => {
625+
getValue = (key:string, options?: GetValueOptions) => {
626626
const flag = this.flags && this.flags[key.toLowerCase().replace(/ /g, '_')];
627627
let res = null;
628628
if (flag) {
629629
res = flag.value;
630630
}
631+
631632
this.evaluateFlag(key);
632633

634+
if (options?.json) {
635+
try {
636+
return JSON.parse(res as string)
637+
} catch (e) {
638+
return options.fallback
639+
}
640+
}
633641
//todo record check for value
634-
635642
return res;
636643
}
637644

0 commit comments

Comments
 (0)