This repository was archived by the owner on Jan 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 939
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(experimental): create a
Connection
sham that only exposes …
…`rpcEndpoint` (#1878)
- Loading branch information
1 parent
b77865f
commit a9b5f6b
Showing
3 changed files
with
36 additions
and
0 deletions.
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
packages/library-legacy-sham/src/__tests__/connection-test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
12 changes: 12 additions & 0 deletions
12
packages/library-legacy-sham/src/__typetests__/connection-typetests.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |