@fakehost/signalr
Version:
A Fake Signalr Service for faking/mocking signalr hub services for testing, prototyping, and demoing
65 lines (62 loc) • 2.25 kB
text/typescript
import { CloseConnectionOptions, Host, ConnectionId } from '@fakehost/exchange';
export { ConnectionId } from '@fakehost/exchange';
type AllKeys<T = object> = {
[K in keyof T]: boolean;
};
type HubClients<T = object> = {
All: {
[K in keyof T]: T[K];
};
Others: {
[K in keyof T]: T[K];
};
Caller: {
[K in keyof T]: T[K];
};
Client: (clientId: ConnectionId) => {
[K in keyof T]: T[K];
};
};
type ConnectionState<State = object> = {
id: ConnectionId;
setState: <Key extends keyof State>(key: Key, value: State[Key]) => void;
getState: <Key extends keyof State>(key: Key) => State[Key] | undefined;
addEventHandler(event: 'disconnect', handler: () => void): void;
removeEventHandler(event: 'disconnect', handler: () => void): void;
};
type SignalrInstanceThis<Receiver = object, State = object> = {
Clients: HubClients<Receiver>;
Connection: ConnectionState<State>;
};
type FormatTarget<Hub extends object, Receiver = object> = 'capitalize' | undefined | ((s: keyof Hub | keyof Receiver) => string);
declare class FakeSignalrHub<Hub extends object, Receiver extends object = object, State extends object = object> {
readonly path: string;
private receivers;
private format?;
private clients;
private handlers;
private host?;
private messageProtocol;
private connectionEvents;
constructor(path: string, receivers?: Partial<AllKeys<Receiver>>, format?: FormatTarget<Hub, Receiver>);
disconnect(options?: CloseConnectionOptions): void;
setHost(host: Host): void;
/**
* There can be differences in casing between the client typescript and the server handler methods in C#.
* This method formats the target to match the casing of the server.
* @param s
* @returns
*/
private formatTarget;
private onConnection;
private onDisconnection;
private handleHandshake;
private serialize;
private deserialize;
private onMessage;
private getSignalrInstance;
get thisInstance(): SignalrInstanceThis<Receiver, State>;
register<Target extends keyof Hub>(target: Target, handler: Handler): void;
}
type Handler = (...args: any[]) => any;
export { FakeSignalrHub };