UNPKG

@iotize/device-client.js

Version:

IoTize Device client for Javascript

78 lines (77 loc) 2.94 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var ConverterProvider = /** @class */ (function () { // public _pathDecoderMap: { [key: string]: BodyConverter<any> }; function ConverterProvider(pathToTypeMap, decodersMap) { this._pathDecoderMapString = pathToTypeMap; this._typeDecoderInstances = decodersMap; } ConverterProvider.prototype._resolveTypeDecoder = function (typeId) { if (!(typeId in this._typeDecoderInstances)) { throw new Error("Missing type decoder: '" + typeId + "'. (available decoders are: " + Object.keys(this._typeDecoderInstances).join(', ') + ")"); } return this._typeDecoderInstances[typeId]; }; ConverterProvider.prototype._resolvePathDecoder = function (path) { if (!(path in this._pathDecoderMapString)) { return null; } var typeId = this._pathDecoderMapString[path]; if (typeof typeId === "object") { return typeId; } return this._resolveTypeDecoder(typeId); }; ConverterProvider.prototype.setBodyConverterForPath = function (path, converter) { if (typeof converter === "string") { if (!(converter in this._typeDecoderInstances)) { throw new Error("Converter " + converter + " does not exist. You must register this converter first"); } } this._pathDecoderMapString[path] = converter; return this; }; Object.defineProperty(ConverterProvider.prototype, "types", { get: function () { return this._typeDecoderInstances; }, enumerable: true, configurable: true }); Object.defineProperty(ConverterProvider.prototype, "paths", { get: function () { return this._pathDecoderMapString; }, enumerable: true, configurable: true }); ConverterProvider.prototype.registerConverter = function (id, converter) { this._typeDecoderInstances[id] = converter; return this; }; /** * @deprecated * @param header */ ConverterProvider.prototype.getFromHeader = function (header) { var path = header.getPath(); return this._resolvePathDecoder(path); }; /** * @deprecated * @param path */ ConverterProvider.prototype.resolveFromPath = function (path) { return this._resolvePathDecoder(path); }; ConverterProvider.prototype.resolveBodyEncoder = function (key) { var converter = this._resolveTypeDecoder(key); return converter; }; ConverterProvider.prototype.resolveBodyDecoder = function (key) { var converter = this._resolveTypeDecoder(key); return converter; }; return ConverterProvider; }()); exports.ConverterProvider = ConverterProvider;