Skip to content

Commit

Permalink
refactor!: simplify chain type registration (#270)
Browse files Browse the repository at this point in the history
  • Loading branch information
tien authored Oct 20, 2024
1 parent bef0f24 commit 08e5517
Show file tree
Hide file tree
Showing 18 changed files with 84 additions and 50 deletions.
30 changes: 30 additions & 0 deletions .changeset/sour-fans-wait.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
"@reactive-dot/react": minor
"@reactive-dot/core": minor
"@reactive-dot/vue": minor
---

BREAKING: simplified chain type registration.

**Old approach:**

```ts
import type { config } from "./config.js";
import type { InferChains } from "@reactive-dot/core";

declare module "@reactive-dot/core" {
export interface Chains extends InferChains<typeof config> {}
}
```

**New approach:**

```ts
import type { config } from "./config.js";

declare module "@reactive-dot/core" {
export interface Register {
config: typeof config;
}
}
```
7 changes: 4 additions & 3 deletions apps/docs/docs/getting-started/setup.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,12 @@ export const config = {
The type declarations extension here will be used to provide you with the right type definitions when using hooks.

```ts title="reactive-dot.d.ts"
import type { config } from "./config";
import type { InferChains } from "@reactive-dot/core";
import type { config } from "./config.js";

declare module "@reactive-dot/core" {
export interface Chains extends InferChains<typeof config> {}
export interface Register {
config: typeof config;
}
}
```

Expand Down
7 changes: 4 additions & 3 deletions apps/docs/docs/guides/multichain.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ npx papi
### Add type information

```ts title="reactive-dot.d.ts"
import type { config } from "./config";
import type { InferChains } from "@reactive-dot/core";
import type { config } from "./config.js";

declare module "@reactive-dot/core" {
export interface Chains extends InferChains<typeof config> {}
export interface Register {
config: typeof config;
}
}
```

Expand Down
6 changes: 3 additions & 3 deletions examples/react/src/reactive-dot.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { config } from "./config.js";
import type { InferChains } from "@reactive-dot/core";

declare module "@reactive-dot/core" {
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
export interface Chains extends InferChains<typeof config> {}
export interface Register {
config: typeof config;
}
}
6 changes: 3 additions & 3 deletions examples/vue/src/reactive-dot.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { config } from "./config.js";
import type { InferChains } from "@reactive-dot/core";

declare module "@reactive-dot/core" {
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
export interface Chains extends InferChains<typeof config> {}
export interface Register {
config: typeof config;
}
}
13 changes: 11 additions & 2 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,21 @@ export type Config = {
};

// eslint-disable-next-line @typescript-eslint/no-empty-object-type
export interface Chains {}
export interface Register {}

export type InferChains<T extends Config> = {
type ResolvedRegister = {
config: Register extends { config: infer config extends Config }
? config
: Config;
};

type InferChains<T extends Config> = {
[P in keyof T["chains"]]: T["chains"][P]["descriptor"];
};

// eslint-disable-next-line @typescript-eslint/no-empty-object-type
export interface Chains extends InferChains<ResolvedRegister["config"]> {}

export type ChainId = keyof Chains;

export type CommonDescriptor = Chains[keyof Chains] extends never
Expand Down
9 changes: 1 addition & 8 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,7 @@ export { getClient } from "./actions/get-client.js";
export { getConnectedWallets } from "./actions/get-connected-wallets.js";
export { preflight, query } from "./actions/query.js";
export type { AsyncValue } from "./async-state.js";
export type {
ChainConfig,
ChainId,
Chains,
CommonDescriptor,
Config,
InferChains,
} from "./config.js";
export type { ChainConfig, ChainId, Config, Register } from "./config.js";
export { MutationError, QueryError, ReactiveDotError } from "./errors.js";
export { Query } from "./query-builder.js";
export { PrefixedStorage } from "./storage.js";
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/internal.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export type { Chains, CommonDescriptor } from "./config.js";
export type { MutationEvent } from "./mutation-event.js";
export {
type InferQueryPayload,
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/development.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { ChainDefinition } from "polkadot-api";

declare module "@reactive-dot/core" {
declare module "@reactive-dot/core/internal.js" {
export interface Chains {
[id: string]: ChainDefinition;
}
Expand Down
5 changes: 3 additions & 2 deletions packages/react/src/hooks/use-mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import type { ChainHookOptions } from "./types.js";
import { useAsyncAction } from "./use-async-action.js";
import { internal_useChainId } from "./use-chain-id.js";
import { useConfig } from "./use-config.js";
import type { ChainId, Chains } from "@reactive-dot/core";
import type { ChainId } from "@reactive-dot/core";
import { MutationError, pending } from "@reactive-dot/core";
import type { Chains } from "@reactive-dot/core/internal.js";
import { useAtomCallback } from "jotai/utils";
import type {
PolkadotSigner,
Expand All @@ -16,7 +17,7 @@ import type {
} from "polkadot-api";
import { useCallback, useContext } from "react";
import { from } from "rxjs";
import { tap, switchMap, catchError } from "rxjs/operators";
import { catchError, switchMap, tap } from "rxjs/operators";

// eslint-disable-next-line @typescript-eslint/no-explicit-any
type TxOptions<T extends Transaction<any, any, any, any>> = Parameters<
Expand Down
13 changes: 6 additions & 7 deletions packages/react/src/hooks/use-query-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ import { queryPayloadAtom } from "../stores/query.js";
import type { ChainHookOptions } from "./types.js";
import { internal_useChainId } from "./use-chain-id.js";
import { useConfig } from "./use-config.js";
import {
type ChainId,
type Chains,
type CommonDescriptor,
Query,
} from "@reactive-dot/core";
import type { QueryInstruction } from "@reactive-dot/core/internal.js";
import { type ChainId, Query } from "@reactive-dot/core";
import type {
Chains,
CommonDescriptor,
QueryInstruction,
} from "@reactive-dot/core/internal.js";
import type { Getter } from "jotai";
import { useAtomCallback } from "jotai/utils";
import { useCallback } from "react";
Expand Down
14 changes: 7 additions & 7 deletions packages/react/src/hooks/use-query-refresher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { getQueryInstructionPayloadAtoms } from "../stores/query.js";
import type { ChainHookOptions } from "./types.js";
import { internal_useChainId } from "./use-chain-id.js";
import { useConfig } from "./use-config.js";
import {
Query,
type ChainId,
type Chains,
type CommonDescriptor,
} from "@reactive-dot/core";
import type { Falsy, QueryInstruction } from "@reactive-dot/core/internal.js";
import { Query, type ChainId } from "@reactive-dot/core";
import type {
Chains,
CommonDescriptor,
Falsy,
QueryInstruction,
} from "@reactive-dot/core/internal.js";
import { useAtomCallback } from "jotai/utils";
import { useCallback } from "react";

Expand Down
6 changes: 1 addition & 5 deletions packages/react/src/hooks/use-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,10 @@ import type { ChainHookOptions } from "./types.js";
import { internal_useChainId } from "./use-chain-id.js";
import { useConfig } from "./use-config.js";
import { useQueryRefresher } from "./use-query-refresher.js";
import { idle, Query, type ChainId } from "@reactive-dot/core";
import {
idle,
Query,
type ChainId,
type Chains,
type CommonDescriptor,
} from "@reactive-dot/core";
import {
flatHead,
stringify,
type Falsy,
Expand Down
3 changes: 2 additions & 1 deletion packages/react/src/hooks/use-typed-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { typedApiAtom } from "../stores/client.js";
import type { ChainHookOptions } from "./types.js";
import { internal_useChainId } from "./use-chain-id.js";
import { useConfig } from "./use-config.js";
import type { ChainId, Chains } from "@reactive-dot/core";
import type { ChainId } from "@reactive-dot/core";
import type { Chains } from "@reactive-dot/core/internal.js";
import { useAtomValue } from "jotai";
import type { TypedApi } from "polkadot-api";

Expand Down
3 changes: 2 additions & 1 deletion packages/vue/src/composables/use-mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import { useAsyncAction } from "./use-async-action.js";
import { useChainId } from "./use-chain-id.js";
import { useSigner } from "./use-signer.js";
import { useTypedApiPromise } from "./use-typed-api.js";
import type { ChainId, Chains } from "@reactive-dot/core";
import type { ChainId } from "@reactive-dot/core";
import { MutationError } from "@reactive-dot/core";
import type { Chains } from "@reactive-dot/core/internal.js";
import type {
PolkadotSigner,
Transaction,
Expand Down
4 changes: 2 additions & 2 deletions packages/vue/src/composables/use-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import { lazyValue, useLazyValuesCache } from "./use-lazy-value.js";
import { useTypedApiPromise } from "./use-typed-api.js";
import {
type ChainId,
type Chains,
type CommonDescriptor,
query as executeQuery,
preflight,
Query,
type QueryError,
} from "@reactive-dot/core";
import {
type Chains,
type CommonDescriptor,
type Falsy,
type FlatHead,
flatHead,
Expand Down
3 changes: 2 additions & 1 deletion packages/vue/src/composables/use-typed-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { internal_useChainId } from "./use-chain-id.js";
import { useClientPromise } from "./use-client.js";
import { useChainConfig } from "./use-config.js";
import { useLazyValue } from "./use-lazy-value.js";
import type { ChainId, Chains } from "@reactive-dot/core";
import type { ChainId } from "@reactive-dot/core";
import type { Chains } from "@reactive-dot/core/internal.js";
import type { TypedApi } from "polkadot-api";
import { computed } from "vue";

Expand Down
2 changes: 1 addition & 1 deletion packages/vue/src/development.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { ChainDefinition } from "polkadot-api";

declare module "@reactive-dot/core" {
declare module "@reactive-dot/core/internal.js" {
export interface Chains {
[id: string]: ChainDefinition;
}
Expand Down

0 comments on commit 08e5517

Please sign in to comment.