From 5ec0fcc4576eefa6f38175287f1ff00bd86ad551 Mon Sep 17 00:00:00 2001 From: Eugene Chybisov Date: Thu, 9 Jun 2022 13:14:40 +0200 Subject: [PATCH] fix: restrict variations of config usage --- .../src/providers/WidgetProvider/types.ts | 4 +- packages/widget/src/types/widget.ts | 57 ++++++++++++++----- 2 files changed, 46 insertions(+), 15 deletions(-) diff --git a/packages/widget/src/providers/WidgetProvider/types.ts b/packages/widget/src/providers/WidgetProvider/types.ts index 9615129ae..51277f068 100644 --- a/packages/widget/src/providers/WidgetProvider/types.ts +++ b/packages/widget/src/providers/WidgetProvider/types.ts @@ -1,9 +1,9 @@ import { WidgetConfig } from '../../types'; -export interface WidgetContextProps extends WidgetConfig { +export type WidgetContextProps = WidgetConfig & { fromChain?: number; toChain?: number; -} +}; export interface WidgetProviderProps { config?: WidgetConfig; diff --git a/packages/widget/src/types/widget.ts b/packages/widget/src/types/widget.ts index e05c3f450..14318ddca 100644 --- a/packages/widget/src/types/widget.ts +++ b/packages/widget/src/types/widget.ts @@ -11,23 +11,54 @@ export type ThemeConfig = { typography?: TypographyOptions; }; -export interface WidgetConfig { +export interface WidgetWalletCallbacks { + connect(): Signer; + disconnect(): void; + provideSigner(): Signer; + switchChain(): Signer; + addToken(): void; +} + +interface WidgetConfigBase { fromAmount?: number; - fromChain?: `${ChainKey}` | number; - fromToken?: string; - toChain?: `${ChainKey}` | number; - toToken?: string; disabledChains?: number[]; - disableInternalWalletManagement?: boolean; - walletCallbacks?: { - connect: { (): Signer }; - disconnect: { (): void }; - provideSigner: { (): Signer }; - switchChain: { (): Signer }; - addToken: { (): void }; - }; containerStyle?: CSSProperties; theme?: ThemeConfig; appearance?: Appearance; disableAppearance?: boolean; } + +type WidgetFromTokenConfig = + | { + fromChain: `${ChainKey}` | number; + fromToken?: string; + } + | { + fromChain?: never; + fromToken?: never; + }; + +type WidgetToTokenConfig = + | { + toChain: `${ChainKey}` | number; + toToken?: string; + } + | { + toChain?: never; + toToken?: never; + }; + +type WidgetWalletManagementConfig = + | { + disableInternalWalletManagement: true; + walletCallbacks: WidgetWalletCallbacks; + } + | { + disableInternalWalletManagement?: false; + walletCallbacks?: never; + }; + +export type WidgetConfig = WidgetConfigBase & + WidgetFromTokenConfig & + WidgetToTokenConfig & + WidgetWalletManagementConfig;