@eclipse-emfcloud/modelserver-client
Version:
Typescript rest client to interact with an EMF.cloud modelserver
153 lines • 6.69 kB
TypeScript
import URI from 'urijs';
import { ModelUpdateResult } from './model-server-client-api-v2';
import * as Type from './utils/type-util';
/**
* A `ModelServerMessage` represents the data payload that is sent by the modelserver
* when responding to incoming requests. It's also used by the client to send messages (e.g. keepAlive messages)
* to the server.
* @typeParam D Concrete type of the `data` property. Default is `unknown`.
*/
export interface ModelServerMessage<D = unknown> {
/** The message data */
data: D;
/** The message type. Is a literal of {@link MessageType} unless the modelserver has been extended with custom types */
type: string;
}
export declare namespace ModelServerMessage {
/**
* Guard guard to check wether a given object is of type {@link ModelServerMessage}.
* @param object The object to check.
* @returns The given object as {@link ModelServerMessage} or `false`.
*/
function is(object: unknown): object is ModelServerMessage;
}
/**
* Enumeration of the default types of a {@link ModelServerMessage}.
*/
export declare enum MessageType {
success = "success",
warning = "warning",
error = "error",
open = "open",
close = "close",
fullUpdate = "fullUpdate",
incrementalUpdate = "incrementalUpdate",
dirtyState = "dirtyState",
validationResult = "validationResult",
keepAlive = "keepAlive",
unknown = "unknown"
}
export declare namespace MessageType {
/**
* Maps the given string to an literal of {@link MessageType}
* @param value The string to map.
* @returns the mapped message type literal. If the given string cannot be mapped to an
* exact type {@link MessageType.unknown} is returned.
*/
function asMessageType(value: string): MessageType;
}
/**
* Representation of an arbitrary model.
* @typeParam C Concrete type of the `content` property. Default is `unknown`.
*/
export interface Model<C = unknown> {
/** The uri of the model. */
modeluri: string;
/** The model content. */
content: C;
}
export declare namespace Model {
/**
* Guard function to check wether a given object is of type {@link Model}.
* @param object The object to check.
* @returns The given object as {@link Model} or `false`.
*/
function is(object: unknown): object is Model;
function toString(model: Model): string;
}
/**
* Type to describe a function that maps a message to a specific type.
*/
export type Mapper<M, D = unknown> = (message: M) => D;
/**
* A Mapper which directly returns the message.
*/
export declare const IdentityMapper: Mapper<any, any>;
/**
* Type to describe a function that maps the {@link ModelServerMessage.data} property to a specific type.
*/
export type MessageDataMapper<D = unknown> = (message: ModelServerMessage) => D;
/**
* A collection of utility functions to map the `data` property of a {@link ModelServerMessage} to a specific type.
* If the `data` object of the given message cannot be mapped to the desired type an error is thrown.
*/
export declare namespace MessageDataMapper {
/**
* Maps the {@link ModelServerMessage.data} property of the given message to string.
* @param message The message to map.
* @returns the `data` property as `string`.
*/
function asString(message: ModelServerMessage): string;
/**
* Maps the {@link ModelServerMessage.data} property of the given message to a string[].
* @param message The message to map.
* @returns The `data` property as `string[]`.
* @throws {@link Error} if the 'data' property is not an array.
*/
function asStringArray(message: ModelServerMessage): string[];
/**
* Maps the {@link ModelServerMessage.data} property of the given message to a boolean.
* @param message The message to map.
* @returns The `data` property as boolean or `false` if `data` is not of type `boolean`.
*/
function asBoolean(message: ModelServerMessage): boolean;
/**
* Maps the {@link ModelServerMessage.data} property of the given message to a {@link Model}[].
* @param message The message to map.
* @returns The `data` property as `Model[]`.
* @throws {@link Error} if the 'data' property is not an array.
*/
function asModelArray(message: ModelServerMessage): Model[];
/**
* Maps the {@link ModelServerMessage.data} property of the given message to a URI[].
* @param message The message to map.
* @returns The `data` property as `URI[]`.
* @throws {@link Error} if the 'data' property is not an URI array.
*/
function asURIArray(message: ModelServerMessage): URI[];
/**
* Maps the {@link ModelServerMessage.data} property of the given message to an {@link AnyObject}.
* @param message The message to map.
* @returns The `data` property as `AnyObject`.
* @throws {@link Error} if the 'data' property is not of type `object`.
*/
function asObject(message: ModelServerMessage): Type.AnyObject;
/**
* Maps the {@link ModelServerMessage.data} property of the given message to the desired type if the data object passes the
* check with the given typeguard successfully.
* @param message The message to map.
* @param typeGuard A type guard function to check wether the data object is of the desired type.
* @typeParam T Concrete type to which the message should be mapped.
* @returns The `data` property as the desired type
* @throws {@link Error} if the check with the given typeguard fails.
*/
function as<T>(message: ModelServerMessage, guard: Type.TypeGuard<T>): T;
/**
* Maps the {@link ModelServerMessage.data} property of the given message to a `boolean` indicating whether the message
* has the {@link MessageType.success} type.
* @param message The message to map.
* @returns `true` if the type of the message is {@link MessageType.success}, `false` otherwise.
*/
function isSuccess(message: ModelServerMessage): boolean;
/**
* Maps the {@link ModelServerMessage.data} property of the given message to a {@link ModelUpdateResult}, indicating
* if the edit operation was successful (success: true), and if it was, how to patch the original model
* to get the updated version of the model.
*
* @param message The message to map.
* @returns a {@link ModelUpdateResult} indicating if the operation was successful, and how to patch the local
* model to get the new model if it was.
*/
function patchModel(message: ModelServerMessage): ModelUpdateResult;
}
//# sourceMappingURL=model-server-message.d.ts.map