From 42290fa87196491d487e5dff1b36a43a8d196618 Mon Sep 17 00:00:00 2001 From: b3hr4d Date: Sat, 17 Feb 2024 18:20:21 +0300 Subject: [PATCH] Update ESLint rules and fix console errors --- .eslintrc | 5 +++- packages/store/src/actor/index.ts | 34 ++++++++++++---------- packages/store/src/actor/types.ts | 48 ++++++++++++++----------------- packages/store/src/agent/index.ts | 4 +-- packages/store/src/helper.ts | 14 ++++----- packages/store/src/index.ts | 12 +++----- packages/store/src/tools/types.ts | 2 +- 7 files changed, 58 insertions(+), 61 deletions(-) diff --git a/.eslintrc b/.eslintrc index 7226ee599d..cecf15bcab 100644 --- a/.eslintrc +++ b/.eslintrc @@ -8,7 +8,10 @@ "prettier" ], "rules": { - "typescript-eslint/no-explicit-any": "off", + "@typescript-eslint/no-unused-vars": [ + "error", + { "argsIgnorePattern": "^_" } + ], "no-console": 1, "prettier/prettier": 2 }, diff --git a/packages/store/src/actor/index.ts b/packages/store/src/actor/index.ts index 42767fd74d..4bb24723df 100644 --- a/packages/store/src/actor/index.ts +++ b/packages/store/src/actor/index.ts @@ -1,6 +1,6 @@ import { Actor } from "@dfinity/agent" import { createStoreWithOptionalDevtools } from "../helper" -import type { ActorSubclass, HttpAgent } from "@dfinity/agent" +import type { HttpAgent } from "@dfinity/agent" import type { CanisterId, ExtractActorMethodArgs, @@ -9,16 +9,16 @@ import type { ActorStore, ActorMethodStates, ActorManagerOptions, - DefaultActorType, FunctionName, ExtractedService, + BaseActor, } from "./types" import { IDL } from "@dfinity/candid" import type { AgentManager, UpdateAgentOptions } from "../agent" export * from "./types" -export class ActorManager = DefaultActorType> { +export class ActorManager { private actor: null | A = null private idlFactory: IDL.InterfaceFactory @@ -67,7 +67,7 @@ export class ActorManager = DefaultActorType> { // Initialize stores this.actorStore = createStoreWithOptionalDevtools( - { ...this.DEFAULT_ACTOR_STATE }, + this.DEFAULT_ACTOR_STATE, { withDevtools, store: `actor-${String(canisterId)}` } ) @@ -81,19 +81,22 @@ export class ActorManager = DefaultActorType> { } public extractService(): ExtractedService { - return this.idlFactory({ IDL })._fields.reduce( - (acc, [functionName, type]) => { - acc[functionName as FunctionName] = (extractorClass, data) => { - return type.accept(extractorClass, data || functionName) - } - - return acc - }, - {} as ExtractedService - ) + return this.idlFactory({ IDL })._fields.reduce((acc, service) => { + const functionName = service[0] as keyof A + const type = service[1] + + const visit = ((extractorClass, data) => { + return type.accept(extractorClass, data || functionName) + }) as ExtractedService[typeof functionName] + + acc[functionName] = visit + + return acc + }, {} as ExtractedService) } private initializeActor = (agent: HttpAgent) => { + // eslint-disable-next-line no-console console.info( `Initializing actor ${this.canisterId} on ${ agent.isLocal() ? "local" : "ic" @@ -127,6 +130,7 @@ export class ActorManager = DefaultActorType> { initialized: true, }) } catch (error) { + // eslint-disable-next-line no-console console.error("Error in initializeActor:", error) this.updateState({ error: error as Error, initializing: false }) } @@ -170,7 +174,7 @@ export class ActorManager = DefaultActorType> { } } -const emptyVisitor = new Proxy({} as ExtractedService, { +const emptyVisitor = new Proxy({} as ExtractedService, { get: function (_, prop) { throw new Error( `Cannot visit function "${String( diff --git a/packages/store/src/actor/types.ts b/packages/store/src/actor/types.ts index d2e251c365..88c3efd732 100644 --- a/packages/store/src/actor/types.ts +++ b/packages/store/src/actor/types.ts @@ -3,15 +3,16 @@ import type { StoreApi } from "zustand" import type { AgentManager } from "../agent" import type { ActorMethod, ActorSubclass, Principal } from "../types" -export type FunctionName = keyof A & string - -export type FunctionType = "query" | "update" - export interface DefaultActorType { - [key: string]: ActorMethod + [key: string]: ActorMethod } -// Type for identifying a canister +export type BaseActor = ActorSubclass + +export type FunctionName = keyof A & string + +export type FunctionType = "query" | "update" + export type CanisterId = string | Principal export interface ActorManagerOptions { @@ -23,33 +24,31 @@ export interface ActorManagerOptions { initializeOnCreate?: boolean } -export type ExtractVisitorType = T extends IDL.Visitor - ? { data: U; return: V } +export type ExtractVisitorType = V extends IDL.Visitor + ? { data: D; returnType: R } : never -export type ExtractedService< - A = DefaultActorType, - M extends FunctionName = FunctionName -> = { - [K in M]: >( +export type ExtractedService = { + [K in M]: >( extractorClass: V, data?: ExtractVisitorType["data"] ) => ReturnType } -// Utility types for extracting method arguments and return types -export type ExtractActorMethodArgs = T extends ActorMethod - ? A +// Extracts the argument types of an ActorMethod +export type ExtractActorMethodArgs = T extends ActorMethod + ? Args : never +// Extracts the return type of an ActorMethod export type ExtractActorMethodReturnType = T extends ActorMethod< - any, - infer R + unknown[], + infer Ret > - ? R + ? Ret : never -export interface ActorMethodState> { +export interface ActorMethodState { [key: string]: { data: ExtractActorMethodReturnType | undefined loading: boolean @@ -58,7 +57,7 @@ export interface ActorMethodState> { } export type ActorMethodStates = { - [M in FunctionName]: ActorMethodState + [M in keyof A]: ActorMethodState } // State structure for an actor in a ReActor @@ -69,13 +68,10 @@ export type ActorState = { methodState: ActorMethodStates } -export type ActorStore = DefaultActorType> = - StoreApi> +export type ActorStore = StoreApi> // Function type for directly calling a method on an actor -export type CallActorMethod> = < - M extends FunctionName ->( +export type CallActorMethod = ( functionName: M, ...args: ExtractActorMethodArgs ) => Promise> diff --git a/packages/store/src/agent/index.ts b/packages/store/src/agent/index.ts index f487d2f777..9ff9ef55b6 100644 --- a/packages/store/src/agent/index.ts +++ b/packages/store/src/agent/index.ts @@ -67,7 +67,6 @@ export class AgentManager { this.updateState({ initialized: true, initializing: false }) } catch (error) { this.updateState({ error: error as Error, initializing: false }) - console.error("Error fetching root key:", error) } } } @@ -105,6 +104,7 @@ export class AgentManager { try { const { AuthClient } = await import("@dfinity/auth-client").catch( (error) => { + // eslint-disable-next-line no-console console.error("Failed to import @dfinity/auth-client:", error) throw new Error( "Authentication failed: @dfinity/auth-client package is missing." @@ -130,8 +130,6 @@ export class AgentManager { return identity } catch (error) { this.updateState({ error: error as Error, authenticating: false }) - - console.error("Error in authenticate:", error) throw error } } diff --git a/packages/store/src/helper.ts b/packages/store/src/helper.ts index 9b7fee4f9f..8f4bcbb2f3 100644 --- a/packages/store/src/helper.ts +++ b/packages/store/src/helper.ts @@ -2,15 +2,15 @@ import { hash } from "@dfinity/agent" import { toHexString } from "@dfinity/candid" import { devtools } from "zustand/middleware" import { createStore } from "zustand/vanilla" -import type { ActorSubclass } from "./types" +import { BaseActor } from "./actor" interface StoreOptions { withDevtools?: boolean store: string } -export function createStoreWithOptionalDevtools( - initialState: any, +export function createStoreWithOptionalDevtools( + initialState: T, options: StoreOptions ) { if (options.withDevtools) { @@ -25,7 +25,7 @@ export function createStoreWithOptionalDevtools( } } -export function jsonToString(json: any) { +export function jsonToString(json: never) { return JSON.stringify( json, (_, value) => (typeof value === "bigint" ? `BigInt(${value})` : value), @@ -33,7 +33,7 @@ export function jsonToString(json: any) { ) } -export const generateRequestHash = (args?: any[]) => { +export const generateRequestHash = (args?: unknown[]) => { const serializedArgs = args ?.map((arg) => { if (typeof arg === "bigint") { @@ -47,12 +47,12 @@ export const generateRequestHash = (args?: any[]) => { return stringToHash(serializedArgs ?? "") } -export const generateHash = (field?: any) => { +export const generateHash = (field?: never) => { const serializedArgs = JSON.stringify(field) return stringToHash(serializedArgs ?? "") } -export const generateActorHash = (actor: ActorSubclass) => { +export const generateActorHash = (actor: BaseActor) => { const serializedArgs = JSON.stringify(actor) return stringToHash(serializedArgs ?? "") } diff --git a/packages/store/src/index.ts b/packages/store/src/index.ts index f0cd9057a5..a2ddfd5267 100644 --- a/packages/store/src/index.ts +++ b/packages/store/src/index.ts @@ -1,9 +1,9 @@ -import type { ActorManagerOptions, DefaultActorType } from "./actor/types" +import type { ActorManagerOptions, BaseActor } from "./actor/types" import type { AgentManagerOptions } from "./agent/types" import { ActorManager } from "./actor" import { AgentManager } from "./agent" -import { ActorSubclass, CreateReActorOptions } from "./types" +import { CreateReActorOptions } from "./types" import { CandidAdapter, CandidAdapterOptions } from "./tools" export * from "./helper" @@ -36,9 +36,7 @@ export const createAgentManager = ( * @category Main * @includeExample ./packages/store/README.md:91-106 */ -export const createActorManager = < - A extends ActorSubclass = DefaultActorType ->( +export const createActorManager = ( options: ActorManagerOptions ): ActorManager => { return new ActorManager(options) @@ -52,9 +50,7 @@ export const createActorManager = < * @category Main * @includeExample ./packages/store/README.md:32-45 */ -export const createReActorStore = < - A extends ActorSubclass = DefaultActorType ->( +export const createReActorStore = ( options: CreateReActorOptions ): ActorManager => { const { diff --git a/packages/store/src/tools/types.ts b/packages/store/src/tools/types.ts index 232c87f228..afa619b663 100644 --- a/packages/store/src/tools/types.ts +++ b/packages/store/src/tools/types.ts @@ -9,5 +9,5 @@ export interface CandidAdapterOptions { export interface CandidDefenition { idlFactory: IDL.InterfaceFactory - init: ({ IDL }: { IDL: any }) => never[] + init: ({ idl }: { idl: typeof IDL }) => never[] }