Skip to content

Commit

Permalink
refactor: rename QueryBuilder to Query
Browse files Browse the repository at this point in the history
  • Loading branch information
tien committed Jun 11, 2024
1 parent 29736f9 commit e871b85
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 28 deletions.
28 changes: 11 additions & 17 deletions packages/core/src/QueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,23 +196,17 @@ export type InferInstructionsPayload<
>;
};

export type InferQueryBuilderResponse<T extends QueryBuilder> =
T extends QueryBuilder<infer Instructions, infer Descriptor>
export type InferQueryResponse<T extends Query> =
T extends Query<infer Instructions, infer Descriptor>
? InferInstructionsResponse<Instructions, Descriptor>
: never;

export type InferQueryBuilderPayload<T extends QueryBuilder> =
T extends QueryBuilder<infer Instructions, infer Descriptor>
export type InferQueryPayload<T extends Query> =
T extends Query<infer Instructions, infer Descriptor>
? InferInstructionsPayload<Instructions, Descriptor>
: never;

export type Query<
TInstructions extends QueryInstruction[] = QueryInstruction[],
> = {
readonly instructions: TInstructions;
};

export default class QueryBuilder<
export default class Query<
const TInstructions extends QueryInstruction[] = QueryInstruction[],
TDescriptor extends ChainDefinition = ReDotDescriptor,
> implements Query<TInstructions>
Expand All @@ -231,7 +225,7 @@ export default class QueryBuilder<
TPallet extends keyof TypedApi<TDescriptor>["constants"],
TConstant extends keyof TypedApi<TDescriptor>["constants"][TPallet],
>(pallet: TPallet, constant: TConstant) {
return new QueryBuilder([
return new Query([
...this.#instructions,
{ instruction: "fetch-constant", pallet, constant },
]);
Expand All @@ -244,7 +238,7 @@ export default class QueryBuilder<
TypedApi<TDescriptor>["query"][TPallet][TStorage]
>["args"],
>(pallet: TPallet, storage: TStorage, args: TArguments) {
return new QueryBuilder([
return new Query([
...this.#instructions,
{ instruction: "read-storage", pallet, storage, args },
]);
Expand All @@ -257,7 +251,7 @@ export default class QueryBuilder<
TypedApi<TDescriptor>["query"][TPallet][TStorage]
>["args"],
>(pallet: TPallet, storage: TStorage, args: TArguments[]) {
return new QueryBuilder([
return new Query([
...this.#instructions,
{ instruction: "read-storage", pallet, storage, args, multi: true },
]);
Expand All @@ -272,7 +266,7 @@ export default class QueryBuilder<
>["args"]
>,
>(pallet: TPallet, storage: TStorage, args: TArguments) {
return new QueryBuilder([
return new Query([
...this.#instructions,
{ instruction: "read-storage-entries", pallet, storage, args },
]);
Expand All @@ -285,7 +279,7 @@ export default class QueryBuilder<
TypedApi<TDescriptor>["apis"][TPallet][TApi]
>["args"],
>(pallet: TPallet, api: TApi, args: TArguments) {
return new QueryBuilder([
return new Query([
...this.#instructions,
{ instruction: "call-api", pallet, api, args },
]);
Expand All @@ -298,7 +292,7 @@ export default class QueryBuilder<
TypedApi<TDescriptor>["apis"][TPallet][TApi]
>["args"],
>(pallet: TPallet, api: TApi, args: TArguments[]) {
return new QueryBuilder([
return new Query([
...this.#instructions,
{ instruction: "call-api", pallet, api, args, multi: true },
]);
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export {
default as QueryBuilder,
type InferQueryBuilderPayload,
type InferQueryBuilderResponse,
default as Query,
type InferQueryPayload,
type InferQueryResponse,
type MultiInstruction,
type QueryInstruction,
} from "./QueryBuilder.js";
Expand Down
14 changes: 6 additions & 8 deletions packages/react/src/hooks/useQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import type { Falsy, FalsyGuard, FlatHead } from "../types.js";
import { flatHead, stringify } from "../utils.js";
import type { ChainHookOptions } from "./types.js";
import {
QueryBuilder,
Query,
QueryError,
QueryInstruction,
type InferQueryBuilderPayload,
type InferQueryPayload,
} from "@reactive-dot/core";
import type { ChainId, Chains, ReDotDescriptor } from "@reactive-dot/types";
import { atom, useAtomValue } from "jotai";
Expand All @@ -23,8 +23,8 @@ import { useContext, useMemo } from "react";
export const useQuery = <
TQuery extends
| ((
builder: QueryBuilder<[], TDescriptor>,
) => QueryBuilder<QueryInstruction<TDescriptor>[], TDescriptor> | Falsy)
builder: Query<[], TDescriptor>,
) => Query<QueryInstruction<TDescriptor>[], TDescriptor> | Falsy)
| Falsy,
TDescriptor extends TChainId extends void
? ReDotDescriptor
Expand All @@ -38,9 +38,7 @@ export const useQuery = <
: FalsyGuard<
ReturnType<Exclude<TQuery, Falsy>>,
FlatHead<
InferQueryBuilderPayload<
Exclude<ReturnType<Exclude<TQuery, Falsy>>, Falsy>
>
InferQueryPayload<Exclude<ReturnType<Exclude<TQuery, Falsy>>, Falsy>>
>
> => {
const contextChainId = useContext(ChainIdContext);
Expand All @@ -51,7 +49,7 @@ export const useQuery = <
}

const query = useMemo(
() => (!builder ? undefined : builder(new QueryBuilder([]))),
() => (!builder ? undefined : builder(new Query([]))),
[builder],
);

Expand Down

0 comments on commit e871b85

Please sign in to comment.