@evmos/dappstore-sdk
Advanced tools
Comparing version 0.0.5 to 0.0.6
# @evmos/dappstore-sdk | ||
## 0.0.6 | ||
### Patch Changes | ||
- 0fffb5f: Improve logging and emit base events on initialize | ||
## 0.0.5 | ||
@@ -4,0 +10,0 @@ |
@@ -5,2 +5,8 @@ import { createHost } from "./host"; | ||
declare class SDKProvider implements StronglyTypedEIP1193Provider { | ||
logger: { | ||
log: (...args: unknown[]) => void; | ||
error: (...args: unknown[]) => void; | ||
info: (...args: unknown[]) => void; | ||
warn: (...args: unknown[]) => void; | ||
}; | ||
unsubscribers: Record<string, Map<Function, Function>>; | ||
@@ -15,2 +21,6 @@ on(event: "accountsChanged", listener: (accounts: string[]) => void): SDKProvider; | ||
} | ||
type ClientOptions = { | ||
autoInit?: boolean; | ||
debug?: boolean; | ||
}; | ||
declare class Client { | ||
@@ -30,13 +40,22 @@ _host: null | ReturnType<typeof createHost>; | ||
get _isInsideIframe(): boolean; | ||
constructor(autoInit?: boolean); | ||
logger: { | ||
log: (...args: unknown[]) => void; | ||
error: (...args: unknown[]) => void; | ||
info: (...args: unknown[]) => void; | ||
warn: (...args: unknown[]) => void; | ||
}; | ||
_debug: boolean; | ||
set debug(value: boolean); | ||
get debug(): boolean; | ||
constructor({ autoInit, debug }?: ClientOptions); | ||
init(): Promise<() => void>; | ||
onAccountsChange(cb: (accounts: string[]) => void): () => void; | ||
onChainChange(cb: (chainId: string) => void): () => void; | ||
onAccountsChange(cb: (accounts: `0x${string}`[]) => void): () => void; | ||
private emitAccountsChange; | ||
onChainChange(cb: (chainId: `0x${string}`) => void): () => void; | ||
private emitChainChange; | ||
ack(): Promise<void>; | ||
} | ||
export declare const createDAppStoreClient: ({ autoInit }?: { | ||
autoInit?: boolean | undefined; | ||
}) => Client; | ||
export declare const createDAppStoreClient: (config?: ClientOptions) => Client; | ||
export type DAppStoreClient = Client; | ||
export {}; | ||
//# sourceMappingURL=client.d.ts.map |
@@ -960,3 +960,4 @@ // ../../node_modules/@trpc/server/dist/unstable-core-do-not-import/createProxy.mjs | ||
router, | ||
target | ||
target, | ||
ctx | ||
}) => { | ||
@@ -995,3 +996,3 @@ const { transformer } = router._def._config; | ||
getRawInput: async () => input, | ||
ctx: {}, | ||
ctx, | ||
type | ||
@@ -1028,3 +1029,3 @@ }); | ||
input, | ||
ctx: {} | ||
ctx | ||
}) | ||
@@ -1061,3 +1062,3 @@ }); | ||
input, | ||
ctx: {} | ||
ctx | ||
}) | ||
@@ -1088,3 +1089,3 @@ }); | ||
getRawInput: async () => input, | ||
ctx: {}, | ||
ctx, | ||
type | ||
@@ -1112,3 +1113,3 @@ }); | ||
input, | ||
ctx: {} | ||
ctx | ||
}) | ||
@@ -1169,3 +1170,3 @@ }); | ||
import { z as z2 } from "zod"; | ||
var t = initTRPC.create({ | ||
var t = initTRPC.context().create({ | ||
allowOutsideOfServer: true | ||
@@ -1213,3 +1214,3 @@ }); | ||
}) | ||
).query(async () => { | ||
).query(async ({}) => { | ||
const accounts = await _provider.request({ | ||
@@ -1219,4 +1220,4 @@ method: "eth_requestAccounts" | ||
const chainId = await _provider.request({ method: "eth_chainId" }); | ||
return { | ||
version: "0.0.5", | ||
const response = { | ||
version: "0.0.6", | ||
state: { | ||
@@ -1227,2 +1228,3 @@ accounts, | ||
}; | ||
return response; | ||
}), | ||
@@ -1235,6 +1237,10 @@ provider: providerRouter | ||
var createHost = ({ provider, target }) => { | ||
return createPostMessageHost({ | ||
const trpcHost = createPostMessageHost({ | ||
target, | ||
router: createHostRouter(provider) | ||
router: createHostRouter(provider), | ||
ctx: { | ||
debug: false | ||
} | ||
}); | ||
return trpcHost; | ||
}; | ||
@@ -1241,0 +1247,0 @@ export { |
@@ -0,1 +1,15 @@ | ||
// src/logger.ts | ||
var noop = () => { | ||
}; | ||
var Log = (ns, enabled = false) => { | ||
const { log, error, warn, info } = enabled ? console : { log: noop, error: noop, warn: noop, info: noop }; | ||
ns = `[${ns}]`; | ||
return { | ||
log: (...args) => log(ns, ...args), | ||
error: (...args) => error(ns, ...args), | ||
info: (...args) => info(ns, ...args), | ||
warn: (...args) => warn(ns, ...args) | ||
}; | ||
}; | ||
// ../../node_modules/@trpc/server/dist/observable/observable.mjs | ||
@@ -197,6 +211,6 @@ function identity(x) { | ||
// ../../node_modules/@trpc/server/dist/unstable-core-do-not-import/createProxy.mjs | ||
var noop = () => { | ||
var noop2 = () => { | ||
}; | ||
function createInnerProxy(callback, path) { | ||
const proxy = new Proxy(noop, { | ||
const proxy = new Proxy(noop2, { | ||
get(_obj, key) { | ||
@@ -223,3 +237,3 @@ if (typeof key !== "string" || key === "then") { | ||
var createFlatProxy = (callback) => { | ||
return new Proxy(noop, { | ||
return new Proxy(noop2, { | ||
get(_obj, name) { | ||
@@ -1990,3 +2004,2 @@ if (typeof name !== "string" || name === "then") { | ||
); | ||
console.log("dispatched", this.outgoing); | ||
this.outgoing = []; | ||
@@ -2036,3 +2049,3 @@ }, | ||
}; | ||
var postMessageLink = (opts = {}) => { | ||
var postMessageLink = (opts) => { | ||
const transformer = getTransformer(opts.transformer); | ||
@@ -2083,6 +2096,10 @@ const client = new PostMessageClient(); | ||
// src/client.ts | ||
var loggerNamespace = "DAppStoreSDK CLIENT"; | ||
var loggerProviderNamespace = "DAppStoreSDK CLIENT PROVIDER"; | ||
var SDKProvider = class { | ||
logger = Log(loggerProviderNamespace); | ||
unsubscribers = {}; | ||
on(event, listener) { | ||
if (event === "accountsChanged" || event === "chainChanged" || event === "connect") { | ||
this.logger.info("Subscribing to event", event); | ||
const { unsubscribe } = trpcClient.provider.on[event].subscribe( | ||
@@ -2105,2 +2122,3 @@ void 0, | ||
this.unsubscribers[event]?.delete(callback); | ||
this.logger.info("Removing listener", event); | ||
return this; | ||
@@ -2112,3 +2130,6 @@ } | ||
} | ||
return trpcClient.provider.request.mutate(args); | ||
this.logger.info("Sending message", args); | ||
const response = trpcClient.provider.request.mutate(args); | ||
this.logger.info("Received response", response); | ||
return response; | ||
}; | ||
@@ -2153,3 +2174,15 @@ }; | ||
} | ||
constructor(autoInit = true) { | ||
logger = Log(loggerNamespace, this.debug); | ||
// @internal | ||
_debug = false; | ||
set debug(value) { | ||
this._debug = value; | ||
this.logger = Log(loggerNamespace, value); | ||
this.provider.logger = Log(loggerProviderNamespace, value); | ||
} | ||
get debug() { | ||
return this._debug; | ||
} | ||
constructor({ autoInit = true, debug = false } = {}) { | ||
this.debug = debug; | ||
if (autoInit) | ||
@@ -2159,2 +2192,3 @@ this.initialized = this.init(); | ||
async init() { | ||
this.logger.info("Initializing DAppStore SDK"); | ||
if (this._isInsideIframe === false) { | ||
@@ -2171,7 +2205,3 @@ throw new Error( | ||
this._accounts = data; | ||
if (this._listeners.accountsChanged) { | ||
this._listeners.accountsChanged.forEach((listener) => { | ||
listener(data); | ||
}); | ||
} | ||
this.emitAccountsChange(data); | ||
} | ||
@@ -2185,7 +2215,3 @@ } | ||
this._chainId = data; | ||
if (this._listeners.chainChanged) { | ||
this._listeners.chainChanged.forEach((listener) => { | ||
listener(data); | ||
}); | ||
} | ||
this.emitChainChange(data); | ||
} | ||
@@ -2201,2 +2227,3 @@ } | ||
if (!this._listeners.accountsChanged) { | ||
this.logger.info("Subscribing to accounts change"); | ||
this._listeners.accountsChanged = /* @__PURE__ */ new Set(); | ||
@@ -2209,4 +2236,13 @@ } | ||
} | ||
emitAccountsChange(accounts) { | ||
if (this._listeners.accountsChanged) { | ||
this.logger.info("accountsChanged event emitted", accounts); | ||
this._listeners.accountsChanged.forEach((listener) => { | ||
listener(accounts); | ||
}); | ||
} | ||
} | ||
onChainChange(cb) { | ||
if (!this._listeners.chainChanged) { | ||
this.logger.info("Subscribing to chain change"); | ||
this._listeners.chainChanged = /* @__PURE__ */ new Set(); | ||
@@ -2219,13 +2255,27 @@ } | ||
} | ||
emitChainChange(chainId) { | ||
if (this._listeners.chainChanged) { | ||
this.logger.info("chainChanged event emitted", chainId); | ||
this._listeners.chainChanged.forEach((listener) => { | ||
listener(chainId); | ||
}); | ||
} | ||
} | ||
async ack() { | ||
const { state } = await trpcClient.ack.query({ | ||
version: "0.0.5" | ||
}); | ||
const args = { | ||
version: "0.0.6", | ||
debug: this.debug | ||
}; | ||
this.logger.info("Sending ack", args); | ||
const ackResponse = await trpcClient.ack.query(args); | ||
this.logger.info("Received ack", ackResponse); | ||
this._state = "ready"; | ||
this._accounts = state.accounts; | ||
this._chainId = state.chainId; | ||
this._accounts = ackResponse.state.accounts; | ||
this._chainId = ackResponse.state.chainId; | ||
this.emitAccountsChange(this._accounts); | ||
this.emitChainChange(this._chainId); | ||
} | ||
}; | ||
var createDAppStoreClient = ({ autoInit = true } = {}) => { | ||
const client = new Client(autoInit); | ||
var createDAppStoreClient = (config = {}) => { | ||
const client = new Client(config); | ||
return client; | ||
@@ -2232,0 +2282,0 @@ }; |
@@ -1,6 +0,7 @@ | ||
import { AnyRouter } from "@trpc/server"; | ||
export declare const createPostMessageHost: <TRouter extends AnyRouter>({ router, target, }: { | ||
import { AnyRouter, inferRouterContext } from "@trpc/server"; | ||
export declare const createPostMessageHost: <TRouter extends AnyRouter>({ router, target, ctx, }: { | ||
router: TRouter; | ||
target: Window; | ||
ctx: inferRouterContext<TRouter>; | ||
}) => () => void; | ||
//# sourceMappingURL=create-post-message-host.d.ts.map |
@@ -11,4 +11,4 @@ import { TRPCLink } from "@trpc/client"; | ||
}; | ||
export declare const postMessageLink: <TRouter extends AnyRouter>(opts?: PostMessageOptions) => TRPCLink<TRouter>; | ||
export declare const postMessageLink: <TRouter extends AnyRouter>(opts: PostMessageOptions) => TRPCLink<TRouter>; | ||
export {}; | ||
//# sourceMappingURL=post-message-link.d.ts.map |
import { ProviderConnectInfo } from "viem"; | ||
type Context = { | ||
debug: boolean; | ||
}; | ||
export declare const createHostRouter: (provider: unknown) => import("@trpc/server/unstable-core-do-not-import").BuiltRouter<{ | ||
ctx: object; | ||
ctx: Context; | ||
meta: object; | ||
@@ -21,3 +24,3 @@ errorShape: import("@trpc/server/unstable-core-do-not-import").DefaultErrorShape; | ||
provider: import("@trpc/server/unstable-core-do-not-import").BuiltRouter<{ | ||
ctx: object; | ||
ctx: Context; | ||
meta: object; | ||
@@ -28,3 +31,3 @@ errorShape: import("@trpc/server/unstable-core-do-not-import").DefaultErrorShape; | ||
on: import("@trpc/server/unstable-core-do-not-import").BuiltRouter<{ | ||
ctx: object; | ||
ctx: Context; | ||
meta: object; | ||
@@ -57,2 +60,3 @@ errorShape: import("@trpc/server/unstable-core-do-not-import").DefaultErrorShape; | ||
export type HostRouter = ReturnType<typeof createHostRouter>; | ||
export {}; | ||
//# sourceMappingURL=host.d.ts.map |
{ | ||
"name": "@evmos/dappstore-sdk", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"type": "module", | ||
@@ -5,0 +5,0 @@ "sideEffects": false, |
import { createHost } from "./host"; | ||
import { Log } from "./logger"; | ||
import { trpcClient } from "./trpc/client"; | ||
@@ -11,3 +12,6 @@ | ||
import { Hex, EIP1193Provider as StronglyTypedEIP1193Provider } from "viem"; | ||
const loggerNamespace = "DAppStoreSDK CLIENT"; | ||
const loggerProviderNamespace = "DAppStoreSDK CLIENT PROVIDER"; | ||
class SDKProvider implements StronglyTypedEIP1193Provider { | ||
logger = Log(loggerProviderNamespace); | ||
unsubscribers: Record<string, Map<Function, Function>> = {}; | ||
@@ -39,2 +43,3 @@ on( | ||
) { | ||
this.logger.info("Subscribing to event", event); | ||
const { unsubscribe } = trpcClient.provider.on[event].subscribe( | ||
@@ -62,2 +67,4 @@ undefined, | ||
this.unsubscribers[event]?.delete(callback); | ||
this.logger.info("Removing listener", event); | ||
return this; | ||
@@ -69,6 +76,14 @@ } | ||
} | ||
return trpcClient.provider.request.mutate(args as never) as never; | ||
this.logger.info("Sending message", args); | ||
const response = trpcClient.provider.request.mutate(args as never) as never; | ||
this.logger.info("Received response", response); | ||
return response; | ||
}; | ||
} | ||
type ClientOptions = { | ||
autoInit?: boolean; | ||
debug?: boolean; | ||
}; | ||
class Client { | ||
@@ -80,3 +95,3 @@ // @internal | ||
// @internal | ||
_provider = new SDKProvider(); | ||
_provider: SDKProvider = new SDKProvider(); | ||
// @internal | ||
@@ -113,7 +128,22 @@ _ready = false; | ||
} | ||
logger = Log(loggerNamespace, this.debug); | ||
constructor(autoInit = true) { | ||
// @internal | ||
_debug: boolean = false; | ||
set debug(value: boolean) { | ||
this._debug = value; | ||
this.logger = Log(loggerNamespace, value); | ||
this.provider.logger = Log(loggerProviderNamespace, value); | ||
} | ||
get debug() { | ||
return this._debug; | ||
} | ||
constructor({ autoInit = true, debug = false }: ClientOptions = {}) { | ||
this.debug = debug; | ||
if (autoInit) this.initialized = this.init(); | ||
} | ||
async init() { | ||
this.logger.info("Initializing DAppStore SDK"); | ||
if (this._isInsideIframe === false) { | ||
@@ -130,7 +160,3 @@ throw new Error( | ||
this._accounts = data; | ||
if (this._listeners.accountsChanged) { | ||
this._listeners.accountsChanged.forEach((listener) => { | ||
listener(data); | ||
}); | ||
} | ||
this.emitAccountsChange(data); | ||
}, | ||
@@ -145,7 +171,3 @@ } | ||
this._chainId = data; | ||
if (this._listeners.chainChanged) { | ||
this._listeners.chainChanged.forEach((listener) => { | ||
listener(data); | ||
}); | ||
} | ||
this.emitChainChange(data); | ||
}, | ||
@@ -159,4 +181,5 @@ } | ||
} | ||
onAccountsChange(cb: (accounts: string[]) => void) { | ||
onAccountsChange(cb: (accounts: `0x${string}`[]) => void) { | ||
if (!this._listeners.accountsChanged) { | ||
this.logger.info("Subscribing to accounts change"); | ||
this._listeners.accountsChanged = new Set(); | ||
@@ -169,5 +192,14 @@ } | ||
} | ||
private emitAccountsChange(accounts: string[]) { | ||
if (this._listeners.accountsChanged) { | ||
this.logger.info("accountsChanged event emitted", accounts); | ||
this._listeners.accountsChanged.forEach((listener) => { | ||
listener(accounts); | ||
}); | ||
} | ||
} | ||
onChainChange(cb: (chainId: string) => void) { | ||
onChainChange(cb: (chainId: `0x${string}`) => void) { | ||
if (!this._listeners.chainChanged) { | ||
this.logger.info("Subscribing to chain change"); | ||
this._listeners.chainChanged = new Set(); | ||
@@ -180,14 +212,30 @@ } | ||
} | ||
private emitChainChange(chainId: `0x${string}`) { | ||
if (this._listeners.chainChanged) { | ||
this.logger.info("chainChanged event emitted", chainId); | ||
this._listeners.chainChanged.forEach((listener) => { | ||
listener(chainId); | ||
}); | ||
} | ||
} | ||
async ack() { | ||
const { state } = await trpcClient.ack.query({ | ||
const args = { | ||
version: process.env.npm_package_version as string, | ||
}); | ||
debug: this.debug, | ||
}; | ||
this.logger.info("Sending ack", args); | ||
const ackResponse = await trpcClient.ack.query(args); | ||
this.logger.info("Received ack", ackResponse); | ||
this._state = "ready"; | ||
this._accounts = state.accounts; | ||
this._chainId = state.chainId; | ||
this._accounts = ackResponse.state.accounts; | ||
this._chainId = ackResponse.state.chainId; | ||
this.emitAccountsChange(this._accounts); | ||
this.emitChainChange(this._chainId); | ||
} | ||
} | ||
export const createDAppStoreClient = ({ autoInit = true } = {}) => { | ||
const client = new Client(autoInit); | ||
export const createDAppStoreClient = (config: ClientOptions = {}) => { | ||
const client = new Client(config); | ||
@@ -194,0 +242,0 @@ return client; |
@@ -9,6 +9,11 @@ import { createPostMessageHost } from "./post-message-integration/create-post-message-host"; | ||
export const createHost = ({ provider, target }: Config) => { | ||
return createPostMessageHost({ | ||
const trpcHost = createPostMessageHost({ | ||
target, | ||
router: createHostRouter(provider), | ||
ctx: { | ||
debug: false, | ||
}, | ||
}); | ||
return trpcHost; | ||
}; |
@@ -7,2 +7,3 @@ import { | ||
getTRPCErrorFromUnknown, | ||
inferRouterContext, | ||
transformTRPCResponse, | ||
@@ -23,5 +24,7 @@ } from "@trpc/server"; | ||
target, | ||
ctx, | ||
}: { | ||
router: TRouter; | ||
target: Window; | ||
ctx: inferRouterContext<TRouter>; | ||
}) => { | ||
@@ -68,3 +71,3 @@ const { transformer } = router._def._config; | ||
getRawInput: async () => input, | ||
ctx: {}, | ||
ctx, | ||
type, | ||
@@ -104,3 +107,3 @@ }); | ||
input, | ||
ctx: {}, | ||
ctx, | ||
}), | ||
@@ -139,3 +142,3 @@ }); | ||
input, | ||
ctx: {}, | ||
ctx, | ||
}), | ||
@@ -174,3 +177,3 @@ }); | ||
getRawInput: async () => input, | ||
ctx: {}, | ||
ctx, | ||
type, | ||
@@ -200,3 +203,3 @@ }); | ||
input, | ||
ctx: {}, | ||
ctx, | ||
}), | ||
@@ -203,0 +206,0 @@ }); |
import { Operation, TRPCClientError, TRPCLink } from "@trpc/client"; | ||
import { observable } from "@trpc/server/observable"; | ||
import type { CombinedDataTransformer } from "@trpc/server"; | ||
import type { CombinedDataTransformer, inferRouterContext } from "@trpc/server"; | ||
/** | ||
@@ -83,3 +83,2 @@ * TRPC doesn't recommend importing unstable-core-do-not-import, | ||
); | ||
console.log("dispatched", this.outgoing); | ||
@@ -138,3 +137,3 @@ this.outgoing = []; | ||
export const postMessageLink = <TRouter extends AnyRouter>( | ||
opts: PostMessageOptions = {} | ||
opts: PostMessageOptions | ||
): TRPCLink<TRouter> => { | ||
@@ -141,0 +140,0 @@ const transformer = getTransformer(opts.transformer); |
@@ -6,3 +6,6 @@ import { initTRPC } from "@trpc/server"; | ||
const t = initTRPC.create({ | ||
type Context = { | ||
debug: boolean; | ||
}; | ||
const t = initTRPC.context<Context>().create({ | ||
allowOutsideOfServer: true, | ||
@@ -60,3 +63,3 @@ }); | ||
) | ||
.query(async () => { | ||
.query(async ({}) => { | ||
const accounts = await _provider.request({ | ||
@@ -66,3 +69,4 @@ method: "eth_requestAccounts", | ||
const chainId = await _provider.request({ method: "eth_chainId" }); | ||
return { | ||
const response = { | ||
version: process.env.npm_package_version as string, | ||
@@ -74,2 +78,4 @@ state: { | ||
}; | ||
return response; | ||
}), | ||
@@ -76,0 +82,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
134613
34
4440