Skip to content

Commit

Permalink
Merge pull request #106 from argentlabs/fix/window-folder
Browse files Browse the repository at this point in the history
fix: add window types
  • Loading branch information
bluecco authored Jun 14, 2024
2 parents dfc71a0 + af0a20b commit 6061ae2
Show file tree
Hide file tree
Showing 18 changed files with 2,535 additions and 436 deletions.
10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@
"types": "./dist/injectedConnector.d.ts",
"import": "./dist/injectedConnector.js",
"require": "./dist/injectedConnector.cjs"
},
"./window": {
"types": "./dist/window.d.ts",
"import": "./dist/window.js",
"require": "./dist/window.cjs"
}
},
"main": "./dist/starknetkit.cjs",
Expand Down Expand Up @@ -102,8 +107,11 @@
"typescript": "^5.1.6",
"vite": "^4.3.8",
"vite-plugin-dts": "^3.0.0",
"vitest": "^1.6.0",
"ws": "^8.8.1",
"zod": "^3.20.6"
"zod": "^3.20.6",
"starknet4": "npm:[email protected]",
"starknet5": "npm:[email protected]"
},
"peerDependencies": {
"starknet": "^6.9.0"
Expand Down
1,750 changes: 1,371 additions & 379 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

9 changes: 8 additions & 1 deletion renovate.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@
"schedule": ["every 2 weeks on Monday"],
"timezone": "Europe/London",
"rebaseWhen": "never",
"ignoreDeps": ["starknet4-deprecated", "starknet4"],
"ignoreDeps": [
"starknet-types",
"starknet4-deprecated",
"starknet4",
"starknet5",
"get-starknet-core",
"get-starknet-coreV3"
],
"packageRules": [
{
"matchUpdateTypes": ["minor", "patch"],
Expand Down
2 changes: 1 addition & 1 deletion src/connectors/webwallet/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const TESTNET_WHITELIST_URL =
"https://static.hydrogen.argent47.net/webwallet/iframe_whitelist_testnet.json"

export const MAINNET_WHITELIST_URL =
"https://static.argent.xyz/webwallet/iframe_whitelist_mainnet.json"
"https://static.argent.net/webwallet/iframe_whitelist_mainnet.json"

export const RPC_NODE_URL_TESTNET =
"https://api.hydrogen.argent47.net/v1/starknet/goerli/rpc/v0.6"
Expand Down
2 changes: 1 addition & 1 deletion src/connectors/webwallet/helpers/trpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
RpcCallsArraySchema,
StarknetMethodArgumentsSchemas,
deployAccountContractSchema,
} from "../../../types/window"
} from "../../../window/window"
import { DEFAULT_WEBWALLET_URL } from "../constants"
import { Permission } from "get-starknet-core"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ export type LoginStatus = {
export type WebWalletStarknetWindowObject = StarknetWindowObject & {
getLoginStatus(): Promise<LoginStatus>
}

export const getArgentStarknetWindowObject = (
options: GetArgentStarknetWindowObject,
proxyLink: CreateTRPCProxyClient<AppRouter>,
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/getStoreVersionFromBrowser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Bowser from "bowser"
import type { StoreVersion } from "../types/modal"
import type { StoreVersion } from "../window/modal"

export const globalWindow = typeof window !== "undefined" ? window : null

Expand Down
2 changes: 1 addition & 1 deletion src/helpers/mapModalWallets.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Connector } from "../connectors/connector"
import { ARGENT_X_ICON } from "../connectors/injected/constants"
import type { ModalWallet, StoreVersion } from "../types/modal"
import type { ModalWallet, StoreVersion } from "../window/modal"
import { isString } from "lodash-es"
import { WalletProvider } from "get-starknet-core"
import type { StarknetWindowObject } from "starknet-types"
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useStarknetkitConnectModal.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { connect } from "../main"
import { ConnectOptions, ModalResult } from "../types/modal"
import { ConnectOptions, ModalResult } from "../window/modal"

type UseStarknetkitConnectors = {
starknetkitConnectModal: () => Promise<ModalResult>
Expand Down
6 changes: 3 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
import { mapModalWallets } from "./helpers/mapModalWallets"
import Modal from "./modal/Modal.svelte"
import css from "./theme.css?inline"
import type { ConnectOptions, ModalResult, ModalWallet } from "./types/modal"
import type { ConnectOptions, ModalResult, ModalWallet } from "./window/modal"

let selectedConnector: Connector | null = null

Expand Down Expand Up @@ -182,11 +182,11 @@ export type {
Connector,
ConnectorData,
DisconnectOptions,
ModalResult,
StarknetWindowObject,
defaultConnectors as starknetkitDefaultConnectors,
}

export type * from "./types/legacy"
export type * from "./window/modal"
export * from "./window"

export { useStarknetkitConnectModal } from "./hooks/useStarknetkitConnectModal"
16 changes: 0 additions & 16 deletions src/types/legacy.ts

This file was deleted.

4 changes: 4 additions & 0 deletions src/window/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from "./modal"
export * from "./starknet"
export * from "./utils/mittx"
export * from "./window"
File renamed without changes.
52 changes: 52 additions & 0 deletions src/window/starknet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import type { StarknetWindowObject, Permission } from "get-starknet-core"
import {
ConnectedStarknetWindowObject as ConnectedStarknetWindowObjectV3,
IStarknetWindowObject as IStarknetWindowObjectV3,
} from "get-starknet-coreV3"
import type { ProviderInterface } from "starknet"
import type { ProviderInterface as ProviderInterface4 } from "starknet4"
import type { ProviderInterface as ProviderInterface5 } from "starknet5"

import type { AccountInterface } from "starknet"
import type { AccountInterface as AccountInterface4 } from "starknet4"
import type { AccountInterface as AccountInterface5 } from "starknet5"

type CommonOmittedProperties =
| "on"
| "off"
| "request"
| "icon"
| "provider"
| "account"

export type BackwardsCompatibleStarknetWindowObject = Omit<
StarknetWindowObject,
"provider" | "account"
> &
Omit<IStarknetWindowObjectV3, CommonOmittedProperties> & {
isConnected?: boolean
} & {
provider?: ProviderInterface | ProviderInterface5 | ProviderInterface4
account?: AccountInterface | AccountInterface5 | AccountInterface4
}

export type BackwardsCompatibleConnectedStarknetWindowObject = Omit<
StarknetWindowObject,
"provider" | "account"
> &
Omit<ConnectedStarknetWindowObjectV3, CommonOmittedProperties> & {
provider?: ProviderInterface | ProviderInterface5 | ProviderInterface4
account?: AccountInterface | AccountInterface5 | AccountInterface4
}

export type Variant = "argentX" | "argentWebWallet"

export interface GetArgentStarknetWindowObject {
id: Variant
icon: string
name: string
version: string
host: string
}

export { Permission }
122 changes: 122 additions & 0 deletions src/window/utils/mittx.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
export type EventType = string | symbol

// An event handler can take an optional event argument
// and should not return a value
export type Handler<T = unknown> = (event: T) => void
export type WildcardHandler<T = Record<string, unknown>> = (
type: keyof T,
event: T[keyof T],
) => void

// An array of all currently registered event handlers for a type
export type EventHandlerList<T = unknown> = Array<Handler<T>>
export type WildCardEventHandlerList<T = Record<string, unknown>> = Array<
WildcardHandler<T>
>

// A map of event types and their corresponding event handlers.
export type EventHandlerMap<Events extends Record<EventType, unknown>> = Map<
keyof Events | "*",
EventHandlerList<Events[keyof Events]> | WildCardEventHandlerList<Events>
>

export interface Emitter<Events extends Record<EventType, unknown>> {
all: EventHandlerMap<Events>

on<Key extends keyof Events>(type: Key, handler: Handler<Events[Key]>): void
on(type: "*", handler: WildcardHandler<Events>): void

off<Key extends keyof Events>(type: Key, handler?: Handler<Events[Key]>): void
off(type: "*", handler: WildcardHandler<Events>): void

emit<Key extends keyof Events>(type: Key, event: Events[Key]): void
emit<Key extends keyof Events>(
type: undefined extends Events[Key] ? Key : never,
): void
}

/**
* Taken from https://github.com/developit/mitt/blob/main/src/index.ts
* Mitt: Tiny (~200b) functional event emitter / pubsub.
* @name mittx
* @returns {MittX}
*/
export function mittx<Events extends Record<EventType, unknown>>(
all?: EventHandlerMap<Events>,
): Emitter<Events> {
type GenericEventHandler =
| Handler<Events[keyof Events]>
| WildcardHandler<Events>
all = all || new Map()

return {
/**
* A Map of event names to registered handler functions.
*/
all,

/**
* Register an event handler for the given type.
* @param {string|symbol} type Type of event to listen for, or `'*'` for all events
* @param {Function} handler Function to call in response to given event
* @memberOf mittx
*/
on<Key extends keyof Events>(type: Key, handler: GenericEventHandler) {
const handlers: Array<GenericEventHandler> | undefined = all!.get(type)
if (handlers) {
handlers.push(handler)
} else {
all!.set(type, [handler] as EventHandlerList<Events[keyof Events]>)
}
},

/**
* Remove an event handler for the given type.
* If `handler` is omitted, all handlers of the given type are removed.
* @param {string|symbol} type Type of event to unregister `handler` from (`'*'` to remove a wildcard handler)
* @param {Function} [handler] Handler function to remove
* @memberOf mittx
*/
off<Key extends keyof Events>(type: Key, handler?: GenericEventHandler) {
const handlers: Array<GenericEventHandler> | undefined = all!.get(type)
if (handlers) {
if (handler) {
handlers.splice(handlers.indexOf(handler) >>> 0, 1)
} else {
all!.set(type, [])
}
}
},

/**
* Invoke all handlers for the given type.
* If present, `'*'` handlers are invoked after type-matched handlers.
*
* Note: Manually firing '*' handlers is not supported.
*
* @param {string|symbol} type The event type to invoke
* @param {Any} [evt] Any value (object is recommended and powerful), passed to each handler
* @memberOf mittx
*/
emit<Key extends keyof Events>(type: Key, evt?: Events[Key]) {
let handlers = all?.get(type)
if (handlers) {
;(handlers as EventHandlerList<Events[keyof Events]>)
.slice()
.map((handler) => {
handler(evt!)
})
}

handlers = all!.get("*")
if (handlers) {
;(handlers as WildCardEventHandlerList<Events>)
.slice()
.forEach((handler) => {
handler(type, evt!)
})
}
},
}
}
Loading

0 comments on commit 6061ae2

Please sign in to comment.