Skip to content

Commit

Permalink
Adds localhost to the clusterApiUrl utility with the option to change…
Browse files Browse the repository at this point in the history
… the port.
  • Loading branch information
fmendez committed Nov 22, 2021
1 parent cd6f931 commit cfbd27e
Show file tree
Hide file tree
Showing 3 changed files with 9,117 additions and 4 deletions.
10 changes: 8 additions & 2 deletions web3.js/src/util/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,32 @@ const endpoint = {
devnet: 'http://api.devnet.solana.com',
testnet: 'http://api.testnet.solana.com',
'mainnet-beta': 'http://api.mainnet-beta.solana.com',
localhost: 'http://localhost',
},
https: {
devnet: 'https://api.devnet.solana.com',
testnet: 'https://api.testnet.solana.com',
'mainnet-beta': 'https://api.mainnet-beta.solana.com',
localhost: 'https://localhost',
},
};

export type Cluster = 'devnet' | 'testnet' | 'mainnet-beta';
export type Cluster = 'devnet' | 'testnet' | 'mainnet-beta' | 'localhost';

/**
* Retrieves the RPC API URL for the specified cluster
*/
export function clusterApiUrl(cluster?: Cluster, tls?: boolean): string {
export function clusterApiUrl(cluster?: Cluster, tls?: boolean, port: string = '8899'): string {
const key = tls === false ? 'http' : 'https';

if (!cluster) {
return endpoint[key]['devnet'];
}

if (cluster === 'localhost') {
return endpoint[key]['localhost'] + `:${port}`;
}

const url = endpoint[key][cluster];
if (!url) {
throw new Error(`Unknown ${key} cluster: ${cluster}`);
Expand Down
11 changes: 9 additions & 2 deletions web3.js/test/cluster.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {expect} from 'chai';
import { expect } from 'chai';

import {clusterApiUrl} from '../src/util/cluster';
import { clusterApiUrl } from '../src/util/cluster';

describe('Cluster Util', () => {
it('invalid', () => {
Expand All @@ -19,4 +19,11 @@ describe('Cluster Util', () => {
'http://api.devnet.solana.com',
);
});

it('localhost', () => {
expect(clusterApiUrl('localhost')).to.eq('https://localhost:8899');
expect(clusterApiUrl('localhost', true)).to.eq('https://localhost:8899');
expect(clusterApiUrl('localhost', false)).to.eq('http://localhost:8899');
expect(clusterApiUrl('localhost', false, '7777')).to.eq('http://localhost:7777');
});
});
Loading

0 comments on commit cfbd27e

Please sign in to comment.