Skip to content

Commit

Permalink
Add default adapter pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
AAorris committed Jan 21, 2025
1 parent a08f1c5 commit fcac202
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions packages/adapter-statsig/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,44 @@ function createStatsigAdapter(options: {
}

export { createStatsigAdapter };

let defaultStatsigAdapter: ReturnType<typeof createStatsigAdapter> | undefined;

export function resetDefaultStatsigAdapter() {
defaultStatsigAdapter = undefined;
}

/**
* Equivalent to `createStatsigAdapter` but with default environment variable names.
*
* Required:
* - `STATSIG_SERVER_API_KEY` - Statsig secret server API key
*
* Optional:
* - `STATSIG_EDGE_CONFIG` - Vercel Edge Config connection string
* - `STATSIG_EDGE_CONFIG_ITEM_KEY` - Vercel Edge Config item key
*/
export default function createDefaultStatsigAdapter() {
if (defaultStatsigAdapter) {
return defaultStatsigAdapter;
}
const statsigServerApiKey = process.env.STATSIG_SERVER_API_KEY as string;
// Edge Config is optional
const edgeConfig = process.env.STATSIG_EDGE_CONFIG;
const edgeConfigItemKey = process.env.STATSIG_EDGE_CONFIG_ITEM_KEY;
if (!(edgeConfig && edgeConfigItemKey)) {
defaultStatsigAdapter = createStatsigAdapter({
statsigServerApiKey,
});
} else {
defaultStatsigAdapter = createStatsigAdapter({
statsigServerApiKey,
edgeConfig: {
connectionString: edgeConfig,
itemKey: edgeConfigItemKey,
},
});
}

return defaultStatsigAdapter;
}

0 comments on commit fcac202

Please sign in to comment.