-
Notifications
You must be signed in to change notification settings - Fork 44
Add Optimizely adapter #164
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
Open
blurrah
wants to merge
22
commits into
main
Choose a base branch
from
optimizely-adapter-edge
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
2f27453
chore: use bundler moduleResolution for tsconfig
blurrah 2425732
feat: add first steps towards adapter for optimizely
blurrah 7a96cbd
feat: add edge-config friendly project config manager and event dispa…
blurrah 351f620
chore: add edge-config package for optimizely
blurrah d62fc76
chore: update naming for project config manager
blurrah 6f9903e
chore: remove runtime origin
blurrah 0c1e59b
feat: support without edge config and add fetch request handler
blurrah 0e1a9c9
feat: add value getter for optimizely decision
blurrah d3842ab
chore: clean up comment and type
blurrah cabaa0d
fix(optimizely): only work with main entrypoint for now
blurrah 2e62300
feat: use universal export to replace XHR with fetch for requests
blurrah 02019f9
refactor: move edge config project manager to index.ts
blurrah bd1e712
fix: use static project manager and allow usage without sdk key
blurrah edef4ae
fix: use universal exports and custom request handler where possible
blurrah e3b0cc0
fix: working decisions after testing
blurrah a0fb5be
feat: add userContext helper
blurrah afe6678
chore: update optimizely sdk to latest version
blurrah 4d8a141
fix(sveltekit): do not allow partial record for request event
blurrah fa52982
feat: allow complete user context from identify function
blurrah 1e0f864
chore: clean up types and old decide type signature
blurrah 2e09aa4
fix: remove attributes from decide function
blurrah bcd73bd
add changeset
dferber90 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@flags-sdk/optimizely': minor | ||
--- | ||
|
||
Implement adapter to resolve feature flags |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import type { LogEvent } from '@optimizely/optimizely-sdk'; | ||
|
||
/** | ||
* Web standards friendly event dispatcher for Optimizely | ||
* uses `waitUntil()` to avoid blocking the visitor's page load | ||
* | ||
* This does not send back the status code to the dispatcher as it runs in `waitUntil()` | ||
*/ | ||
export async function dispatchEvent(event: LogEvent) { | ||
// Non-POST requests not supported | ||
if (event.httpVerb !== 'POST') { | ||
throw new Error( | ||
'Optimizely Event Dispatcher: Only POST requests are supported', | ||
); | ||
} | ||
|
||
const url = new URL(event.url); | ||
const data = JSON.stringify(event.params); | ||
|
||
const dispatch = fetch(url, { | ||
method: 'POST', | ||
body: data, | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
}); | ||
|
||
import('@vercel/functions').then(({ waitUntil }) => { | ||
waitUntil(dispatch); | ||
}); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,224 @@ | ||
export { getProviderData } from './provider'; | ||
import { | ||
Client, | ||
OpaqueConfigManager, | ||
OptimizelyDecision, | ||
UserAttributes, | ||
} from '@optimizely/optimizely-sdk'; | ||
|
||
import type { Adapter, JsonValue } from 'flags'; | ||
import { dispatchEvent } from './edge-runtime-hooks'; | ||
import { | ||
createForwardingEventProcessor, | ||
createInstance, | ||
createPollingProjectConfigManager, | ||
createStaticProjectConfigManager, | ||
RequestHandler, | ||
} from '@optimizely/optimizely-sdk/universal'; | ||
import { createClient } from '@vercel/edge-config'; | ||
|
||
let defaultOptimizelyAdapter: | ||
| ReturnType<typeof createOptimizelyAdapter> | ||
| undefined; | ||
|
||
/** | ||
* The user context for the Optimizely adapter | ||
*/ | ||
export type UserContext = { | ||
userId: string; | ||
attributes?: UserAttributes; | ||
}; | ||
|
||
type AdapterResponse = { | ||
decide: <T>( | ||
getValue: (decision: OptimizelyDecision) => T, | ||
) => Adapter<T, UserContext>; | ||
initialize: () => Promise<Client>; | ||
}; | ||
|
||
/** | ||
* The node instance has a hardcoded XHR request handler that will break in edge runtime, | ||
* so we need to use a custom request handler that uses fetch. | ||
*/ | ||
const requestHandler: RequestHandler = { | ||
makeRequest(requestUrl, headers, method, data) { | ||
const abortController = new AbortController(); | ||
|
||
const responsePromise = fetch(requestUrl, { | ||
headers: headers as Record<string, string>, | ||
method, | ||
body: data, | ||
signal: abortController.signal, | ||
}); | ||
return { | ||
abort: () => abortController.abort(), | ||
responsePromise: responsePromise.then(async (response) => { | ||
const headersObj: Record<string, string> = {}; | ||
response.headers.forEach((value, key) => { | ||
headersObj[key] = value; | ||
}); | ||
return { | ||
statusCode: response.status, | ||
body: (await response.text()) ?? '', | ||
headers: headersObj, | ||
}; | ||
}), | ||
}; | ||
}, | ||
}; | ||
|
||
export function createOptimizelyAdapter({ | ||
sdkKey, | ||
edgeConfig, | ||
}: { | ||
sdkKey?: string; | ||
edgeConfig?: { | ||
connectionString: string; | ||
itemKey: string; | ||
}; | ||
}): AdapterResponse { | ||
let optimizelyInstance: Client | undefined; | ||
|
||
const initializeOptimizely = async () => { | ||
let projectConfigManager: OpaqueConfigManager | undefined; | ||
if (edgeConfig) { | ||
const edgeConfigClient = createClient(edgeConfig.connectionString); | ||
const datafile = await edgeConfigClient.get<JsonValue>( | ||
edgeConfig.itemKey, | ||
); | ||
|
||
if (!datafile) { | ||
throw new Error( | ||
'Optimizely Adapter: Could not get datafile from edge config', | ||
); | ||
} | ||
|
||
projectConfigManager = createStaticProjectConfigManager({ | ||
datafile: JSON.stringify(datafile), | ||
}); | ||
} | ||
|
||
if (!projectConfigManager && sdkKey) { | ||
projectConfigManager = createPollingProjectConfigManager({ | ||
sdkKey: sdkKey, | ||
updateInterval: 10000, | ||
requestHandler, | ||
}); | ||
} | ||
|
||
if (!projectConfigManager) { | ||
throw new Error( | ||
'Optimizely Adapter: Could not create project config manager, either edgeConfig or sdkKey must be provided', | ||
); | ||
} | ||
|
||
try { | ||
optimizelyInstance = createInstance({ | ||
clientEngine: 'javascript-sdk/flags-sdk', | ||
projectConfigManager, | ||
// @ts-expect-error - dispatchEvent runs in `waitUntil` so it's not going to return a response | ||
eventProcessor: createForwardingEventProcessor({ dispatchEvent }), | ||
requestHandler, | ||
}); | ||
} catch (error) { | ||
throw new Error( | ||
`Optimizely Adapter: Error creating optimizely instance, ${ | ||
error instanceof Error ? error.message : error | ||
}`, | ||
); | ||
} | ||
|
||
// This resolves instantly when using the edge config, the timeout is just for fetching the datafile from the polling project config manager | ||
await optimizelyInstance.onReady({ timeout: 500 }); | ||
}; | ||
|
||
let _initializePromise: Promise<void> | undefined; | ||
const initialize = async () => { | ||
if (!_initializePromise) { | ||
_initializePromise = initializeOptimizely(); | ||
} | ||
await _initializePromise; | ||
if (!optimizelyInstance) { | ||
throw new Error( | ||
'Optimizely Adapter: Optimizely instance not initialized', | ||
); | ||
} | ||
return optimizelyInstance; | ||
}; | ||
|
||
function decide<T>( | ||
getValue: (decision: OptimizelyDecision) => T, | ||
): Adapter<T, UserContext> { | ||
return { | ||
decide: async ({ key, entities }) => { | ||
await initialize(); | ||
if (!optimizelyInstance) { | ||
throw new Error( | ||
'Optimizely Adapter: Optimizely instance not initialized', | ||
); | ||
} | ||
if (!entities || !entities.userId) { | ||
throw new Error('Optimizely Adapter: User ID not provided'); | ||
} | ||
const context = optimizelyInstance.createUserContext( | ||
entities?.userId, | ||
entities?.attributes, | ||
); | ||
return getValue(context.decide(key)); | ||
}, | ||
}; | ||
} | ||
|
||
return { | ||
decide, | ||
initialize, | ||
}; | ||
} | ||
|
||
function getOrCreateDefaultOptimizelyAdapter(): AdapterResponse { | ||
const sdkKey = process.env.OPTIMIZELY_SDK_KEY; | ||
const edgeConfigConnectionString = process.env.EDGE_CONFIG_CONNECTION_STRING; | ||
const edgeConfigItemKey = process.env.OPTIMIZELY_DATAFILE_ITEM_KEY; | ||
|
||
if (!defaultOptimizelyAdapter) { | ||
if (edgeConfigConnectionString && edgeConfigItemKey) { | ||
defaultOptimizelyAdapter = createOptimizelyAdapter({ | ||
sdkKey, | ||
edgeConfig: { | ||
connectionString: edgeConfigConnectionString, | ||
itemKey: edgeConfigItemKey, | ||
}, | ||
}); | ||
} else { | ||
defaultOptimizelyAdapter = createOptimizelyAdapter({ | ||
sdkKey, | ||
}); | ||
} | ||
} | ||
return defaultOptimizelyAdapter; | ||
} | ||
|
||
/** | ||
* The default Optimizely adapter. | ||
* | ||
* This is a convenience object that pre-initializes the Optimizely SDK and provides | ||
* the adapter functions for the Feature Flags. | ||
* | ||
* This is the recommended way to use the Optimizely adapter. | ||
* | ||
* ```ts | ||
* // flags.ts | ||
* import { flag } from 'flags/next'; | ||
* import { optimizelyAdapter } from '@flags-sdk/optimizely'; | ||
* | ||
* const flag = flag({ | ||
* key: 'my-flag', | ||
* defaultValue: false, | ||
* adapter: optimizelyAdapter.decide((decision) => decision.enabled), | ||
* }); | ||
* ``` | ||
*/ | ||
export const optimizelyAdapter: AdapterResponse = { | ||
decide: (...args) => getOrCreateDefaultOptimizelyAdapter().decide(...args), | ||
initialize: () => getOrCreateDefaultOptimizelyAdapter().initialize(), | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is due to me updating all deps by messing around with PNPM versions, undefined is no longer allowed in Sveltekit minor updates