diff --git a/.changeset/sour-fans-wait.md b/.changeset/sour-fans-wait.md new file mode 100644 index 00000000..02e0eb19 --- /dev/null +++ b/.changeset/sour-fans-wait.md @@ -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 {} +} +``` + +**New approach:** + +```ts +import type { config } from "./config.js"; + +declare module "@reactive-dot/core" { + export interface Register { + config: typeof config; + } +} +``` diff --git a/apps/docs/docs/getting-started/setup.mdx b/apps/docs/docs/getting-started/setup.mdx index 00b38a73..d11a4491 100644 --- a/apps/docs/docs/getting-started/setup.mdx +++ b/apps/docs/docs/getting-started/setup.mdx @@ -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 {} + export interface Register { + config: typeof config; + } } ``` diff --git a/apps/docs/docs/guides/multichain.md b/apps/docs/docs/guides/multichain.md index 80cd1e2a..1864ba47 100644 --- a/apps/docs/docs/guides/multichain.md +++ b/apps/docs/docs/guides/multichain.md @@ -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 {} + export interface Register { + config: typeof config; + } } ``` diff --git a/examples/react/src/reactive-dot.d.ts b/examples/react/src/reactive-dot.d.ts index 9ee45608..c73c6035 100644 --- a/examples/react/src/reactive-dot.d.ts +++ b/examples/react/src/reactive-dot.d.ts @@ -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 {} + export interface Register { + config: typeof config; + } } diff --git a/examples/vue/src/reactive-dot.d.ts b/examples/vue/src/reactive-dot.d.ts index 9ee45608..c73c6035 100644 --- a/examples/vue/src/reactive-dot.d.ts +++ b/examples/vue/src/reactive-dot.d.ts @@ -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 {} + export interface Register { + config: typeof config; + } } diff --git a/packages/core/src/config.ts b/packages/core/src/config.ts index 5b0d7c23..83e5b9f4 100644 --- a/packages/core/src/config.ts +++ b/packages/core/src/config.ts @@ -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 = { +type ResolvedRegister = { + config: Register extends { config: infer config extends Config } + ? config + : Config; +}; + +type InferChains = { [P in keyof T["chains"]]: T["chains"][P]["descriptor"]; }; +// eslint-disable-next-line @typescript-eslint/no-empty-object-type +export interface Chains extends InferChains {} + export type ChainId = keyof Chains; export type CommonDescriptor = Chains[keyof Chains] extends never diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index ba741ec5..c7fdb036 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -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"; diff --git a/packages/core/src/internal.ts b/packages/core/src/internal.ts index b2370b44..3b10e0ec 100644 --- a/packages/core/src/internal.ts +++ b/packages/core/src/internal.ts @@ -1,3 +1,4 @@ +export type { Chains, CommonDescriptor } from "./config.js"; export type { MutationEvent } from "./mutation-event.js"; export { type InferQueryPayload, diff --git a/packages/react/src/development.d.ts b/packages/react/src/development.d.ts index 9751cfc1..592ec5a9 100644 --- a/packages/react/src/development.d.ts +++ b/packages/react/src/development.d.ts @@ -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; } diff --git a/packages/react/src/hooks/use-mutation.ts b/packages/react/src/hooks/use-mutation.ts index 0d89151c..8493e540 100644 --- a/packages/react/src/hooks/use-mutation.ts +++ b/packages/react/src/hooks/use-mutation.ts @@ -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, @@ -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> = Parameters< diff --git a/packages/react/src/hooks/use-query-loader.ts b/packages/react/src/hooks/use-query-loader.ts index 34e3ba09..7428386c 100644 --- a/packages/react/src/hooks/use-query-loader.ts +++ b/packages/react/src/hooks/use-query-loader.ts @@ -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"; diff --git a/packages/react/src/hooks/use-query-refresher.ts b/packages/react/src/hooks/use-query-refresher.ts index e21ffe82..018c749e 100644 --- a/packages/react/src/hooks/use-query-refresher.ts +++ b/packages/react/src/hooks/use-query-refresher.ts @@ -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"; diff --git a/packages/react/src/hooks/use-query.ts b/packages/react/src/hooks/use-query.ts index 49eb67e9..4da0bbf0 100644 --- a/packages/react/src/hooks/use-query.ts +++ b/packages/react/src/hooks/use-query.ts @@ -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, diff --git a/packages/react/src/hooks/use-typed-api.ts b/packages/react/src/hooks/use-typed-api.ts index 8903149c..051150a2 100644 --- a/packages/react/src/hooks/use-typed-api.ts +++ b/packages/react/src/hooks/use-typed-api.ts @@ -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"; diff --git a/packages/vue/src/composables/use-mutation.ts b/packages/vue/src/composables/use-mutation.ts index 23d6cc32..9598314f 100644 --- a/packages/vue/src/composables/use-mutation.ts +++ b/packages/vue/src/composables/use-mutation.ts @@ -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, diff --git a/packages/vue/src/composables/use-query.ts b/packages/vue/src/composables/use-query.ts index e4f8b881..955eb6c3 100644 --- a/packages/vue/src/composables/use-query.ts +++ b/packages/vue/src/composables/use-query.ts @@ -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, diff --git a/packages/vue/src/composables/use-typed-api.ts b/packages/vue/src/composables/use-typed-api.ts index e9dcd067..e7785d67 100644 --- a/packages/vue/src/composables/use-typed-api.ts +++ b/packages/vue/src/composables/use-typed-api.ts @@ -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"; diff --git a/packages/vue/src/development.d.ts b/packages/vue/src/development.d.ts index 9751cfc1..592ec5a9 100644 --- a/packages/vue/src/development.d.ts +++ b/packages/vue/src/development.d.ts @@ -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; }