Skip to content

Commit

Permalink
Allow changing of API endpoint for an active Session
Browse files Browse the repository at this point in the history
Fixes #73
  • Loading branch information
aaroncox committed Jan 17, 2024
1 parent 33fda2b commit 7e02ed4
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
41 changes: 40 additions & 1 deletion test/tests/session.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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')
})
})
})

0 comments on commit 7e02ed4

Please sign in to comment.