enhancement: adding data field to session class (#95)

This commit is contained in:
Daniel Fugere
2025-03-25 04:07:21 +08:00
committed by GitHub
parent 07dfadb55a
commit 0e1fa8f968
4 changed files with 77 additions and 4 deletions
+6
View File
@@ -63,6 +63,7 @@ export interface RestoreArgs {
actor?: NameType
permission?: NameType
walletPlugin?: Record<string, any>
data?: Record<string, any>
}
export interface SessionKitArgs {
@@ -619,6 +620,7 @@ export class SessionKit {
id: args.walletPlugin.id,
data: args.walletPlugin.data,
},
data: args.data,
}
} else {
// Otherwise throw an error since we can't establish the session data
@@ -668,6 +670,10 @@ export class SessionKit {
this.getSessionOptions(options)
)
if (serializedSession.data) {
session.data = serializedSession.data
}
// Save the session to storage if it has a storage instance.
this.persistSession(session, options?.setAsDefault)
+31 -4
View File
@@ -80,6 +80,7 @@ export interface SerializedSession {
default?: boolean
permission: NameType
walletPlugin: SerializedWalletPlugin
data?: Record<string, any>
}
/**
@@ -100,6 +101,21 @@ export class Session {
readonly transactPluginsOptions: TransactPluginsOptions = {}
readonly ui?: UserInterface
readonly walletPlugin: WalletPlugin
private _data: Record<string, any> = {}
/**
* Get the data stored in this session instance.
*/
get data(): Record<string, any> {
return this._data
}
/**
* Set the data stored in this session instance.
*/
set data(data: Record<string, any>) {
this._data = data
}
/**
* The constructor of the `Session` class.
@@ -601,13 +617,24 @@ export class Session {
return walletResponse.signatures
}
serialize = (): SerializedSession =>
Serializer.objectify({
serialize = (): SerializedSession => {
const serializableData: Record<string, any> = {
chain: this.chain.id,
actor: this.permissionLevel.actor,
permission: this.permissionLevel.permission,
walletPlugin: this.walletPlugin.serialize(),
})
walletPlugin: {
id: this.walletPlugin.id,
data: this.walletPlugin.data,
},
}
// Only include data if it's not empty
if (Object.keys(this._data).length > 0) {
serializableData.data = this.data
}
return Serializer.objectify(serializableData)
}
getPluginTranslations(transactPlugin: TransactPlugin | WalletPlugin): LocaleDefinitions {
if (!transactPlugin.translations) {
+14
View File
@@ -364,6 +364,20 @@ suite('kit', function () {
}
assertSessionMatchesMockSession(restored)
})
test('session data', async function () {
const sessionKit = new SessionKit(mockSessionKitArgs, {
...mockSessionKitOptions,
storage: new MockStorage(),
})
const {session} = await sessionKit.login()
session.data.customField = 'data value'
sessionKit.persistSession(session)
const restored = await sessionKit.restore()
if (!restored) {
throw new Error('Failed to restore session')
}
assert.equal(restored.data.customField, 'data value')
})
test('session by chain id (checksum256)', async function () {
// New kit w/ empty storage
const sessionKit = new SessionKit(mockSessionKitArgs, {
+26
View File
@@ -426,6 +426,32 @@ suite('session', function () {
assert.doesNotThrow(() => {
JSON.stringify(serialized)
})
// Ensure data field is not present when it's empty
assert.isUndefined(serialized.data)
})
test('serializes with custom data field', function () {
const original = new Session(mockSessionArgs, mockSessionOptions)
original.data.randomField = 'randomData'
original.data.testNumber = 123
original.data.testBoolean = true
original.data.testObject = {key: 'value'}
const serialized = original.serialize()
// Check if data exists before accessing properties
assert.isDefined(serialized.data)
if (serialized.data) {
assert.equal(serialized.data.randomField, 'randomData')
assert.equal(serialized.data.testNumber, 123)
assert.equal(serialized.data.testBoolean, true)
assert.deepEqual(serialized.data.testObject, {key: 'value'})
}
// Make sure we can still stringify the serialized result
assert.doesNotThrow(() => {
JSON.stringify(serialized)
})
})
})
suite('sign transaction', function () {