Skip to content

[FSSDK-10992] add universal entrypoint #1008

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

Merged
merged 3 commits into from
Feb 25, 2025
Merged
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
95 changes: 95 additions & 0 deletions lib/entrypoint.universal.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/**
* Copyright 2025, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { expectTypeOf } from 'vitest';

import * as universal from './index.universal';

type WithoutReadonly<T> = { -readonly [P in keyof T]: T[P] };

const universalEntrypoint: WithoutReadonly<typeof universal> = universal;

import {
Config,
Client,
StaticConfigManagerConfig,
OpaqueConfigManager,
EventDispatcher,
OpaqueEventProcessor,
OpaqueLevelPreset,
LoggerConfig,
OpaqueLogger,
ErrorHandler,
OpaqueErrorNotifier,
} from './export_types';

import { UniversalPollingConfigManagerConfig } from './project_config/config_manager_factory.universal';
import { RequestHandler } from './utils/http_request_handler/http';
import { UniversalBatchEventProcessorOptions } from './event_processor/event_processor_factory.universal';
import {
DECISION_SOURCES,
DECISION_NOTIFICATION_TYPES,
NOTIFICATION_TYPES,
} from './utils/enums';

import { LogLevel } from './logging/logger';

import { OptimizelyDecideOption } from './shared_types';

export type UniversalEntrypoint = {
// client factory
createInstance: (config: Config) => Client | null;

// config manager related exports
createStaticProjectConfigManager: (config: StaticConfigManagerConfig) => OpaqueConfigManager;
createPollingProjectConfigManager: (config: UniversalPollingConfigManagerConfig) => OpaqueConfigManager;

// event processor related exports
createEventDispatcher: (requestHandler: RequestHandler) => EventDispatcher;
createForwardingEventProcessor: (eventDispatcher: EventDispatcher) => OpaqueEventProcessor;
createBatchEventProcessor: (options: UniversalBatchEventProcessorOptions) => OpaqueEventProcessor;

// TODO: odp manager related exports
// createOdpManager: (options: OdpManagerOptions) => OpaqueOdpManager;

// TODO: vuid manager related exports
// createVuidManager: (options: VuidManagerOptions) => OpaqueVuidManager;

// logger related exports
LogLevel: typeof LogLevel;
DebugLog: OpaqueLevelPreset,
InfoLog: OpaqueLevelPreset,
WarnLog: OpaqueLevelPreset,
ErrorLog: OpaqueLevelPreset,
createLogger: (config: LoggerConfig) => OpaqueLogger;

// error related exports
createErrorNotifier: (errorHandler: ErrorHandler) => OpaqueErrorNotifier;

// enums
DECISION_SOURCES: typeof DECISION_SOURCES;
DECISION_NOTIFICATION_TYPES: typeof DECISION_NOTIFICATION_TYPES;
NOTIFICATION_TYPES: typeof NOTIFICATION_TYPES;

// decide options
OptimizelyDecideOption: typeof OptimizelyDecideOption;

// client engine
clientEngine: string;
}


expectTypeOf(universalEntrypoint).toEqualTypeOf<UniversalEntrypoint>();
23 changes: 23 additions & 0 deletions lib/event_processor/event_dispatcher/event_dispatcher_factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Copyright 2025, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { RequestHandler } from '../../utils/http_request_handler/http';
import { DefaultEventDispatcher } from './default_dispatcher';
import { EventDispatcher } from './event_dispatcher';

export const createEventDispatcher = (requestHander: RequestHandler): EventDispatcher => {
return new DefaultEventDispatcher(requestHander);
}
56 changes: 56 additions & 0 deletions lib/event_processor/event_processor_factory.universal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* Copyright 2025, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { getForwardingEventProcessor } from './forwarding_event_processor';
import { EventDispatcher } from './event_dispatcher/event_dispatcher';

import {
getOpaqueBatchEventProcessor,
BatchEventProcessorOptions,
OpaqueEventProcessor,
wrapEventProcessor,
getPrefixEventStore,
} from './event_processor_factory';

import { FAILED_EVENT_RETRY_INTERVAL } from './event_processor_factory';

export const createForwardingEventProcessor = (
eventDispatcher: EventDispatcher
): OpaqueEventProcessor => {
return wrapEventProcessor(getForwardingEventProcessor(eventDispatcher));
};

export type UniversalBatchEventProcessorOptions = Omit<BatchEventProcessorOptions, 'eventDispatcher'> & {
eventDispatcher: EventDispatcher;
}

export const createBatchEventProcessor = (
options: UniversalBatchEventProcessorOptions
): OpaqueEventProcessor => {
const eventStore = options.eventStore ? getPrefixEventStore(options.eventStore) : undefined;

return getOpaqueBatchEventProcessor({
eventDispatcher: options.eventDispatcher,
closingEventDispatcher: options.closingEventDispatcher,
flushInterval: options.flushInterval,
batchSize: options.batchSize,
retryOptions: {
maxRetries: 5,
},
failedEventRetryInterval: FAILED_EVENT_RETRY_INTERVAL,
eventStore: eventStore,
});
};
2 changes: 0 additions & 2 deletions lib/export_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ export type {
NotificationPayload,
} from './notification_center/type';

export type { OptimizelyDecideOption } from './shared_types';

export type {
UserAttributeValue,
UserAttributes,
Expand Down
1 change: 0 additions & 1 deletion lib/index.lite.ts

This file was deleted.

115 changes: 115 additions & 0 deletions lib/index.universal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/**
* Copyright 2025, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Client, Config } from './shared_types';
import { getOptimizelyInstance } from './client_factory';
import { JAVASCRIPT_CLIENT_ENGINE } from './utils/enums';

/**
* Creates an instance of the Optimizely class
* @param {Config} config
* @return {Client|null} the Optimizely client object
* null on error
*/
export const createInstance = function(config: Config): Client | null {
return getOptimizelyInstance(config);
};

