simpleddp-node
Version:
The aim of this library is to simplify the process of working with meteor server over DDP protocol using external JS environments
217 lines (216 loc) • 6.65 kB
TypeScript
import DDP, { PUBLIC_EVENTS } from './ddp/ddp';
import ws from 'ws';
import { ddpEventListener } from './classes/ddpEventListener.js';
import { ddpSubscription } from './classes/ddpSubscription.js';
import { ddpCollection } from './classes/ddpCollection.js';
export { ddpEventListener, ddpSubscription, ddpCollection };
export type MeteorError = {
error: number;
reason: string;
message: string;
errorType: 'Meteor.Error';
isClientSafe?: boolean;
};
export type DDPMessage = {
collection: PropertyKey;
id: any;
fields: {};
cleared: any[];
f: (p: {
removed: boolean;
added: any;
changed: boolean;
}) => any;
msg: string;
name: string;
params: any[];
result: any;
sub: any;
subs: string | string[];
filter: (newObjFullCopy: {
_id: any;
}, number: number, collection1: any[]) => any;
error?: Error;
};
export type SimpleDDPConnectOptions = {
endpoint: string;
SocketConstructor: typeof WebSocket | typeof ws;
autoConnect?: boolean;
autoReconnect?: boolean;
reconnectInterval?: number;
clearDataOnReconnection?: boolean;
maxTimeout?: number;
cleanQueue?: boolean;
plugins?: any[];
ddpVersion?: string;
};
export type LoginParams = {
password: string;
user: {
username: string;
};
};
export type User = {
id: string;
token: string;
tokenExpires: Date;
type: 'password' | 'resume';
};
/**
* Creates an instance of DDPServerConnection class. After being constructed, the instance will
* establish a connection with the DDP server and will try to maintain it open.
*/
declare class DDPClient {
private _id;
private _opGenId;
private _opts;
ddpConnection: DDP;
subs: ddpSubscription[];
/**
All collections data received from server.
*/
collections: {
[key: PropertyKey]: any[];
};
onChangeFuncs: DDPMessage[];
connected: boolean;
maxTimeout: number | undefined;
clearDataOnReconnection: boolean;
tryingToConnect: boolean;
tryingToDisconnect: boolean;
willTryToReconnect: boolean;
connectedEvent: {
stop: () => void;
start: () => void;
};
connectedEventRestartSubs: {
stop: () => void;
start: () => void;
};
disconnectedEvent: {
stop: () => void;
start: () => void;
};
addedEvent: {
stop: () => void;
start: () => void;
};
changedEvent: {
stop: () => void;
start: () => void;
};
removedEvent: {
stop: () => void;
start: () => void;
};
userId: string | undefined;
loggedIn: boolean;
token: string | undefined;
/**
* @example
* var opts = {
* endpoint: "ws://someserver.com/websocket",
* SocketConstructor: WebSocket,
* reconnectInterval: 5000
* };
* var server = new simpleDDP(opts);
*/
constructor(opts: SimpleDDPConnectOptions, plugins?: any[]);
login(obj: LoginParams): Promise<User>;
logout(): Promise<void>;
private restartSubs;
/**
* Use this for fetching the subscribed data and for reactivity inside the collection.
*/
collection<T>(name: string): ddpCollection<T>;
/**
* Dispatcher for ddp added messages.
*/
private dispatchAdded;
/**
* Dispatcher for ddp changed messages.
*/
private dispatchChanged;
/**
* Dispatcher for ddp removed messages.
*/
private dispatchRemoved;
/**
* Connects to the ddp server. The method is called automatically by the class constructor if the autoConnect option is set to true (default behavior).
* @public
* @return {Promise} - Promise which resolves when connection is established.
*/
connect(): Promise<any>;
/**
* Disconnects from the ddp server by closing the WebSocket connection. You can listen on the disconnected event to be notified of the disconnection.
*/
disconnect(): Promise<any>;
/**
* Calls a remote method with arguments passed in array.
* @example
* server.apply("method1").then(function(result) {
* console.log(result); //show result message in console
* if (result.someId) {
* //server sends us someId, lets call next method using this id
* return server.apply("method2",[result.someId]);
* } else {
* //we didn't recieve an id, lets throw an error
* throw "no id sent";
* }
* }).then(function(result) {
* console.log(result); //show result message from second method
* }).catch(function(error) {
* console.log(result); //show error message in console
* });
*/
apply<T extends any[], R>(method: string, args?: T, atBeginning?: boolean): Promise<any>;
/**
* Calls a remote method with arguments passed after the first argument.
* Syntactic sugar for @see apply.
*/
call<TArgs extends any[], R = unknown>(method: string, ...args: TArgs): Promise<R>;
/**
* Tries to subscribe to a specific publication on server.
* Starts the subscription if the same subscription exists.
*/
sub(pubname: string, args: any[]): ddpSubscription;
/**
* Tries to subscribe to a specific publication on server.
* Syntactic sugar for @see sub.
*/
subscribe(pubname: string, ...args: any[]): ddpSubscription;
/**
* Starts listening server for basic DDP event running f each time the message arrives.
* Default suppoted events @see PUBLIC_EVENTS const.
* @example
* server.on('connected', () => {
* // you can show a success message here
* });
*
* server.on('disconnected', () => {
* // you can show a reconnection message here
* });
*/
on<T extends typeof PUBLIC_EVENTS[number] = typeof PUBLIC_EVENTS[number]>(event: T, f: T extends 'login' ? (user: User) => void : (message: DDPMessage, id: string) => void): ReturnType<typeof ddpEventListener>;
/**
* Stops all reactivity.
*/
stopChangeListeners(): void;
/**
* Removes all documents like if it was removed by the server publication.
*/
clearData(): Promise<any>;
/**
* Imports the data like if it was published by the server.
*/
importData(data: string | object): Promise<any>;
/**
* Exports the data
*/
exportData(format?: string): object | string | undefined;
/**
* Marks every passed @see ddpSubscription object as ready like if it was done by the server publication.
*/
markAsReady(subs: any[]): Promise<any>;
}
export default DDPClient;