UNPKG

@flipt-io/vercel-adapter

Version:
132 lines 3.72 kB
// src/index.ts import { FliptClient } from "@flipt-io/flipt-client-js"; var client = null; var initialize = async (options) => { if (client) { await client.refresh(); return client; } const config = { url: options?.url ?? process.env.FLIPT_URL ?? "http://localhost:8080", namespace: options?.namespace ?? process.env.FLIPT_NAMESPACE ?? "default", authentication: options?.authentication ?? { clientToken: process.env.FLIPT_CLIENT_TOKEN ?? "" }, updateInterval: options?.updateInterval ?? (process.env.NODE_ENV === "development" ? 5 : void 0) // 5 seconds in development if not set }; client = await 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: 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 = options?.namespace ?? "default"; const flags = client2.listFlags(); const baseUrl = (options?.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}` } ] }; } } export { __resetClientForTesting, createFliptAdapter, fliptAdapter, getProviderData }; //# sourceMappingURL=index.mjs.map