UNPKG

7.59 kBTypeScriptView Raw
1import WebSocket from "ws";
2import type { Command, CommandTypeKey } from "reactotron-core-contract";
3import { ClientOptions } from "./client-options";
4export type { ClientOptions };
5export { assertHasLoggerPlugin } from "./plugins/logger";
6export type { LoggerPlugin } from "./plugins/logger";
7export { assertHasStateResponsePlugin, hasStateResponsePlugin } from "./plugins/state-responses";
8export type { StateResponsePlugin } from "./plugins/state-responses";
9export declare enum ArgType {
10 String = "string"
11}
12export interface CustomCommandArg {
13 name: string;
14 type: ArgType;
15}
16export interface LifeCycleMethods {
17 onCommand?: (command: Command) => void;
18 onConnect?: () => void;
19 onDisconnect?: () => void;
20}
21type AnyFunction = (...args: any[]) => any;
22export interface Plugin<Client> extends LifeCycleMethods {
23 features?: {
24 [key: string]: AnyFunction;
25 };
26 onPlugin?: (client: Client) => void;
27}
28export type PluginCreator<Client> = (client: Client) => Plugin<Client>;
29interface DisplayConfig {
30 name: string;
31 value?: object | string | number | boolean | null | undefined;
32 preview?: string;
33 image?: string | {
34 uri: string;
35 };
36 important?: boolean;
37}
38interface ArgTypeMap {
39 [ArgType.String]: string;
40}
41type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
42export type CustomCommandArgs<Args extends CustomCommandArg[]> = UnionToIntersection<Args extends Array<infer U> ? U extends CustomCommandArg ? {
43 [K in U as U["name"]]: ArgTypeMap[U["type"]];
44} : never : never>;
45export interface CustomCommand<Args extends CustomCommandArg[] = CustomCommandArg[]> {
46 id?: number;
47 command: string;
48 handler: (args?: CustomCommandArgs<Args>) => void;
49 title?: string;
50 description?: string;
51 args?: Args;
52}
53type ExtractFeatures<T> = T extends {
54 features: infer U;
55} ? U : never;
56type PluginFeatures<Client, P extends PluginCreator<Client>> = ExtractFeatures<ReturnType<P>>;
57export type InferFeaturesFromPlugins<Client, Plugins extends PluginCreator<Client>[]> = UnionToIntersection<PluginFeatures<Client, Plugins[number]>>;
58type InferFeaturesFromPlugin<Client, P extends PluginCreator<Client>> = UnionToIntersection<PluginFeatures<Client, P>>;
59export interface ReactotronCore {
60 options: ClientOptions<this>;
61 plugins: Plugin<this>[];
62 startTimer: () => () => number;
63 close: () => void;
64 send: <Type extends CommandTypeKey, Payload extends Command<Type>["payload"]>(type: Type, payload?: Payload, important?: boolean) => void;
65 display: (config: DisplayConfig) => void;
66 onCustomCommand: <Args extends CustomCommandArg[] = Exclude<CustomCommand["args"], undefined>>(config: CustomCommand<Args>) => () => void | ((config: string, optHandler?: () => void) => () => void);
67 /**
68 * Set the configuration options.
69 */
70 configure: (options: ClientOptions<this>) => ClientOptions<this>["plugins"] extends PluginCreator<this>[] ? this & InferFeaturesFromPlugins<this, ClientOptions<this>["plugins"]> : this;
71 use: <P extends PluginCreator<this>>(pluginCreator: P) => this & InferFeaturesFromPlugin<this, P>;
72 connect: () => this;
73}
74export type InferFeatures<Client = ReactotronCore, PC extends PluginCreator<Client> = PluginCreator<Client>> = PC extends (client: Client) => {
75 features: infer U;
76} ? U : never;
77export declare const corePlugins: (((reactotron: ReactotronCore) => {
78 features: {
79 log: (...args: any[]) => void;
80 logImportant: (...args: any[]) => void;
81 debug: (message: any, important?: any) => void;
82 warn: (message: any) => void;
83 error: (message: any, stack: any) => void;
84 };
85}) | ((reactotron: ReactotronCore) => {
86 features: {
87 image: (payload: import("./plugins/image").ImagePayload) => void;
88 };
89}) | ((reactotron: ReactotronCore) => {
90 features: {
91 benchmark: (title: string) => {
92 step: (stepTitle: string) => void;
93 stop: (stopTitle: string) => void;
94 last: (stopTitle: string) => void;
95 };
96 };
97}) | ((reactotron: ReactotronCore) => {
98 features: {
99 stateActionComplete: (name: string, action: Record<string, any>, important?: any) => void;
100 stateValuesResponse: (path: string, value: any, valid?: any) => void;
101 stateKeysResponse: (path: string, keys: string[], valid?: boolean) => void;
102 stateValuesChange: (changes: {
103 path: string;
104 value: any;
105 }[]) => false | void;
106 stateBackupResponse: (state: Record<string, any>) => void;
107 };
108}) | ((reactotron: ReactotronCore) => {
109 features: {
110 apiResponse: (request: {
111 status: number;
112 }, response: any, duration: number) => void;
113 };
114}) | ((reactotron: ReactotronCore) => {
115 features: {
116 clear: () => void;
117 };
118}) | ((reactotron: ReactotronCore) => {
119 onCommand: ({ type, payload }: Command<CommandTypeKey, any>) => void;
120 features: {
121 repl: (name: string, value: import("./plugins/repl").AcceptableRepls) => void;
122 };
123}))[];
124export type InferPluginsFromCreators<Client, PC extends PluginCreator<Client>[]> = PC extends Array<infer P extends PluginCreator<Client>> ? ReturnType<P>[] : never;
125type CorePluginFeatures = InferFeaturesFromPlugins<ReactotronCore, typeof corePlugins>;
126export interface Reactotron extends ReactotronCore, CorePluginFeatures {
127}
128export declare class ReactotronImpl implements Omit<ReactotronCore, "options" | "plugins" | "configure" | "connect" | "use"> {
129 options: ClientOptions<ReactotronCore>;
130 /**
131 * Are we connected to a server?
132 */
133 connected: boolean;
134 /**
135 * The socket we're using.
136 */
137 socket: WebSocket;
138 /**
139 * Available plugins.
140 */
141 plugins: Plugin<this>[];
142 /**
143 * Messages that need to be sent.
144 */
145 sendQueue: string[];
146 /**
147 * Are we ready to start communicating?
148 */
149 isReady: boolean;
150 /**
151 * The last time we sent a message.
152 */
153 lastMessageDate: Date;
154 /**
155 * The registered custom commands
156 */
157 customCommands: CustomCommand[];
158 /**
159 * The current ID for custom commands
160 */
161 customCommandLatestId: number;
162 /**
163 * Starts a timer and returns a function you can call to stop it and return the elapsed time.
164 */
165 startTimer: () => () => number;
166 /**
167 * Set the configuration options.
168 */
169 configure(options: ClientOptions<this>): ClientOptions<this>["plugins"] extends PluginCreator<this>[] ? this & InferFeaturesFromPlugins<this, ClientOptions<this>["plugins"]> : this;
170 close(): void;
171 /**
172 * Connect to the Reactotron server.
173 */
174 connect(): this;
175 /**
176 * Sends a command to the server
177 */
178 send: <Type extends CommandTypeKey, Payload extends import("reactotron-core-contract").CommandMap[Type]>(type: Type, payload?: Payload, important?: boolean) => void;
179 /**
180 * Sends a custom command to the server to displays nicely.
181 */
182 display(config: DisplayConfig): void;
183 /**
184 * Client libraries can hijack this to report errors.
185 */
186 reportError(this: any, error: Error): void;
187 /**
188 * Adds a plugin to the system
189 */
190 use(pluginCreator: PluginCreator<this>): this & PluginFeatures<this, typeof pluginCreator>;
191 onCustomCommand(config: CustomCommand | string, optHandler?: () => void): () => void;
192}
193export declare function createClient<Client extends ReactotronCore = ReactotronCore>(options?: ClientOptions<Client>): Client;