export { createEventDispatcher } from './event_processor/event_dispatcher/event_dispatcher_factory';

export { createPollingProjectConfigManager } from './project_config/config_manager_factory.universal';

export { createForwardingEventProcessor, createBatchEventProcessor } from './event_processor/event_processor_factory.universal';

// TODO: decide on universal odp manager factory interface
// export { createOdpManager } from './odp/odp_manager_factory.node';
// export { createVuidManager } from './vuid/vuid_manager_factory.node';

export * from './common_exports';

export const clientEngine: string = JAVASCRIPT_CLIENT_ENGINE;

// type exports
export type { RequestHandler } from './utils/http_request_handler/http';

// config manager related types
export type {
StaticConfigManagerConfig,
OpaqueConfigManager,
} from './project_config/config_manager_factory';

export type { UniversalPollingConfigManagerConfig } from './project_config/config_manager_factory.universal';

// event processor related types
export type {
LogEvent,
EventDispatcherResponse,
EventDispatcher,
} from './event_processor/event_dispatcher/event_dispatcher';

export type { UniversalBatchEventProcessorOptions } from './event_processor/event_processor_factory.universal';

export type {
OpaqueEventProcessor,
} from './event_processor/event_processor_factory';

// Logger related types
export type {
LogHandler,
} from './logging/logger';

export type {
OpaqueLevelPreset,
LoggerConfig,
OpaqueLogger,
} from './logging/logger_factory';

// Error related types
export type { ErrorHandler } from './error/error_handler';
export type { OpaqueErrorNotifier } from './error/error_notifier_factory';

export type { Cache } from './utils/cache/cache';

export type {
NotificationType,
NotificationPayload,
} from './notification_center/type';

export type {
UserAttributeValue,
UserAttributes,
OptimizelyConfig,
FeatureVariableValue,
OptimizelyVariable,
OptimizelyVariation,
OptimizelyExperiment,
OptimizelyFeature,
OptimizelyDecisionContext,
OptimizelyForcedDecision,
EventTags,
Event,
DatafileOptions,
UserProfileService,
UserProfile,
ListenerPayload,
OptimizelyDecision,
OptimizelyUserContext,
Config,
Client,
ActivateListenerPayload,
TrackListenerPayload,
NotificationCenter,
OptimizelySegmentOption,
} from './shared_types';
32 changes: 32 additions & 0 deletions lib/project_config/config_manager_factory.universal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Copyright 2025, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { getOpaquePollingConfigManager, OpaqueConfigManager, PollingConfigManagerConfig } from "./config_manager_factory";
import { NodeRequestHandler } from "../utils/http_request_handler/request_handler.node";
import { ProjectConfigManager } from "./project_config_manager";
import { DEFAULT_URL_TEMPLATE, DEFAULT_AUTHENTICATED_URL_TEMPLATE } from './constant';
import { RequestHandler } from "../utils/http_request_handler/http";

export type UniversalPollingConfigManagerConfig = PollingConfigManagerConfig & {
requestHandler: RequestHandler;
}

export const createPollingProjectConfigManager = (config: UniversalPollingConfigManagerConfig): OpaqueConfigManager => {
const defaultConfig = {
autoUpdate: true,
};
return getOpaquePollingConfigManager({ ...defaultConfig, ...config });
};
9 changes: 4 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,10 @@
"import": "./dist/index.react_native.es.min.js",
"require": "./dist/index.react_native.min.js"
},
"./lite": {
"types": "./dist/index.lite.d.ts",
"node": "./dist/index.lite.min.js",
"import": "./dist/index.lite.es.js",
"default": "./dist/index.lite.min.js"
"./universal": {
"types": "./dist/index.universal.d.ts",
"import": "./dist/index.universal.es.min.js",
"require": "./dist/index.universal.min.js"
}
},
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,11 @@ const bundles = {
'cjs-node-min': cjsBundleFor('node'),
'cjs-browser-min': cjsBundleFor('browser'),
'cjs-react-native-min': cjsBundleFor('react_native'),
'cjs-lite': cjsBundleFor('lite'),
'cjs-universal': cjsBundleFor('universal'),
'esm-browser-min': esmBundleFor('browser'),
'esm-node-min': esmBundleFor('node', { ext: '.mjs' }),
'esm-react-native-min': esmBundleFor('react_native'),
'esm-lite': esmBundleFor('lite'),
'esm-universal': esmBundleFor('universal'),
'json-schema': jsonSchemaBundle,
umd: umdBundle,
};
Expand Down
Loading