Skip to content

feat: (WIP) Support arbitrary analytics endpoint URLs #285

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

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
20 changes: 12 additions & 8 deletions flagsmith-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ let AsyncStorage: AsyncStorageType = null;
const DEFAULT_FLAGSMITH_KEY = "FLAGSMITH_DB";
const DEFAULT_FLAGSMITH_EVENT = "FLAGSMITH_EVENT";
let FlagsmithEvent = DEFAULT_FLAGSMITH_EVENT;
const defaultAPI = 'https://edge.api.flagsmith.com/api/v1/';
export const DEFAULT_API_URL = 'https://edge.api.flagsmith.com/api/v1/';
export const ANALYTICS_ENDPOINT = "./analytics/flags/"
let eventSource: typeof EventSource;
const initError = function(caller: string) {
return "Attempted to " + caller + " a user before calling flagsmith.init. Call flagsmith.init first, if you wish to prevent it sending a request for flags, call init with preventFetch:true."
Expand Down Expand Up @@ -226,15 +227,15 @@ const Flagsmith = class {
};

analyticsFlags = () => {
const { api } = this;
const { analyticsUrl } = this;

if (!this.evaluationEvent || !this.evaluationContext.environment || !this.evaluationEvent[this.evaluationContext.environment.apiKey]) {
if (!analyticsUrl || !this.evaluationEvent || !this.evaluationContext.environment || !this.evaluationEvent[this.evaluationContext.environment.apiKey]) {
return
}

if (this.evaluationEvent && Object.getOwnPropertyNames(this.evaluationEvent).length !== 0 && Object.getOwnPropertyNames(this.evaluationEvent[this.evaluationContext.environment.apiKey]).length !== 0) {
return this.getJSON(api + 'analytics/flags/', 'POST', JSON.stringify(this.evaluationEvent[this.evaluationContext.environment.apiKey]))
.then((res) => {
return this.getJSON(analyticsUrl, 'POST', JSON.stringify(this.evaluationEvent[this.evaluationContext.environment.apiKey]))
.then(() => {
if (!this.evaluationContext.environment) {
return;
}
Expand All @@ -259,6 +260,7 @@ const Flagsmith = class {
canUseStorage = false
analyticsInterval: NodeJS.Timer | null= null
api: string|null= null
analyticsUrl: string | null = null
cacheFlags= false
ts: number|null= null
enableAnalytics= false
Expand All @@ -282,7 +284,7 @@ const Flagsmith = class {
try {
const {
environmentID,
api = defaultAPI,
api = DEFAULT_API_URL,
headers,
onChange,
cacheFlags,
Expand All @@ -294,6 +296,7 @@ const Flagsmith = class {
enableLogs,
enableDynatrace,
enableAnalytics,
analyticsUrl,
realtime,
eventSourceUrl= "https://realtime.flagsmith.com/",
AsyncStorage: _AsyncStorage,
Expand All @@ -319,11 +322,12 @@ const Flagsmith = class {
} : evaluationContext.identity;
this.evaluationContext = evaluationContext;
this.api = api;
this.analyticsUrl = analyticsUrl || new URL(ANALYTICS_ENDPOINT, new Request(api).url).href
this.headers = headers;
this.getFlagInterval = null;
this.analyticsInterval = null;
this.onChange = onChange;
const WRONG_FLAGSMITH_CONFIG = 'Wrong Flagsmith Configuration: preventFetch is true and no defaulFlags provided'
const WRONG_FLAGSMITH_CONFIG = 'Wrong Flagsmith Configuration: preventFetch is true and no defaultFlags provided'
this._trigger = _trigger || this._trigger;
this._triggerLoadingState = _triggerLoadingState || this._triggerLoadingState;
this.onError = (message: Error) => {
Expand Down Expand Up @@ -587,7 +591,7 @@ const Flagsmith = class {
setState(state: IState) {
if (state) {
this.initialised = true;
this.api = state.api || this.api || defaultAPI;
this.api = state.api || this.api || DEFAULT_API_URL;
this.flags = state.flags || this.flags;
this.evaluationContext = state.evaluationContext || this.evaluationContext,
this.evaluationEvent = state.evaluationEvent || this.evaluationEvent;
Expand Down
6 changes: 6 additions & 0 deletions test/init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,4 +257,10 @@ describe('Flagsmith.init', () => {
});
expect(onError).toHaveBeenCalledWith(new Error('Mocked fetch error'));
});
test('should resolve analytics URL, if not specified, relative to API URL', async () => {
const { flagsmith, initConfig } = getFlagsmith()
initConfig.api = 'https://flagsmith.example.com'
await flagsmith.init(initConfig)
expect(flagsmith.analyticsUrl).toEqual('https://flagsmith.example.com/analytics/flags/')
})
});
17 changes: 16 additions & 1 deletion types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,18 @@ export declare type LoadingState = {
export type OnChange<F extends string = string> = (previousFlags: IFlags<F> | null, params: IRetrieveInfo, loadingState:LoadingState) => void
export interface IInitConfig<F extends string = string, T extends string = string> {
AsyncStorage?: any;
/** Absolute and versioned URL of Flagsmith API to use, including a trailing slash.
* Defaults to {@link DEFAULT_API_URL}.
* @example "https://flagsmith.example.com/api/v1/
*/
api?: string;
/**
* Absolute or relative URL of the Flagsmith analytics events API endpoint. Use this if your analytics and flags API
* endpoints are on different domains, e.g. if you are fetching flags from an Edge Proxy.
* Defaults to {@link ANALYTICS_ENDPOINT} relative to {@link api}.
* @example "https://flagsmith.example.com/api/v1/analytics"
*/
analyticsUrl?: string;
evaluationContext?: ClientEvaluationContext;
cacheFlags?: boolean;
cacheOptions?: ICacheOptions;
Expand Down Expand Up @@ -270,9 +281,13 @@ export interface IFlagsmith<F extends string = string, T extends string = string
loadStale: boolean;
};
/**
* Used internally, this is the api provided in flagsmith.init, defaults to our production API
* @see IInitConfig.api
*/
api: string
/**
* @see IInitConfig.analyticsUrl
*/
analyticsUrl: string
}

export {};
Loading