Updating session kit to ui-16 + localization support

This commit is contained in:
Aaron Cox
2023-02-28 16:55:19 -05:00
parent 65ba4e82cc
commit 1b8e13e855
6 changed files with 63 additions and 24 deletions
+2 -2
View File
@@ -19,7 +19,7 @@
"tslib": "^2.1.0"
},
"peerDependencies": {
"@wharfkit/session": "^0.3.0-ui-14.1"
"@wharfkit/session": "^0.3.0-ui-16"
},
"devDependencies": {
"@rollup/plugin-alias": "^3.1.4",
@@ -34,7 +34,7 @@
"@types/node": "^18.7.18",
"@typescript-eslint/eslint-plugin": "^5.20.0",
"@typescript-eslint/parser": "^5.20.0",
"@wharfkit/session": "^0.3.0-ui-14.1",
"@wharfkit/session": "^0.3.0-ui-16",
"@wharfkit/wallet-plugin-privatekey": "^0.3.0-ui-13.1",
"chai": "^4.3.4",
"eslint": "^8.13.0",
+3 -2
View File
@@ -1,5 +1,6 @@
import fs from 'fs'
import dts from 'rollup-plugin-dts'
import json from '@rollup/plugin-json'
import typescript from '@rollup/plugin-typescript'
import pkg from './package.json'
@@ -29,7 +30,7 @@ export default [
sourcemap: true,
exports: 'named',
},
plugins: [typescript({target: 'es6'})],
plugins: [typescript({target: 'es6'}), json()],
external,
},
{
@@ -40,7 +41,7 @@ export default [
format: 'esm',
sourcemap: true,
},
plugins: [typescript({target: 'es2020'})],
plugins: [typescript({target: 'es2020'}), json()],
external,
},
{
+33 -16
View File
@@ -2,41 +2,56 @@ import {
AbstractTransactPlugin,
Cancelable,
Canceled,
LocaleDefinitions,
PromptResponse,
TransactContext,
TransactHookResponse,
TransactHookTypes,
} from '@wharfkit/session'
import defaultTranslations from './translations.json'
export interface MockOptions {
prompt: boolean
promptOptions: {
continueOnDecline: boolean
timeout: number
}
translations?: LocaleDefinitions
}
export class TransactPluginMock extends AbstractTransactPlugin {
/** Define any translations for this plugin */
public translations = defaultTranslations
/** Set a unique ID for the plugin */
public id = 'transact-plugin-mock'
/** Save the options being passed specifically for this plugin */
readonly options?: MockOptions
constructor(options?: MockOptions) {
super()
this.options = options
}
/** Register any hooks required for the plugin to operate */
register(context: TransactContext): void {
context.addHook(TransactHookTypes.beforeSign, async (request, context) => {
if (this.options?.prompt && context.ui) {
// Customize the body to present the developer with information aboutt this prompt
let body = 'An example prompt from the TransactPluginMock for testing purposes.'
// Retrieve translation helper from the UI, passing the app ID
const t = context.ui.getTranslate(this.id)
// Customize the body to present the developer with information about this prompt
let body = t('body', {
default: 'An example prompt from the TransactPluginMock for testing purposes.',
})
if (this.options.promptOptions.timeout) {
body = `${body} This prompt will automatically cancel in ${
this.options.promptOptions.timeout / 1000
} seconds.`
body +=
' ' +
t('timeout', {
default: `This prompt will automatically cancel in {{timeout}} seconds.`,
timeout: this.options.promptOptions.timeout / 1000,
})
}
// Initiate a new cancelable prompt to inform the user of the fee required
console.log('TransactPluginMock called context.ui.prompt().')
const prompt: Cancelable<PromptResponse> = context.ui.prompt({
title: 'Example Prompt!',
title: t('title', {default: 'Example Prompt!'}),
body,
elements: [
{
@@ -44,16 +59,20 @@ export class TransactPluginMock extends AbstractTransactPlugin {
},
],
})
// Create a timer to test the external cancelation of the prompt, if defined
let timer
if (this.options.promptOptions.timeout) {
console.log('TransactPluginMock setTimeout has begun.')
const {timeout} = this.options.promptOptions
timer = setTimeout(() => {
console.log('TransactPluginMock setTimeout has executed.')
if (!context.ui) {
throw new Error('No UI defined')
}
prompt.cancel(
`Test prompt timed out automatically after ${timeout / 1000} seconds.`
t('timeout-trigger', {
default: `Test prompt timed out automatically after {{timeout}} seconds.`,
timeout: timeout / 1000,
})
)
}, timeout)
}
@@ -63,7 +82,7 @@ export class TransactPluginMock extends AbstractTransactPlugin {
.then(async () => {
// If the prompt was accepted, return the request
console.log('Prompt was accepted and returned to TransactPluginMock.')
return new Promise((r) => r({request})) as Promise<TransactHookResponse>
return
})
.catch((e) => {
// Throw if what we caught was a cancelation of the prompt to abort
@@ -84,7 +103,8 @@ export class TransactPluginMock extends AbstractTransactPlugin {
// If the prompt was rejected, and we're configured to continue, return the request
console.log('Prompt was rejected and returned to TransactPluginMock.')
// Otherwise if it wasn't a cancel, it was a reject, and continue without modification
return new Promise((r) => r({request})) as Promise<TransactHookResponse>
console.log('returning void')
return
} else {
// Otherwise if it was rejected and we shouldn't continue, and throw an error
console.log('Prompt was rejected and threw an error.')
@@ -99,9 +119,6 @@ export class TransactPluginMock extends AbstractTransactPlugin {
}
})
}
return {
request,
}
})
}
}
+20
View File
@@ -0,0 +1,20 @@
{
"en": {
"title": "Example Prompt!",
"body": "An example prompt from the TransactPluginMock for testing purposes.",
"timeout": "This prompt will automatically cancel in {{timeout}} seconds.",
"timeout-trigger": "Test prompt timed out automatically after {{timeout}} seconds."
},
"ko": {
"title": "예제 프롬프트!",
"body": "테스트 목적으로 TransactPluginMock에서 프롬프트 예제입니다.",
"timeout": "이 프롬프트는 {{timeout}} 초 후에 자동으로 취소됩니다.",
"timeout-trigger": "{{timeout}}초 후에 테스트 프롬프트가 자동으로 시간 초과되었습니다."
},
"zh": {
"title": "示例提示!",
"body": "来自 TransactPluginMock 的示例提示,用于测试目的。",
"timeout": "此提示将在 {{timeout}} 秒内自动取消。",
"timeout-trigger": "测试提示在 {{timeout}} 秒后自动超时。"
}
}
+1
View File
@@ -9,6 +9,7 @@
"module": "es2020",
"moduleResolution": "node",
"noImplicitAny": false,
"resolveJsonModule": true,
"sourceMap": true,
"strict": true,
"target": "es2020"
+4 -4
View File
@@ -550,10 +550,10 @@
"@typescript-eslint/types" "5.47.1"
eslint-visitor-keys "^3.3.0"
"@wharfkit/session@^0.3.0-ui-14.1":
version "0.3.0-ui-14.1"
resolved "https://registry.yarnpkg.com/@wharfkit/session/-/session-0.3.0-ui-14.1.tgz#9958e4a2ba681a23e2dd197ed12159cf51c56091"
integrity sha512-38RYkdVUPVdIWyf/BAdWuv5zengMvrmh2zWEv6cXDHE9sENXLNyqmSFMUX6wCaQd0GYY4+ydxFLV9IeYtO8Vjg==
"@wharfkit/session@^0.3.0-ui-16":
version "0.3.0-ui-16"
resolved "https://registry.yarnpkg.com/@wharfkit/session/-/session-0.3.0-ui-16.tgz#4859de0c6047dd98cd195ee8603b51f410269086"
integrity sha512-O5DxdEu82EEkVMhRlNmqdLMDqg4MK2kSUi1YA27VNqpBVnOQqOlDGYvruV0e6KD+/ZBhsQaElMW/pQIsRoqw2w==
dependencies:
"@greymass/eosio" "^0.6.9"
eosio-signing-request "^2.5.2"