UNPKG

@eclipse-ditto/ditto-javascript-client-dom

Version:

DOM implementation of Eclipse Ditto JavaScript API to be used in browsers.

185 lines 6.45 kB
/*! * Copyright (c) 2019 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at * http://www.eclipse.org/legal/epl-2.0 * * SPDX-License-Identifier: EPL-2.0 */ import { AuthProvider, DittoURL } from '../../auth/auth-provider'; import { DittoProtocolResponse } from '../../model/ditto-protocol'; import { GenericResponse } from '../../model/response'; import { RequestHandler, ResilienceHandlerFactoryBuildStep, WebSocketBindingMessage, WebSocketImplementationBuilderHandler } from './resilience/websocket-resilience-interfaces'; /** * A Factory for a WebSocketRequestSender. */ export interface WebSocketImplementationBuilderUrl { /** * Sets the connection details for the web socket connection. * * @param url - The Url of the service. * @param authProviders - The auth providers to use. * @return a Promise for the reestablished web socket connection. */ withConnectionDetails(url: DittoURL, authProviders: AuthProvider[]): WebSocketImplementationBuilderHandler; } export declare class WebSocketRequestHandler implements RequestHandler { private readonly resilienceHandler; private readonly requests; private readonly subscriptions; constructor(resilienceHandlerFactory: ResilienceHandlerFactoryBuildStep); /** * Builds a request for the web socket connection. * * @param topic - The topic of the message. * @param path - The path of the message. * @param value - The payload of the message. * @param header - The headers of the message. * @returns A request string that can be sent over the web socket connection */ private static buildRequest; /** * Sends a message over the web socket connection and returns it's response. * * @param topic - The topic of the request. * @param path - The path of the request. * @param value - The payload of the request. * @param header - The headers of the request. * @returns A Promise for the request's response */ sendRequest(topic: string, path: string, value: any, header: object): Promise<GenericResponse>; /** * Sends a message over the web socket connection. * * @param topic - The topic of the message. * @param path - The path of the message. * @param value - The payload of the message. * @param header - The headers of the message. * @returns A Promise that resolves once the message was sent */ sendMessage(topic: string, path: string, value: any, header: object): Promise<void>; /** * Registers a subscription. * * @param subscription - The subscription to register. * @returns The ID of the subscription */ subscribe(subscription: Subscription): string; /** * Deletes a subscription. * * @param id - The ID of the subscription to delete. */ deleteSubscription(id: string): void; /** * Sends a protocol message to request a change in the information sent to the web socket connection. * * @param message - The message to send. * @returns A Promise that resolves once the request was acknowledged */ sendProtocolMessage(message: WebSocketBindingMessage): Promise<void>; handleInput(id: string, message: DittoProtocolResponse): void; handleError(id: string, cause: object): void; handleMessage(message: DittoProtocolResponse): boolean; /** * Matches an incoming response to the correlating request and resolves it. * * @param id - The correlation-id of the response. * @param response - The incoming response. */ private handleResponse; /** * Generates an unused request correlation-id and returns it. * * @returns The correlation-id */ private getRequestId; /** * Generates an unused subscription id and returns it. * * @returns The id */ private getSubscriptionId; /** * Generates a uuid. * * @returns The uuid */ private static generateId; } /** * A Factory for a WebSocketRequestSender. */ export interface WebSocketResponseHandlerFactory { buildInstance(url: string): Promise<WebSocketRequestHandler>; } /** * A subscription to messages matching a certain pattern. */ declare abstract class Subscription { private readonly _callback; protected constructor(_callback: (message: ProtocolResponseValue) => any); /** * Checks whether a message matches the subscription. * * @param message - The message to check. * @returns Whether the message matches or not */ abstract matches(message: DittoProtocolResponse): boolean; /** * Calls the callback function with the message provided. * * @param message - The message to send. */ callback(message: DittoProtocolResponse): void; } /** * A standard subscription to messages matching a specific pattern. */ export declare class StandardSubscription extends Subscription { private readonly topic; private readonly path; private readonly subResources; constructor(callback: (message: ProtocolResponseValue) => any, topic: string, path: string, subResources: boolean); matches(message: DittoProtocolResponse): boolean; /** * Checks whether a path matches the subscription's path. * * @param path - The path to check. * @returns Whether the path matches or not */ private checkPath; /** * Checks whether a topic matches the subscription's topic. * * @param topic - The topic to check. * @returns Whether the topic matches or not */ private checkTopic; } /** * A subscription to all messages of a type. */ export declare class AllSubscription extends Subscription { private readonly type; constructor(callback: (message: ProtocolResponseValue) => any, type: string); matches(message: DittoProtocolResponse): boolean; } /** * The message sent to subscription callbacks. */ export interface ProtocolResponseValue { topic: string; path: string; action: string; headers?: { [key: string]: any; }; value?: any; } export {}; //# sourceMappingURL=websocket-request-handler.d.ts.map