Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: target chains option #279

Merged
merged 1 commit into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/dirty-bees-repeat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@reactive-dot/react": minor
"@reactive-dot/core": minor
"@reactive-dot/vue": minor
---

Added target chains option to define default chains for providing type definitions.
15 changes: 9 additions & 6 deletions packages/core/src/chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ export type ChainId = keyof Chains;

export type CommonDescriptor = Chains[keyof Chains] extends never
? ChainDefinition
: Chains[keyof Chains];
: ResolvedRegister["config"]["targetChains"] extends undefined
? Chains[keyof Chains]
: Chains[NonNullable<ResolvedRegister["config"]["targetChains"]>[number]];

export type ChainDescriptorOf<T> = T extends ChainId
? Chains[T] extends ChainDefinition
? Chains[T]
: CommonDescriptor
: CommonDescriptor;
export type ChainDescriptorOf<T extends ChainId | undefined> =
undefined extends T
? CommonDescriptor
: T extends ChainId
? Chains[T]
: never;
19 changes: 15 additions & 4 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,22 @@ export type ChainConfig = {
readonly provider: Gettable<JsonRpcProvider>;
};

export type Config = {
readonly chains: Record<string, ChainConfig>;
readonly wallets?: Array<WalletAggregator | Wallet>;
export type Config<
TChains extends Readonly<Record<string, ChainConfig>> = Readonly<
Record<string, ChainConfig>
>,
TTargetChainIds extends ReadonlyArray<
Extract<keyof TChains, string>
> = ReadonlyArray<Extract<keyof TChains, string>>,
> = {
readonly chains: TChains;
readonly targetChains?: TTargetChainIds;
readonly wallets?: ReadonlyArray<WalletAggregator | Wallet>;
};

export function defineConfig<const TConfig extends Config>(config: TConfig) {
export function defineConfig<
const TChains extends Readonly<Record<string, ChainConfig>>,
const TTargetChainIds extends ReadonlyArray<Extract<keyof TChains, string>>,
>(config: Config<TChains, TTargetChainIds>) {
return config;
}
4 changes: 3 additions & 1 deletion packages/react/src/hooks/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { ChainId } from "@reactive-dot/core";

export type ChainHookOptions<TChainId extends ChainId = ChainId> = {
export type ChainHookOptions<
TChainId extends ChainId | undefined = ChainId | undefined,
> = {
/**
* Override default chain ID
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/hooks/use-mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export function useMutation<
builder: TypedApi<ChainDescriptorOf<TChainId>>["tx"],
) => // eslint-disable-next-line @typescript-eslint/no-explicit-any
Transaction<any, any, any, any>,
TChainId extends ChainId,
TChainId extends ChainId | undefined,
>(
action: TAction,
options?: ChainHookOptions<TChainId> & {
Expand Down
6 changes: 3 additions & 3 deletions packages/react/src/hooks/use-query-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function useQueryLoader() {
builder: Query<[], TDescriptor>,
) => Query<QueryInstruction<TDescriptor>[], TDescriptor>,
TDescriptor extends ChainDescriptorOf<TChainId>,
TChainId extends ChainId,
TChainId extends ChainId | undefined,
>(
builder: TQuery,
options?: ChainHookOptions<TChainId>,
Expand All @@ -50,8 +50,8 @@ export function useQueryLoader() {
(
get,
_,
builder: <TChainId extends ChainId>(
query: Query<[]>,
builder: <TChainId extends ChainId | undefined>(
query: Query<[], ChainDescriptorOf<TChainId>>,
options?: ChainHookOptions<TChainId>,
) => Query<[]>,
) => _loadQuery(get)(builder),
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/hooks/use-query-refresher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function useQueryRefresher<
) => Query<QueryInstruction<TDescriptor>[], TDescriptor> | Falsy)
| Falsy,
TDescriptor extends ChainDescriptorOf<TChainId>,
TChainId extends ChainId,
TChainId extends ChainId | undefined,
>(builder: TQuery, options?: ChainHookOptions<TChainId>) {
const config = useConfig();
const chainId = internal_useChainId(options);
Expand Down
4 changes: 2 additions & 2 deletions packages/react/src/hooks/use-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function useLazyLoadQuery<
) => Query<QueryInstruction<TDescriptor>[], TDescriptor> | Falsy)
| Falsy,
TDescriptor extends ChainDescriptorOf<TChainId>,
TChainId extends ChainId,
TChainId extends ChainId | undefined,
>(builder: TQuery, options?: ChainHookOptions<TChainId>) {
const config = useConfig();
const chainId = internal_useChainId(options);
Expand Down Expand Up @@ -90,7 +90,7 @@ export function useLazyLoadQueryWithRefresh<
) => Query<QueryInstruction<TDescriptor>[], TDescriptor> | Falsy)
| Falsy,
TDescriptor extends ChainDescriptorOf<TChainId>,
TChainId extends ChainId,
TChainId extends ChainId | undefined,
>(builder: TQuery, options?: ChainHookOptions<TChainId>) {
const data = useLazyLoadQuery(builder, options);
const refresh = useQueryRefresher(builder, options);
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/hooks/use-typed-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type { TypedApi } from "polkadot-api";
* @param options - Additional options
* @returns Polkadot-API typed API
*/
export function useTypedApi<TChainId extends ChainId>(
export function useTypedApi<TChainId extends ChainId | undefined>(
options?: ChainHookOptions<TChainId>,
) {
return useAtomValue(
Expand Down
2 changes: 1 addition & 1 deletion packages/vue/src/composables/use-mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export function useMutation<
builder: TypedApi<ChainDescriptorOf<TChainId>>["tx"],
) => // eslint-disable-next-line @typescript-eslint/no-explicit-any
Transaction<any, any, any, any>,
TChainId extends ChainId,
TChainId extends ChainId | undefined,
>(
action: TAction,
options?: ChainComposableOptions<TChainId> & {
Expand Down
2 changes: 1 addition & 1 deletion packages/vue/src/composables/use-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export function useQuery<
builder: Query<[], TDescriptor>,
) => Query<QueryInstruction<TDescriptor>[], TDescriptor> | Falsy,
TDescriptor extends ChainDescriptorOf<TChainId>,
TChainId extends ChainId,
TChainId extends ChainId | undefined,
>(builder: TQuery, options?: ChainComposableOptions<TChainId>) {
const chainId = internal_useChainId(options);
const typedApiPromise = useTypedApiPromise(options);
Expand Down
4 changes: 2 additions & 2 deletions packages/vue/src/composables/use-typed-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { computed } from "vue";
* @param options - Additional options
* @returns Polkadot-API typed API
*/
export function useTypedApi<TChainId extends ChainId>(
export function useTypedApi<TChainId extends ChainId | undefined>(
options?: ChainComposableOptions<TChainId>,
) {
return useAsyncData(useTypedApiPromise(options));
Expand All @@ -24,7 +24,7 @@ export function useTypedApi<TChainId extends ChainId>(
/**
* @internal
*/
export function useTypedApiPromise<TChainId extends ChainId>(
export function useTypedApiPromise<TChainId extends ChainId | undefined>(
options?: ChainComposableOptions<TChainId>,
) {
const chainId = internal_useChainId(options);
Expand Down
4 changes: 3 additions & 1 deletion packages/vue/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import type { MutationEvent as BaseMutationEvent } from "@reactive-dot/core/inte
import type { TxEvent } from "polkadot-api";
import type { MaybeRefOrGetter, Ref } from "vue";

export type ChainComposableOptions<TChainId extends ChainId = ChainId> = {
export type ChainComposableOptions<
TChainId extends ChainId | undefined = ChainId | undefined,
> = {
/**
* Override default chain ID
*/
Expand Down