UNPKG

@yunu-lab/rpc-react

Version:
83 lines (69 loc) 4 kB
import { default as default_2 } from 'react'; import { Message } from '@yunu-lab/rpc-ts'; import { Rpc } from '@yunu-lab/rpc-ts'; import { RpcRepository } from '@yunu-lab/rpc-ts'; import { StorageType } from '@yunu-lab/rpc-ts'; import { Store } from '@reduxjs/toolkit'; import { UnknownAction } from 'redux'; import { z } from 'zod'; export declare const createRpcHooks: <TTypes extends Record<string, Rpc<any>>>(typeKeys: Array<keyof TTypes>) => RpcHooks<TTypes>; export declare const extendStore: (options: ExtendStoreOptions) => { store: Store<any, UnknownAction, unknown>; repository: RpcRepository<any, {}>; unsubscribe: string; }; export declare interface ExtendStoreOptions { repository: RpcRepository<any>; store: Store; slices?: Record<string, any>; } declare type InferRpcType<T> = T extends Rpc<infer S> ? z.infer<S> : never; export { Rpc } export declare interface RpcContextType<TTypes extends Record<string, Rpc<any>>> { repository: RpcRepository<TTypes>; } declare type RpcHooks<TTypes extends Record<string, Rpc<any>>> = { [K in keyof TTypes as `use${ToPascalCase<string & K>}`]: { (): { [P in K as `${P & string}s`]: InferRpcType<TTypes[P]>[]; } & { [P in K as `${P & string}Map`]: Record<string, InferRpcType<TTypes[P]>>; } & { findById: (id: string | number) => InferRpcType<TTypes[K]> | null; findAll: () => InferRpcType<TTypes[K]>[]; mergeRpc: (data: Record<string, Partial<InferRpcType<TTypes[K]>> | null> | InferRpcType<TTypes[K]>[]) => void; }; (id: string | number): InferRpcType<TTypes[K]> | null; }; } & { [K in keyof TTypes as `use${ToPascalCase<string & K>}FullRelatedData`]: <TResult = InferRpcType<TTypes[K]>>(id?: string | number) => TResult | TResult[] | null; } & { [K in keyof TTypes as `use${ToPascalCase<string & K>}Listener`]: <RpcStorageType extends Record<keyof TTypes, StorageType> = Record<keyof TTypes, StorageType>>(callback: (event: { type: K; payload: RpcStorageType[K] extends "collection" ? Array<InferRpcType<TTypes[K]>> : RpcStorageType[K] extends "singleton" ? InferRpcType<TTypes[K]> : Array<InferRpcType<TTypes[K]>>; }) => void) => () => void; } & { useDataListener: <RpcStorageType extends Record<keyof TTypes, StorageType> = Record<keyof TTypes, StorageType>>(callback: (events: Array<{ type: keyof TTypes; payload: RpcStorageType[keyof TTypes] extends "collection" ? Array<InferRpcType<TTypes[keyof TTypes]>> : RpcStorageType[keyof TTypes] extends "singleton" ? InferRpcType<TTypes[keyof TTypes]> : Array<InferRpcType<TTypes[keyof TTypes]>>; }>) => void, options?: { types?: (keyof TTypes)[]; }) => () => void; } & { [K in keyof TTypes as `use${ToPascalCase<string & K>}Related`]: <TTarget extends keyof TTypes>(id: string | number, targetType: TTarget) => Array<TTypes[TTarget] extends Rpc<infer S> ? z.infer<S> : never>; } & { useHandleMessages: () => { handleMessages<RpcStorageType extends Record<keyof TTypes, StorageType>>(messages: Array<Message<TTypes>>, callbacks?: { [K in keyof TTypes]?: (data: RpcStorageType[K] extends "collection" ? Record<string, Partial<InferRpcType<TTypes[K]>> | null> | Array<InferRpcType<TTypes[K]>> : RpcStorageType[K] extends "singleton" ? Partial<InferRpcType<TTypes[K]>> : never) => void; }): void; }; }; export declare const RpcProvider: default_2.FC<RpcProviderProps>; export declare interface RpcProviderProps { children: default_2.ReactNode; repository: RpcRepository; } export { RpcRepository } declare type ToPascalCase<S extends string> = S extends `${infer First}_${infer Rest}` ? `${Capitalize<First>}${ToPascalCase<Rest>}` : Capitalize<S>; export declare const useRpc: <TTypes extends Record<string, Rpc<any>>>() => RpcContextType<TTypes>; export { }