@flagsmith/flagsmith 11.0.0-internal.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +38 -0
- package/evaluation-context.d.ts +27 -0
- package/evaluation-context.ts +29 -0
- package/flagsmith-core.d.ts +18 -0
- package/index.d.ts +8 -0
- package/index.js +2 -0
- package/index.js.map +1 -0
- package/index.mjs +2 -0
- package/index.mjs.map +1 -0
- package/isomorphic.d.ts +5 -0
- package/isomorphic.js +2 -0
- package/isomorphic.js.map +1 -0
- package/isomorphic.mjs +2 -0
- package/isomorphic.mjs.map +1 -0
- package/next-middleware.d.ts +5 -0
- package/next-middleware.js +2 -0
- package/next-middleware.js.map +1 -0
- package/next-middleware.mjs +2 -0
- package/next-middleware.mjs.map +1 -0
- package/package.json +57 -0
- package/react.d.ts +32 -0
- package/react.js +2 -0
- package/react.js.map +1 -0
- package/react.mjs +2 -0
- package/react.mjs.map +1 -0
- package/src/evaluation-context.d.ts +27 -0
- package/src/flagsmith-core.d.ts +18 -0
- package/src/flagsmith-core.ts +1092 -0
- package/src/index.d.ts +8 -0
- package/src/index.ts +22 -0
- package/src/isomorphic.d.ts +5 -0
- package/src/isomorphic.ts +27 -0
- package/src/next-middleware.d.ts +5 -0
- package/src/next-middleware.ts +8 -0
- package/src/react.d.ts +32 -0
- package/src/react.tsx +200 -0
- package/src/readme.md +1 -0
- package/src/types.d.ts +338 -0
- package/src/utils/angular-fetch.ts +36 -0
- package/src/utils/async-storage.ts +41 -0
- package/src/utils/emitter.ts +90 -0
- package/src/utils/ensureTrailingSlash.ts +3 -0
- package/src/utils/get-changes.ts +19 -0
- package/src/utils/set-dynatrace-value.ts +15 -0
- package/src/utils/types.ts +24 -0
- package/src/utils/version.ts +2 -0
- package/types.d.ts +338 -0
- package/utils/angular-fetch.d.ts +6 -0
- package/utils/async-storage.d.ts +7 -0
- package/utils/emitter.d.ts +11 -0
- package/utils/ensureTrailingSlash.d.ts +1 -0
- package/utils/get-changes.d.ts +2 -0
- package/utils/set-dynatrace-value.d.ts +2 -0
- package/utils/types.d.ts +7 -0
- package/utils/version.d.ts +1 -0
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { IFlagsmith } from './types';
|
|
2
|
+
declare const flagsmith: IFlagsmith;
|
|
3
|
+
export default flagsmith;
|
|
4
|
+
export * from './types';
|
|
5
|
+
export declare const createFlagsmithInstance: <
|
|
6
|
+
F extends string = string,
|
|
7
|
+
T extends string = string,
|
|
8
|
+
>() => IFlagsmith<F, T>;
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { IFlagsmith } from './types';
|
|
2
|
+
|
|
3
|
+
// @ts-ignore
|
|
4
|
+
globalThis.FlagsmithEventSource = typeof EventSource!== "undefined"? EventSource: null;
|
|
5
|
+
|
|
6
|
+
import fetch from "unfetch"
|
|
7
|
+
import AsyncStorage from "./utils/async-storage";
|
|
8
|
+
import core, { LikeFetch } from './flagsmith-core';
|
|
9
|
+
import _EventSource from 'reconnecting-eventsource'
|
|
10
|
+
// @ts-expect-error
|
|
11
|
+
const _fetch = fetch as LikeFetch
|
|
12
|
+
const flagsmith = core({AsyncStorage, fetch:_fetch, eventSource:_EventSource});
|
|
13
|
+
if (typeof window !== "undefined") {
|
|
14
|
+
// @ts-expect-error, some people wish to use flagsmith globally
|
|
15
|
+
window.flagsmith = flagsmith;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export default flagsmith;
|
|
19
|
+
export const createFlagsmithInstance = ():IFlagsmith=>{
|
|
20
|
+
return core({ AsyncStorage, fetch:_fetch, eventSource:_EventSource})
|
|
21
|
+
}
|
|
22
|
+
export { FlagSource } from './flagsmith-core';
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import AsyncStorage from "./utils/async-storage";
|
|
2
|
+
import {IFlagsmith} from "./types";
|
|
3
|
+
import core from './flagsmith-core'
|
|
4
|
+
|
|
5
|
+
// @ts-ignore
|
|
6
|
+
globalThis.FlagsmithEventSource = typeof EventSource !== 'undefined' ? EventSource : null;
|
|
7
|
+
import eventSource from 'reconnecting-eventsource'
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
const flagsmith: IFlagsmith = core({
|
|
11
|
+
AsyncStorage,
|
|
12
|
+
eventSource: typeof window !=='undefined'?eventSource : null
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
if (typeof window !== "undefined") {
|
|
16
|
+
// @ts-ignore
|
|
17
|
+
window.flagsmith = flagsmith;
|
|
18
|
+
}
|
|
19
|
+
export default flagsmith;
|
|
20
|
+
|
|
21
|
+
export const createFlagsmithInstance = (): IFlagsmith => {
|
|
22
|
+
return core({
|
|
23
|
+
AsyncStorage,
|
|
24
|
+
eventSource: typeof window !=='undefined'?eventSource : null
|
|
25
|
+
})
|
|
26
|
+
}
|
|
27
|
+
export { FlagSource } from './flagsmith-core';
|
package/src/react.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import React, { FC } from 'react';
|
|
2
|
+
import { IFlagsmith, IFlagsmithTrait, IFlagsmithFeature, IState, LoadingState } from './types';
|
|
3
|
+
export * from './types';
|
|
4
|
+
export declare const FlagsmithContext: React.Context<IFlagsmith>;
|
|
5
|
+
export declare type FlagsmithContextType<F extends string = string, T extends string = string> = {
|
|
6
|
+
flagsmith: IFlagsmith<F, T>;
|
|
7
|
+
options?: Parameters<IFlagsmith<F, T>['init']>[0];
|
|
8
|
+
serverState?: IState;
|
|
9
|
+
children: React.ReactNode;
|
|
10
|
+
};
|
|
11
|
+
type UseFlagsReturn<
|
|
12
|
+
F extends string | Record<string, any>,
|
|
13
|
+
T extends string
|
|
14
|
+
> = [F] extends [string]
|
|
15
|
+
? {
|
|
16
|
+
[K in F]: IFlagsmithFeature;
|
|
17
|
+
} & {
|
|
18
|
+
[K in T]: IFlagsmithTrait;
|
|
19
|
+
}
|
|
20
|
+
: {
|
|
21
|
+
[K in keyof F]: IFlagsmithFeature<F[K]>;
|
|
22
|
+
} & {
|
|
23
|
+
[K in T]: IFlagsmithTrait;
|
|
24
|
+
};
|
|
25
|
+
export declare const FlagsmithProvider: FC<FlagsmithContextType>;
|
|
26
|
+
export declare function useFlags<
|
|
27
|
+
F extends string | Record<string, any>,
|
|
28
|
+
T extends string = string
|
|
29
|
+
>(_flags: readonly (F | keyof F)[], _traits?: readonly T[]): UseFlagsReturn<F, T>;
|
|
30
|
+
export declare const useFlagsmith: <F extends string | Record<string, any>,
|
|
31
|
+
T extends string = string>() => IFlagsmith<F, T>;
|
|
32
|
+
export declare const useFlagsmithLoading: () => LoadingState | undefined;
|
package/src/react.tsx
ADDED
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import React, { createContext, FC, useCallback, useContext, useEffect, useMemo, useRef, useState } from 'react'
|
|
2
|
+
import Emitter from './utils/emitter'
|
|
3
|
+
const events = new Emitter()
|
|
4
|
+
|
|
5
|
+
import { IFlagsmith, IFlagsmithTrait, IFlagsmithFeature, IState } from './types'
|
|
6
|
+
|
|
7
|
+
export const FlagsmithContext = createContext<IFlagsmith<string, string> | null>(null)
|
|
8
|
+
export type FlagsmithContextType = {
|
|
9
|
+
flagsmith: IFlagsmith // The flagsmith instance
|
|
10
|
+
options?: Parameters<IFlagsmith['init']>[0] // Initialisation options, if you do not provide this you will have to call init manually
|
|
11
|
+
serverState?: IState
|
|
12
|
+
children: React.ReactNode
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export const FlagsmithProvider: FC<FlagsmithContextType> = ({ flagsmith, options, serverState, children }) => {
|
|
16
|
+
const firstRenderRef = useRef(true)
|
|
17
|
+
if (flagsmith && !flagsmith?._trigger) {
|
|
18
|
+
flagsmith._trigger = () => {
|
|
19
|
+
// @ts-expect-error using internal function, consumers would never call this
|
|
20
|
+
flagsmith?.log('React - trigger event received')
|
|
21
|
+
events.emit('event')
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (flagsmith && !flagsmith?._triggerLoadingState) {
|
|
26
|
+
flagsmith._triggerLoadingState = () => {
|
|
27
|
+
events.emit('loading_event')
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
if (serverState && !flagsmith.initialised) {
|
|
32
|
+
flagsmith.setState(serverState)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (firstRenderRef.current) {
|
|
36
|
+
firstRenderRef.current = false
|
|
37
|
+
if (options) {
|
|
38
|
+
flagsmith
|
|
39
|
+
.init({
|
|
40
|
+
...options,
|
|
41
|
+
state: options.state || serverState,
|
|
42
|
+
onChange: (...args) => {
|
|
43
|
+
if (options.onChange) {
|
|
44
|
+
options.onChange(...args)
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
})
|
|
48
|
+
.catch((error) => {
|
|
49
|
+
// @ts-expect-error using internal function, consumers would never call this
|
|
50
|
+
flagsmith?.log('React - Failed to initialize flagsmith', error)
|
|
51
|
+
events.emit('event')
|
|
52
|
+
})
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return <FlagsmithContext.Provider value={flagsmith}>{children}</FlagsmithContext.Provider>
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const useConstant = function <T>(value: T): T {
|
|
59
|
+
const ref = useRef(value)
|
|
60
|
+
if (!ref.current) {
|
|
61
|
+
ref.current = value
|
|
62
|
+
}
|
|
63
|
+
return ref.current
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const flagsAsArray = (_flags: any): string[] => {
|
|
67
|
+
if (typeof _flags === 'string') {
|
|
68
|
+
return [_flags]
|
|
69
|
+
} else if (typeof _flags === 'object') {
|
|
70
|
+
// eslint-disable-next-line no-prototype-builtins
|
|
71
|
+
if (_flags.hasOwnProperty('length')) {
|
|
72
|
+
return _flags
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
throw new Error('Flagsmith: please supply an array of strings or a single string of flag keys to useFlags')
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const getRenderKey = (flagsmith: IFlagsmith, flags: string[], traits: string[] = []) => {
|
|
79
|
+
return flags
|
|
80
|
+
.map((k) => {
|
|
81
|
+
return `${flagsmith.getValue(k)}${flagsmith.hasFeature(k)}`
|
|
82
|
+
})
|
|
83
|
+
.concat(traits.map((t) => `${flagsmith.getTrait(t)}`))
|
|
84
|
+
.join(',')
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function useFlagsmithLoading() {
|
|
88
|
+
const flagsmith = useContext(FlagsmithContext)
|
|
89
|
+
const [loadingState, setLoadingState] = useState(flagsmith?.loadingState)
|
|
90
|
+
const [subscribed, setSubscribed] = useState(false)
|
|
91
|
+
const refSubscribed = useRef(subscribed)
|
|
92
|
+
|
|
93
|
+
const eventListener = useCallback(() => {
|
|
94
|
+
setLoadingState(flagsmith?.loadingState)
|
|
95
|
+
}, [flagsmith])
|
|
96
|
+
if (!refSubscribed.current) {
|
|
97
|
+
events.on('loading_event', eventListener)
|
|
98
|
+
refSubscribed.current = true
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
useEffect(() => {
|
|
102
|
+
if (!subscribed && flagsmith?.initialised) {
|
|
103
|
+
events.on('loading_event', eventListener)
|
|
104
|
+
setSubscribed(true)
|
|
105
|
+
}
|
|
106
|
+
return () => {
|
|
107
|
+
if (subscribed) {
|
|
108
|
+
events.off('loading_event', eventListener)
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}, [flagsmith, subscribed, eventListener])
|
|
112
|
+
|
|
113
|
+
return loadingState
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
type UseFlagsReturn<F extends string | Record<string, any>, T extends string> = F extends string
|
|
117
|
+
? {
|
|
118
|
+
[K in F]: IFlagsmithFeature
|
|
119
|
+
} & {
|
|
120
|
+
[K in T]: IFlagsmithTrait
|
|
121
|
+
}
|
|
122
|
+
: {
|
|
123
|
+
[K in keyof F]: IFlagsmithFeature<F[K]>
|
|
124
|
+
} & {
|
|
125
|
+
[K in T]: IFlagsmithTrait
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Example usage:
|
|
130
|
+
*
|
|
131
|
+
* // A) Using string flags:
|
|
132
|
+
* useFlags<"featureOne"|"featureTwo">(["featureOne", "featureTwo"]);
|
|
133
|
+
*
|
|
134
|
+
* // B) Using an object for F - this can be generated by our CLI: https://github.com/Flagsmith/flagsmith-cli :
|
|
135
|
+
* interface MyFeatureInterface {
|
|
136
|
+
* featureOne: string;
|
|
137
|
+
* featureTwo: number;
|
|
138
|
+
* }
|
|
139
|
+
* useFlags<MyFeatureInterface>(["featureOne", "featureTwo"]);
|
|
140
|
+
*/
|
|
141
|
+
export function useFlags<F extends string | Record<string, any>, T extends string = string>(
|
|
142
|
+
_flags: readonly (F | keyof F)[],
|
|
143
|
+
_traits: readonly T[] = []
|
|
144
|
+
) {
|
|
145
|
+
const firstRender = useRef(true)
|
|
146
|
+
const flags = useConstant<string[]>(flagsAsArray(_flags))
|
|
147
|
+
const traits = useConstant<string[]>(flagsAsArray(_traits))
|
|
148
|
+
const flagsmith = useContext(FlagsmithContext)
|
|
149
|
+
const [renderRef, setRenderRef] = useState(getRenderKey(flagsmith as IFlagsmith, flags, traits))
|
|
150
|
+
const eventListener = useCallback(() => {
|
|
151
|
+
const newRenderKey = getRenderKey(flagsmith as IFlagsmith, flags, traits)
|
|
152
|
+
if (newRenderKey !== renderRef) {
|
|
153
|
+
// @ts-expect-error using internal function, consumers would never call this
|
|
154
|
+
flagsmith?.log('React - useFlags flags and traits have changed')
|
|
155
|
+
setRenderRef(newRenderKey)
|
|
156
|
+
}
|
|
157
|
+
}, [renderRef])
|
|
158
|
+
const emitterRef = useRef(events.once('event', eventListener))
|
|
159
|
+
|
|
160
|
+
if (firstRender.current) {
|
|
161
|
+
firstRender.current = false
|
|
162
|
+
// @ts-expect-error using internal function, consumers would never call this
|
|
163
|
+
flagsmith?.log('React - Initialising event listeners')
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
useEffect(() => {
|
|
167
|
+
return () => {
|
|
168
|
+
emitterRef.current?.()
|
|
169
|
+
}
|
|
170
|
+
}, [])
|
|
171
|
+
|
|
172
|
+
const res = useMemo(() => {
|
|
173
|
+
const res: any = {}
|
|
174
|
+
flags
|
|
175
|
+
.map((k) => {
|
|
176
|
+
res[k] = {
|
|
177
|
+
enabled: flagsmith!.hasFeature(k),
|
|
178
|
+
value: flagsmith!.getValue(k),
|
|
179
|
+
}
|
|
180
|
+
})
|
|
181
|
+
.concat(
|
|
182
|
+
traits?.map((v) => {
|
|
183
|
+
res[v] = flagsmith!.getTrait(v)
|
|
184
|
+
})
|
|
185
|
+
)
|
|
186
|
+
return res
|
|
187
|
+
}, [renderRef])
|
|
188
|
+
|
|
189
|
+
return res as UseFlagsReturn<F, T>
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
export function useFlagsmith<F extends string | Record<string, any>, T extends string = string>() {
|
|
193
|
+
const context = useContext(FlagsmithContext)
|
|
194
|
+
|
|
195
|
+
if (!context) {
|
|
196
|
+
throw new Error('useFlagsmith must be used with in a FlagsmithProvider')
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
return context as unknown as IFlagsmith<F, T>
|
|
200
|
+
}
|
package/src/readme.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
This folder contains auto-generated sourcemaps.
|
package/src/types.d.ts
ADDED
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
import { EvaluationContext, IdentityEvaluationContext, TraitEvaluationContext } from "./evaluation-context";
|
|
2
|
+
import { FlagSource } from "./flagsmith-core";
|
|
3
|
+
|
|
4
|
+
type IFlagsmithValue<T = string | number | boolean | null> = T
|
|
5
|
+
|
|
6
|
+
export type DynatraceObject = {
|
|
7
|
+
"javaLongOrObject": Record<string, number>,
|
|
8
|
+
"date": Record<string, Date>,
|
|
9
|
+
"shortString": Record<string, string>,
|
|
10
|
+
"javaDouble": Record<string, number>,
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface IFlagsmithFeature<Value = IFlagsmithValue> {
|
|
14
|
+
id?: number;
|
|
15
|
+
enabled: boolean;
|
|
16
|
+
value: Value;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export declare type IFlagsmithTrait = IFlagsmithValue | TraitEvaluationContext;
|
|
20
|
+
export declare type IFlags<F extends string = string> = Record<F, IFlagsmithFeature>;
|
|
21
|
+
export declare type ITraits<T extends string = string> = Record<T, IFlagsmithTrait>;
|
|
22
|
+
export declare type Traits<T extends string = string> = Record<T, TraitEvaluationContext | null>;
|
|
23
|
+
|
|
24
|
+
export interface ClientIdentityEvaluationContext extends Omit<IdentityEvaluationContext, "traits"> {
|
|
25
|
+
traits?: null | ITraits;
|
|
26
|
+
}
|
|
27
|
+
export interface ClientEvaluationContext extends Omit<EvaluationContext, "identity"> {
|
|
28
|
+
identity?: null | ClientIdentityEvaluationContext;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export declare type GetValueOptions<T = Array<any> | object> = {
|
|
32
|
+
skipAnalytics?: boolean
|
|
33
|
+
json?: boolean
|
|
34
|
+
fallback?: T
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export declare type HasFeatureOptions = {
|
|
38
|
+
skipAnalytics?: boolean
|
|
39
|
+
fallback?: boolean
|
|
40
|
+
} | boolean
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
export declare type IIdentity<T = string> = T;
|
|
44
|
+
|
|
45
|
+
export interface IRetrieveInfo {
|
|
46
|
+
isFromServer: boolean;
|
|
47
|
+
flagsChanged: string[] | null;
|
|
48
|
+
traitsChanged: string[] | null;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface IState<F extends string = string> {
|
|
52
|
+
api: string;
|
|
53
|
+
flags?: IFlags<FKey<F>>;
|
|
54
|
+
evaluationContext?: EvaluationContext;
|
|
55
|
+
evaluationEvent?: Record<string, Record<string, number>> | null;
|
|
56
|
+
ts?: number;
|
|
57
|
+
identity?: string;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
declare type ICacheOptions = {
|
|
61
|
+
ttl?: number;
|
|
62
|
+
skipAPI?: boolean;
|
|
63
|
+
storageKey?: string;
|
|
64
|
+
loadStale?: boolean;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
export declare type IDatadogRum = {
|
|
68
|
+
trackTraits: boolean
|
|
69
|
+
client: {
|
|
70
|
+
setUser: (newUser: {
|
|
71
|
+
[x: string]: unknown
|
|
72
|
+
}) => void;
|
|
73
|
+
getUser: () => {
|
|
74
|
+
[x: string]: unknown
|
|
75
|
+
};
|
|
76
|
+
[extraProps: string]: any
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
export type ISentryClient = {
|
|
82
|
+
getIntegrationByName(name:"FeatureFlags"): {
|
|
83
|
+
addFeatureFlag(flag: string, enabled: boolean): void;
|
|
84
|
+
} | undefined;
|
|
85
|
+
} | undefined;
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
export { FlagSource };
|
|
89
|
+
|
|
90
|
+
export declare type LoadingState = {
|
|
91
|
+
error: Error | null, // Current error, resets on next attempt to fetch flags
|
|
92
|
+
isFetching: boolean, // Whether there is a current request to fetch server flags
|
|
93
|
+
isLoading: boolean, // Whether any flag data exists
|
|
94
|
+
source: FlagSource // Indicates freshness of flags
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export type OnChange<F extends string = string> = (previousFlags: IFlags<FKey<F>> | null, params: IRetrieveInfo, loadingState:LoadingState) => void
|
|
98
|
+
|
|
99
|
+
export type ApplicationMetadata = {
|
|
100
|
+
name: string;
|
|
101
|
+
version?: string;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export interface IInitConfig<F extends string | Record<string, any> = string, T extends string = string> {
|
|
105
|
+
AsyncStorage?: any;
|
|
106
|
+
api?: string;
|
|
107
|
+
evaluationContext?: ClientEvaluationContext;
|
|
108
|
+
cacheFlags?: boolean;
|
|
109
|
+
cacheOptions?: ICacheOptions;
|
|
110
|
+
datadogRum?: IDatadogRum;
|
|
111
|
+
sentryClient?: ISentryClient;
|
|
112
|
+
defaultFlags?: IFlags<FKey<F>>;
|
|
113
|
+
fetch?: any;
|
|
114
|
+
realtime?: boolean;
|
|
115
|
+
eventSourceUrl?: string;
|
|
116
|
+
enableAnalytics?: boolean;
|
|
117
|
+
enableDynatrace?: boolean;
|
|
118
|
+
enableLogs?: boolean;
|
|
119
|
+
angularHttpClient?: any;
|
|
120
|
+
environmentID?: string;
|
|
121
|
+
headers?: object;
|
|
122
|
+
identity?: IIdentity;
|
|
123
|
+
traits?: ITraits<T>;
|
|
124
|
+
onChange?: OnChange<FKey<F>>;
|
|
125
|
+
onError?: (err: Error) => void;
|
|
126
|
+
preventFetch?: boolean;
|
|
127
|
+
state?: IState;
|
|
128
|
+
_trigger?: () => void;
|
|
129
|
+
_triggerLoadingState?: () => void;
|
|
130
|
+
/**
|
|
131
|
+
* Customer application metadata
|
|
132
|
+
*/
|
|
133
|
+
applicationMetadata?: ApplicationMetadata;
|
|
134
|
+
/**
|
|
135
|
+
* Configuration for the evaluation analytics pipeline. When provided,
|
|
136
|
+
* individual flag evaluation events are buffered and sent to the pipeline endpoint.
|
|
137
|
+
*/
|
|
138
|
+
evaluationAnalyticsConfig?: {
|
|
139
|
+
/** URL of the pipeline server (e.g. 'https://analytics.flagsmith.com/'). */
|
|
140
|
+
analyticsServerUrl: string;
|
|
141
|
+
/** Maximum events to buffer in memory before dropping oldest. Default 1000. */
|
|
142
|
+
maxBuffer?: number;
|
|
143
|
+
/** Flush interval in milliseconds. Set to 0 to flush immediately after each evaluation. Default 10000 (10s). */
|
|
144
|
+
flushInterval?: number;
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export interface IPipelineEvent {
|
|
149
|
+
event_id: string; // flag_name or event_name
|
|
150
|
+
event_type: 'flag_evaluation' | 'custom_event';
|
|
151
|
+
evaluated_at: number;
|
|
152
|
+
identity_identifier: string | null;
|
|
153
|
+
enabled?: boolean | null;
|
|
154
|
+
value: IFlagsmithValue;
|
|
155
|
+
traits?: { [key: string]: null | TraitEvaluationContext } | null;
|
|
156
|
+
metadata?: Record<string, any> | null;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export interface IPipelineEventBatch {
|
|
160
|
+
events: IPipelineEvent[];
|
|
161
|
+
environment_key: string;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
export interface IFlagsmithResponse {
|
|
165
|
+
identifier?: string,
|
|
166
|
+
traits?: {
|
|
167
|
+
trait_key: string;
|
|
168
|
+
trait_value: IFlagsmithValue;
|
|
169
|
+
transient?: boolean;
|
|
170
|
+
}[];
|
|
171
|
+
flags?: {
|
|
172
|
+
enabled: boolean;
|
|
173
|
+
feature_state_value: IFlagsmithValue;
|
|
174
|
+
feature: {
|
|
175
|
+
id: number;
|
|
176
|
+
name: string;
|
|
177
|
+
};
|
|
178
|
+
}[];
|
|
179
|
+
}
|
|
180
|
+
type FKey<F> = F extends string ? F : keyof F;
|
|
181
|
+
type FValue<F, K extends FKey<F>> = F extends Record<string, any>
|
|
182
|
+
? F[K] | null
|
|
183
|
+
: IFlagsmithValue;
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Example usage:
|
|
187
|
+
*
|
|
188
|
+
* // A) Using string flags:
|
|
189
|
+
* import flagsmith from 'flagsmith' as IFlagsmith<"featureOne"|"featureTwo">;
|
|
190
|
+
*
|
|
191
|
+
* // B) Using an object for F - this can be generated by our CLI: https://github.com/Flagsmith/flagsmith-cli :
|
|
192
|
+
* interface MyFeatureInterface {
|
|
193
|
+
* featureOne: string;
|
|
194
|
+
* featureTwo: number;
|
|
195
|
+
* }
|
|
196
|
+
* import flagsmith from 'flagsmith' as IFlagsmith<MyFeatureInterface>;
|
|
197
|
+
*/
|
|
198
|
+
export interface IFlagsmith<
|
|
199
|
+
F extends string | Record<string, any> = string,
|
|
200
|
+
T extends string = string
|
|
201
|
+
>
|
|
202
|
+
{
|
|
203
|
+
/**
|
|
204
|
+
* Initialise the sdk against a particular environment
|
|
205
|
+
*/
|
|
206
|
+
init: (config: IInitConfig<FKey<F>, T>) => Promise<void>;
|
|
207
|
+
/**
|
|
208
|
+
* Set evaluation context. Refresh the flags.
|
|
209
|
+
*/
|
|
210
|
+
setContext: (context: ClientEvaluationContext) => Promise<void>;
|
|
211
|
+
/**
|
|
212
|
+
* Merge current evaluation context with the provided one. Refresh the flags.
|
|
213
|
+
*/
|
|
214
|
+
updateContext: (context: ClientEvaluationContext) => Promise<void>;
|
|
215
|
+
/**
|
|
216
|
+
/**
|
|
217
|
+
* Get current context.
|
|
218
|
+
*/
|
|
219
|
+
getContext: () => EvaluationContext;
|
|
220
|
+
/**
|
|
221
|
+
* Trigger a manual fetch of the environment features
|
|
222
|
+
*/
|
|
223
|
+
getFlags: () => Promise<void>;
|
|
224
|
+
/**
|
|
225
|
+
* Returns the current flags
|
|
226
|
+
*/
|
|
227
|
+
getAllFlags: () => IFlags<FKey<F>>;
|
|
228
|
+
/**
|
|
229
|
+
* Identify user, triggers a call to get flags if `flagsmith.init` has been called
|
|
230
|
+
* */
|
|
231
|
+
identify: (userId: string, traits?: Record<T, IFlagsmithValue>) => Promise<void>;
|
|
232
|
+
/**
|
|
233
|
+
* Retrieves the current state of flagsmith
|
|
234
|
+
*/
|
|
235
|
+
getState: () => IState;
|
|
236
|
+
/**
|
|
237
|
+
* Set the current state of flagsmith
|
|
238
|
+
*/
|
|
239
|
+
setState: (state: IState) => void;
|
|
240
|
+
/**
|
|
241
|
+
* Clears the identity, triggers a call to getFlags
|
|
242
|
+
*/
|
|
243
|
+
logout: () => Promise<void>;
|
|
244
|
+
/**
|
|
245
|
+
* Polls the flagsmith API, specify interval in ms
|
|
246
|
+
*/
|
|
247
|
+
startListening: (interval?: number) => void;
|
|
248
|
+
/**
|
|
249
|
+
* Stops polling
|
|
250
|
+
*/
|
|
251
|
+
stopListening: () => void;
|
|
252
|
+
/**
|
|
253
|
+
* Returns whether a feature is enabled, or a fallback value if it does not exist.
|
|
254
|
+
* @param {HasFeatureOptions} [optionsOrSkipAnalytics=false] If `true`, will not track analytics for this flag
|
|
255
|
+
* evaluation. Using a boolean for this parameter is deprecated - use `{ skipAnalytics: true }` instead.
|
|
256
|
+
* @param [optionsOrSkipAnalytics.fallback=false] Returns this value if the feature does not exist.
|
|
257
|
+
* @param [optionsOrSkipAnalytics.skipAnalytics=false] If `true`, do not track analytics for this feature evaluation.
|
|
258
|
+
* @example
|
|
259
|
+
* flagsmith.hasFeature("power_user_feature")
|
|
260
|
+
* @example
|
|
261
|
+
* flagsmith.hasFeature("enabled_by_default_feature", { fallback: true })
|
|
262
|
+
*/
|
|
263
|
+
hasFeature: (key: FKey<F>, optionsOrSkipAnalytics?: HasFeatureOptions) => boolean;
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* Returns the value of a feature, or a fallback value.
|
|
267
|
+
* @param [options.json=false] Deserialise the feature value using `JSON.parse` and return the result or `options.fallback`.
|
|
268
|
+
* @param [options.fallback=null] Return this value in any of these cases:
|
|
269
|
+
* * The feature does not exist.
|
|
270
|
+
* * The feature has no value.
|
|
271
|
+
* * `options.json` is `true` and the feature's value is not valid JSON.
|
|
272
|
+
* @param [options.skipAnalytics=false] If `true`, do not track analytics for this feature evaluation.
|
|
273
|
+
* @param [skipAnalytics=false] Deprecated - use `options.skipAnalytics` instead.
|
|
274
|
+
* @example
|
|
275
|
+
* flagsmith.getValue("remote_config") // "{\"hello\":\"world\"}"
|
|
276
|
+
* flagsmith.getValue("remote_config", { json: true }) // { hello: "world" }
|
|
277
|
+
* @example
|
|
278
|
+
* flagsmith.getValue("font_size") // "12px"
|
|
279
|
+
* flagsmith.getValue("font_size", { json: true, fallback: "8px" }) // "8px"
|
|
280
|
+
*/
|
|
281
|
+
getValue<K extends FKey<F>>(
|
|
282
|
+
key: K,
|
|
283
|
+
options?: GetValueOptions<FValue<F, K>>,
|
|
284
|
+
skipAnalytics?: boolean
|
|
285
|
+
): IFlagsmithValue<FValue<F, K>>;
|
|
286
|
+
/**
|
|
287
|
+
* Get the value of a particular trait for the identified user
|
|
288
|
+
*/
|
|
289
|
+
getTrait: (key: T) => IFlagsmithValue;
|
|
290
|
+
/**
|
|
291
|
+
* Get the values of all traits for the identified user
|
|
292
|
+
*/
|
|
293
|
+
getAllTraits: () => Record<string, IFlagsmithValue>;
|
|
294
|
+
/**
|
|
295
|
+
* Set a specific trait for a given user id, triggers a call to get flags
|
|
296
|
+
* */
|
|
297
|
+
setTrait: (key: T, value: IFlagsmithTrait) => Promise<void>;
|
|
298
|
+
/**
|
|
299
|
+
* Set a key value set of traits for a given user, triggers a call to get flags
|
|
300
|
+
*/
|
|
301
|
+
setTraits: (traits: ITraits) => Promise<void>;
|
|
302
|
+
/**
|
|
303
|
+
* The stored identity of the user
|
|
304
|
+
*/
|
|
305
|
+
identity?: IIdentity;
|
|
306
|
+
/**
|
|
307
|
+
* Whether the flagsmith SDK is initialised
|
|
308
|
+
*/
|
|
309
|
+
initialised?: boolean;
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* Returns ths current loading state
|
|
313
|
+
*/
|
|
314
|
+
loadingState?: LoadingState;
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* Used internally, this function will callback separately to onChange whenever flags are updated
|
|
318
|
+
*/
|
|
319
|
+
_trigger?: () => void;
|
|
320
|
+
/**
|
|
321
|
+
* Used internally, this function will trigger the useFlagsmithLoading hook when loading state changes
|
|
322
|
+
*/
|
|
323
|
+
_triggerLoadingState?: () => void;
|
|
324
|
+
/**
|
|
325
|
+
* Used internally, this is the cache options provided in flagsmith.init
|
|
326
|
+
*/
|
|
327
|
+
cacheOptions: {
|
|
328
|
+
ttl: number;
|
|
329
|
+
skipAPI: boolean;
|
|
330
|
+
loadStale: boolean;
|
|
331
|
+
};
|
|
332
|
+
/**
|
|
333
|
+
* Used internally, this is the api provided in flagsmith.init, defaults to our production API
|
|
334
|
+
*/
|
|
335
|
+
api: string
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
export {};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export default (angularHttpClient: any) => (url: string, params: {
|
|
2
|
+
headers: Record<string, string>,
|
|
3
|
+
method: "GET" | "POST" | "PUT",
|
|
4
|
+
body?: string
|
|
5
|
+
}) => {
|
|
6
|
+
const { headers, method, body } = params;
|
|
7
|
+
const options = { headers, observe: 'response', responseType: 'text' };
|
|
8
|
+
|
|
9
|
+
const buildResponse = (response: any, ok: boolean) => {
|
|
10
|
+
const { status, headers, body, error, message } = response;
|
|
11
|
+
return {
|
|
12
|
+
status: status ?? (ok ? 200 : 500),
|
|
13
|
+
ok,
|
|
14
|
+
headers: { get: (name: string) => headers?.get?.(name) ?? null },
|
|
15
|
+
text: () => {
|
|
16
|
+
const value = body ?? error ?? message ?? '';
|
|
17
|
+
return Promise.resolve(typeof value !== 'string' ? JSON.stringify(value) : value);
|
|
18
|
+
},
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
return new Promise((resolve) => {
|
|
23
|
+
const onNext = (res: any) => resolve(buildResponse(res, res.status ? res.status >= 200 && res.status < 300 : true));
|
|
24
|
+
const onError = (err: any) => resolve(buildResponse(err, false));
|
|
25
|
+
switch (method) {
|
|
26
|
+
case "GET":
|
|
27
|
+
return angularHttpClient.get(url, options).subscribe(onNext, onError);
|
|
28
|
+
case "POST":
|
|
29
|
+
return angularHttpClient.post(url, body ?? '', options).subscribe(onNext, onError);
|
|
30
|
+
case "PUT":
|
|
31
|
+
return angularHttpClient.post(url, body ?? '', options).subscribe(onNext, onError);
|
|
32
|
+
default:
|
|
33
|
+
return onError({ status: 405, message: `Unsupported method: ${method}` });
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
};
|