@berachain/edge-config-helpers 0.1.0

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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) Berachain Foundation
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/dist/index.cjs ADDED
@@ -0,0 +1 @@
1
+ "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; }function s(n){return n.missingContext==="closed"?!1:n.defaultReturn}function f(n){if(!_optionalChain([n, 'access', _ => _.flag, 'access', _2 => _2.chain, 'optionalAccess', _3 => _3.length]))return n.gate.defaultReturn;let t=_optionalChain([n, 'access', _4 => _4.chains, 'access', _5 => _5.find, 'call', _6 => _6(i=>i.id===n.chainId), 'optionalAccess', _7 => _7.name]);return t?n.flag.chain.includes(t):s(n.gate)}function T(n){if(!_optionalChain([n, 'access', _8 => _8.flag, 'access', _9 => _9.branch, 'optionalAccess', _10 => _10.length]))return n.gate.defaultReturn;if(!n.environment)return s(n.gate);let t=n.environment==="development"?"preview":n.environment;return n.flag.branch.includes(t)}function x(n){return _optionalChain([n, 'access', _11 => _11.flag, 'access', _12 => _12.app, 'optionalAccess', _13 => _13.length])?n.app?n.flag.app.includes(n.app):s(n.gate):n.gate.defaultReturn}function h(n){let t=n.flag[n.key];return t?n.key==="startAt"?new Date(t)<=new Date:new Date(t)>=new Date:n.defaultReturn}function C(n){let t=_nullishCoalesce(n.missingContext, () => ("open"));function i(e){return _optionalChain([n, 'access', _14 => _14.chains, 'access', _15 => _15.find, 'call', _16 => _16(a=>a.id===e), 'optionalAccess', _17 => _17.name])}function p(e){return e==="production"}function u(e,a){if(!e)return!1;if(typeof e=="boolean")return e;let r={defaultReturn:e.condition!=="OR",missingContext:t},o=f({flag:e,chainId:a.chainId,chains:n.chains,gate:r}),d=T({flag:e,environment:a.environment,gate:r}),c=x({flag:e,app:a.app,gate:r}),g=h({flag:e,key:"startAt",defaultReturn:r.defaultReturn}),l=h({flag:e,key:"endAt",defaultReturn:r.defaultReturn});return e.condition==="OR"?o||d||c||g||l:o&&d&&c&&g&&l}return{chainName:i,isProduction:p,isFlagEnabled:u}}exports.createEdgeHelpers = C;
@@ -0,0 +1,45 @@
1
+ export type ChainTarget<TName extends string = string> = {
2
+ readonly id: number;
3
+ readonly name: TName;
4
+ };
5
+ export type FlagBranch = "production" | "staging" | "preview";
6
+ /**
7
+ * Structural flag value. Each deployment's generated schema should be
8
+ * assignable to this.
9
+ */
10
+ export type DynamicEnabled<TChain extends string = string, TApp extends string = string> = boolean | {
11
+ chain?: readonly TChain[];
12
+ branch?: readonly FlagBranch[];
13
+ app?: readonly TApp[];
14
+ startAt?: string;
15
+ endAt?: string;
16
+ condition?: "AND" | "OR";
17
+ };
18
+ export type EdgeContext<TApp extends string = string> = {
19
+ chainId: number;
20
+ environment?: string;
21
+ app?: TApp;
22
+ };
23
+ /**
24
+ * What to do when a criterion is set but the current value is missing.
25
+ *
26
+ * - `open` — historical monobera behavior: a missing value does not fail an
27
+ * AND gate (and does not satisfy an OR gate).
28
+ * - `closed` — a configured gate with no current value does not match.
29
+ */
30
+ export type MissingContext = "open" | "closed";
31
+ /**
32
+ * Pure edge-config evaluation. Does not read `process.env`.
33
+ *
34
+ * Call once per deployment with that deployment's chain ids and names.
35
+ * Pass the current chain, environment, and app on every `isFlagEnabled` call.
36
+ */
37
+ export declare function createEdgeHelpers<const TChain extends string, const TApp extends string = string>(config: {
38
+ chains: readonly ChainTarget<TChain>[];
39
+ missingContext?: MissingContext;
40
+ }): {
41
+ chainName: (chainId: number) => TChain | undefined;
42
+ isProduction: (environment: string | undefined) => boolean;
43
+ isFlagEnabled: (flag: DynamicEnabled<TChain, TApp> | null | undefined, current: EdgeContext<TApp>) => boolean;
44
+ };
45
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,WAAW,CAAC,KAAK,SAAS,MAAM,GAAG,MAAM,IAAI;IACvD,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,IAAI,EAAE,KAAK,CAAC;CACtB,CAAC;AAEF,MAAM,MAAM,UAAU,GAAG,YAAY,GAAG,SAAS,GAAG,SAAS,CAAC;AAE9D;;;GAGG;AACH,MAAM,MAAM,cAAc,CACxB,MAAM,SAAS,MAAM,GAAG,MAAM,EAC9B,IAAI,SAAS,MAAM,GAAG,MAAM,IAE1B,OAAO,GACP;IACE,KAAK,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IAC1B,MAAM,CAAC,EAAE,SAAS,UAAU,EAAE,CAAC;IAC/B,GAAG,CAAC,EAAE,SAAS,IAAI,EAAE,CAAC;IACtB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,SAAS,CAAC,EAAE,KAAK,GAAG,IAAI,CAAC;CAC1B,CAAC;AAEN,MAAM,MAAM,WAAW,CAAC,IAAI,SAAS,MAAM,GAAG,MAAM,IAAI;IACtD,OAAO,EAAE,MAAM,CAAC;IAChB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,GAAG,CAAC,EAAE,IAAI,CAAC;CACZ,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,MAAM,cAAc,GAAG,MAAM,GAAG,QAAQ,CAAC;AAqF/C;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAC/B,KAAK,CAAC,MAAM,SAAS,MAAM,EAC3B,KAAK,CAAC,IAAI,SAAS,MAAM,GAAG,MAAM,EAClC,MAAM,EAAE;IACR,MAAM,EAAE,SAAS,WAAW,CAAC,MAAM,CAAC,EAAE,CAAC;IACvC,cAAc,CAAC,EAAE,cAAc,CAAC;CACjC;yBAG6B,MAAM,KAAG,MAAM,GAAG,SAAS;gCAIpB,MAAM,GAAG,SAAS,KAAG,OAAO;0BAKvD,cAAc,CAAC,MAAM,EAAE,IAAI,CAAC,GAAG,IAAI,GAAG,SAAS,WAC5C,WAAW,CAAC,IAAI,CAAC,KACzB,OAAO;EA+CX"}
package/dist/index.mjs ADDED
@@ -0,0 +1 @@
1
+ function s(n){return n.missingContext==="closed"?!1:n.defaultReturn}function f(n){if(!n.flag.chain?.length)return n.gate.defaultReturn;let t=n.chains.find(i=>i.id===n.chainId)?.name;return t?n.flag.chain.includes(t):s(n.gate)}function T(n){if(!n.flag.branch?.length)return n.gate.defaultReturn;if(!n.environment)return s(n.gate);let t=n.environment==="development"?"preview":n.environment;return n.flag.branch.includes(t)}function x(n){return n.flag.app?.length?n.app?n.flag.app.includes(n.app):s(n.gate):n.gate.defaultReturn}function h(n){let t=n.flag[n.key];return t?n.key==="startAt"?new Date(t)<=new Date:new Date(t)>=new Date:n.defaultReturn}function C(n){let t=n.missingContext??"open";function i(e){return n.chains.find(a=>a.id===e)?.name}function p(e){return e==="production"}function u(e,a){if(!e)return!1;if(typeof e=="boolean")return e;let r={defaultReturn:e.condition!=="OR",missingContext:t},o=f({flag:e,chainId:a.chainId,chains:n.chains,gate:r}),d=T({flag:e,environment:a.environment,gate:r}),c=x({flag:e,app:a.app,gate:r}),g=h({flag:e,key:"startAt",defaultReturn:r.defaultReturn}),l=h({flag:e,key:"endAt",defaultReturn:r.defaultReturn});return e.condition==="OR"?o||d||c||g||l:o&&d&&c&&g&&l}return{chainName:i,isProduction:p,isFlagEnabled:u}}export{C as createEdgeHelpers};
package/package.json ADDED
@@ -0,0 +1,32 @@
1
+ {
2
+ "name": "@berachain/edge-config-helpers",
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "sideEffects": false,
6
+ "files": [
7
+ "dist"
8
+ ],
9
+ "author": "Berachain",
10
+ "license": "MIT",
11
+ "devDependencies": {
12
+ "@types/node": "24.1.0",
13
+ "tsup": "8.5.1",
14
+ "typescript": "npm:@typescript/typescript6@6.0.2",
15
+ "vitest": "3.2.6",
16
+ "@berachain/utils": "^0.1.0"
17
+ },
18
+ "exports": {
19
+ ".": {
20
+ "types": "./dist/index.d.ts",
21
+ "require": "./dist/index.cjs",
22
+ "default": "./dist/index.mjs"
23
+ }
24
+ },
25
+ "scripts": {
26
+ "build": "tsup -- --cjs --esm",
27
+ "clean": "git clean -xdf dist .turbo node_modules",
28
+ "dev": "tsup --watch",
29
+ "check-types": "node ../../.scripts/commands.mjs check-types:dts",
30
+ "test": "vitest run --root ../.. --project edge-config-helpers"
31
+ }
32
+ }