UNPKG

@flags-sdk/flagsmith

Version:

Flagsmith provider for the Flags SDK

163 lines (161 loc) 4.2 kB
// src/index.ts import flagsmith from "flagsmith"; // src/provider/index.ts async function getProviderData(options) { const hints = []; if (!options.environmentKey) { hints.push({ key: "flagsmith/missing-environment-id", text: "Missing Flagsmith Environment ID" }); } if (!options.projectId) { hints.push({ key: "flagsmith/missing-project-id", text: "Missing Flagsmith Project ID" }); } if (hints.length > 0) return { definitions: {}, hints }; try { const res = await fetch(`https://api.flagsmith.com/api/v1/flags/`, { method: "GET", headers: { "X-Environment-Key": options.environmentKey }, // @ts-expect-error used by some Next.js versions cache: "no-store" }); if (res.status !== 200) { return { definitions: {}, hints: [ { key: `flagsmith/response-not-ok/${options.environmentKey}`, text: `Failed to fetch Flagsmith (Received ${res.status} response)` } ] }; } const data = await res.json(); const definitions = data?.reduce( (acc, flag) => { acc[flag.feature.name] = { origin: `https://app.flagsmith.com/project/${options.projectId}/environment/${options.environmentKey}/features/?feature=${flag?.id}`, description: flag.feature.description, createdAt: new Date(flag.feature.created_date).getTime(), options: [ { value: flag.feature_state_value, label: flag.feature_state_value } ] }; return acc; }, {} ); return { definitions, hints: [] }; } catch (error) { return { definitions: {}, hints: [ { key: "flagsmith/response-not-ok", text: `Failed to fetch Flagsmith` } ] }; } } // src/index.ts var defaultFlagsmithAdapter; function createFlagsmithAdapter(params) { async function initialize() { await flagsmith.init({ fetch: globalThis.fetch, ...params }); } function booleanValue() { return { async decide({ key, defaultValue, entities: identity }) { await initialize(); if (identity) { await flagsmith.identify(identity); } const state = flagsmith.getState(); const flagState = state.flags?.[key]; if (!flagState) { return defaultValue; } return flagState.enabled; } }; } function stringValue() { return { async decide({ key, defaultValue, entities: identity }) { await initialize(); if (identity) { await flagsmith.identify(identity); } const state = flagsmith.getState(); const flagState = state.flags?.[key]; if (!flagState || !flagState.enabled) { return defaultValue; } return flagState.value; } }; } function numberValue() { return { async decide({ key, defaultValue, entities: identity }) { await initialize(); if (identity) { await flagsmith.identify(identity); } const state = flagsmith.getState(); const flagState = state.flags?.[key]; if (!flagState || !flagState.enabled) { return defaultValue; } return flagState.value; } }; } return { booleanValue, stringValue, numberValue }; } function assertEnv(name) { const value = process.env[name]; console.log("value", process.env); if (!value) { throw new Error(`Flagsmith Adapter: Missing ${name} environment variable`); } return value; } var getOrCreateDefaultFlagsmithAdapter = () => { if (!defaultFlagsmithAdapter) { const environmentId = assertEnv("FLAGSMITH_ENVIRONMENT_ID"); defaultFlagsmithAdapter = createFlagsmithAdapter({ environmentID: environmentId }); } return defaultFlagsmithAdapter; }; var flagsmithAdapter = getOrCreateDefaultFlagsmithAdapter(); export { flagsmithAdapter, getOrCreateDefaultFlagsmithAdapter, getProviderData }; //# sourceMappingURL=index.js.map