UNPKG

@eclipse-emfcloud/modelserver-client

Version:

Typescript rest client to interact with an EMF.cloud modelserver

236 lines 10.3 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.MessageDataMapper = exports.IdentityMapper = exports.Model = exports.MessageType = exports.ModelServerMessage = void 0; /******************************************************************************** * Copyright (c) 2021-2022 STMicroelectronics and others. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0 which is available at * https://www.eclipse.org/legal/epl-2.0, or the MIT License which is * available at https://opensource.org/licenses/MIT. * * SPDX-License-Identifier: EPL-2.0 OR MIT *******************************************************************************/ const jsonpatch = __importStar(require("fast-json-patch")); const fast_json_patch_1 = require("fast-json-patch"); const patch_utils_1 = require("./utils/patch-utils"); const Type = __importStar(require("./utils/type-util")); var ModelServerMessage; (function (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) { return Type.AnyObject.is(object) && Type.isString(object, 'type') && object.data !== undefined; } ModelServerMessage.is = is; })(ModelServerMessage = exports.ModelServerMessage || (exports.ModelServerMessage = {})); /** * Enumeration of the default types of a {@link ModelServerMessage}. */ // eslint-disable-next-line no-shadow var MessageType; (function (MessageType) { MessageType["success"] = "success"; MessageType["warning"] = "warning"; MessageType["error"] = "error"; MessageType["open"] = "open"; MessageType["close"] = "close"; MessageType["fullUpdate"] = "fullUpdate"; MessageType["incrementalUpdate"] = "incrementalUpdate"; MessageType["dirtyState"] = "dirtyState"; MessageType["validationResult"] = "validationResult"; MessageType["keepAlive"] = "keepAlive"; MessageType["unknown"] = "unknown"; })(MessageType = exports.MessageType || (exports.MessageType = {})); (function (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) { if (value in MessageType) { return MessageType[value]; } return MessageType.unknown; } MessageType.asMessageType = asMessageType; })(MessageType = exports.MessageType || (exports.MessageType = {})); var Model; (function (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) { return Type.AnyObject.is(object) && Type.isString(object, 'modeluri') && Type.isObject(object, 'content'); } Model.is = is; function toString(model) { return JSON.stringify(model, undefined, 2); } Model.toString = toString; })(Model = exports.Model || (exports.Model = {})); /** * A Mapper which directly returns the message. */ const IdentityMapper = m => m; exports.IdentityMapper = IdentityMapper; /** * 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. */ var MessageDataMapper; (function (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) { return Type.asString(message.data); } MessageDataMapper.asString = asString; /** * 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) { return Type.asStringArray(message.data); } MessageDataMapper.asStringArray = asStringArray; /** * 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) { return typeof message.data === 'boolean' ? message.data : false; } MessageDataMapper.asBoolean = asBoolean; /** * 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) { return Type.asModelArray(message.data); } MessageDataMapper.asModelArray = asModelArray; /** * 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) { return Type.asURIArray(message.data); } MessageDataMapper.asURIArray = asURIArray; /** * 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) { return Type.asObject(message.data); } MessageDataMapper.asObject = asObject; /** * 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(message, guard) { return Type.asType(message.data, guard); } MessageDataMapper.as = as; /** * 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) { return message.type === 'success'; } MessageDataMapper.isSuccess = isSuccess; /** * 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) { if (isSuccess(message)) { const data = message.data; const patch = data ? data.patch : undefined; const allPatches = data ? data.allPatches : undefined; if (patch || allPatches) { return { success: isSuccess(message), patch, patchModel: (oldModel, copy, modeluri) => { const modelToPatch = copy ? (0, fast_json_patch_1.deepClone)(oldModel) : oldModel; const patchToApply = modeluri ? getPatch(allPatches, modeluri) : patch_utils_1.Operations.isPatch(patch) ? patch : undefined; return patchToApply ? jsonpatch.applyPatch(modelToPatch, patchToApply).newDocument : modelToPatch; }, allPatches }; } else { return { success: true }; } } else { return { success: false }; } } MessageDataMapper.patchModel = patchModel; function getPatch(patches, modeluri) { var _a; return (_a = patches.find(mp => mp.modelUri === modeluri.toString())) === null || _a === void 0 ? void 0 : _a.patch; } })(MessageDataMapper = exports.MessageDataMapper || (exports.MessageDataMapper = {})); //# sourceMappingURL=model-server-message.js.map