Skip to content

Commit b6c9d9d

Browse files
committed
refactor(network): replace networkName with resolvedNetwork in various files
1 parent 68ffaa9 commit b6c9d9d

File tree

5 files changed

+215
-87
lines changed

5 files changed

+215
-87
lines changed

packages/e2e/src/e2e.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ describe('all', () => {
278278
pkpPublicKey: ctx.aliceViemAccountPkp.pubkey,
279279
clientLitClient: ctx.litClient,
280280
fallbackLitClient: ctx.litClient,
281-
networkName: process.env['NETWORK'] ?? 'naga-dev',
281+
resolvedNetwork: ctx.resolvedNetwork,
282282
})
283283
);
284284
});

packages/e2e/src/helper/network.ts

Lines changed: 165 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,171 @@
1-
const NETWORK_IMPORT_MAP = {
2-
'naga-dev': 'nagaDev',
3-
'naga-test': 'nagaTest',
4-
'naga-local': 'nagaLocal',
5-
'naga-staging': 'nagaStaging',
1+
import { z } from 'zod';
2+
3+
/**
4+
* Canonical metadata for Lit e2e network targets.
5+
* - `importName` feeds `@lit-protocol/networks` dynamic imports.
6+
* - `type` lets higher level helpers branch on local vs live behaviour.
7+
*
8+
* @example
9+
* ```ts
10+
* NETWORKS['naga-dev'];
11+
* // { importName: 'nagaDev', type: 'live' }
12+
* ```
13+
*/
14+
export const NETWORKS = {
15+
'naga-dev': { importName: 'nagaDev', type: 'live' },
16+
'naga-test': { importName: 'nagaTest', type: 'live' },
17+
'naga-local': { importName: 'nagaLocal', type: 'local' },
18+
'naga-staging': { importName: 'nagaStaging', type: 'live' },
619
} as const;
720

8-
export type SupportedNetwork = keyof typeof NETWORK_IMPORT_MAP;
21+
export type NetworkName = keyof typeof NETWORKS;
922

10-
export function resolveNetworkImportName(
11-
network?: string
12-
): (typeof NETWORK_IMPORT_MAP)[SupportedNetwork] {
13-
const key = (network as SupportedNetwork | undefined) ?? 'naga-dev';
14-
if (key in NETWORK_IMPORT_MAP) {
15-
return NETWORK_IMPORT_MAP[key];
16-
}
23+
export type NetworkConfig = (typeof NETWORKS)[NetworkName];
24+
25+
export type NetworkType = NetworkConfig['type'];
26+
27+
export type NetworkImportName = NetworkConfig['importName'];
28+
29+
const NETWORK_NAME_VALUES = Object.keys(NETWORKS) as NetworkName[];
30+
31+
const NETWORK_NAME_TUPLE = NETWORK_NAME_VALUES as [
32+
NetworkName,
33+
...NetworkName[]
34+
];
35+
36+
/**
37+
* Shared schema so callers can parse env/config values consistently.
38+
*
39+
* @example
40+
* ```ts
41+
* NetworkNameSchema.parse('naga-local');
42+
* // 'naga-local'
43+
* ```
44+
*/
45+
export const NetworkNameSchema = z.enum(NETWORK_NAME_TUPLE);
1746

18-
throw new Error(
19-
`Unsupported network "${network}". Supported networks: ${Object.keys(
20-
NETWORK_IMPORT_MAP
21-
).join(', ')}`
47+
export const DEFAULT_NETWORK_NAME: NetworkName = 'naga-dev';
48+
49+
/**
50+
* Ordered list of network identifiers. Useful when presenting choices to users.
51+
*
52+
* @example
53+
* ```ts
54+
* SUPPORTED_NETWORK_NAMES;
55+
* // ['naga-dev', 'naga-test', 'naga-local', 'naga-staging']
56+
* ```
57+
*/
58+
export const SUPPORTED_NETWORK_NAMES = [...NETWORK_NAME_VALUES] as const;
59+
60+
type NetworksModule = typeof import('@lit-protocol/networks');
61+
62+
export type LitNetworkModule = NetworksModule[NetworkImportName];
63+
64+
/**
65+
* Type guard used when consuming untyped sources such as env variables.
66+
*
67+
* @example
68+
* ```ts
69+
* isNetworkName('naga-test');
70+
* // true
71+
*
72+
* isNetworkName('unknown-network');
73+
* // false
74+
* ```
75+
*/
76+
export function isNetworkName(value: unknown): value is NetworkName {
77+
return (
78+
typeof value === 'string' &&
79+
Object.prototype.hasOwnProperty.call(NETWORKS, value)
2280
);
2381
}
82+
83+
/**
84+
* Normalises any caller-provided identifier to the canonical network tuple used
85+
* by init flows and tests. Always returns a full `NETWORKS` entry alongside the
86+
* resolved name, so callers can keep a single source of truth for network metadata.
87+
*
88+
* @example
89+
* ```ts
90+
* getNetworkConfig('naga-test');
91+
* // { name: 'naga-test', importName: 'nagaTest', type: 'live' }
92+
*
93+
* getNetworkConfig();
94+
* // { name: 'naga-dev', importName: 'nagaDev', type: 'live' }
95+
* ```
96+
*/
97+
export function getNetworkConfig(network?: string): {
98+
name: NetworkName;
99+
importName: NetworkImportName;
100+
type: NetworkType;
101+
} {
102+
const candidate = (network ?? DEFAULT_NETWORK_NAME) as string;
103+
104+
if (!isNetworkName(candidate)) {
105+
throw new Error(
106+
`Unsupported network "${network}". Supported networks: ${SUPPORTED_NETWORK_NAMES.join(
107+
', '
108+
)}`
109+
);
110+
}
111+
112+
const name: NetworkName = candidate;
113+
const { importName, type } = NETWORKS[name];
114+
115+
return { name, importName, type };
116+
}
117+
118+
/**
119+
* Convenience wrapper used where only the `importName` string matters.
120+
*
121+
* @example
122+
* ```ts
123+
* resolveNetworkImportName('naga-local');
124+
* // 'nagaLocal'
125+
* ```
126+
*/
127+
export function resolveNetworkImportName(network?: string): NetworkImportName {
128+
return getNetworkConfig(network).importName;
129+
}
130+
131+
export type ResolveNetworkOptions = {
132+
network?: string;
133+
rpcUrlOverride?: string;
134+
};
135+
136+
export type ResolvedNetwork = {
137+
name: NetworkName;
138+
importName: NetworkImportName;
139+
type: NetworkType;
140+
networkModule: LitNetworkModule;
141+
};
142+
143+
/**
144+
* Fully resolves a Lit network by combining metadata with the backing module.
145+
*
146+
* @example
147+
* ```ts
148+
* const { name, networkModule } = await resolveNetwork({
149+
* network: 'naga-local',
150+
* rpcUrlOverride: 'http://127.0.0.1:8545',
151+
* });
152+
* // name === 'naga-local'
153+
* // networkModule is the hydrated Lit network module with overrides applied
154+
* ```
155+
*/
156+
export async function resolveNetwork(
157+
options: ResolveNetworkOptions = {}
158+
): Promise<ResolvedNetwork> {
159+
const { network, rpcUrlOverride } = options;
160+
const { name, importName, type } = getNetworkConfig(network);
161+
162+
const networksModule: NetworksModule = await import('@lit-protocol/networks');
163+
const baseNetworkModule = networksModule[importName];
164+
165+
const networkModule =
166+
rpcUrlOverride && typeof baseNetworkModule.withOverrides === 'function'
167+
? baseNetworkModule.withOverrides({ rpcUrl: rpcUrlOverride })
168+
: baseNetworkModule;
169+
170+
return { name, importName, type, networkModule };
171+
}

0 commit comments

Comments
 (0)