From 7e02ed41ff8ba18948454ad92803c5cc909fe69a Mon Sep 17 00:00:00 2001 From: Aaron Cox Date: Wed, 17 Jan 2024 10:05:01 -0800 Subject: [PATCH] Allow changing of API endpoint for an active Session Fixes #73 --- src/session.ts | 7 +++++++ test/tests/session.ts | 41 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/src/session.ts b/src/session.ts index e325a05..193a4f0 100644 --- a/src/session.ts +++ b/src/session.ts @@ -191,6 +191,13 @@ export class Session { return new APIClient({provider: new FetchProvider(this.chain.url, {fetch: this.fetch})}) } + /** + * Alters the session config to change the API endpoint in use + */ + setEndpoint(url: string) { + this.chain.url = url + } + /** * Templates in any missing fields from partial transactions. * diff --git a/test/tests/session.ts b/test/tests/session.ts index 270b9ee..75df4e5 100644 --- a/test/tests/session.ts +++ b/test/tests/session.ts @@ -1,7 +1,15 @@ import {assert} from 'chai' import SessionKit, {BaseTransactPlugin, ChainDefinition, Session, SessionOptions} from '$lib' -import {ABI, ABIDef, Name, PermissionLevel, Signature, TimePointSec} from '@wharfkit/antelope' +import { + ABI, + ABIDef, + FetchProvider, + Name, + PermissionLevel, + Signature, + TimePointSec, +} from '@wharfkit/antelope' import {mockFetch} from '@wharfkit/mock-data' import {MockTransactPlugin, MockTransactResourceProviderPlugin} from '@wharfkit/mock-data' @@ -470,4 +478,35 @@ suite('session', function () { assert.instanceOf(signatures[0], Signature) }) }) + suite('change api', function () { + test('able to change api endpoint', async function () { + // Start with a Session + const testSession = new Session( + { + chain: { + id: '73e4385a2708e6d7048834fbc1079f2fabb17b3c125b146af438971e90716c4d', + url: 'https://jungle4.greymass.com', + }, + permissionLevel: 'account@permission', + walletPlugin: new WalletPluginPrivateKey( + '5Jtoxgny5tT7NiNFp1MLogviuPJ9NniWjnU4wKzaX4t7pL4kJ8s' + ), + }, + { + fetch: mockFetch, + } + ) + + // Check for the default API endpoint + const provider = testSession.client.provider as FetchProvider + assert.equal(provider.url, 'https://jungle4.greymass.com') + + // Change the API endpoint + testSession.setEndpoint('https://wax.greymass.com') + + // Check for that the API endpoint has changed + const provider2 = testSession.client.provider as FetchProvider + assert.equal(provider2.url, 'https://wax.greymass.com') + }) + }) })