@flags-sdk/flagsmith
Version:
Flagsmith provider for the Flags SDK
163 lines (157 loc) • 5.39 kB
JavaScript
;Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }// src/index.ts
var _flagsmith = require('flagsmith'); var _flagsmith2 = _interopRequireDefault(_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 = _optionalChain([data, 'optionalAccess', _ => _.reduce, 'call', _2 => _2(
(acc, flag) => {
acc[flag.feature.name] = {
origin: `https://app.flagsmith.com/project/${options.projectId}/environment/${options.environmentKey}/features/?feature=${_optionalChain([flag, 'optionalAccess', _3 => _3.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 _flagsmith2.default.init({ fetch: globalThis.fetch, ...params });
}
function booleanValue() {
return {
async decide({
key,
defaultValue,
entities: identity
}) {
await initialize();
if (identity) {
await _flagsmith2.default.identify(identity);
}
const state = _flagsmith2.default.getState();
const flagState = _optionalChain([state, 'access', _4 => _4.flags, 'optionalAccess', _5 => _5[key]]);
if (!flagState) {
return defaultValue;
}
return flagState.enabled;
}
};
}
function stringValue() {
return {
async decide({ key, defaultValue, entities: identity }) {
await initialize();
if (identity) {
await _flagsmith2.default.identify(identity);
}
const state = _flagsmith2.default.getState();
const flagState = _optionalChain([state, 'access', _6 => _6.flags, 'optionalAccess', _7 => _7[key]]);
if (!flagState || !flagState.enabled) {
return defaultValue;
}
return flagState.value;
}
};
}
function numberValue() {
return {
async decide({ key, defaultValue, entities: identity }) {
await initialize();
if (identity) {
await _flagsmith2.default.identify(identity);
}
const state = _flagsmith2.default.getState();
const flagState = _optionalChain([state, 'access', _8 => _8.flags, 'optionalAccess', _9 => _9[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();
exports.flagsmithAdapter = flagsmithAdapter; exports.getOrCreateDefaultFlagsmithAdapter = getOrCreateDefaultFlagsmithAdapter; exports.getProviderData = getProviderData;
//# sourceMappingURL=index.cjs.map