@lunora/flags 1.0.0-alpha.5 → 1.0.0-alpha.51

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,100 +0,0 @@
1
- import { LunoraError } from '@lunora/errors';
2
- import { OpenFeature, ErrorCode } from '@openfeature/server-sdk';
3
-
4
- const DOMAIN = "lunora";
5
- let clientBinding;
6
- const bindClient = ({ hooks, logger, provider }) => {
7
- if (clientBinding === void 0) {
8
- clientBinding = (async () => {
9
- await OpenFeature.setProviderAndWait(DOMAIN, provider());
10
- const client = OpenFeature.getClient(DOMAIN);
11
- if (logger) {
12
- client.setLogger(logger);
13
- }
14
- if (hooks && hooks.length > 0) {
15
- client.addHooks(...hooks);
16
- }
17
- return client;
18
- })();
19
- clientBinding.catch(() => {
20
- clientBinding = void 0;
21
- });
22
- }
23
- return clientBinding;
24
- };
25
- const resetFlags = async () => {
26
- clientBinding = void 0;
27
- await OpenFeature.clearProviders();
28
- };
29
- const memoKey = (type, flagKey, defaultValue, context) => {
30
- const contextKeys = Object.keys(context);
31
- const prefix = JSON.stringify([type, flagKey, defaultValue]);
32
- if (contextKeys.length === 0) {
33
- return `${prefix}:[]`;
34
- }
35
- const entries = contextKeys.toSorted((a, b) => a.localeCompare(b)).map((name) => [name, context[name]]);
36
- return `${prefix}:${JSON.stringify(entries)}`;
37
- };
38
- const resolveDetails = (client, type, flagKey, defaultValue, context) => {
39
- switch (type) {
40
- case "boolean": {
41
- return client.getBooleanDetails(flagKey, defaultValue, context);
42
- }
43
- case "number": {
44
- return client.getNumberDetails(flagKey, defaultValue, context);
45
- }
46
- case "object": {
47
- return client.getObjectDetails(flagKey, defaultValue, context);
48
- }
49
- case "string": {
50
- return client.getStringDetails(flagKey, defaultValue, context);
51
- }
52
- default: {
53
- throw new LunoraError("INTERNAL", `createFlags: unknown flag type "${type}"`);
54
- }
55
- }
56
- };
57
- const createFlags = (options) => {
58
- const { hooks, logger, provider, targetingKey } = options;
59
- let resolvedTargetingKey;
60
- try {
61
- resolvedTargetingKey = typeof targetingKey === "function" ? targetingKey() : targetingKey;
62
- } catch {
63
- resolvedTargetingKey = void 0;
64
- }
65
- const memo = /* @__PURE__ */ new Map();
66
- const evaluate = (type, flagKey, defaultValue, context) => {
67
- const merged = resolvedTargetingKey === void 0 ? { ...context } : { targetingKey: resolvedTargetingKey, ...context };
68
- const key = memoKey(type, flagKey, defaultValue, merged);
69
- const cached = memo.get(key);
70
- if (cached) {
71
- return cached;
72
- }
73
- const pending = bindClient({ hooks, logger, provider }).then((client) => resolveDetails(client, type, flagKey, defaultValue, merged)).catch((error) => {
74
- return {
75
- errorCode: ErrorCode.GENERAL,
76
- errorMessage: error instanceof Error ? error.message : String(error),
77
- flagKey,
78
- flagMetadata: {},
79
- reason: "ERROR",
80
- value: defaultValue
81
- };
82
- });
83
- memo.set(key, pending);
84
- return pending;
85
- };
86
- return {
87
- boolean: (flagKey, defaultValue, context) => evaluate("boolean", flagKey, defaultValue, context).then((details) => details.value),
88
- details: {
89
- boolean: (flagKey, defaultValue, context) => evaluate("boolean", flagKey, defaultValue, context),
90
- number: (flagKey, defaultValue, context) => evaluate("number", flagKey, defaultValue, context),
91
- object: (flagKey, defaultValue, context) => evaluate("object", flagKey, defaultValue, context),
92
- string: (flagKey, defaultValue, context) => evaluate("string", flagKey, defaultValue, context)
93
- },
94
- number: (flagKey, defaultValue, context) => evaluate("number", flagKey, defaultValue, context).then((details) => details.value),
95
- object: (flagKey, defaultValue, context) => evaluate("object", flagKey, defaultValue, context).then((details) => details.value),
96
- string: (flagKey, defaultValue, context) => evaluate("string", flagKey, defaultValue, context).then((details) => details.value)
97
- };
98
- };
99
-
100
- export { createFlags, resetFlags };
@@ -1,15 +0,0 @@
1
- const defineFlags = (config) => {
2
- if (typeof config.provider !== "function") {
3
- throw new TypeError('defineFlags: `provider` must be a function `(env) => Provider` (e.g. flagshipProvider({ binding: "FLAGS" }))');
4
- }
5
- if (config.identify !== void 0 && typeof config.identify !== "function") {
6
- throw new TypeError("defineFlags: `identify` must be a function `(auth) => string | undefined` when provided");
7
- }
8
- if (config.hooks !== void 0 && !Array.isArray(config.hooks)) {
9
- throw new TypeError("defineFlags: `hooks` must be an array of OpenFeature hooks when provided");
10
- }
11
- return { ...config, isLunoraFlags: true };
12
- };
13
- const isFlagsDefinition = (value) => typeof value === "object" && value !== null && value.isLunoraFlags === true;
14
-
15
- export { defineFlags, isFlagsDefinition };