Skip to content
This repository was archived by the owner on Jan 22, 2025. It is now read-only.

Commit

Permalink
refactor(experimental): create a Connection sham that only exposes …
Browse files Browse the repository at this point in the history
…`rpcEndpoint` (#1878)
  • Loading branch information
steveluscher authored Nov 22, 2023
1 parent b77865f commit a9b5f6b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
12 changes: 12 additions & 0 deletions packages/library-legacy-sham/src/__tests__/connection-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Connection } from '../connection';

describe('ConnectionSham', () => {
it.each(['gopher:', 'tel:', 'ws://', 'wss://'])('fatals if the endpoint supplied starts with `%s`', scheme => {
expect(() => new Connection(scheme + 'url')).toThrow();
});
it.each(['https', 'http'])('offers the `%s` endpoint URL through the `rpcEndpoint` property', scheme => {
const endpoint = scheme + '://url';
const connection = new Connection(endpoint);
expect(connection).toHaveProperty('rpcEndpoint', endpoint);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
import { Connection as LegacyConnection } from '@solana/web3.js-legacy';

import { Connection } from '../connection';

new Connection('https://some.rpc').rpcEndpoint satisfies 'https://some.rpc';

// @ts-expect-error This is only a partial sham
new Connection('https://some.rpc') satisfies LegacyConnection;

// @ts-expect-error This is only a partial sham
Connection satisfies typeof LegacyConnection;
12 changes: 12 additions & 0 deletions packages/library-legacy-sham/src/connection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export class Connection<TRpcEndpoint extends string> {
#endpoint: TRpcEndpoint;
constructor(putativeEndpoint: TRpcEndpoint) {
if (/^https?:/.test(putativeEndpoint) === false) {
throw new TypeError('Endpoint URL must start with `http:` or `https:`.');
}
this.#endpoint = putativeEndpoint;
}
get rpcEndpoint() {
return this.#endpoint;
}
}

0 comments on commit a9b5f6b

Please sign in to comment.