UNPKG

@flipt-io/vercel-adapter

Version:
132 lines (127 loc) 5.06 kB
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } 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 _fliptclientjs = require('@flipt-io/flipt-client-js'); var client = null; var initialize = async (options) => { if (client) { await client.refresh(); return client; } const config = { url: _nullishCoalesce(_nullishCoalesce(_optionalChain([options, 'optionalAccess', _ => _.url]), () => ( process.env.FLIPT_URL)), () => ( "http://localhost:8080")), namespace: _nullishCoalesce(_nullishCoalesce(_optionalChain([options, 'optionalAccess', _2 => _2.namespace]), () => ( process.env.FLIPT_NAMESPACE)), () => ( "default")), authentication: _nullishCoalesce(_optionalChain([options, 'optionalAccess', _3 => _3.authentication]), () => ( { clientToken: _nullishCoalesce(process.env.FLIPT_CLIENT_TOKEN, () => ( "")) })), updateInterval: _nullishCoalesce(_optionalChain([options, 'optionalAccess', _4 => _4.updateInterval]), () => ( (process.env.NODE_ENV === "development" ? 5 : void 0))) // 5 seconds in development if not set }; client = await _fliptclientjs.FliptClient.init({ ...config }); return client; }; var isFliptContext = (context) => { return context != null && typeof context === "object" && "id" in context; }; async function predecide(context) { if (!isFliptContext(context)) { throw new Error( "vercel-flipt-adapter: Invalid or missing context from identify. See https://flags-sdk.dev/concepts/identify" ); } return await Promise.resolve(context); } function createFliptAdapter(options) { async function getClient() { return initialize(options); } function boolean(getValue, _opts) { return { decide: async ({ key, entities }) => { const context = await predecide(entities); const client2 = await getClient(); const result = client2.evaluateBoolean({ flagKey: key, entityId: context.id, context: transformContext(context) }); return getValue(result); } }; } function variant(getValue, _opts) { return { decide: async ({ key, entities }) => { const context = await predecide(entities); const client2 = await getClient(); const result = client2.evaluateVariant({ flagKey: key, entityId: context.id, context: transformContext(context) }); return getValue({ variantKey: result.variantKey, attachment: _nullishCoalesce(result.variantAttachment, () => ( void 0)) }); } }; } return { boolean, variant, initialize: () => getClient() }; } function transformContext(context) { const transformedContext = {}; Object.entries(context).forEach(([key, value]) => { if (key !== "id") { transformedContext[key] = String(value); } }); return transformedContext; } var fliptAdapter = createFliptAdapter(); function __resetClientForTesting() { client = null; } async function getProviderData(options) { const hints = []; if (hints.length > 0) { return { definitions: {}, hints }; } try { const client2 = await initialize(options); const namespace = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _5 => _5.namespace]), () => ( "default")); const flags = client2.listFlags(); const baseUrl = (_nullishCoalesce(_nullishCoalesce(_optionalChain([options, 'optionalAccess', _6 => _6.url]), () => ( process.env.FLIPT_URL)), () => ( "http://localhost:8080"))).replace( /\/+$/, "" ); return { definitions: flags.reduce((acc, flag) => { const options2 = flag.type === "BOOLEAN_FLAG_TYPE" ? [ { value: true, label: "Enabled" }, { value: false, label: "Disabled" } ] : []; acc[flag.key] = { description: flag.description, origin: `${baseUrl}/#/namespaces/${namespace}/flags/${flag.key}`, options: options2 }; return acc; }, {}), hints }; } catch (e) { return { definitions: {}, hints: [ { key: "flipt/failed-to-fetch", text: `Failed to fetch Flipt flags: ${e.message}` } ] }; } } exports.__resetClientForTesting = __resetClientForTesting; exports.createFliptAdapter = createFliptAdapter; exports.fliptAdapter = fliptAdapter; exports.getProviderData = getProviderData; //# sourceMappingURL=index.js.map