mirror of
https://github.com/wharfkit/session.git
synced 2026-07-21 10:02:11 +00:00
enhancement: adding data field to session class (#95)
This commit is contained in:
@@ -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
@@ -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) {
|
||||
|
||||
@@ -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, {
|
||||
|
||||
@@ -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 () {
|
||||
|
||||
Reference in New Issue
Block a user