diff --git a/client-react-hooks/src/cache-key.ts b/client-react-hooks/src/cache-key.ts index ebe98e6..3af66fa 100644 --- a/client-react-hooks/src/cache-key.ts +++ b/client-react-hooks/src/cache-key.ts @@ -1,15 +1,19 @@ import { ComputeOutputId, StoreId } from "@nillion/client-core"; +/** `REACT_QUERY_STATE_KEY_PREFIX` is a string that is used to prefix the cache keys */ export const REACT_QUERY_STATE_KEY_PREFIX = "__NILLION"; +/** `createStoreCacheKey` is a function that takes a `StoreId` or `string` and returns a cache key */ export const createStoreCacheKey = ( id: StoreId | string, ): readonly unknown[] => [REACT_QUERY_STATE_KEY_PREFIX, "STORE", id]; +/** `createPermissionsCacheKey` is a function that takes a `StoreId` or `string` or `null` and returns a cache key */ export const createPermissionsCacheKey = ( id: StoreId | string | null, ): readonly unknown[] => [REACT_QUERY_STATE_KEY_PREFIX, "PERMISSIONS", id]; +/** `createComputeResultCacheKey` is a function that takes a `ComputeOutputId` or `string` and returns a cache key */ export const createComputeResultCacheKey = ( id: ComputeOutputId | string, ): readonly unknown[] => [REACT_QUERY_STATE_KEY_PREFIX, "COMPUTE", id]; diff --git a/client-react-hooks/src/logging.ts b/client-react-hooks/src/logging.ts index cca3b48..70ace69 100644 --- a/client-react-hooks/src/logging.ts +++ b/client-react-hooks/src/logging.ts @@ -1,3 +1,7 @@ import debug from "debug"; +/** + * `Log` is a debug logger that can be used to log messages to the console. + * @param message - string + */ export const Log = debug("nillion:react-hooks"); diff --git a/client-react-hooks/src/nil-hook-base.ts b/client-react-hooks/src/nil-hook-base.ts index 0de2045..c6a3d88 100644 --- a/client-react-hooks/src/nil-hook-base.ts +++ b/client-react-hooks/src/nil-hook-base.ts @@ -1,5 +1,12 @@ import { UseMutationResult } from "@tanstack/react-query"; +/** + * `NilHookState` is a set of states that a NilHook can be in: + * - Idle: Waiting to receive a request + * - Loading: Waiting for the request to complete + * - Success: The request was successful + * - Error: The request had an error + */ export const NilHookState = { Idle: { status: "idle", @@ -31,6 +38,9 @@ export const NilHookState = { }, } as const; +/** + * `UseNilHook` is a hook that allows you to execute a NilHook operation, and check its status. + */ export type UseNilHook = NilHookBaseResult< ExecuteArgs, ExecuteResult @@ -42,11 +52,24 @@ export type UseNilHook = NilHookBaseResult< | NilHookErrorResult ); +/** + * NilHookBaseResult is a set of functions that a NilHook can use. + * execute - A function that executes the NilHook. + * executeAsync - A function that executes the NilHook asynchronously. + */ export interface NilHookBaseResult { execute: (args: ExecuteArgs) => void; executeAsync: (args: ExecuteArgs) => Promise; } +/** + * NilHookIdleResult is a set of states that a NilHook can be in when it is idle. + * status - The status of the NilHook. + * isLoading - Whether the NilHook is loading. + * isSuccess - Whether the NilHook is successful. + * isError - Whether the NilHook has an error. + * isIdle - Whether the NilHook is idle. + */ export interface NilHookIdleResult { status: "idle"; isLoading: false; @@ -55,6 +78,14 @@ export interface NilHookIdleResult { isIdle: true; } +/** + * NilHookLoadingResult is a set of states that a NilHook can be in when it is loading. + * status - The status of the NilHook, namely "loading". + * isLoading - Whether the NilHook is loading. + * isSuccess - Whether the NilHook is successful. + * isError - Whether the NilHook has an error. + * isIdle - Whether the NilHook is idle. + */ export interface NilHookLoadingResult { status: "loading"; isLoading: true; @@ -63,6 +94,15 @@ export interface NilHookLoadingResult { isIdle: false; } +/** + * NilHookSuccessResult is a set of states that a NilHook can be in when it is successful. + * status - The status of the NilHook namely "success". + * data - The data of the NilHook. + * isLoading - Whether the NilHook is loading. + * isSuccess - Whether the NilHook is successful. + * isError - Whether the NilHook has an error. + * isIdle - Whether the NilHook is idle. + */ export interface NilHookSuccessResult { status: "success"; data: R; @@ -72,6 +112,15 @@ export interface NilHookSuccessResult { isIdle: false; } +/** + * NilHookErrorResult is a set of states that a NilHook can be in when it has an error. + * status - The status of the NilHook namely "error". + * error - The error of the NilHook. + * isLoading - Whether the NilHook is loading. + * isSuccess - Whether the NilHook is successful. + * isError - Whether the NilHook has an error. + * isIdle - Whether the NilHook is idle. + */ export interface NilHookErrorResult { status: "error"; error: Error; @@ -81,6 +130,11 @@ export interface NilHookErrorResult { isIdle: false; } +/** + * nilHookBaseResult is a function that returns the state of a NilHook. + * @param mutate - The result of a mutation. + * @returns The state of the NilHook, which can be idle, loading, success, or error. + */ export const nilHookBaseResult = ( mutate: UseMutationResult, ) => { diff --git a/client-react-hooks/src/nillion-provider.tsx b/client-react-hooks/src/nillion-provider.tsx index 8894186..b923e13 100644 --- a/client-react-hooks/src/nillion-provider.tsx +++ b/client-react-hooks/src/nillion-provider.tsx @@ -24,12 +24,25 @@ import { NetworkConfig, NillionClient } from "@nillion/client-vms"; import { Log } from "./logging"; +/** + * `WithConfigProps` + * `config` is a ProviderNetworkConfig + * `network` is a NamedNetwork + * `client` is not allowed + **/ interface WithConfigProps { config?: ProviderNetworkConfig; network?: NamedNetwork; client?: never; } +/** + * `ProviderNetworkConfig` + * `bootnodes` is an array of Multiaddr or string + * `clusterId` is a ClusterId or string + * `nilChainId` is a ChainId or string + * `nilChainEndpoint` is a Url or string + */ interface ProviderNetworkConfig { bootnodes?: (Multiaddr | string)[]; clusterId?: ClusterId | string; @@ -37,19 +50,38 @@ interface ProviderNetworkConfig { nilChainEndpoint?: Url | string; } +/** + * `WithClientProps` + * `client` is a `NillionClient` + * `config` is not allowed + * `network` is not allowed + */ interface WithClientProps { client: NillionClient; config?: never; network?: never; } +/** + * `NillionProviderProps` + * Alias for either WithConfigProps or WithClientProps + */ export type NillionProviderProps = WithConfigProps | WithClientProps; +/** + * `NillionContext` + * `client` is a NillionClient + * `logout` is a function that returns a Promise + */ export interface NillionContext { client: NillionClient; logout: () => Promise; } +/** + * `NillionContext` + * It provides a `NillionClient` context + */ export const NillionContext = createContext( undefined, ); @@ -57,6 +89,11 @@ export const NillionContext = createContext( // Moving this into the hook means the client doesn't persist when strict mode is enabled const client = NillionClient.create(); +/** + * NillionProvider + * @param NillionProviderProps - expects provider props or a `ReactNode` + * @returns ReactNode + */ export const NillionProvider: React.FC< NillionProviderProps & { children: ReactNode } > = (props): ReactNode => { diff --git a/client-react-hooks/src/use-nil-compute-output.ts b/client-react-hooks/src/use-nil-compute-output.ts index c068974..3bba2aa 100644 --- a/client-react-hooks/src/use-nil-compute-output.ts +++ b/client-react-hooks/src/use-nil-compute-output.ts @@ -5,14 +5,27 @@ import { ComputeOutputId, NadaPrimitiveValue } from "@nillion/client-core"; import { nilHookBaseResult, UseNilHook } from "./nil-hook-base"; import { useNillion } from "./use-nillion"; +/** + * `ExecuteArgs` is an interface that can be passed to the `execute` function. + * @param id - `ComputeOutputId` or `string` + */ interface ExecuteArgs { id: ComputeOutputId | string; } type ExecuteResult = Record; +/** + * `UseNilComputeOutput` is a hook that allows you to execute a compute output. + * execute - It executes the NilHook synchronously, allowing the user to check for its status via {@link isSuccess} and {@link isError}. + * executeAsync - It executes the NilHook asynchronously, allowing the usage of `async/await` or `.then()`. + */ type UseNilComputeOutput = UseNilHook; +/** + * `useNilComputeOutput` is a hook that allows you to execute a compute output. + * @returns {@link UseNilComputeOutput} + */ export const useNilComputeOutput = (): UseNilComputeOutput => { const { client: nilClient } = useNillion(); @@ -27,9 +40,11 @@ export const useNilComputeOutput = (): UseNilComputeOutput => { }); return { + /** execute function that takes an ExecuteArgs object and executes the compute output */ execute: (args: ExecuteArgs) => { mutate.mutate(args); }, + /** executeAsync function that takes an ExecuteArgs object and executes the compute output asynchronously */ executeAsync: async (args: ExecuteArgs) => mutate.mutateAsync(args), ...nilHookBaseResult(mutate), }; diff --git a/client-react-hooks/src/use-nil-compute.ts b/client-react-hooks/src/use-nil-compute.ts index a25d66c..1978be6 100644 --- a/client-react-hooks/src/use-nil-compute.ts +++ b/client-react-hooks/src/use-nil-compute.ts @@ -10,6 +10,11 @@ import { import { nilHookBaseResult, UseNilHook } from "./nil-hook-base"; import { useNillion } from "./use-nillion"; +/** ExecuteArgs is an interface that can be passed to the `execute` function. + * @param bindings - `ProgramBindings` + * @param values - `NadaValues` + * @param storeIds - array of `StoreId`s or strings + */ interface ExecuteArgs { bindings: ProgramBindings; values?: NadaValues; @@ -17,8 +22,17 @@ interface ExecuteArgs { } type ExecuteResult = ComputeOutputId; +/** + * `UseNilCompute` is a hook that allows you to execute a compute operation on Nillion. + * execute - It executes the NilHook synchronously, allowing the user to check for its status via {@link isSuccess} and {@link isError}. + * executeAsync - It executes the NilHook asynchronously, allowing the usage of `async/await` or `.then()`. + */ type UseNilCompute = UseNilHook; +/** + * `useNilCompute` is a hook that allows you to execute a compute. + * @returns {@link UseNilCompute} + */ export const useNilCompute = (): UseNilCompute => { const { client: nilClient } = useNillion(); @@ -38,9 +52,11 @@ export const useNilCompute = (): UseNilCompute => { }); return { + /** `execute` function that takes an `ExecuteArgs` object and executes the compute */ execute: (args: ExecuteArgs) => { mutate.mutate(args); }, + /** `executeAsync` function that takes an `ExecuteArgs` object and executes the compute asynchronously */ executeAsync: async (args: ExecuteArgs) => mutate.mutateAsync(args), ...nilHookBaseResult(mutate), }; diff --git a/client-react-hooks/src/use-nil-delete-value.ts b/client-react-hooks/src/use-nil-delete-value.ts index c16ca01..3170d7a 100644 --- a/client-react-hooks/src/use-nil-delete-value.ts +++ b/client-react-hooks/src/use-nil-delete-value.ts @@ -7,14 +7,27 @@ import { nilHookBaseResult } from "./nil-hook-base"; import { UseNilHook } from "./nil-hook-base"; import { useNillion } from "./use-nillion"; +/** + * `ExecuteArgs` is an interface that can be passed to the `execute` function + * @param id - `StoreId` or `string` + */ interface ExecuteArgs { id: StoreId | string; } type ExecuteResult = StoreId; +/** + * `UseNilDeleteValue` is a hook that allows you to delete a value from a store. + * execute - It executes the NilHook synchronously, allowing the user to check for its status via {@link isSuccess} and {@link isError}. + * executeAsync - It executes the NilHook asynchronously, allowing the usage of `async/await` or `.then()`. + */ type UseNilDeleteValue = UseNilHook; +/** + * `useNilDeleteValue` is a hook that allows you to delete a value from a store. + * @returns {@link UseNilDeleteValue} + */ export const useNilDeleteValue = (): UseNilDeleteValue => { const { client: nilClient } = useNillion(); const queryClient = useQueryClient(); @@ -39,9 +52,11 @@ export const useNilDeleteValue = (): UseNilDeleteValue => { }); return { + /** execute function that takes an `ExecuteArgs` object and executes the delete value */ execute: (args: ExecuteArgs) => { mutate.mutate(args); }, + /** executeAsync function that takes an `ExecuteArgs` object and executes the delete value asynchronously */ executeAsync: async (args: ExecuteArgs) => mutate.mutateAsync(args), ...nilHookBaseResult(mutate), }; diff --git a/client-react-hooks/src/use-nil-fetch-store-acl.ts b/client-react-hooks/src/use-nil-fetch-store-acl.ts index 8dbbc26..458f65f 100644 --- a/client-react-hooks/src/use-nil-fetch-store-acl.ts +++ b/client-react-hooks/src/use-nil-fetch-store-acl.ts @@ -5,13 +5,26 @@ import { StoreAcl, StoreId } from "@nillion/client-core"; import { nilHookBaseResult, UseNilHook } from "./nil-hook-base"; import { useNillion } from "./use-nillion"; +/** + * `ExecuteArgs` is an interface that can be passed to the `execute` function. + * @param id - `StoreId` or `string` + */ interface ExecuteArgs { id: StoreId | string; } type ExecuteResult = StoreAcl; +/** + * `UseNilFetchStoreAcl` is a hook that allows you to fetch a store acl. + * execute - executes the NilHook synchronously, allowing the user to check for its status via {@link isSuccess} and {@link isError}. + * executeAsync - executes the NilHook asynchronously, allowing the usage of `async/await` or `.then()`. + */ type UseNilFetchStoreAcl = UseNilHook; +/** + * `useNilFetchStoreAcl` is a hook that allows you to fetch a store acl. + * @returns {@link UseNilFetchStoreAcl} + */ export const useNilFetchStoreAcl = (): UseNilFetchStoreAcl => { const { client } = useNillion(); @@ -26,9 +39,11 @@ export const useNilFetchStoreAcl = (): UseNilFetchStoreAcl => { }); return { + /* execute function that takes an `ExecuteArgs` object and executes the fetch store acl */ execute: (args: ExecuteArgs) => { mutate.mutate(args); }, + /* executeAsync function that takes an `ExecuteArgs` object and executes the fetch store acl asynchronously */ executeAsync: async (args: ExecuteArgs) => mutate.mutateAsync(args), ...nilHookBaseResult(mutate), }; diff --git a/client-react-hooks/src/use-nil-fetch-value.ts b/client-react-hooks/src/use-nil-fetch-value.ts index 9eeb3ab..b58ff71 100644 --- a/client-react-hooks/src/use-nil-fetch-value.ts +++ b/client-react-hooks/src/use-nil-fetch-value.ts @@ -13,19 +13,38 @@ import { nilHookBaseResult } from "./nil-hook-base"; import { UseNilHook } from "./nil-hook-base"; import { useNillion } from "./use-nillion"; +/** + * `Options` is an interface that can be passed to the `useNilFetchValue` hook. + * @param type - `NadaValueType` + * @param staleAfter - `number` + */ interface Options { type: NadaValueType; staleAfter?: number; } +/** + * `ExecuteArgs` is an interface that can be passed to the `execute` function. + * @param id - `StoreId` or `string` + * @param name - `NamedValue` or `string` + */ interface ExecuteArgs { id: StoreId | string; name: NamedValue | string; } type ExecuteResult = NadaPrimitiveValue; +/** + * `UseNilFetchValue` is a hook that allows you to fetch a value from a store. + * execute - It executes the NilHook synchronously, allowing the user to check for its status via {@link isSuccess} and {@link isError}. + * executeAsync - It executes the NilHook asynchronously, allowing the usage of `async/await` or `.then()`. + */ type UseNilFetchValue = UseNilHook; +/** + * `useNilFetchValue` is a hook that allows you to fetch a value from a store. + * @returns {@link UseNilFetchValue} + */ export const useNilFetchValue = (options: Options): UseNilFetchValue => { const { client: nilClient } = useNillion(); const queryClient = useQueryClient(); @@ -69,9 +88,11 @@ export const useNilFetchValue = (options: Options): UseNilFetchValue => { }); return { + /** execute function that takes an ExecuteArgs object and executes the fetch value */ execute: (args: ExecuteArgs) => { mutate.mutate(args); }, + /** executeAsync function that takes an ExecuteArgs object and executes the fetch value asynchronously */ executeAsync: async (args: ExecuteArgs) => mutate.mutateAsync(args), ...nilHookBaseResult(mutate), }; diff --git a/client-react-hooks/src/use-nil-set-store-acl.ts b/client-react-hooks/src/use-nil-set-store-acl.ts index 91313f2..9bbfb5a 100644 --- a/client-react-hooks/src/use-nil-set-store-acl.ts +++ b/client-react-hooks/src/use-nil-set-store-acl.ts @@ -6,14 +6,28 @@ import { nilHookBaseResult } from "./nil-hook-base"; import { UseNilHook } from "./nil-hook-base"; import { useNillion } from "./use-nillion"; +/** + * `ExecuteArgs` is an interface that can be passed to the `execute` function. + * @param id - `StoreId` or `string` + * @param acl - `StoreAcl` + */ interface ExecuteArgs { id: StoreId | string; acl: StoreAcl; } type ExecuteResult = ActionId; +/** + * `UseNilSetStoreAcl` is a hook that allows you to set a store acl. + * execute - It executes the NilHook synchronously, allowing the user to check for its status via {@link isSuccess} and {@link isError}. + * executeAsync - It executes the NilHook asynchronously, allowing the usage of `async/await` or `.then()`. + */ type UseNilSetStoreAcl = UseNilHook; +/** + * `useNilSetStoreAcl` is a hook that allows you to set a store acl. + * @returns {@link UseNilSetStoreAcl} + */ export const useNilSetStoreAcl = (): UseNilSetStoreAcl => { const { client } = useNillion(); @@ -28,9 +42,11 @@ export const useNilSetStoreAcl = (): UseNilSetStoreAcl => { }); return { + /** `execute` function that takes an `ExecuteArgs` object and executes the set store acl */ execute: (args: ExecuteArgs) => { mutate.mutate(args); }, + /** `executeAsync` function that takes an `ExecuteArgs` object and executes the set store acl asynchronously */ executeAsync: async (args: ExecuteArgs) => mutate.mutateAsync(args), ...nilHookBaseResult(mutate), }; diff --git a/client-react-hooks/src/use-nil-store-program.ts b/client-react-hooks/src/use-nil-store-program.ts index 994d2ca..1bc1863 100644 --- a/client-react-hooks/src/use-nil-store-program.ts +++ b/client-react-hooks/src/use-nil-store-program.ts @@ -6,6 +6,11 @@ import { nilHookBaseResult } from "./nil-hook-base"; import { UseNilHook } from "./nil-hook-base"; import { useNillion } from "./use-nillion"; +/** + * `ExecuteArgs` is an interface that can be passed to the `execute` function. + * @param name - `ProgramName` or `string` + * @param program - `Uint8Array` + */ interface ExecuteArgs { name: ProgramName | string; program: Uint8Array; @@ -14,6 +19,10 @@ type ExecuteResult = ProgramId; type UseNilStoreProgram = UseNilHook; +/** + * `useNilStoreProgram` is a hook that allows you to store a program in Nillion. + * @returns {@link UseNilStoreProgram} + */ export const useNilStoreProgram = (): UseNilStoreProgram => { const { client: nilClient } = useNillion(); @@ -28,9 +37,11 @@ export const useNilStoreProgram = (): UseNilStoreProgram => { }); return { + /** `execute` function that takes an `ExecuteArgs` object and executes the store program */ execute: (args: ExecuteArgs) => { mutate.mutate(args); }, + /** `executeAsync` function that takes an `ExecuteArgs` object and executes the store program asynchronously */ executeAsync: async (args: ExecuteArgs) => mutate.mutateAsync(args), ...nilHookBaseResult(mutate), }; diff --git a/client-react-hooks/src/use-nil-store-value.ts b/client-react-hooks/src/use-nil-store-value.ts index 6f7c970..e8f97b7 100644 --- a/client-react-hooks/src/use-nil-store-value.ts +++ b/client-react-hooks/src/use-nil-store-value.ts @@ -12,6 +12,12 @@ import { createStoreCacheKey } from "./cache-key"; import { nilHookBaseResult, UseNilHook } from "./nil-hook-base"; import { useNillion } from "./use-nillion"; +/** `ExecuteArgs` is an interface that can be passed to the `execute` function + * @param name - `NamedValue` or `string` + * @param data - `NadaPrimitiveValue` + * @param ttl - `Days` or `number` + * @param acl - `StoreAcl` + */ interface ExecuteArgs { name: NamedValue | string; data: NadaPrimitiveValue; @@ -23,6 +29,10 @@ type ExecuteResult = StoreId; type UseNilStoreValue = UseNilHook; +/** + * `useNilStoreValue` is a hook that allows you to store a value in Nillion. + * @returns {@link UseNilStoreValue} + */ export const useNilStoreValue = (): UseNilStoreValue => { const { client: nilClient } = useNillion(); const queryClient = useQueryClient(); @@ -48,9 +58,11 @@ export const useNilStoreValue = (): UseNilStoreValue => { }); return { + /** `execute` function that takes an `ExecuteArgs` object and executes the store value */ execute: (args: ExecuteArgs) => { mutate.mutate(args); }, + /** `executeAsync` function that takes an `ExecuteArgs` object and executes the store value asynchronously */ executeAsync: async (args: ExecuteArgs) => mutate.mutateAsync(args), ...nilHookBaseResult(mutate), }; diff --git a/client-react-hooks/src/use-nil-update-value.ts b/client-react-hooks/src/use-nil-update-value.ts index 4d1e74a..7ed5953 100644 --- a/client-react-hooks/src/use-nil-update-value.ts +++ b/client-react-hooks/src/use-nil-update-value.ts @@ -11,6 +11,12 @@ import { createStoreCacheKey } from "./cache-key"; import { nilHookBaseResult, UseNilHook } from "./nil-hook-base"; import { useNillion } from "./use-nillion"; +/** `ExecuteArgs` is an interface that can be passed to the `execute` function + * @param id - `StoreId` or `string` + * @param name - `NamedValue` or `string` + * @param data - `NadaPrimitiveValue` + * @param ttl - `Days` or `number` + */ interface ExecuteArgs { id: StoreId | string; name: NamedValue | string; @@ -22,6 +28,10 @@ type ExecuteResult = StoreId; type UseNilUpdateValue = UseNilHook; +/** + * `useNilUpdateValue` is a hook that allows you to update a value in Nillion. + * @returns {@link UseNilUpdateValue} + */ export const useNilUpdateValue = (): UseNilUpdateValue => { const { client: nilClient } = useNillion(); const queryClient = useQueryClient(); @@ -46,9 +56,11 @@ export const useNilUpdateValue = (): UseNilUpdateValue => { }); return { + /** `execute` function that takes an `ExecuteArgs` object and executes the update value */ execute: (args: ExecuteArgs) => { mutate.mutate(args); }, + /** `executeAsync` function that takes an `ExecuteArgs` object and executes the update value asynchronously */ executeAsync: async (args: ExecuteArgs) => mutate.mutateAsync(args), ...nilHookBaseResult(mutate), }; diff --git a/client-react-hooks/src/use-nillion-auth.ts b/client-react-hooks/src/use-nillion-auth.ts index fa6774b..d2d1a42 100644 --- a/client-react-hooks/src/use-nillion-auth.ts +++ b/client-react-hooks/src/use-nillion-auth.ts @@ -7,18 +7,41 @@ import { UserCredentials as ClientUserCredentials } from "@nillion/client-vms"; import { Log } from "./logging"; import { NillionContext } from "./nillion-provider"; +/** + * This file provides a hook for using the Nillion authentication context. + * It provides a simple interface for logging in and out of the Nillion client. + */ export interface UseNillionAuthContext { authenticated: boolean; login: (args: UserCredentials) => Promise; logout: () => Promise; } +/** + * `UserCredentials` is a type that can be passed to the `login` function. + * It is a union of the `UserSeed` type and a string, which can be used to + * create a `UserSeed`. Additionally, it can include a nodeSeed and a signer + * field, which can be used to specify a custom signer for the client. + * @param userSeed - `UserSeed` or string + * @param nodeSeed - string + * @param signer - "kelpr" or a function that returns a `Promise` + */ export interface UserCredentials { userSeed: UserSeed | string; nodeSeed?: string; signer?: "keplr" | (() => Promise); } +/** + * `useNillionAuth` is a hook that provides access to the Nillion authentication + * context. It returns an object with three fields: `authenticated`, `login`, + * and `logout`. The `authenticated` field is a boolean that indicates whether + * the client is currently authenticated. The `login` function can be called + * with a `UserCredentials` object to log in to the client. The `logout` function + * can be called to log out of the client. + * @returns UseNillionAuthContext + * @throws Error if NillionContext is not set + */ export function useNillionAuth(): UseNillionAuthContext { const context = useContext(NillionContext); if (!context) { @@ -31,6 +54,7 @@ export function useNillionAuth(): UseNillionAuthContext { return { authenticated, + /** login function that takes a UserCredentials object and logs in to the client */ login: (credentials: UserCredentials) => { if (authenticated) { Log("Client already logged in."); @@ -46,6 +70,7 @@ export function useNillionAuth(): UseNillionAuthContext { return context.client.connect(); } }, + /** logout function that logs out of the client */ logout: context.logout, }; } diff --git a/client-react-hooks/src/use-nillion.ts b/client-react-hooks/src/use-nillion.ts index 0b7a90f..5beb151 100644 --- a/client-react-hooks/src/use-nillion.ts +++ b/client-react-hooks/src/use-nillion.ts @@ -2,6 +2,12 @@ import { useContext } from "react"; import { NillionContext } from "./nillion-provider"; +/** + * `useNillion` + * A custom hook that returns the {@link NillionContext}. + * @returns {@link NillionContext} + * @throws error if `NillionContext` is undefined + */ export function useNillion(): NillionContext { const context = useContext(NillionContext); if (!context) {