Skip to content

Commit 172c38e

Browse files
authored
Merge pull request #18 from wharfkit/making-useable-in-ssr
Making the plugin usable in SSR
2 parents ef28220 + 89fa050 commit 172c38e

File tree

3 files changed

+33
-30
lines changed

3 files changed

+33
-30
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
"version": "1.3.1",
55
"homepage": "https://github.com/wharfkit/wallet-plugin-wombat",
66
"license": "BSD-3-Clause",
7-
"main": "lib/wallet-plugin-wombat.js",
8-
"module": "lib/wallet-plugin-wombat.m.js",
9-
"types": "lib/wallet-plugin-wombat.d.ts",
7+
"main": "lib/cjs/index.js",
8+
"module": "lib/esm/index.js",
9+
"types": "lib/types/index.d.ts",
1010
"sideEffects": false,
1111
"files": [
1212
"lib/*",

rollup.config.mjs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@ export default [
3030
input: 'src/index.ts',
3131
output: {
3232
banner,
33-
file: pkg.main,
33+
dir: pkg.main.split('/').slice(0, -1).join('/'),
3434
format: 'cjs',
3535
sourcemap: true,
3636
exports: 'named',
37+
inlineDynamicImports: true,
3738
},
3839
plugins: [
3940
typescript({target: 'es6'}),
@@ -50,7 +51,7 @@ export default [
5051
input: 'src/index.ts',
5152
output: {
5253
banner,
53-
file: pkg.module,
54+
dir: pkg.module.split('/').slice(0, -1).join('/'),
5455
format: 'esm',
5556
sourcemap: true,
5657
},
@@ -67,7 +68,7 @@ export default [
6768
},
6869
{
6970
input: 'src/index.ts',
70-
output: {banner, file: pkg.types, format: 'esm'},
71+
output: {banner, dir: pkg.types, format: 'esm'},
7172
plugins: [dts()],
7273
},
7374
]

src/index.ts

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
WalletPluginMetadata,
1111
WalletPluginSignResponse,
1212
} from '@wharfkit/session'
13-
import {handleLogin, handleLogout, handleSignatureRequest} from '@wharfkit/protocol-scatter'
1413

1514
export class WalletPluginWombat extends AbstractWalletPlugin implements WalletPlugin {
1615
id = 'wombat'
@@ -42,22 +41,26 @@ export class WalletPluginWombat extends AbstractWalletPlugin implements WalletPl
4241
homepage: 'https://www.wombat.app/',
4342
download: 'https://www.wombat.app/the-app',
4443
})
44+
45+
private async loadScatterProtocol() {
46+
if (typeof window !== 'undefined') {
47+
return import('@wharfkit/protocol-scatter')
48+
}
49+
return null
50+
}
51+
4552
/**
4653
* Performs the wallet logic required to login and return the chain and permission level to use.
4754
*
4855
* @param context LoginContext
4956
* @returns Promise<WalletPluginLoginResponse>
5057
*/
51-
login(context: LoginContext): Promise<WalletPluginLoginResponse> {
52-
return new Promise((resolve, reject) => {
53-
handleLogin(context)
54-
.then((response) => {
55-
resolve(response)
56-
})
57-
.catch((error) => {
58-
reject(error)
59-
})
60-
})
58+
async login(context: LoginContext): Promise<WalletPluginLoginResponse> {
59+
const scatterProtocol = await this.loadScatterProtocol()
60+
if (!scatterProtocol) {
61+
throw new Error('Scatter protocol is not available in this environment')
62+
}
63+
return scatterProtocol.handleLogin(context)
6164
}
6265

6366
/**
@@ -66,17 +69,12 @@ export class WalletPluginWombat extends AbstractWalletPlugin implements WalletPl
6669
* @param context: LogoutContext
6770
* @returns Promise<void>
6871
*/
69-
70-
logout(context: LogoutContext): Promise<void> {
71-
return new Promise((resolve, reject) => {
72-
handleLogout(context)
73-
.then(() => {
74-
resolve()
75-
})
76-
.catch((error) => {
77-
reject(error)
78-
})
79-
})
72+
async logout(context: LogoutContext): Promise<void> {
73+
const scatterProtocol = await this.loadScatterProtocol()
74+
if (!scatterProtocol) {
75+
throw new Error('Scatter protocol is not available in this environment')
76+
}
77+
return scatterProtocol.handleLogout(context)
8078
}
8179

8280
/**
@@ -86,10 +84,14 @@ export class WalletPluginWombat extends AbstractWalletPlugin implements WalletPl
8684
* @param resolved ResolvedSigningRequest
8785
* @returns Promise<Signature>
8886
*/
89-
sign(
87+
async sign(
9088
resolved: ResolvedSigningRequest,
9189
context: TransactContext
9290
): Promise<WalletPluginSignResponse> {
93-
return handleSignatureRequest(resolved, context)
91+
const scatterProtocol = await this.loadScatterProtocol()
92+
if (!scatterProtocol) {
93+
throw new Error('Scatter protocol is not available in this environment')
94+
}
95+
return scatterProtocol.handleSignatureRequest(resolved, context)
9496
}
9597
}

0 commit comments

Comments
 (0)